• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

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.
0
bcsmess
Asked:
bcsmess
  • 11
  • 8
  • 4
  • +1
1 Solution
 
pshersbyCommented:
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

0
 
CEHJCommented:
     Object o = yourField;
      System.out.println(o.getClass().isInterface());
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
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();
0
 
bcsmessAuthor Commented:
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.
0
 
bcsmessAuthor Commented:
I'm pretty sure all the suggestions above only returns the class that holds the variable...(i'm not exactly sure what getDeclaringClass does)
0
 
objectsCommented:
sorry that should have been:

Field field = cls.getField(fieldname);
Class fieldtype = field.getType();
boolean isinterface = fieldtype.isInterface();
0
 
CEHJCommented:
>>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());
0
 
objectsCommented:
> System.out.println(field.getType().isInterface());

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


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

0
 
CEHJCommented:
Works fine for me getting the initialized type of a field and testing whether an interface or not thanks very much ;-)
0
 
objectsCommented:
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.
0
 
objectsCommented:
> 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.
0
 
CEHJCommented:
>>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.
0
 
objectsCommented:
ROTFL
0
 
bcsmessAuthor Commented:
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
0
 
objectsCommented:
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.
0
 
CEHJCommented:
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.
0
 
bcsmessAuthor Commented:

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

Thanks again..
0
 
objectsCommented:
works fine here.
0
 
objectsCommented:
:)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 11
  • 8
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now