Re: does the following code function as expected?
Aryeh M. Friedman wrote:
Assuming standard Java naming conventions does the following code (in
the general case) do the following:
Did you try it? What test cases did you try?
1. List all imported packages and/or explicitilly named dotted classes
No. It'll also fail on static imports.
2. List all simple class names
Do you mean in the source? It'll fail on constants from a class, e.g.,
char cb = Util.SOME_CONSTANT;
Assuming there is no other use of Util than to bring in constants.
3. Not list keywords, literals, instance names
No. For example, it'll miss (i.e., list) StreamTokenizer.TT_EOF.
4. For any thing matching items 1 and 2 list them only once in
outpurimport java.io.*;
Are you asking if Set only holds one of each item, as determined by equals()?
Note on final applications: I want to write a tool that will
determine from source only what classes the current source file depend
on. After a little more processing the final output is a DAG
representing the order stuff would need to be compiled in for a non-
JIT compiler.
This approach will not work. It'll miss any occurrence of idioms such as
Class.forName( "foo.bar.jdbc.YourDriver" ), and its parsing is far too simple
to handle the cases it does attempt. Furthermore, it doesn't follow the
dependency chain past the direct references in the source.
Bytecode analysis will be more reliable than source analysis. (For one thing,
there are no imports in bytecode.) Even there, the dynamic nature of Java
makes this not a perfectly solvable problem in general, although workable
subsets of the problem can be approached.
--
Lew