From 4d8a96df73b35410ff05de83c6e37aa09d6b321f Mon Sep 17 00:00:00 2001 From: Sebastian Zarnekow Date: Thu, 8 Aug 2024 11:07:23 +0200 Subject: [PATCH] Encapsulate reflective access by single MethodHandle Signed-off-by: Sebastian Zarnekow --- .../xbase/ui/validation/XbaseUIValidator.java | 55 ++++++------------- 1 file changed, 16 insertions(+), 39 deletions(-) diff --git a/org.eclipse.xtext.xbase.ui/src/org/eclipse/xtext/xbase/ui/validation/XbaseUIValidator.java b/org.eclipse.xtext.xbase.ui/src/org/eclipse/xtext/xbase/ui/validation/XbaseUIValidator.java index 56b178371d6..0739446d3ab 100644 --- a/org.eclipse.xtext.xbase.ui/src/org/eclipse/xtext/xbase/ui/validation/XbaseUIValidator.java +++ b/org.eclipse.xtext.xbase.ui/src/org/eclipse/xtext/xbase/ui/validation/XbaseUIValidator.java @@ -14,10 +14,12 @@ import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; +import java.util.Collections; import java.util.List; import java.util.Map; import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Status; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EPackage; import org.eclipse.emf.ecore.EStructuralFeature; @@ -239,51 +241,26 @@ protected IClasspathEntry getResolvedClasspathEntry(IJavaProject javaProject, /* return result; } + private final static MethodHandle GET_ROOT_PATH_TO_RESOLVED_ENTRIES = findAccessor(); - 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 getRootPathToResolvedEntries(PerProjectInfo info) { - 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 { - Map result = (Map) GET_ROOT_PATH_TO_RESOLVED_ENTRIES_METHOD_HANDLE.invoke(info); - return result; - } 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 { - Map result = (Map) ROOT_PATH_TO_RESOLVED_ENTRIES_FIELD_HANDLE.invoke(info); - return result; - } catch (Throwable e) { - throw new RuntimeException("failed to call rootPathToResolvedEntries field", e); - } - } - } - - private static MethodHandle initializeRootPathToResolvedEntriesFieldHandle() { + private static MethodHandle findAccessor() { try { - MethodHandle handle = MethodHandles.lookup().findGetter(PerProjectInfo.class, "rootPathToResolvedEntries", Map.class); - return handle; + if (JavaCore.getPlugin().getBundle().getVersion().compareTo(new Version(3, 39, 0)) >= 0) { + return MethodHandles.lookup().findGetter(PerProjectInfo.class, "rootPathToResolvedEntries", Map.class); + } else { + return MethodHandles.lookup().findVirtual(PerProjectInfo.class, "getRootPathToResolvedEntries", MethodType.methodType(Map.class)); + } } catch (ReflectiveOperationException e) { - return null; + + return MethodHandles.constant(Map.class, Collections.emptyMap()); } } - - private static MethodHandle initializeGetRootPathToResolvedEntriesMethodHandle() { + + protected Map getRootPathToResolvedEntries(PerProjectInfo info) throws JavaModelException { try { - MethodHandle handle = MethodHandles.lookup().findVirtual(PerProjectInfo.class, "getRootPathToResolvedEntries", MethodType.methodType(Map.class)); - return handle; - } catch (ReflectiveOperationException e) { - return null; + return (Map) GET_ROOT_PATH_TO_RESOLVED_ENTRIES.invoke(info); + } catch(Throwable t) { + throw new JavaModelException(Status.error(t.getMessage(), t)); } }