Skip to content

Commit

Permalink
Fix 4 more parameterized search tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Stryker <[email protected]>
  • Loading branch information
Rob Stryker committed Feb 20, 2025
1 parent 965ed48 commit 588822f
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.core.LocalVariable;
import org.eclipse.jdt.internal.core.NamedMember;
import org.eclipse.jdt.internal.core.search.matching.DOMPatternLocator;
import org.eclipse.jdt.internal.core.search.matching.MatchLocator;
import org.eclipse.jdt.internal.core.search.matching.MatchingNodeSet;
import org.eclipse.jdt.internal.core.search.matching.NodeSetWrapper;
import org.eclipse.jdt.internal.core.search.matching.PossibleMatch;
import org.eclipse.jdt.internal.core.search.matching.SearchMatchingUtility;

public class DOMJavaSearchDelegate implements IJavaSearchDelegate {
private Map<PossibleMatch, NodeSetWrapper> matchToWrapper = new HashMap<>();
Expand Down Expand Up @@ -175,7 +175,8 @@ public void acceptAST(org.eclipse.jdt.core.ICompilationUnit source,
int level = wrapper.trustedASTNodeLevels.get(node);
SearchMatch match = toMatch(locator, node, level, possibleMatch);
if (match != null && match.getElement() != null) {
SearchMatchingUtility.reportSearchMatch(locator, match);
DOMPatternLocator locator2 = DOMPatternLocatorFactory.createWrapper(locator.patternLocator);
locator2.reportSearchMatch(locator, node, match);
}
}
}
Expand Down Expand Up @@ -286,7 +287,7 @@ private SearchMatch toMatch(MatchLocator locator, org.eclipse.jdt.core.dom.ASTNo
getParticipant(locator), resource);
}
if (node instanceof Type nt) {
IBinding b = DOMASTNodeUtils.getBinding(nt);
//IBinding b = DOMASTNodeUtils.getBinding(nt);
IJavaElement element = DOMASTNodeUtils.getEnclosingJavaElement(node);
if (element instanceof LocalVariable) {
element = element.getParent();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat Inc and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*******************************************************************************/
package org.eclipse.jdt.internal.core.search;

import org.eclipse.jdt.internal.core.search.matching.ConstructorLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMConstructorLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMFieldLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMLocalVariableLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMMethodLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMPackageReferenceLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMPatternLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMSuperTypeReferenceLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMTypeDeclarationLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMTypeParameterLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMTypeReferenceLocator;
import org.eclipse.jdt.internal.core.search.matching.FieldLocator;
import org.eclipse.jdt.internal.core.search.matching.LocalVariableLocator;
import org.eclipse.jdt.internal.core.search.matching.MethodLocator;
import org.eclipse.jdt.internal.core.search.matching.PackageReferenceLocator;
import org.eclipse.jdt.internal.core.search.matching.PatternLocator;
import org.eclipse.jdt.internal.core.search.matching.SuperTypeReferenceLocator;
import org.eclipse.jdt.internal.core.search.matching.TypeDeclarationLocator;
import org.eclipse.jdt.internal.core.search.matching.TypeParameterLocator;
import org.eclipse.jdt.internal.core.search.matching.TypeReferenceLocator;

public class DOMPatternLocatorFactory {

public static DOMPatternLocator createWrapper(PatternLocator locator) {
// TODO implement all this.
if( locator instanceof FieldLocator fl) {
return new DOMFieldLocator(fl);
}
if( locator instanceof ConstructorLocator cl) {
return new DOMConstructorLocator(cl);
}
if( locator instanceof LocalVariableLocator lcl) {
return new DOMLocalVariableLocator(lcl);
}
if( locator instanceof MethodLocator ml) {
return new DOMMethodLocator(ml);
}
if( locator instanceof PackageReferenceLocator prl) {
return new DOMPackageReferenceLocator(prl);
}
if( locator instanceof SuperTypeReferenceLocator strl) {
return new DOMSuperTypeReferenceLocator(strl);
}
if( locator instanceof TypeDeclarationLocator tdl) {
return new DOMTypeDeclarationLocator(tdl);
}
if( locator instanceof TypeParameterLocator tpl) {
return new DOMTypeParameterLocator(tpl);
}
if( locator instanceof TypeReferenceLocator trl) {
return new DOMTypeReferenceLocator(trl);
}
return new DOMPatternLocator(null); // stub
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,10 @@
import org.eclipse.jdt.core.dom.TypeParameter;
import org.eclipse.jdt.core.dom.UnionType;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.internal.core.search.matching.ConstructorLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMConstructorLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMFieldLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMLocalVariableLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMMethodLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMPackageReferenceLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMPatternLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMSuperTypeReferenceLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMTypeDeclarationLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMTypeParameterLocator;
import org.eclipse.jdt.internal.core.search.matching.DOMTypeReferenceLocator;
import org.eclipse.jdt.internal.core.search.matching.FieldLocator;
import org.eclipse.jdt.internal.core.search.matching.LocalVariableLocator;
import org.eclipse.jdt.internal.core.search.matching.MatchLocator;
import org.eclipse.jdt.internal.core.search.matching.MethodLocator;
import org.eclipse.jdt.internal.core.search.matching.NodeSetWrapper;
import org.eclipse.jdt.internal.core.search.matching.PackageReferenceLocator;
import org.eclipse.jdt.internal.core.search.matching.PatternLocator;
import org.eclipse.jdt.internal.core.search.matching.SuperTypeReferenceLocator;
import org.eclipse.jdt.internal.core.search.matching.TypeDeclarationLocator;
import org.eclipse.jdt.internal.core.search.matching.TypeParameterLocator;
import org.eclipse.jdt.internal.core.search.matching.TypeReferenceLocator;

/**
* Visits an AST to feel the possible match with nodes
Expand All @@ -91,43 +73,11 @@ public PatternLocatorVisitor(PatternLocator patternLocator, NodeSetWrapper nodeS
private DOMPatternLocator getWrapper(PatternLocator locator) {
DOMPatternLocator l = wrapperMap.get(locator);
if(l == null ) {
l = createWrapper(locator);
l = DOMPatternLocatorFactory.createWrapper(locator);
wrapperMap.put(locator, l);
}
return l;
}

private DOMPatternLocator createWrapper(PatternLocator locator) {
// TODO implement all this.
if( locator instanceof FieldLocator fl) {
return new DOMFieldLocator(fl);
}
if( locator instanceof ConstructorLocator cl) {
return new DOMConstructorLocator(cl);
}
if( locator instanceof LocalVariableLocator lcl) {
return new DOMLocalVariableLocator(lcl);
}
if( locator instanceof MethodLocator ml) {
return new DOMMethodLocator(ml);
}
if( locator instanceof PackageReferenceLocator prl) {
return new DOMPackageReferenceLocator(prl);
}
if( locator instanceof SuperTypeReferenceLocator strl) {
return new DOMSuperTypeReferenceLocator(strl);
}
if( locator instanceof TypeDeclarationLocator tdl) {
return new DOMTypeDeclarationLocator(tdl);
}
if( locator instanceof TypeParameterLocator tpl) {
return new DOMTypeParameterLocator(tpl);
}
if( locator instanceof TypeReferenceLocator trl) {
return new DOMTypeReferenceLocator(trl);
}
return new DOMPatternLocator(null); // stub
}

private <T extends ASTNode> boolean defaultVisitImplementation(T node, BiFunction<T, DOMPatternLocator, Integer> levelFunc) {
defaultVisitImplementationWithFunc(node, levelFunc, DOMASTNodeUtils::getBinding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
*******************************************************************************/
package org.eclipse.jdt.internal.core.search.matching;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.ArrayType;
import org.eclipse.jdt.core.dom.FieldAccess;
Expand All @@ -26,6 +28,7 @@
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.VariableDeclaration;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.internal.compiler.lookup.IntersectionTypeBinding18;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
Expand Down Expand Up @@ -328,4 +331,11 @@ protected int resolveLevelForType(char[] qualifiedPattern, ITypeBinding type, II
}
return IMPOSSIBLE_MATCH;
}

/*
* Subclasses can override this if they want to make last minute changes to the match
*/
public void reportSearchMatch(MatchLocator locator, ASTNode node, SearchMatch match) throws CoreException {
SearchMatchingUtility.reportSearchMatch(locator, match);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.BreakStatement;
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
Expand All @@ -40,6 +42,7 @@
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.TypeDeclarationMatch;
import org.eclipse.jdt.core.search.TypeReferenceMatch;
Expand Down Expand Up @@ -445,5 +448,45 @@ protected int resolveLevelForType(ITypeBinding typeBinding) {
this.locator.pattern.qualification,
typeBinding);
}

@Override
public void reportSearchMatch(MatchLocator locator, ASTNode node, SearchMatch match) throws CoreException {
if( node instanceof QualifiedType qtt) {
ASTNode n2 = findNodeMatchingPatternQualifier(qtt.getQualifier());
if( n2 != null ) {
int matchStart = match.getOffset();
int matchEnd = matchStart + match.getLength();
int newStart = n2.getStartPosition();
int newLength = matchEnd - newStart;
match.setOffset(newStart);
match.setLength(newLength);
}
}
SearchMatchingUtility.reportSearchMatch(locator, match);
}
private ASTNode findNodeMatchingPatternQualifier(Type qualifier) {
if( qualifier instanceof ParameterizedType pt) {
return findNodeMatchingPatternQualifier(pt.getType());
}
Name[] retNode = new Name[] {null};
String needle = this.locator.pattern.qualification == null ? null : new String(this.locator.pattern.qualification);
if( needle != null ) {
qualifier.accept(new ASTVisitor() {
@Override
public boolean visit(QualifiedName node) {
if( node.getName().toString().endsWith(needle)) {
retNode[0] = node.getName();
}
return retNode[0] == null;
}
@Override
public boolean visit(ParameterizedType node) {
node.getType().accept(this);
return false;
}
});
}
return retNode[0];
}

}

0 comments on commit 588822f

Please sign in to comment.