Solved

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

Posted on 2004-10-15
19
304 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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
 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 video teaches viewers about errors in exception handling.

777 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