?
Solved

Check if a Type is an interface or class - Uses BCel Api

Posted on 2004-10-15
19
Medium Priority
?
327 Views
Last Modified: 2010-03-31
I have a JavaClass class under the following Bcel API

http://jakarta.apache.org/bcel/apidocs/index.html

class.getFields gets the fields
 
I have a field f I want to see if its INHERITED class (not declared class) is an interface or a regular class.
 - Inherited being if in Class1, I have field F initiialized as HashTable F = .... I want to get HashTable.

I can do this by doing f.getType() however now this returns a Type.  How do I check if this is an interface, etc.  or convert  this to a Class or something.
(Use the above link for the API)

Please help.

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
19 Comments
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12318381
The Field[] returned by the getFields() method contains objects that extend org.apache.bcel.classfile.AccessFlags (i.e. the Field class extends AccessFlags) - there is a suite of methods in this class, including "isInterface()" which you can use to decide the type of the field...

0
 
LVL 18

Expert Comment

by:armoghan
ID: 12318522
correctly said by cjjclifford Class.inTnterface() method returns that it is a class or interface
see this link for details
http://java.sun.com/developer/technicalArticles/DataTypes/ObjectsConscious/
0
 

Author Comment

by:bcsmess
ID: 12318576
Does isInterface() check if the class that declares it  is an inteface or if it's the initialized type class (HashTab).  I did it and it returns false for when getType is a Set though Set is an inteface.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 18

Expert Comment

by:armoghan
ID: 12318716
see this simple example

  Class c = null;
  try {
    c = Class.forName("java.util.Set");
  }
  catch (ClassNotFoundException ex) {
  }
   if(c.isInterface())
   {
     System.out.println("Is a interface");
   }
   else
     System.out.println("Is a class");
0
 

Author Comment

by:bcsmess
ID: 12318718
I also tried,

String f = field.getType().toString();
Class c = Class.forName(f);
return c.isInterface();

But it says it can't find the class that has the name f.  Do I need to crop the string somehow?
0
 

Author Comment

by:bcsmess
ID: 12318766
Your example works but how do you put a field.getType() into that String format.
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12318805
f
need to be fulll package path
0
 
LVL 18

Accepted Solution

by:
armoghan earned 1500 total points
ID: 12318808
if you are talking about such code, It will return class as the object is instanceof a Class,
Set s = new HashSet();
   Class c = null;
   c = s.getClass();

   if (c.isInterface()) {
     System.out.println("Is a interface");
   }
   else
    {
 System.out.println("Is a class");
   Class cl[] = c.getInterfaces();
}


so what you can do is call
   Class cl[] = c.getInterfaces();

and get all the interfaces
0
 

Author Comment

by:bcsmess
ID: 12318852
Well again, I don't want the declared class but the class Set to be checked.  s.getClass() returns the class that declares it, I think.  Is there anyway to do it like my example.  Thanks again.
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12318860
you need to do
field.getType().getName();
to get the full name
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12318875
Why dont you do simply
return  field.getType().isInterface();
0
 

Author Comment

by:bcsmess
ID: 12318943
There's no such method isInterface for Type.
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12318971
field is java.lang.reflect.Field?
it has method
public Class getType()
0
 

Author Comment

by:bcsmess
ID: 12319027
No.  In my original question, it gives you the API for the class file I"m talking about.  It's under a Bcel api.
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12319081
ohh sorry, I forgot about the original question :(
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12319099
Have you tried getSignature() ?
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12319112
Doesnot seem to have a method to get the Class object back
0
 

Author Comment

by:bcsmess
ID: 12319224
i can't tell what getsignature does.
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.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

801 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