Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eliminate the usage of jdt internal API to get list of imports #518

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading