1717import java .util .HashSet ;
1818import java .util .Map ;
1919import 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+ }
0 commit comments