Solved

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

Posted on 2004-10-14
24
215 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Passing list of object to Oracle Database Procedure 3 59
wordappend challenge 8 145
simple java question 3 44
javap not working 8 36
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

912 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now