Skip to content

Commit df87c33

Browse files
mickaelistriadatho7561
authored andcommitted
[Search] Fix error when matching package declaration
1 parent 162e99f commit df87c33

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/core/search/DOMASTNodeUtils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.eclipse.jdt.core.dom.MethodRef;
3838
import org.eclipse.jdt.core.dom.MethodReference;
3939
import org.eclipse.jdt.core.dom.Name;
40+
import org.eclipse.jdt.core.dom.PackageDeclaration;
4041
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
4142
import org.eclipse.jdt.core.dom.SuperFieldAccess;
4243
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
@@ -57,6 +58,7 @@ public static IJavaElement getEnclosingJavaElement(ASTNode node) {
5758
|| node instanceof FieldDeclaration
5859
|| node instanceof Initializer
5960
|| node instanceof ImportDeclaration
61+
|| node instanceof PackageDeclaration
6062
|| node instanceof CompilationUnit
6163
|| node instanceof AnnotationTypeMemberDeclaration
6264
|| node.getLocationInParent() == FieldDeclaration.FRAGMENTS_PROPERTY) {
@@ -79,7 +81,7 @@ public static IJavaElement getDeclaringJavaElement(ASTNode key) {
7981

8082
private static IJavaElement findElementForNodeCustom(ASTNode key) {
8183
if( key instanceof FieldDeclaration fd ) {
82-
List fragments = fd.fragments();
84+
List<?> fragments = fd.fragments();
8385
if( fragments.size() > 0 ) {
8486
VariableDeclarationFragment vdf = (VariableDeclarationFragment)fragments.get(0);
8587
if( vdf != null ) {
@@ -92,7 +94,7 @@ private static IJavaElement findElementForNodeCustom(ASTNode key) {
9294
ASTNode parentNode = i.getParent();
9395
int domOccurance = -1;
9496
if( parentNode instanceof AbstractTypeDeclaration typeDecl) {
95-
List parentBody = typeDecl.bodyDeclarations();
97+
List<?> parentBody = typeDecl.bodyDeclarations();
9698
for( int z = 0; z < parentBody.size() && domOccurance == -1; z++ ) {
9799
if( parentBody.get(z) == key) {
98100
domOccurance = z + 1;
@@ -119,10 +121,14 @@ private static IJavaElement findElementForNodeCustom(ASTNode key) {
119121
if( key instanceof ImportDeclaration id) {
120122
ASTNode parentNode = id.getParent();
121123
if( parentNode instanceof CompilationUnit unit) {
122-
IJavaElement parentEl = ((CompilationUnit)id.getParent()).getJavaElement();
124+
IJavaElement parentEl = unit.getJavaElement();
123125
return ((org.eclipse.jdt.internal.core.CompilationUnit) parentEl).getImport(id.getName().toString());
124126
}
125127
}
128+
if (key instanceof PackageDeclaration pack && pack.getParent() instanceof CompilationUnit unit) {
129+
IJavaElement parentEl = unit.getJavaElement();
130+
return ((org.eclipse.jdt.internal.core.CompilationUnit) parentEl).getPackageDeclaration(pack.getName().getFullyQualifiedName());
131+
}
126132
return null;
127133
}
128134

0 commit comments

Comments
 (0)