forked from eclipse-jdt/eclipse.jdt.core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make MatchLocator capable of working with DOM
Extra strategy in MatchLocator to use DOM, and tweaks in various Locators to properly handle DOM. Tests in JavaSearchTests help a lot (currently ~130/270 passing) It basically clones and adapt some methods and adapt them to DOM, A new flag is introduced to decide the strategy. Done-ish (often with remaining issues) * FieldLocator * LocalVariableLocator * MethodLocator * SuperTypeReferenceLocator * TypeDeclarationLocator * TypeParameterLocator * TypeReferenceLocator TODO: * AndLocator * ModuleLocator * PackageDeclarationLocator * PackageReferenceLocator * OrLocator * VariableLocator # Conflicts: # org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaSearchTests.java # org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/FieldLocator.java # org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/LocalVariableLocator.java # org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/MatchLocator.java # org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/MatchingNodeSet.java # org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/MethodLocator.java # org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/PackageReferenceLocator.java # org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/PatternLocator.java # org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/SuperTypeReferenceLocator.java # org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeParameterLocator.java # org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeReferenceLocator.java
- Loading branch information
1 parent
55d99cd
commit 97d6f5b
Showing
15 changed files
with
2,003 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...clipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/DOMASTNodeUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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.matching; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.eclipse.jdt.core.IJavaElement; | ||
import org.eclipse.jdt.core.dom.ASTNode; | ||
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; | ||
import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration; | ||
import org.eclipse.jdt.core.dom.Comment; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.dom.EnumConstantDeclaration; | ||
import org.eclipse.jdt.core.dom.FieldAccess; | ||
import org.eclipse.jdt.core.dom.IBinding; | ||
import org.eclipse.jdt.core.dom.MethodDeclaration; | ||
import org.eclipse.jdt.core.dom.MethodInvocation; | ||
import org.eclipse.jdt.core.dom.MethodRef; | ||
import org.eclipse.jdt.core.dom.MethodReference; | ||
import org.eclipse.jdt.core.dom.Name; | ||
import org.eclipse.jdt.core.dom.SuperFieldAccess; | ||
import org.eclipse.jdt.core.dom.SuperMethodInvocation; | ||
import org.eclipse.jdt.core.dom.SuperMethodReference; | ||
import org.eclipse.jdt.core.dom.Type; | ||
import org.eclipse.jdt.core.dom.VariableDeclaration; | ||
|
||
public class DOMASTNodeUtils { | ||
|
||
public static IJavaElement getEnclosingJavaElement(ASTNode node) { | ||
if (node == null) { | ||
return null; | ||
} | ||
if (node instanceof AbstractTypeDeclaration | ||
|| node instanceof MethodDeclaration | ||
|| node instanceof VariableDeclaration | ||
|| node instanceof CompilationUnit | ||
|| node instanceof AnnotationTypeMemberDeclaration) { | ||
return getDeclaringJavaElement(node); | ||
} | ||
return getEnclosingJavaElement(node.getParent()); | ||
} | ||
|
||
public static IJavaElement getDeclaringJavaElement(ASTNode key) { | ||
if (key instanceof CompilationUnit unit) { | ||
return unit.getJavaElement(); | ||
} | ||
return Optional.ofNullable(key).map(DOMASTNodeUtils::getBinding).map(IBinding::getJavaElement).orElse(null); | ||
} | ||
|
||
private static IBinding getBinding(ASTNode astNode) { | ||
if (astNode instanceof Name name) { | ||
return name.resolveBinding(); | ||
} | ||
if (astNode instanceof VariableDeclaration variable) { | ||
return variable.resolveBinding(); | ||
} | ||
if (astNode instanceof EnumConstantDeclaration enumConstantDeclaration) { | ||
return enumConstantDeclaration.resolveVariable(); | ||
} | ||
if (astNode instanceof FieldAccess fieldAcces) { | ||
return fieldAcces.resolveFieldBinding(); | ||
} | ||
if (astNode instanceof MethodInvocation method) { | ||
return method.resolveMethodBinding(); | ||
} | ||
if (astNode instanceof Type type) { | ||
return type.resolveBinding(); | ||
} | ||
if (astNode instanceof AbstractTypeDeclaration type) { | ||
return type.resolveBinding(); | ||
} | ||
if (astNode instanceof MethodDeclaration method) { | ||
return method.resolveBinding(); | ||
} | ||
if (astNode instanceof SuperFieldAccess superField) { | ||
return superField.resolveFieldBinding(); | ||
} | ||
if (astNode instanceof SuperMethodInvocation superMethod) { | ||
return superMethod.resolveMethodBinding(); | ||
} | ||
if (astNode instanceof SuperMethodReference superRef) { | ||
return superRef.resolveMethodBinding(); | ||
} | ||
if (astNode instanceof MethodRef methodRef) { | ||
return methodRef.resolveBinding(); | ||
} | ||
if (astNode instanceof MethodReference methodRef) { | ||
return methodRef.resolveMethodBinding(); | ||
} | ||
// TODO more... | ||
return null; | ||
} | ||
|
||
public static boolean insideDocComment(org.eclipse.jdt.core.dom.ASTNode node) { | ||
return node.getRoot() instanceof org.eclipse.jdt.core.dom.CompilationUnit unit && | ||
((List<Comment>)unit.getCommentList()).stream().anyMatch(comment -> comment.getStartPosition() <= node.getStartPosition() && comment.getStartPosition() + comment.getLength() >= node.getStartPosition() + node.getLength()); | ||
} | ||
} |
Oops, something went wrong.