Link to home
Start Free TrialLog in
Avatar of bcsmess
bcsmess

asked on

Check if a field's type is a class or interface.

I have a field declared in class.  How do I check to see that the field's type is a class or an interface.

I can use field's getType() method to get me the type but somehow I'm not sure if the method returns a class or something else.

Thanks.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of pshersby
pshersby

I may be missing something here, do you mean that you have a field declared

e.g.       Aclass myfield;

surely if this is the case you know what the field is, you don't have to determine it at run-time.

OR

perhaps you have a declaration like that that references some run-time object, in this case surely the object must be an instance of a class because how would you create an instance of an Interface - you may certainly use the interface but somewhere there would be a class.

Perhaps a simple example of what you are doing would help

     Object o = yourField;
      System.out.println(o.getClass().isInterface());
Use the following where cls is a Class instance for the class you are checking, and fieldname is the name of the field:

File field = cls.getField(fieldname);
Class fieldtype = filed.getDeclaringClass();
boolean isinterface = fieldtype.isInterface();
Avatar of bcsmess

ASKER

What I want is to check if the initialized type of the field is an interface.
So say I have a Class1 that has a field Field 1.

Field1 is initialized:  HashTable Field1 = ....

Thus I want to grab HashTable and check to see if HashTable is an interface.
I believe the getType() method returns this class for you but it seems methods are recognizing its return value as a class.

I have:
      static String extendOrIn(Class jc){
            if (jc.isInterface()) {return "...";}
            else {
                  if (isAbstract(jc.getModifiers())) {return "parallelogram";}
                  else {return "...";}
      }}


But it seems to not take the type returned by the method into the argument.

Thanks.
Avatar of bcsmess

ASKER

I'm pretty sure all the suggestions above only returns the class that holds the variable...(i'm not exactly sure what getDeclaringClass does)
sorry that should have been:

Field field = cls.getField(fieldname);
Class fieldtype = field.getType();
boolean isinterface = fieldtype.isInterface();
>>I can use field's getType() method to get me the type but somehow I'm not sure if the method returns a class

It does:

System.out.println(field.getType().isInterface());
> System.out.println(field.getType().isInterface());

I already posted that.
Please read previous posts before commenting.
And i've already answered the question much earlier ;-)
your earlier suggestion won't work
and if you think it would then why are you now suggesting doing what I already suggested :)
>>your earlier suggestion won't work

Yes it will. This was the question:

>>What I want is to check if the initialized type of the field is an interface.

Yours is the one that wont' work. Yours gets the *declared* type of the field, not the initialized type

>>why are you now suggesting ...

I'm only confirming that bcsmess' original idea was correct


The code I posted works fine, the code you posted does not :)

Works fine for me getting the initialized type of a field and testing whether an interface or not thanks very much ;-)
bcsmess,

you need to pass the class and field name (or use that to get the class) to your extendOrIn method and use the code I posted earlier to determine if an interface or not.
> Works fine for me getting the initialized type of a field and testing whether an interface or not thanks very much

The code you posted was getting the class of the instance, not the field.
>>The code you posted was getting the class of the instance

That's precisely what "the initialized type of the field" is. The declared type of the field is something else.
ROTFL
Avatar of bcsmess

ASKER

The code doesn't work:

Field field = cls.getField(fieldname);
Class fieldtype = field.getType();
boolean isinterface = fieldtype.isInterface();

It says the error:  Type Mismatch, cannot convert from Type to Class
which line?

from the Field javadoc:
 Class getType()
          Returns a Class object that identifies the declared type for the field represented by this Field object.
bcsmess, while you're here, to avoid any time-wasting, can you confirm it's

"the initialized type of the field"

you want and not the declared type.
Avatar of bcsmess

ASKER


I know the API says it.  Try it though and let me know because it's not working on my end

Thanks again..
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial