Solved

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

Posted on 2004-10-14
24
214 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
changePi Challenge 15 76
Starting to learn JAVA, 7 48
Java / Linux and Regular Expressions 11 47
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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.

757 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