Solved

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

Posted on 2004-10-15
19
315 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 59
spring jars download 1 53
Adding multiple JVM environments to RedHat 6 7 70
Why doesn't this text field show up on my Applet frame? 2 28
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…
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…
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…
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.

730 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