Solved

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

Posted on 2004-10-14
24
218 Views
Last Modified: 2010-03-31
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
Comment
Question by:bcsmess
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 8
  • 4
  • +1
24 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12312515
0
 
LVL 2

Expert Comment

by:pshersby
ID: 12312522
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12312531
     Object o = yourField;
      System.out.println(o.getClass().isInterface());
0
Independent Software Vendors: 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!

 
LVL 92

Expert Comment

by:objects
ID: 12312907
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
 

Author Comment

by:bcsmess
ID: 12312976
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
 

Author Comment

by:bcsmess
ID: 12313000
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
 
LVL 92

Expert Comment

by:objects
ID: 12313197
sorry that should have been:

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

Expert Comment

by:CEHJ
ID: 12313463
>>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
 
LVL 92

Expert Comment

by:objects
ID: 12313482
> System.out.println(field.getType().isInterface());

I already posted that.
Please read previous posts before commenting.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12313489
And i've already answered the question much earlier ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12313515
your earlier suggestion won't work
and if you think it would then why are you now suggesting doing what I already suggested :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12313609
>>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
 
LVL 92

Expert Comment

by:objects
ID: 12313676
The code I posted works fine, the code you posted does not :)

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12313706
Works fine for me getting the initialized type of a field and testing whether an interface or not thanks very much ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12313729
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
 
LVL 92

Expert Comment

by:objects
ID: 12313762
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12313787
>>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
 
LVL 92

Expert Comment

by:objects
ID: 12313798
ROTFL
0
 

Author Comment

by:bcsmess
ID: 12317233
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
 
LVL 92

Expert Comment

by:objects
ID: 12317271
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12317472
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
 

Author Comment

by:bcsmess
ID: 12317894

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

Thanks again..
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12317933
works fine here.
0
 
LVL 92

Expert Comment

by:objects
ID: 12324388
:)
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 84
ejb entity bean example 2 56
Cisco ASA: Java web start no go, asdm launcher no go 3 86
What browser will run Java? 7 169
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question