Re: programmatic way for detecting imports....

From: John C. Bollinger (jobollin_at_indiana.edu)
Date: 05/14/04


Date: Fri, 14 May 2004 09:38:06 -0500

MB wrote:

> I need to load a jar file and analyze it analyze it as follows. (I
> need a programmatic way for this)
>
> 1) for each class in a jar see what are the imports for that class.
> (if I can get a list of imports with what are the methods that are
> imported, then it will be good)

It is not possible to determine by analysis of a class file what
"import" statements were in the corresponding source file (if there was
one). The "import" statement is a Java feature that provides
convenience for programmers but has no representation in bytecode.

You may, however, really mean that you want to dynamically find a class'
dependencies on other classes. This has little to do with import
statements in the source code, but it is more likely to be a sensible
thing to do. Check out Roedy's response, and don't neglect the cross
references near the bottom of the page he points you to.

> 2) find if the imports are in the current class path or not.

The easiest way to determine whether a specific class is in a specific
class path is to attempt to load it. You can create a URLClassLoader
instance distinct from that for the running application to perform the
class loading without risking any interference with your checker app.

> I think it should be possible as Eclipse does this whenever you load a
> jar file.

Eclipse certainly does detect when your project depends on classes that
are not available within the project, and it will specifically flag
import statements that refer to such classes, to be sure. In that case,
however, it has the source files to work with. You may also find, if
you dig deeper, that many of the diagnostics that Eclipse provides it
obtains by compiling project sources with a compiler that issues verbose
and prolific diagnostics.

John Bollinger
jobollin@indiana.edu



Relevant Pages

  • Re: Extract information from a Java source (possible with syntax errors)
    ... Stefan Istrate a écrit: ... I want to extract information from a Java source file about the ... If you could trust thrid party tools, like eclipse compiler, you could get all you asked (AST, imports, etc.). ...
    (comp.lang.java.programmer)
  • Re: Indirect - Why does it seem to only work sometimes?
    ... The source file is renamed to standard one, ... or imports data from selected source file into workbook. ... in some of Excel NG's some time ago. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Imports question
    ... At the very top of your .vb source file ... Imports Microsoft.VisualBasic ... keyword at the top of your .cs source file: ... Prev by Date: ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Importing modules
    ... If you're having to set up your imports in a specific order, ... you have either a circular dependency or are overusing 'from ... you can get namespace problems. ... If builders built buildings the way programmers wrote ...
    (comp.lang.python)
  • Re: vb.net "Name is not declared" question
    ... Imports Lucene.Net.Documents ... details and modify your source file appropriately. ... Parser Error Message: ... >> start learning VB.NET from scratch, ...
    (microsoft.public.dotnet.general)