Skip to content

Commit

Permalink
Merge pull request #518 from ajm01/issue-517
Browse files Browse the repository at this point in the history
eliminate the usage of jdt internal API to get list of imports
  • Loading branch information
turkeylurkey committed Feb 16, 2024
2 parents fe4a294 + b2cd1d0 commit da30e70
Showing 1 changed file with 16 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.ImportContainerInfo;
import org.eclipse.jdt.internal.core.JavaModelManager;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.lsp4jakarta.jdt.core.JakartaCorePlugin;

Expand Down Expand Up @@ -113,35 +111,19 @@ public static boolean isMatchedJavaElement(IType type, String javaElementName, S
* otherwise.
*/
public static boolean isImportedJavaElement(ICompilationUnit unit, String javaElementFQName) throws JavaModelException {

if (!unit.isOpen()) {
unit.open(null);
}

IImportContainer container = unit.getImportContainer();
if (container == null) {
return false;
}

// The following code uses JDT internal class and looks like
// ICompilationUnit#getImports()
// To avoid creating an array of IImportDeclaration, we do the following code:
JavaModelManager manager = JavaModelManager.getJavaModelManager();
Object info = manager.getInfo(container);
if (info == null) {
if (manager.getInfo(unit) != null) {
// CU was opened, but no import container, then no imports
return false;
} else {
try {
unit.open(null);
} catch (JavaModelException e) {
e.printStackTrace();
} // force opening of CU
info = manager.getInfo(container);
if (info == null)
// after opening, if no import container, then no imports
return false;
}
}
IJavaElement[] elements = ((ImportContainerInfo) info).getChildren();
for (IJavaElement child : elements) {
IImportDeclaration importDeclaration = (IImportDeclaration) child;
IImportDeclaration[] importDeclArray = unit.getImports();

for (IImportDeclaration importDeclaration : importDeclArray) {
if (importDeclaration.isOnDemand()) {
String fqn = importDeclaration.getElementName();
String qualifier = fqn.substring(0, fqn.lastIndexOf('.'));
Expand All @@ -165,35 +147,18 @@ public static boolean isImportedJavaElement(ICompilationUnit unit, String javaEl
* false otherwise.
*/
protected static boolean isImportedJavaElement(ICompilationUnit unit, String[] javaElementFQNames) throws JavaModelException {
if (!unit.isOpen()) {
unit.open(null);
}

IImportContainer container = unit.getImportContainer();
if (container == null) {
return false;
}

// The following code uses JDT internal class and looks like
// ICompilationUnit#getImports()
// To avoid creating an array of IImportDeclaration, we do the following code:
JavaModelManager manager = JavaModelManager.getJavaModelManager();
Object info = manager.getInfo(container);
if (info == null) {
if (manager.getInfo(unit) != null) {
// CU was opened, but no import container, then no imports
return false;
} else {
try {
unit.open(null);
} catch (JavaModelException e) {
e.printStackTrace();
} // force opening of CU
info = manager.getInfo(container);
if (info == null)
// after opening, if no import container, then no imports
return false;
}
}
IJavaElement[] elements = ((ImportContainerInfo) info).getChildren();
for (IJavaElement child : elements) {
IImportDeclaration importDeclaration = (IImportDeclaration) child;
IImportDeclaration[] importDeclArray = unit.getImports();

for (IImportDeclaration importDeclaration : importDeclArray) {
if (importDeclaration.isOnDemand()) {
String fqn = importDeclaration.getElementName();
String qualifier = fqn.substring(0, fqn.lastIndexOf('.'));
Expand All @@ -210,6 +175,7 @@ protected static boolean isImportedJavaElement(ICompilationUnit unit, String[] j
}
}
return false;

}

/**
Expand Down

0 comments on commit da30e70

Please sign in to comment.