2424import java .util .Collections ;
2525import java .util .HashMap ;
2626import java .util .HashSet ;
27+ import java .util .IdentityHashMap ;
2728import java .util .List ;
2829import java .util .Map ;
2930import java .util .Objects ;
117118import org .eclipse .jdt .internal .core .util .Util ;
118119
119120public class DOMJavaSearchDelegate implements IJavaSearchDelegate {
120- private Map <PossibleMatch , NodeSetWrapper > matchToWrapper = new HashMap <>();
121121 public DOMJavaSearchDelegate () {
122-
122+ // must be kept as it's used by an extension point
123123 }
124124
125125 @ Override
126126 public void locateMatches (MatchLocator locator , IJavaProject javaProject , PossibleMatch [] possibleMatches , int start ,
127127 int length ) throws CoreException {
128128 locator .initialize ((JavaProject )javaProject , length );
129+
130+ Map <MatchingNodeSet , NodeSetWrapper > wrappedSets = new IdentityHashMap <>(); // PossibleMatch hashCode collides too easily
129131 for ( int i = 0 ; i < possibleMatches .length ; i ++ ) {
130- matchToWrapper .put (possibleMatches [i ], wrapNodeSet (possibleMatches [i ].nodeSet ));
132+ wrappedSets .put (possibleMatches [i ]. nodeSet , wrapNodeSet (possibleMatches [i ].nodeSet ));
131133 }
132134
133-
134- Map <String , String > map = javaProject .getOptions (true );
135- map .put (CompilerOptions .OPTION_TaskTags , org .eclipse .jdt .internal .compiler .util .Util .EMPTY_STRING );
136- locator .options = new CompilerOptions (map );
135+ Map <String , String > options = javaProject .getOptions (true );
136+ options .put (CompilerOptions .OPTION_TaskTags , org .eclipse .jdt .internal .compiler .util .Util .EMPTY_STRING );
137+ locator .options = new CompilerOptions (options );
137138
138139 // Original implementation used Map throughout, however,
139140 // PossibleMatch was determined to be a bad / non-unique key where the
@@ -192,15 +193,10 @@ public void acceptAST(org.eclipse.jdt.core.ICompilationUnit source,
192193 if (pm != null ) {
193194 for (int i = 0 ; i < possibleMatches .length ; i ++) {
194195 if (possibleMatches [i ] == pm ) {
195- // String s = CharOperation.toString(pm.compoundName);
196- // if( "g1.t.s.ref.R1".equals(s)) {
197- // if( "g3.t.ref.R4".equals(s)) {
198- // int z = 5; z++; if( z == 3 ) {}
199- // }
200196 domUnits [i ] = ast ;
201197 nonNullDomIndexes .add (i );
202198 locator .currentPossibleMatch = pm ;
203- NodeSetWrapper wrapper = matchToWrapper .get (possibleMatches [i ]);
199+ NodeSetWrapper wrapper = wrappedSets .get (possibleMatches [i ]. nodeSet );
204200 ast .accept (new PatternLocatorVisitor (locator , wrapper ));
205201 return ;
206202 }
@@ -214,7 +210,7 @@ public void acceptAST(org.eclipse.jdt.core.ICompilationUnit source,
214210 for (int x : nonNullDomIndexes ) {
215211 PossibleMatch possibleMatch = possibleMatches [x ];
216212 locator .currentPossibleMatch = possibleMatch ;
217- NodeSetWrapper wrapper = this . matchToWrapper . get (possibleMatch );
213+ NodeSetWrapper wrapper = wrappedSets . get (possibleMatch . nodeSet );
218214 for (org .eclipse .jdt .core .dom .ASTNode node : wrapper .trustedASTNodeLevels .keySet ()) {
219215 int level = wrapper .trustedASTNodeLevels .get (node );
220216 SearchMatch match = toMatch (locator , node , level , possibleMatch );
0 commit comments