remove need/support for wilcard type injection

This commit is contained in:
Anton Keks 2020-05-17 20:08:23 +03:00
parent 153f392bcc
commit ae52d74dc7
5 changed files with 32 additions and 51 deletions

View File

@ -26,7 +26,7 @@ import java.io.File;
* @author Anton Keks
*/
public class CommandLineProcessor implements CommandProcessor, StateTransitionListener {
private final FeederRegistry<? extends FeederCreator> feederRegistry;
private final FeederRegistry feederRegistry;
private final ExporterRegistry exporters;
private StateMachine stateMachine;
private ScanningResultList scanningResults;
@ -40,12 +40,12 @@ public class CommandLineProcessor implements CommandProcessor, StateTransitionLi
boolean autoQuit;
boolean appendToFile;
CommandLineProcessor(FeederRegistry<? extends FeederCreator> feederCreators, ExporterRegistry exporters) {
CommandLineProcessor(FeederRegistry feederCreators, ExporterRegistry exporters) {
this.feederRegistry = feederCreators;
this.exporters = exporters;
}
@Inject public CommandLineProcessor(FeederRegistry<? extends FeederCreator> feederCreators, ExporterRegistry exporters, StateMachine stateMachine, ScanningResultList scanningResults) {
@Inject public CommandLineProcessor(FeederRegistry feederCreators, ExporterRegistry exporters, StateMachine stateMachine, ScanningResultList scanningResults) {
this(feederCreators, exporters);
this.stateMachine = stateMachine;
this.scanningResults = scanningResults;

View File

@ -4,7 +4,6 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.*;
import static java.util.Arrays.stream;
@ -58,7 +57,7 @@ public class Injector {
Type[] types = constructor.getGenericParameterTypes();
Annotation[][] ans = constructor.getParameterAnnotations();
return range(0, types.length).mapToObj(i -> isCollection(types[i]) ?
requireAll(getParamClass((ParameterizedType) types[i])) :
requireAll(getParamClass(types[i])) :
require(new Key<>(toClass(types[i]), findName(ans[i])))
).toArray();
}
@ -73,9 +72,8 @@ public class Injector {
return type instanceof ParameterizedType && Collection.class.isAssignableFrom(toClass(type));
}
private Class<?> getParamClass(ParameterizedType type) {
Type t = type.getActualTypeArguments()[0];
return (Class<?>) (t instanceof WildcardType ? ((WildcardType) t).getUpperBounds()[0] : t);
private Class<?> getParamClass(Type type) {
return (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0];
}
private String findName(Annotation[] ans) {

View File

@ -11,6 +11,6 @@ package net.azib.ipscan.feeders;
*
* @author Anton Keks
*/
public interface FeederRegistry<T extends FeederCreator> extends Iterable<T> {
public interface FeederRegistry extends Iterable<FeederCreator> {
void select(String feederId);
}

View File

@ -8,10 +8,7 @@ package net.azib.ipscan.gui.feeders;
import net.azib.ipscan.config.GUIConfig;
import net.azib.ipscan.di.Inject;
import net.azib.ipscan.di.Named;
import net.azib.ipscan.feeders.Feeder;
import net.azib.ipscan.feeders.FeederException;
import net.azib.ipscan.feeders.FeederRegistry;
import net.azib.ipscan.feeders.RescanFeeder;
import net.azib.ipscan.feeders.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.TableItem;
@ -24,7 +21,7 @@ import java.util.List;
*
* @author Anton Keks
*/
public class FeederGUIRegistry implements FeederRegistry<AbstractFeederGUI> {
public class FeederGUIRegistry implements FeederRegistry {
private final List<AbstractFeederGUI> feederGUIList;
private final Combo feederSelectionCombo;
private final GUIConfig guiConfig;
@ -62,8 +59,9 @@ public class FeederGUIRegistry implements FeederRegistry<AbstractFeederGUI> {
currentFeederGUI.setVisible(true);
}
public Iterator<AbstractFeederGUI> iterator() {
return feederGUIList.iterator();
@SuppressWarnings({"unchecked", "rawtypes"})
public Iterator<FeederCreator> iterator() {
return (Iterator) feederGUIList.iterator();
}
/**

View File

@ -1,32 +1,20 @@
/**
* This file is a part of Angry IP Scanner source code,
* see http://www.angryip.org/ for more information.
* Licensed under GPLv2.
*/
package net.azib.ipscan.config;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import net.azib.ipscan.exporters.Exporter;
import net.azib.ipscan.exporters.ExporterRegistry;
import net.azib.ipscan.feeders.FeederCreator;
import net.azib.ipscan.feeders.FeederRegistry;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import net.azib.ipscan.exporters.Exporter;
import net.azib.ipscan.exporters.ExporterRegistry;
import net.azib.ipscan.feeders.FeederCreator;
import net.azib.ipscan.feeders.FeederRegistry;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Test;
/**
* CommandLineProcessorTest
*
* @author Anton Keks
*/
public class CommandLineProcessorTest {
private CommandLineProcessor processor;
private FeederCreator feederCreator;
@ -40,7 +28,7 @@ public class CommandLineProcessorTest {
}
@Test
public void toStringGeneratesUsageHelp() throws Exception {
public void toStringGeneratesUsageHelp() {
when(feederCreator.getFeederId()).thenReturn("feeder.range");
when(feederCreator.serializePartsLabels()).thenReturn(new String[] {"feeder.range.to"});
Exporter exporter = mock(Exporter.class);
@ -58,7 +46,7 @@ public class CommandLineProcessorTest {
}
@Test
public void minimal() throws Exception {
public void minimal() {
when(feederCreator.getFeederId()).thenReturn("feeder.feeder");
when(feederCreator.serializePartsLabels()).thenReturn(new String[] {"1st", "2nd"});
Exporter txtExporter = mock(Exporter.class);
@ -74,11 +62,11 @@ public class CommandLineProcessorTest {
assertFalse(processor.appendToFile);
assertTrue("specifying exporter should enable autoStart", processor.autoStart);
verify(feederCreator).unserialize(new String[] {"arg1", "arg2"});
verify(feederCreator).unserialize("arg1", "arg2");
}
@Test
public void options() throws Exception {
public void options() {
when(feederCreator.getFeederId()).thenReturn("feeder.mega");
when(feederCreator.serializePartsLabels()).thenReturn(new String[0]);
@ -89,48 +77,45 @@ public class CommandLineProcessorTest {
assertTrue(processor.autoStart);
assertTrue(processor.appendToFile);
verify(feederCreator).unserialize(new String[0]);
verify(feederCreator).unserialize();
}
@Test(expected=IllegalArgumentException.class)
public void missingRequiredFeeder() throws Exception {
public void missingRequiredFeeder() {
processor.parse("-o", "exporter");
}
@Test(expected=IllegalArgumentException.class)
public void inexistentExporter() throws Exception {
public void inexistentExporter() {
processor.parse("-o", "blah");
}
@Test(expected=IllegalArgumentException.class)
public void inexistentFeeder() throws Exception {
public void inexistentFeeder() {
processor.parse("-f:blah");
}
@Test(expected=IllegalArgumentException.class)
public void extraExporter() throws Exception {
public void extraExporter() {
processor.parse("-f:feeder", "-o", "exporter.txt", "-o", "exporter.xml");
}
@Test(expected=IllegalArgumentException.class)
public void extraFeeder() throws Exception {
public void extraFeeder() {
processor.parse("-f:feeder", "-o", "exporter.xml", "-f:feeder");
}
public static class MockFeederRegistry implements FeederRegistry<FeederCreator> {
public static class MockFeederRegistry implements FeederRegistry {
private List<FeederCreator> list;
public MockFeederRegistry(FeederCreator ... creators) {
list = Arrays.asList(creators);
}
public void select(String feederId) {
}
public void select(String feederId) { }
public Iterator<FeederCreator> iterator() {
return list.iterator();
}
}
}