Solved

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

Posted on 2004-10-15
19
295 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
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
 
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 500 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
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 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

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

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

18 Experts available now in Live!

Get 1:1 Help Now