This discussion is archived
1 2 Previous Next 19 Replies Latest reply: Feb 24, 2010 1:35 AM by 791266 RSS

get .Class using just name.

843798 Newbie
Currently Being Moderated
HI,

is there a way to get the .Class using just name.

i.e.

given JPanel.class

it sould return .Class instance of JPanel.


Thank you.

Edited by: Neo__matriX on Feb 20, 2010 8:30 AM
  • 1. Re: get .Class using just name.
    843798 Newbie
    Currently Being Moderated
    Are you talking about a class literal?
    import javax.swing.JPanel;
    ...
       Class k = JPanel.class;
  • 2. Re: get .Class using just name.
    843798 Newbie
    Currently Being Moderated
    Sorry about not being clear.


    i have HashMap of class names some of them belongs to java library and my HashMap does not store fully-qualified name.

    and i dont want to have 50 lines of import statement for all possible classes.

    that's why i am lloking a way to get the *.class* instance or fully-qualified name from unqualified name (i.e. JPanel).


    i hope i have added some clearity in my question.

    Thank you,
  • 3. Re: get .Class using just name.
    843798 Newbie
    Currently Being Moderated
    Neo__matriX wrote:
    i have HashMap of class names
    some of them belongs to java library and my HashMap does not store fully-qualified name.

    i hope i have added some clarity in my question.
    Not really.
    A map has keys and values.
    Are you saying your map keys are Strings of unqualified class names?
    What are the values? (not that it matters, just trying to understand what you are trying to do).
    and i do not want to have 50 lines of import statement for all possible classes.
    If you are talking about Strings with class names rather than class literals
    the import statements make no difference anyway.
    that's why i am looking a way to get the *.class* instance or fully-qualified name from unqualified name (i.e. JPanel).
    You could write code to scan the classpath (scan into every jar on the classpath) and store the name of every class found
    in an [apache MultiMap|http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/MultiMap.html] with the unqualified class name as key.

    Ok, that was the preparation.

    Now, when you have an unqualified class name
    you can look up in the MultiMap and see what candidates there are.

    OFC the candidate lists might change when you add more jars to your classpath.
  • 4. Re: get .Class using just name.
    EJP Guru
    Currently Being Moderated
    my HashMap does not store fully-qualified name.
    Why not? Change that. It is a recipe for ambiguity.
  • 5. Re: get .Class using just name.
    843798 Newbie
    Currently Being Moderated
    ejp wrote:
    my HashMap does not store fully-qualified name.
    Why not? Change that. It is a recipe for ambiguity.
    well i can not change it as what i am trying to do is reading lots of java source files.

    finding out if it extands any other class then put it in to HashMap(className, superClass) and then build depth of inheratance tree, problem is it works fine for all the source classes but if any of them extends java class (i.e. JPanel) then it will throw an error as there will be no entry for JPanel in my HashMap.

    if there is any way to find that if class belongs to which library or to find fully-qualified name then i can use classLoader to load that class and finish my depth of inheratance tree.


    Thank you.
  • 6. Re: get .Class using just name.
    DrClap Expert
    Currently Being Moderated
    Neo__matriX wrote:
    ejp wrote:
    my HashMap does not store fully-qualified name.
    Why not? Change that. It is a recipe for ambiguity.
    well i can not change it as what i am trying to do is reading lots of java source files.

    if there is any way to find that if class belongs to which library or to find fully-qualified name then i can use classLoader to load that class and finish my depth of inheratance tree.
    Well, of course there is. The first line of the class should be "package a.b.c" and that tells you most of the FQN. Or if it isn't there, the class is in the default package. Then when you find the class declaration, that tells you the rest of the FQN.
  • 7. Re: get .Class using just name.
    796085 Newbie
    Currently Being Moderated
    If you're going to do that, you need to do it the same way as the compiler does - an that means using fully qualified names.

    Let's say your map contains the String "Type". What class does that refer to? It could be:

    java.lang.reflect.Type
    java.net.InetAddress.Cache.Type
    java.net.Proxy.Type
    java.security.KeyRep.Type
    javax.sound.sampled.AudioFileFormat.Type
    javax.sound.sampled.BooleanControl.Type
    javax.sound.sampled.CompoundControl.Type
    javax.sound.sampled.EnumControl.Type
    javax.sound.sampled.FloatControl.Type
    javax.sound.sampled.LineEvent.Type

    plus loads of others from GWT, BCEL, ASM, Xalan, HtmlUnit, etc. if you're using any of those in your project.

    In short, you need fully qualified names. Period.
  • 8. Re: get .Class using just name.
    843798 Newbie
    Currently Being Moderated
    Neo__matriX wrote:
    ejp wrote:
    my HashMap does not store fully-qualified name.
    Why not? Change that. It is a recipe for ambiguity.
    well i can not change it as what i am trying to do is reading lots of java source files.
    If you parse the class files instead
    javac will already have resolved all class names for you.
  • 9. Re: get .Class using just name.
    EJP Guru
    Currently Being Moderated
    well i can not change it
    There is no other correct solution.
    as what i am trying to do is reading lots of java source files.
    That's not a reason why you can't change it.
  • 10. Re: get .Class using just name.
    843798 Newbie
    Currently Being Moderated
    ejp wrote:
    well i can not change it
    There is no other correct solution.
    as what i am trying to do is reading lots of java source files.
    That's not a reason why you can't change it.
    what i am trying to say here is when i read a source file it gives me only the superclass name not the full-qualified name.

    as i am using javaparser to parse file and getting the superclass name using getExtends() method which returns superclass name as a string.

    Edited by: Neo__matriX on Feb 21, 2010 2:33 PM
  • 11. Re: get .Class using just name.
    EJP Guru
    Currently Being Moderated
    what i am trying to say here is when i read a source file it gives me only the superclass name not the full-qualified name.
    And as has already been pointed out, that claim is nonsense. What do you think the package statement is for?
    as i am using javaparser to parse file and getting the superclass name using getExtends() method which returns superclass name as a string.
    So you do have access to the fully qualified name. So why say you don't? Several times?
  • 12. Re: get .Class using just name.
    843798 Newbie
    Currently Being Moderated
    ejp wrote:
    what i am trying to say here is when i read a source file it gives me only the superclass name not the full-qualified name.
    And as has already been pointed out, that claim is nonsense. What do you think the package statement is for?
    i dont get what you mean here.
    as i am using javaparser to parse file and getting the superclass name using getExtends() method which returns superclass name as a string.
    So you do have access to the fully qualified name. So why say you don't? Several times?
    i have clearly stated that it gives me super class name and not full-qualified name.

    exmple
    public class ObjectBench extends JPanel {
    ...
    ...
    ...
    ...
    }
    when i parse this java source file and use getExtends() it returns JPanel

    Thank you.
  • 13. Re: get .Class using just name.
    EJP Guru
    Currently Being Moderated
    what i am trying to say here is when i read a source file it gives me only the superclass name not the full-qualified name.
    And as has already been pointed out, that claim is nonsense. What do you think the package statement is for?
    i dont get what you mean here.
    I mean that when you read the source file one of the things you have to read is the package statement.
    when i parse this java source file
    With what?
    and use getExtends()
    That's not part of the JDK. What is it? Are you using some Java parser that doesn't give you the package of a class?
  • 14. Re: get .Class using just name.
    796085 Newbie
    Currently Being Moderated
    ejp wrote:
    That's not part of the JDK. What is it? Are you using some Java parser that doesn't give you the package of a class?
    To be fair, the superclass isn't necessarily in the same package as the class being parsed.

    @OP, did you read my response #7? What do you think about it? There really is no choice but to deal with fully qualified names and that means replicating the behaviour of the parser - which isn't easy and means understanding scoping rules, keeping a symbol table, parsing import statements, etc. - or else dealing with class files instead of source files.
1 2 Previous Next