Results 1 to 10 of 10
  1. #1
    kfranic is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Question Get the list of imports

    Hi *,
    let's say I have following code:

    Java Code:
    import java.util.Vector;
    
    public class Test {
      public void printMe() {
        Vector<String> v = new Vector<String>();
        v.add("test");
        System.out.println(v.get(0));
      }
    }
    Does someone knows how can I get the list of imports, in this example only "java.util.Vector"?

    I started with following, but I do not see how can I get required info:
    Java Code:
    ClassLoader classLoader = MainClass.class.getClassLoader();
    Class aClass = classLoader.loadClass("Test");
    Any ideas?

    Thanks in advance!

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Post

    Why don't you read the class as a string and then search all the lines which start with import.

    I don't think java provides you any api for this :-(

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    imports are just syntactic sugar for the source code. Don't expect to be able to detect imports from the .class files.

    If you need this, you will have to read and parse the source (i.e. .java) file. But why do you need this at all?

    db

  4. #4
    kfranic is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Thumbs up

    Hi guys,
    thanks for your input.
    I've went with parsing .class file as string... This did the trick!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kfranic View Post
    Hi guys,
    thanks for your input.
    I've went with parsing .class file as string... This did the trick!
    AFAIK there is no notion of 'imported' classes in a class file ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    kfranic is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    AFAIK there is no notion of 'imported' classes in a class file ...

    kind regards,

    Jos
    Indeed.
    I'm doing dumb parsing of the class file (like strings in UNIX) and only take the pattern I need.
    Not very nice, but it works :)

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kfranic View Post
    Indeed.
    I'm doing dumb parsing of the class file (like strings in UNIX) and only take the pattern I need.
    Not very nice, but it works :)
    Ah, ok, you mean the .java source file.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    kfranic is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    No I actually mean class file.
    Try to run "strings" command on .class file...
    I've noticed that few tings can't be parsed directly that way (e.g. "type a=Interface.constant"), but so far it works well enough.

    Simple grep does not help as one can use imports with "*" or they can just type inline full class name with package... Grep source files approach for "import" keyword was no OK in my env. I would have to do a parser with lexer support for Java, etc... Too complicated for task at hand. Unless you (or some other reader) know of a GPL or similar licensed product that can parse source file (.java) with similar results I'd be interested... I guess Eclipse and e.g Net Beans have to do it somehow... But googling did not help, perhaps I've used wrong keywords...
    Last edited by kfranic; 03-15-2011 at 08:54 PM.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kfranic View Post
    I would have to do a parser with lexer support for Java, etc... Too complicated for task at hand. Unless you (or some other reader) know of a GPL or similar licensed product that can parse source file (.java) with similar results I'd be interested... I guess Eclipse and e.g Net Beans have to do it somehow... But googling did not help, perhaps I've used wrong keywords...
    You can use Javac'c API; if you go to the index page of the API documentation you'll see a 'wall' of bricks. Every brick mentions one of the available Java technologies in the SE suite. The top row, second brick from the left mentions 'javac'; click it and you'll see the entire javac API documentation. The idea is to have javac parse your source file(s) and process the AST (Abstract Syntax Tree) with an appropriate visitor. The visitor looks something like this:

    Java Code:
    class TreeVisitor extends TreePathScanner<Void, Void> {
    
    	public Void visitCompilationUnit(CompilationUnitTree node, Void arg) {
    		
    		System.out.println("CompilationUnitTree: "+node);
    		return super.visitCompilationUnit(node, arg);
    	}
    
    	public Void visitImport(ImportTree node, Void arg) {
    		
    		System.out.println("ImportTree: "+node);
    		return super.visitImport(node, arg);
    	}
    
    	// more node visitora optionally here ...
    }
    It's the last method above you're interested in. Read the mentioned API documentation for the details. The AST definition is in package com.sun.source.tree.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    kfranic is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Static Imports in Java 5
    By mrbharatmehta in forum Advanced Java
    Replies: 3
    Last Post: 03-26-2009, 10:19 AM
  2. Imports
    By whosadork in forum New To Java
    Replies: 5
    Last Post: 09-07-2008, 01:32 AM
  3. cannot find imports..
    By little_polarbear in forum New To Java
    Replies: 4
    Last Post: 08-25-2008, 03:57 AM
  4. How to use Static Imports
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:37 PM
  5. Static imports (Java 5 and above)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 10:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •