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

use method handles for reflection #3128

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Changes from 1 commit
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 @@ -11,8 +11,9 @@
import static com.google.common.collect.Lists.*;
import static org.eclipse.xtext.xbase.validation.IssueCodes.*;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -239,27 +240,51 @@ protected IClasspathEntry getResolvedClasspathEntry(IJavaProject javaProject, /*
}


protected final boolean isJdtCoreVersionAtLeast3390 = JavaCore.getPlugin().getBundle().getVersion().compareTo(new Version(3, 39, 0)) >= 0;
private final static boolean IS_JDT_CORE_VERSION_AT_LEAST3390 = JavaCore.getPlugin().getBundle().getVersion().compareTo(new Version(3, 39, 0)) >= 0;

private final static MethodHandle GET_ROOT_PATH_TO_RESOLVED_ENTRIES_METHOD_HANDLE = initializeGetRootPathToResolvedEntriesMethodHandle();
private final static MethodHandle ROOT_PATH_TO_RESOLVED_ENTRIES_FIELD_HANDLE = initializeRootPathToResolvedEntriesFieldHandle();

protected Map<IPath, IClasspathEntry> getRootPathToResolvedEntries(PerProjectInfo info) {
if (isJdtCoreVersionAtLeast3390) {
if (IS_JDT_CORE_VERSION_AT_LEAST3390) {
if (GET_ROOT_PATH_TO_RESOLVED_ENTRIES_METHOD_HANDLE == null) {
throw new RuntimeException("getRootPathToResolvedEntries method not found");
}
try {
Method m = PerProjectInfo.class.getDeclaredMethod("getRootPathToResolvedEntries");
@SuppressWarnings("unchecked")
Map<IPath, IClasspathEntry> result = (Map<IPath, IClasspathEntry>) m.invoke(info);
Map<IPath, IClasspathEntry> result = (Map<IPath, IClasspathEntry>) GET_ROOT_PATH_TO_RESOLVED_ENTRIES_METHOD_HANDLE.invoke(info);
return result;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
} catch (Throwable e) {
throw new RuntimeException("failed to call getRootPathToResolvedEntries method", e);
}
} else {
if (ROOT_PATH_TO_RESOLVED_ENTRIES_FIELD_HANDLE == null) {
throw new RuntimeException("rootPathToResolvedEntries field not found");
}
try {
Field f = PerProjectInfo.class.getDeclaredField("rootPathToResolvedEntries");
@SuppressWarnings("unchecked")
Map<IPath, IClasspathEntry> result = (Map<IPath, IClasspathEntry>) f.get(info);
Map<IPath, IClasspathEntry> result = (Map<IPath, IClasspathEntry>) ROOT_PATH_TO_RESOLVED_ENTRIES_FIELD_HANDLE.invoke(info);
return result;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
} catch (Throwable e) {
throw new RuntimeException("failed to call rootPathToResolvedEntries field", e);
}
}
}

private static MethodHandle initializeRootPathToResolvedEntriesFieldHandle() {
try {
MethodHandle handle = MethodHandles.lookup().findGetter(PerProjectInfo.class, "rootPathToResolvedEntries", Map.class);
return handle;
} catch (ReflectiveOperationException e) {
return null;
}
}

private static MethodHandle initializeGetRootPathToResolvedEntriesMethodHandle() {
try {
MethodHandle handle = MethodHandles.lookup().findVirtual(PerProjectInfo.class, "getRootPathToResolvedEntries", MethodType.methodType(Map.class));
return handle;
} catch (ReflectiveOperationException e) {
return null;
}
}

}
Loading