A
Aryeh M. Friedman
Assuming standard Java naming conventions does the following code (in
the general case) do the following:
1. List all imported packages and/or explicitilly named dotted classes
2. List all simple class names
3. Not list keywords, literals, instance names
4. For any thing matching items 1 and 2 list them only once in
outpurimport java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
throws Throwable
{
FileReader rd = new FileReader("Main.java");
StreamTokenizer st = new StreamTokenizer(rd);
boolean endImports=false;
Set<String> out=new HashSet<String>();
int token = st.nextToken();
while (token != StreamTokenizer.TT_EOF)
{
token = st.nextToken();
if(token==StreamTokenizer.TT_WORD)
// this is completely naive but it is good
enough for Aryeh's style
if((Character.isUpperCase(st.sval.charAt(0))&&st.sval.indexOf(".")==-1)
||(!
Character.isUpperCase(st.sval.charAt(0))&&st.sval.indexOf(".")!=-1&&!
endImports)){
if(Character.isUpperCase(st.sval.charAt(0))&&!endImports) {
endImports=true;
continue;
}
if(!st.sval.matches("[A-Z]*"))
out.add(st.sval);
}
}
rd.close();
System.out.println(out);
}
}
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.
the general case) do the following:
1. List all imported packages and/or explicitilly named dotted classes
2. List all simple class names
3. Not list keywords, literals, instance names
4. For any thing matching items 1 and 2 list them only once in
outpurimport java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
throws Throwable
{
FileReader rd = new FileReader("Main.java");
StreamTokenizer st = new StreamTokenizer(rd);
boolean endImports=false;
Set<String> out=new HashSet<String>();
int token = st.nextToken();
while (token != StreamTokenizer.TT_EOF)
{
token = st.nextToken();
if(token==StreamTokenizer.TT_WORD)
// this is completely naive but it is good
enough for Aryeh's style
if((Character.isUpperCase(st.sval.charAt(0))&&st.sval.indexOf(".")==-1)
||(!
Character.isUpperCase(st.sval.charAt(0))&&st.sval.indexOf(".")!=-1&&!
endImports)){
if(Character.isUpperCase(st.sval.charAt(0))&&!endImports) {
endImports=true;
continue;
}
if(!st.sval.matches("[A-Z]*"))
out.add(st.sval);
}
}
rd.close();
System.out.println(out);
}
}
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.