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