0

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?

6
  • 1
    That's right. Class.forName returns a Class object. Commented Jun 19, 2014 at 20:30
  • Right, you need to make firstOption = Class.forName("yada yada").newInstance(); if you want to check it with instanceOf. Commented Jun 19, 2014 at 20:34
  • @azurefrog IOption is an interface, so you cannot create a new instance using this approach. Commented Jun 19, 2014 at 20:35
  • But he's not getting the class for an IOption, he's getting the class for a DwelltimeOptions which implements that interface. You can't new instance an enum? Commented Jun 19, 2014 at 20:36
  • @azurefrog oh, didn't read that part of the question. Commented Jun 19, 2014 at 20:42

2 Answers 2

2

Class#forName returns an instance of Class<?>, so in this code:

firstOption = Class.forName("com.nim.tools.options."  + capitalize(args.get(0)) + "Options");

If the argument you pass in args.get(0) is I and you have IOptions, then firstOption is the same as Class<IOption> and not an instance of an object that implements IOption interface.

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

Yes, your assumption is right.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, the Class.forName method returns a Class object representing the class, not an instance of the class. So the only thing instanceof will return true for is Class (and Object). If you want to know if the class implements an interface (or a superclass), and you have the relevant Class objects, then you can use Class's isAssignableFrom method.

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false.

if (IOptions.class.isAssignableFrom(firstOption)) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.