Skip to content

Commit 11f631c

Browse files
committed
Initial commit of the Doclet class
1 parent 0acb45b commit 11f631c

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

src/javaxt/utils/src/Doclet.java

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package javaxt.utils.src;
2+
3+
import java.util.*;
4+
import javax.lang.model.SourceVersion;
5+
import jdk.javadoc.doclet.Reporter;
6+
import jdk.javadoc.doclet.DocletEnvironment;
7+
8+
9+
//******************************************************************************
10+
//** Doclet Class
11+
//******************************************************************************
12+
/**
13+
* Custom doclet implementation that uses the Parser class instead of the
14+
* native java libraries to parse source files.
15+
*
16+
******************************************************************************/
17+
18+
public class Doclet implements jdk.javadoc.doclet.Doclet {
19+
private String fileName;
20+
private String directory;
21+
22+
private boolean processArg(String opt, List<String> arguments){
23+
if (opt.equals("-d")) directory = arguments.get(0);
24+
if (opt.equals("-filename")) fileName = arguments.get(0);
25+
return true;
26+
}
27+
28+
29+
@Override
30+
public void init(Locale locale, Reporter reporter) {
31+
}
32+
33+
@Override
34+
public boolean run(DocletEnvironment docEnv) {
35+
//System.out.println("directory: " + directory);
36+
//System.out.println("fileName: " + fileName);
37+
38+
39+
HashSet<javaxt.io.File> files = new HashSet<>();
40+
Iterator it = docEnv.getIncludedElements().iterator();
41+
while (it.hasNext()){
42+
Object obj = it.next(); //com.sun.tools.javac.code.Symbol$ClassSymbol
43+
44+
//Get classname
45+
String className = obj.toString();
46+
47+
48+
//Get path
49+
String path = null;
50+
try{
51+
java.lang.reflect.Field field;
52+
Object f;
53+
54+
//Get sourcefile (DirectoryFileObject)
55+
Object sourceFile = getFieldValue("sourcefile", obj);
56+
//System.out.println("sourcefile: " + sourceFile);
57+
58+
59+
//Get base path
60+
Object basePath = getFieldValue("userPackageRootDir", sourceFile);
61+
//System.out.println("userPackageRootDir: " + basePath);
62+
javaxt.io.Directory dir = new javaxt.io.Directory(basePath.toString());
63+
64+
65+
//Get relative path
66+
Object relativePath = getFieldValue("relativePath", sourceFile);
67+
//System.out.println("relativePath: " + relativePath);
68+
field = relativePath.getClass().getSuperclass().getDeclaredField("path");
69+
field.setAccessible(true);
70+
f = field.get(relativePath);
71+
//System.out.println("path: " + f);
72+
path = dir + f.toString();
73+
files.add(new javaxt.io.File(path));
74+
}
75+
catch(Throwable e){
76+
}
77+
78+
79+
// if (path!=null){
80+
// System.out.println(className);
81+
// System.out.println("path: " + path);
82+
// System.out.println();
83+
// }
84+
}
85+
86+
for (javaxt.io.File file : files){
87+
//System.out.println(file);
88+
try{
89+
90+
ArrayList<Class> classes = new Parser(file).getClasses();
91+
for (Class c : classes){
92+
//System.out.println(" - " + c.getName());
93+
}
94+
}
95+
catch(Exception e){
96+
System.err.println("Failed to parse " + file.getName(false));
97+
}
98+
}
99+
100+
return true;
101+
}
102+
103+
104+
private Object getFieldValue(String fieldName, Object obj) throws Exception {
105+
java.lang.reflect.Field field;
106+
field = obj.getClass().getDeclaredField(fieldName);
107+
field.setAccessible(true);
108+
return field.get(obj);
109+
}
110+
111+
@Override
112+
public String getName() {
113+
return "JavaXT Doclet";
114+
}
115+
116+
@Override
117+
public SourceVersion getSupportedSourceVersion() {
118+
return SourceVersion.latest();
119+
}
120+
121+
@Override
122+
public Set<? extends Option> getSupportedOptions() {
123+
HashSet<Option> options = new HashSet<>();
124+
options.add(createOption("-filename"));
125+
options.add(createOption("-d"));
126+
return options;
127+
}
128+
129+
private Option createOption(String key){
130+
return new Option() {
131+
private final List<String> keys = Arrays.asList(
132+
key
133+
);
134+
135+
@Override
136+
public int getArgumentCount() {
137+
return 1;
138+
}
139+
140+
@Override
141+
public String getDescription() {
142+
return "an option with aliases";
143+
}
144+
145+
@Override
146+
public Option.Kind getKind() {
147+
return Option.Kind.STANDARD;
148+
}
149+
150+
@Override
151+
public List<String> getNames() {
152+
return keys;
153+
}
154+
155+
@Override
156+
public String getParameters() {
157+
return "file";
158+
}
159+
160+
@Override
161+
public boolean process(String opt, List<String> arguments) {
162+
return processArg(opt, arguments);
163+
}
164+
};
165+
}
166+
}

0 commit comments

Comments
 (0)