forked from couchbase/couchbase-lite-java-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcludeDoclet.java
109 lines (96 loc) · 3.43 KB
/
ExcludeDoclet.java
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
import com.sun.javadoc.*;
import com.sun.tools.doclets.standard.Standard;
import com.sun.tools.javadoc.Main;
import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.List;
public class ExcludeDoclet
{
public static void main(String[] args)
{
String name = ExcludeDoclet.class.getName();
Main.execute(name, name, args);
}
public static boolean validOptions(String[][] options, DocErrorReporter reporter)
throws java.io.IOException
{
return Standard.validOptions(options, reporter);
}
public static int optionLength(String option)
{
return Standard.optionLength(option);
}
public static boolean start(RootDoc root)
throws java.io.IOException
{
return Standard.start((RootDoc)process(root, RootDoc.class));
}
private static boolean exclude(Doc doc)
{
if (doc instanceof ProgramElementDoc) {
ProgramElementDoc programElementDoc = (ProgramElementDoc) doc;
if (programElementDoc.containingPackage().tags("exclude").length > 0)
return true;
if (programElementDoc.containingClass() != null && programElementDoc.containingClass().tags("exclude").length > 0)
return true;
}
return doc.tags("exclude").length > 0;
}
private static Object process(Object obj, Class expect)
{
if (obj == null)
return null;
Class cls = obj.getClass();
if (cls.getName().startsWith("com.sun.")) {
return Proxy.newProxyInstance(cls.getClassLoader(),
cls.getInterfaces(),
new ExcludeHandler(obj));
} else if (obj instanceof Object[]) {
Class componentType = expect.getComponentType();
Object[] array = (Object[])obj;
List list = new ArrayList(array.length);
for (int i = 0; i < array.length; i++) {
Object entry = array[i];
if ((entry instanceof Doc) && exclude((Doc)entry))
continue;
list.add(process(entry, componentType));
}
return list.toArray((Object[])Array.newInstance(componentType, list.size()));
} else {
return obj;
}
}
private static class ExcludeHandler
implements InvocationHandler
{
private Object target;
public ExcludeHandler(Object target)
{
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
if (args != null) {
String methodName = method.getName();
if (methodName.equals("compareTo") ||
methodName.equals("equals") ||
methodName.equals("overrides") ||
methodName.equals("subclassOf")) {
args[0] = unwrap(args[0]);
}
}
try {
return process(method.invoke(target, args), method.getReturnType());
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
private Object unwrap(Object proxy)
{
if (proxy instanceof Proxy)
return ((ExcludeHandler)Proxy.getInvocationHandler(proxy)).target;
return proxy;
}
}
}