3737import org .eclipse .jdt .core .dom .MethodRef ;
3838import org .eclipse .jdt .core .dom .MethodReference ;
3939import org .eclipse .jdt .core .dom .Name ;
40+ import org .eclipse .jdt .core .dom .PackageDeclaration ;
4041import org .eclipse .jdt .core .dom .SuperConstructorInvocation ;
4142import org .eclipse .jdt .core .dom .SuperFieldAccess ;
4243import 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