I have the following code dealing with command line options:
I have these classes:
public enum DwelltimeOptions implements IDwelltimeOptions{
public DwelltimeOptions findOption(String s){
return null;
}
}
public interface IDwelltimeOptions extends IOptions{
public void compare(ReconToolCompare rtc) throws ReconToolException;
}
public interface IOptions {
public IOptions findOption(String s);
}
the problem is that my firstOption variable below doesn't appear to be an instanceof IOptions, even though I believe it should be.
Object firstOption = null;
try {
firstOption = Class.forName("com.nim.tools.options." + capitalize(args.get(0)) + "Options");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(firstOption instanceof IOptions){
secondOption = ((IOptions) firstOption).findOption(args.get(1).substring(1));
}
else{
ReconTool.logError("Unrecognized option: \"" + args.get(0) + "\"");
ReconTool.printOutOptions();
System.exit(0);
}
firstOption variable is "class com.nim.tools.options.DwelltimeOptions" and the DwelltimeOptions class implements IDwelltimeOptions which extends IOptions...so DwelltimeOptions should be an instance of IOptions.
Actually, I think I just realized the problem. firstOption variable is actually a Class object and can't be used in an instanceof context and can't be compared with an Interface such as IOptions?
Class.forNamereturns aClassobject.firstOption = Class.forName("yada yada").newInstance();if you want to check it withinstanceOf.IOptionis an interface, so you cannot create a new instance using this approach.IOption, he's getting the class for aDwelltimeOptionswhich implements that interface. You can't new instance an enum?