Skip to content

Commit 2b38431

Browse files
author
jossonsmith
committed
Clean up ASTScriptVisitor: Fixed bugs of last refactoring and continue to add comments
1 parent 57afd16 commit 2b38431

File tree

8 files changed

+498
-320
lines changed

8 files changed

+498
-320
lines changed

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTJ2SDocVisitor.java

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.List;
1919
import org.eclipse.jdt.core.dom.ASTNode;
2020
import org.eclipse.jdt.core.dom.Block;
21+
import org.eclipse.jdt.core.dom.BodyDeclaration;
2122
import org.eclipse.jdt.core.dom.CatchClause;
2223
import org.eclipse.jdt.core.dom.Comment;
2324
import org.eclipse.jdt.core.dom.CompilationUnit;
@@ -33,16 +34,19 @@
3334
import org.eclipse.jdt.core.dom.TextElement;
3435

3536
/**
37+
* This level of Visitor will try to focus on dealing with those
38+
* j2s* Javadoc tags.
39+
*
3640
* @author zhou renjian
3741
*
3842
* 2006-12-4
3943
*/
40-
public class ASTJ2SDocVisitor extends ASTKeywordParser {
44+
public class ASTJ2SDocVisitor extends ASTKeywordVisitor {
4145

4246
private Javadoc[] nativeJavadoc = null;
4347

4448
private ASTNode javadocRoot = null;
45-
49+
4650
private boolean isDebugging = false;
4751

4852

@@ -134,7 +138,7 @@ boolean visitNativeJavadoc(Javadoc javadoc, Block node, boolean superVisit) {
134138
return false;
135139
}
136140
}
137-
if (isDebugging) {
141+
if (isDebugging()) {
138142
for (Iterator iter = tags.iterator(); iter.hasNext();) {
139143
TagElement tagEl = (TagElement) iter.next();
140144
if ("@j2sDebug".equals(tagEl.getTagName())) {
@@ -278,4 +282,47 @@ private int getPreviousStartPosition(Block node) {
278282
return previousStart;
279283
}
280284

285+
286+
/**
287+
* Method with "j2s*" tag.
288+
*
289+
* @param node
290+
* @return
291+
*/
292+
protected TagElement getJ2SDocTag(BodyDeclaration node, String tagName) {
293+
Javadoc javadoc = node.getJavadoc();
294+
if (javadoc != null) {
295+
List tags = javadoc.tags();
296+
if (tags.size() != 0) {
297+
for (Iterator iter = tags.iterator(); iter.hasNext();) {
298+
TagElement tagEl = (TagElement) iter.next();
299+
if (tagName.equals(tagEl.getTagName())) {
300+
return tagEl;
301+
}
302+
}
303+
}
304+
}
305+
return null;
306+
}
307+
308+
/**
309+
* Native method without "j2sDebug" or "j2sNative" tag should be ignored
310+
* directly.
311+
*
312+
* @param node
313+
* @return
314+
*/
315+
protected boolean isMethodNativeIgnored(MethodDeclaration node) {
316+
if ((node.getModifiers() & Modifier.NATIVE) != 0) {
317+
if (isDebugging() && getJ2SDocTag(node, "@j2sDebug") != null) {
318+
return false;
319+
}
320+
if (getJ2SDocTag(node, "@j2sNative") != null) {
321+
return false;
322+
}
323+
return true;
324+
}
325+
return true; // interface!
326+
}
327+
281328
}

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTJ2SMapVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ protected boolean isInheritedFieldName(ITypeBinding binding, String name) {
172172
*/
173173
return false;
174174
}
175-
if (binding != null) {
175+
if (binding == null) {
176176
return false;
177177
}
178178
ITypeBinding superclass = binding.getSuperclass();

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTMethodVisitor.java

Lines changed: 149 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
import java.util.HashSet;
1818
import java.util.Map;
1919
import java.util.Set;
20+
import org.eclipse.jdt.core.dom.ASTNode;
21+
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
22+
import org.eclipse.jdt.core.dom.IMethodBinding;
23+
import org.eclipse.jdt.core.dom.ITypeBinding;
24+
import org.eclipse.jdt.core.dom.MethodDeclaration;
25+
import org.eclipse.jdt.core.dom.Modifier;
2026

2127
/**
2228
* @author zhou renjian
@@ -27,8 +33,96 @@ public class ASTMethodVisitor extends ASTTypeVisitor {
2733

2834
private static Set methodSet;
2935
private static Map pmMap;
36+
public static final String PACKAGE_PREFIX;
37+
public static String[] mapDocument;
38+
public static String[] mapNode;
39+
public static String[] mapNodeList;
40+
public static String[] mapNamedNodeMap;
41+
public static String[] mapCharacterData;
42+
public static String[] mapAttr;
43+
public static String[] mapElement;
44+
public static String[] mapDocumentType;
45+
public static String[] mapNotation;
46+
public static String[] mapEntity;
47+
public static String[] mapProcessingInstruction;
48+
3049

3150
static {
51+
PACKAGE_PREFIX = "org.w3c.dom.";
52+
53+
mapDocument = new String[] {
54+
"Document",
55+
"doctype",
56+
"implementation",
57+
"documentElement"
58+
};
59+
mapNode = new String[] {
60+
"Node",
61+
"nodeName",
62+
"nodeValue",
63+
"nodeType",
64+
"parentNode",
65+
"childNodes",
66+
"firstChild",
67+
"lastChild",
68+
"previousSibling",
69+
"nextSibling",
70+
"attributes",
71+
"ownerDocument",
72+
"namespaceURI",
73+
"prefix",
74+
"localName"
75+
};
76+
mapNodeList = new String[] {
77+
"NodeList",
78+
"length"
79+
};
80+
mapNamedNodeMap = new String[] {
81+
"NamedNodeMap",
82+
"length"
83+
};
84+
mapCharacterData = new String[] {
85+
"CharacterData",
86+
"data",
87+
"length"
88+
};
89+
mapAttr = new String[] {
90+
"Attr",
91+
"name",
92+
"specified",
93+
"value",
94+
"ownerElement",
95+
};
96+
mapElement = new String[] {
97+
"Element",
98+
"tagName"
99+
};
100+
mapDocumentType = new String[] {
101+
"DocumentType",
102+
"name",
103+
"entities",
104+
"notations",
105+
"publicId",
106+
"systemId",
107+
"internalSubset"
108+
};
109+
mapNotation = new String[] {
110+
"Notation",
111+
"publicId",
112+
"systemId"
113+
};
114+
mapEntity = new String[] {
115+
"Entity",
116+
"publicId",
117+
"systemId",
118+
"notationName"
119+
};
120+
mapProcessingInstruction = new String[] {
121+
"ProcessingInstruction",
122+
"target",
123+
"data"
124+
};
125+
32126
init();
33127
}
34128

@@ -54,80 +148,6 @@ public static String translate(String className, String methodName) {
54148
return (String) pmMap.get(className + "." + methodName);
55149
}
56150

57-
public static final String PACKAGE_PREFIX = "org.w3c.dom.";
58-
public static String[] mapDocument = new String[] {
59-
"Document",
60-
"doctype",
61-
"implementation",
62-
"documentElement"
63-
};
64-
public static String[] mapNode = new String[] {
65-
"Node",
66-
"nodeName",
67-
"nodeValue",
68-
"nodeType",
69-
"parentNode",
70-
"childNodes",
71-
"firstChild",
72-
"lastChild",
73-
"previousSibling",
74-
"nextSibling",
75-
"attributes",
76-
"ownerDocument",
77-
"namespaceURI",
78-
"prefix",
79-
"localName"
80-
};
81-
public static String[] mapNodeList = new String[] {
82-
"NodeList",
83-
"length"
84-
};
85-
public static String[] mapNamedNodeMap = new String[] {
86-
"NamedNodeMap",
87-
"length"
88-
};
89-
public static String[] mapCharacterData = new String[] {
90-
"CharacterData",
91-
"data",
92-
"length"
93-
};
94-
public static String[] mapAttr = new String[] {
95-
"Attr",
96-
"name",
97-
"specified",
98-
"value",
99-
"ownerElement",
100-
};
101-
public static String[] mapElement = new String[] {
102-
"Element",
103-
"tagName"
104-
};
105-
public static String[] mapDocumentType = new String[] {
106-
"DocumentType",
107-
"name",
108-
"entities",
109-
"notations",
110-
"publicId",
111-
"systemId",
112-
"internalSubset"
113-
};
114-
public static String[] mapNotation = new String[] {
115-
"Notation",
116-
"publicId",
117-
"systemId"
118-
};
119-
public static String[] mapEntity = new String[] {
120-
"Entity",
121-
"publicId",
122-
"systemId",
123-
"notationName"
124-
};
125-
public static String[] mapProcessingInstruction = new String[] {
126-
"ProcessingInstruction",
127-
"target",
128-
"data"
129-
};
130-
131151
protected static void registerMap(String[] map) {
132152
for (int i = 1; i < map.length; i++) {
133153
register(PACKAGE_PREFIX + map[0],
@@ -148,4 +168,58 @@ public static void registerAllMaps() {
148168
registerMap(mapNotation);
149169
registerMap(mapEntity);
150170
registerMap(mapProcessingInstruction);
151-
}}
171+
}
172+
173+
174+
175+
private boolean testForceOverriding(IMethodBinding method) {
176+
String methodName = method.getName();
177+
ITypeBinding classInHierarchy = method.getDeclaringClass();
178+
do {
179+
IMethodBinding[] methods = classInHierarchy.getDeclaredMethods();
180+
int count = 0;
181+
IMethodBinding superMethod = null;
182+
for (int i= 0; i < methods.length; i++) {
183+
if (methodName.equals(methods[i].getName())) {
184+
count++;
185+
superMethod = methods[i];
186+
}
187+
}
188+
if (count > 1) {
189+
return false;
190+
} else if (count == 1) {
191+
if (!Bindings.isSubsignature(method, superMethod)) {
192+
return false;
193+
} else if ((superMethod.getModifiers() & Modifier.PRIVATE) != 0) {
194+
return false;
195+
}
196+
}
197+
classInHierarchy = classInHierarchy.getSuperclass();
198+
} while (classInHierarchy != null);
199+
return true;
200+
}
201+
202+
/**
203+
* Check whether the given method can be defined by "Clazz.overrideMethod" or not.
204+
* @param node
205+
* @return
206+
*/
207+
protected boolean canAutoOverride(MethodDeclaration node) {
208+
boolean isOK2AutoOverriding = false;
209+
IMethodBinding methodBinding = node.resolveBinding();
210+
if (testForceOverriding(methodBinding)) {
211+
IMethodBinding superMethod = Bindings.findMethodDeclarationInHierarchy(methodBinding.getDeclaringClass(), methodBinding);
212+
if (superMethod != null) {
213+
ASTNode parentRoot = node.getParent();
214+
while (parentRoot != null && !(parentRoot instanceof AbstractTypeDeclaration)) {
215+
parentRoot = parentRoot.getParent();
216+
}
217+
if (parentRoot != null) {
218+
isOK2AutoOverriding = !MethodReferenceASTVisitor.checkReference(parentRoot, superMethod.getKey());
219+
}
220+
}
221+
}
222+
return isOK2AutoOverriding;
223+
}
224+
225+
}

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTPackageVisitor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313

1414
package net.sf.j2s.core.astvisitors;
1515

16-
import org.eclipse.jdt.core.dom.ASTVisitor;
1716

1817
/**
1918
* @author zhou renjian
2019
*
2120
* 2006-12-3
2221
*/
23-
public class ASTPackageVisitor extends ASTVisitor {
22+
public class ASTPackageVisitor extends ASTTigerVisitor {
2423

2524
protected String thisPackageName = "";
2625

0 commit comments

Comments
 (0)