Results 1 to 10 of 10
Thread: Get the list of imports
- 03-14-2011, 03:23 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
Get the list of imports
Hi *,
let's say I have following code:
Does someone knows how can I get the list of imports, in this example only "java.util.Vector"?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)); } }
I started with following, but I do not see how can I get required info:
Any ideas?Java Code:ClassLoader classLoader = MainClass.class.getClassLoader(); Class aClass = classLoader.loadClass("Test");
Thanks in advance!
- 03-15-2011, 04:18 AM #2
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
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 :-(
- 03-15-2011, 06:05 AM #3
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
- 03-15-2011, 03:27 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
Hi guys,
thanks for your input.
I've went with parsing .class file as string... This did the trick!
- 03-15-2011, 04:12 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 03-15-2011, 08:08 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
- 03-15-2011, 08:33 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 03-15-2011, 08:50 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
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.
- 03-16-2011, 07:26 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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:
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.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 ... }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-17-2011, 11:25 AM #10
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Static Imports in Java 5
By mrbharatmehta in forum Advanced JavaReplies: 3Last Post: 03-26-2009, 10:19 AM -
Imports
By whosadork in forum New To JavaReplies: 5Last Post: 09-07-2008, 01:32 AM -
cannot find imports..
By little_polarbear in forum New To JavaReplies: 4Last Post: 08-25-2008, 03:57 AM -
How to use Static Imports
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:37 PM -
Static imports (Java 5 and above)
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 10:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks