diff options
author | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2020-11-13 04:37:27 +0100 |
---|---|---|
committer | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2020-11-13 04:44:39 +0100 |
commit | 3aace094f336393330ed275e1fb6d6c1f9187e14 (patch) | |
tree | 3c235a645f057a393194bf789fc4ce4bfaaef340 /src/eclipseAgent/lombok/eclipse/agent | |
parent | 219bb4bea5035c370614474f630dac454cfe4223 (diff) | |
download | lombok-3aace094f336393330ed275e1fb6d6c1f9187e14.tar.gz lombok-3aace094f336393330ed275e1fb6d6c1f9187e14.tar.bz2 lombok-3aace094f336393330ed275e1fb6d6c1f9187e14.zip |
[refactor] reflection code streamlined by sending it through the Permit class
Diffstat (limited to 'src/eclipseAgent/lombok/eclipse/agent')
5 files changed, 43 insertions, 62 deletions
diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java index b90d5762..99367a22 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java @@ -101,6 +101,7 @@ import lombok.eclipse.EclipseNode; import lombok.eclipse.TransformEclipseAST; import lombok.eclipse.handlers.SetGeneratedByVisitor; import lombok.patcher.Symbols; +import lombok.permit.Permit; public class PatchDelegate { @@ -846,18 +847,20 @@ public class PatchDelegate { private static final class Reflection { public static final Method classScopeBuildFieldsAndMethodsMethod; - + public static final Throwable initCause; static { Method m = null; + Throwable c = null; try { - m = ClassScope.class.getDeclaredMethod("buildFieldsAndMethods"); - m.setAccessible(true); + m = Permit.getMethod(ClassScope.class, "buildFieldsAndMethods"); } catch (Throwable t) { + c = t; // That's problematic, but as long as no local classes are used we don't actually need it. // Better fail on local classes than crash altogether. } classScopeBuildFieldsAndMethodsMethod = m; + initCause = c; } } @@ -894,16 +897,14 @@ public class PatchDelegate { ClassScope cs = ((SourceTypeBinding)inner).scope; if (cs != null) { try { - Reflection.classScopeBuildFieldsAndMethodsMethod.invoke(cs); + Permit.invoke(Reflection.initCause, Reflection.classScopeBuildFieldsAndMethodsMethod, cs); } catch (Exception e) { // See 'Reflection' class for why we ignore this exception. } } } - if (!(binding instanceof ReferenceBinding)) { - return; - } + if (!(binding instanceof ReferenceBinding)) return; ReferenceBinding rb = (ReferenceBinding) binding; MethodBinding[] availableMethods = rb.availableMethods(); diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegatePortal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegatePortal.java index 89b02f01..3d8c86c9 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegatePortal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegatePortal.java @@ -21,10 +21,9 @@ */ package lombok.eclipse.agent; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import lombok.Lombok; +import lombok.permit.Permit; public class PatchDelegatePortal { static final String CLASS_SCOPE = "org.eclipse.jdt.internal.compiler.lookup.ClassScope"; @@ -32,47 +31,13 @@ public class PatchDelegatePortal { static final String SOURCE_TYPE_ELEMENT_INFO = "org.eclipse.jdt.internal.core.SourceTypeElementInfo"; public static boolean handleDelegateForType(Object classScope) { - try { - return (Boolean) Reflection.handleDelegateForType.invoke(null, classScope); - } catch (NoClassDefFoundError e) { - //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly - //do anything useful here. - return false; - } catch (IllegalAccessException e) { - throw Lombok.sneakyThrow(e); - } catch (InvocationTargetException e) { - throw Lombok.sneakyThrow(e.getCause()); - } catch (NullPointerException e) { - if (!"false".equals(System.getProperty("lombok.debug.reflection", "false"))) { - e.initCause(Reflection.problem); - throw e; - } - //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly - //do anything useful here. - return false; - } + Boolean v = (Boolean) Permit.invokeSneaky(Reflection.problem, Reflection.handleDelegateForType, null, classScope); + if (v == null) return false; + return v.booleanValue(); } public static Object[] getChildren(Object returnValue, Object javaElement) { - try { - return (Object[]) Reflection.getChildren.invoke(null, returnValue, javaElement); - } catch (NoClassDefFoundError e) { - //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly - //do anything useful here. - return (Object[]) returnValue; - } catch (IllegalAccessException e) { - throw Lombok.sneakyThrow(e); - } catch (InvocationTargetException e) { - throw Lombok.sneakyThrow(e.getCause()); - } catch (NullPointerException e) { - if (!"false".equals(System.getProperty("lombok.debug.reflection", "false"))) { - e.initCause(Reflection.problem); - throw e; - } - //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly - //do anything useful here. - return (Object[]) returnValue; - } + return (Object[]) Permit.invokeSneaky(Reflection.problem, Reflection.getChildren, null, returnValue, javaElement); } private static final class Reflection { @@ -84,8 +49,8 @@ public class PatchDelegatePortal { Method m = null, n = null; Throwable problem_ = null; try { - m = PatchDelegate.class.getMethod("handleDelegateForType", Class.forName(CLASS_SCOPE)); - n = PatchDelegate.class.getMethod("getChildren", Class.forName(I_JAVA_ELEMENT_ARRAY), Class.forName(SOURCE_TYPE_ELEMENT_INFO)); + m = Permit.getMethod(PatchDelegate.class, "handleDelegateForType", Class.forName(CLASS_SCOPE)); + n = Permit.getMethod(PatchDelegate.class, "getChildren", Class.forName(I_JAVA_ELEMENT_ARRAY), Class.forName(SOURCE_TYPE_ELEMENT_INFO)); } catch (Throwable t) { // That's problematic, but as long as no local classes are used we don't actually need it. // Better fail on local classes than crash altogether. diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java index 0f602671..c916ca26 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java @@ -99,13 +99,13 @@ public class PatchExtensionMethod { private static final Method shortMethod = getMethod("invalidMethod", MessageSend.class, MethodBinding.class); private static final Method longMethod = getMethod("invalidMethod", MessageSend.class, MethodBinding.class, Scope.class); + private static Throwable initProblem; private static Method getMethod(String name, Class<?>... types) { try { - Method m = ProblemReporter.class.getMethod(name, types); - m.setAccessible(true); - return m; + return Permit.getMethod(ProblemReporter.class, name, types); } catch (Exception e) { + initProblem = e; return null; } } @@ -120,8 +120,9 @@ public class PatchExtensionMethod { static void invoke(ProblemReporter problemReporter, MessageSend messageSend, MethodBinding method, Scope scope) { if (messageSend != null) { try { - if (shortMethod != null) shortMethod.invoke(problemReporter, messageSend, method); - else if (longMethod != null) longMethod.invoke(problemReporter, messageSend, method, scope); + if (shortMethod != null) Permit.invoke(initProblem, shortMethod, problemReporter, messageSend, method); + else if (longMethod != null) Permit.invoke(initProblem, longMethod, problemReporter, messageSend, method, scope); + else Permit.reportReflectionProblem(initProblem, "method named 'invalidMethod' not found in ProblemReporter.class"); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodCompletionProposal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodCompletionProposal.java index 08a42d1c..1b94d1b9 100755 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodCompletionProposal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodCompletionProposal.java @@ -198,7 +198,7 @@ public class PatchExtensionMethodCompletionProposal { public static final Field completionEngineField; public static final Field nameLookupField; public static final Method createJavaCompletionProposalMethod; - + static { replacementOffsetField = accessField(AbstractJavaCompletionProposal.class, "fReplacementOffset"); contextField = accessField(CompletionProposalCollector.class, "fContext"); diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodPortal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodPortal.java index 90a23c20..82df71f6 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodPortal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodPortal.java @@ -54,7 +54,7 @@ public class PatchExtensionMethodPortal { //do anything useful here. } } - + public static void invalidMethod(Object problemReporter, Object messageSend, Object method) { try { Reflection.invalidMethod.invoke(null, problemReporter, messageSend, method); @@ -62,23 +62,37 @@ public class PatchExtensionMethodPortal { //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly //do anything useful here. } catch (IllegalAccessException e) { + handleReflectionDebug(e); throw Lombok.sneakyThrow(e); } catch (InvocationTargetException e) { + handleReflectionDebug(e.getCause()); throw Lombok.sneakyThrow(e.getCause()); } catch (NullPointerException e) { - if (!"false".equals(System.getProperty("lombok.debug.reflection", "false"))) { - e.initCause(Reflection.problem); - throw e; - } + handleReflectionDebug(e); //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly //do anything useful here. } } - + + public static boolean isDebugReflection() { + return !"false".equals(System.getProperty("lombok.debug.reflection", "false")); + } + + public static void handleReflectionDebug(Throwable t) { + if (!isDebugReflection()) return; + + System.err.println("** LOMBOK REFLECTION exception: " + t.getClass() + ": " + (t.getMessage() == null ? "(no message)" : t.getMessage())); + t.printStackTrace(System.err); + if (Reflection.problem != null) { + System.err.println("*** ADDITIONALLY, exception occurred setting up reflection: "); + Reflection.problem.printStackTrace(System.err); + } + } + private static final class Reflection { public static final Method resolveType, errorNoMethodFor, invalidMethod; public static final Throwable problem; - + static { Method m = null, n = null, o = null; Throwable problem_ = null; |