Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-15
19
Medium Priority
?
335 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 video teaches viewers about errors in exception handling.
Suggested Courses

609 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