aboutsummaryrefslogtreecommitdiff
path: root/src/eclipseAgent/lombok/launch
diff options
context:
space:
mode:
Diffstat (limited to 'src/eclipseAgent/lombok/launch')
-rwxr-xr-xsrc/eclipseAgent/lombok/launch/PatchFixesHider.java209
1 files changed, 150 insertions, 59 deletions
diff --git a/src/eclipseAgent/lombok/launch/PatchFixesHider.java b/src/eclipseAgent/lombok/launch/PatchFixesHider.java
index deab0be1..02df3f5f 100755
--- a/src/eclipseAgent/lombok/launch/PatchFixesHider.java
+++ b/src/eclipseAgent/lombok/launch/PatchFixesHider.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010-2019 The Project Lombok Authors.
+ * Copyright (C) 2010-2021 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -35,27 +35,22 @@ import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IAnnotatable;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IField;
+import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jdt.core.dom.ArrayType;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
+import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
-import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Expression;
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
-import org.eclipse.jdt.internal.compiler.ast.ForeachStatement;
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
-import org.eclipse.jdt.internal.compiler.ast.MessageSend;
+import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
-import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
-import org.eclipse.jdt.internal.compiler.lookup.Scope;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
-import org.eclipse.jdt.internal.compiler.parser.Parser;
-import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
import org.eclipse.jdt.internal.core.SourceField;
import org.eclipse.jdt.internal.core.dom.rewrite.NodeRewriteEvent;
import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEvent;
@@ -63,7 +58,7 @@ import org.eclipse.jdt.internal.core.dom.rewrite.TokenScanner;
import org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup;
import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil;
-import lombok.eclipse.EclipseAugments;
+import static lombok.eclipse.EcjAugments.ASTNode_generatedBy;
/** These contain a mix of the following:
* <ul>
@@ -84,20 +79,24 @@ final class PatchFixesHider {
public static final class Util {
private static ClassLoader shadowLoader;
+ public static ClassLoader getShadowLoader() {
+ if (shadowLoader == null) {
+ try {
+ Class.forName("lombok.core.LombokNode");
+ // If we get here, then lombok is already available.
+ shadowLoader = Util.class.getClassLoader();
+ } catch (ClassNotFoundException e) {
+ // If we get here, it isn't, and we should use the shadowloader.
+ shadowLoader = Main.getShadowClassLoader();
+ }
+ }
+
+ return shadowLoader;
+ }
+
public static Class<?> shadowLoadClass(String name) {
try {
- if (shadowLoader == null) {
- try {
- Class.forName("lombok.core.LombokNode");
- // If we get here, then lombok is already available.
- shadowLoader = Util.class.getClassLoader();
- } catch (ClassNotFoundException e) {
- // If we get here, it isn't, and we should use the shadowloader.
- shadowLoader = Main.getShadowClassLoader();
- }
- }
-
- return Class.forName(name, true, shadowLoader);
+ return Class.forName(name, true, getShadowLoader());
} catch (ClassNotFoundException e) {
throw sneakyThrow(e);
}
@@ -111,6 +110,20 @@ final class PatchFixesHider {
}
}
+ public static Method findMethod(Class<?> type, String name, String... parameterTypes) {
+ for (Method m : type.getDeclaredMethods()) {
+ if (name.equals(m.getName()) && sameTypes(m.getParameterTypes(), parameterTypes)) {
+ return m;
+ }
+ }
+ throw sneakyThrow(new NoSuchMethodException(type.getName() + "::" + name));
+ }
+
+ public static Method findMethodAnyArgs(Class<?> type, String name) {
+ for (Method m : type.getDeclaredMethods()) if (name.equals(m.getName())) return m;
+ throw sneakyThrow(new NoSuchMethodException(type.getName() + "::" + name));
+ }
+
public static Object invokeMethod(Method method, Object... args) {
try {
return method.invoke(null, args);
@@ -131,6 +144,14 @@ final class PatchFixesHider {
private static <T extends Throwable> void sneakyThrow0(Throwable t) throws T {
throw (T)t;
}
+
+ private static boolean sameTypes(Class<?>[] types, String[] typeNames) {
+ if (types.length != typeNames.length) return false;
+ for (int i = 0; i < types.length; i++) {
+ if (!types[i].getName().equals(typeNames[i])) return false;
+ }
+ return true;
+ }
}
/** Contains patch fixes that are dependent on lombok internals. */
@@ -170,20 +191,25 @@ final class PatchFixesHider {
}
public static final class Transform {
- private static final Method TRANSFORM;
- private static final Method TRANSFORM_SWAPPED;
+ private static Method TRANSFORM;
+ private static Method TRANSFORM_SWAPPED;
- static {
+ private static synchronized void init(ClassLoader prepend) {
+ if (TRANSFORM != null) return;
+
+ Main.prependClassLoader(prepend);
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.TransformEclipseAST");
- TRANSFORM = Util.findMethod(shadowed, "transform", Parser.class, CompilationUnitDeclaration.class);
- TRANSFORM_SWAPPED = Util.findMethod(shadowed, "transform_swapped", CompilationUnitDeclaration.class, Parser.class);
+ TRANSFORM = Util.findMethodAnyArgs(shadowed, "transform");
+ TRANSFORM_SWAPPED = Util.findMethodAnyArgs(shadowed, "transform_swapped");
}
- public static void transform(Parser parser, CompilationUnitDeclaration ast) throws IOException {
+ public static void transform(Object parser, Object ast) throws IOException {
+ init(parser.getClass().getClassLoader());
Util.invokeMethod(TRANSFORM, parser, ast);
}
- public static void transform_swapped(CompilationUnitDeclaration ast, Parser parser) throws IOException {
+ public static void transform_swapped(Object ast, Object parser) throws IOException {
+ init(parser.getClass().getClassLoader());
Util.invokeMethod(TRANSFORM_SWAPPED, ast, parser);
}
}
@@ -191,15 +217,21 @@ final class PatchFixesHider {
/** Contains patch code to support {@code @Delegate} */
public static final class Delegate {
private static final Method HANDLE_DELEGATE_FOR_TYPE;
+ private static final Method ADD_GENERATED_DELEGATE_METHODS;
static {
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchDelegatePortal");
HANDLE_DELEGATE_FOR_TYPE = Util.findMethod(shadowed, "handleDelegateForType", Object.class);
+ ADD_GENERATED_DELEGATE_METHODS = Util.findMethod(shadowed, "addGeneratedDelegateMethods", Object.class, Object.class);
}
public static boolean handleDelegateForType(Object classScope) {
return (Boolean) Util.invokeMethod(HANDLE_DELEGATE_FOR_TYPE, classScope);
}
+
+ public static Object[] addGeneratedDelegateMethods(Object returnValue, Object javaElement) {
+ return (Object[]) Util.invokeMethod(ADD_GENERATED_DELEGATE_METHODS, returnValue, javaElement);
+ }
}
/** Contains patch code to support {@code val} (eclipse specific) */
@@ -236,65 +268,120 @@ final class PatchFixesHider {
/** Contains patch code to support {@code val} (eclipse and ecj) */
public static final class Val {
- private static final Method SKIP_RESOLVE_INITIALIZER_IF_ALREADY_CALLED;
- private static final Method SKIP_RESOLVE_INITIALIZER_IF_ALREADY_CALLED2;
+ private static final String BLOCK_SCOPE_SIG = "org.eclipse.jdt.internal.compiler.lookup.BlockScope";
+ private static final String LOCAL_DECLARATION_SIG = "org.eclipse.jdt.internal.compiler.ast.LocalDeclaration";
+ private static final String FOREACH_STATEMENT_SIG = "org.eclipse.jdt.internal.compiler.ast.ForeachStatement";
+
private static final Method HANDLE_VAL_FOR_LOCAL_DECLARATION;
private static final Method HANDLE_VAL_FOR_FOR_EACH;
static {
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchVal");
- SKIP_RESOLVE_INITIALIZER_IF_ALREADY_CALLED = Util.findMethod(shadowed, "skipResolveInitializerIfAlreadyCalled", Expression.class, BlockScope.class);
- SKIP_RESOLVE_INITIALIZER_IF_ALREADY_CALLED2 = Util.findMethod(shadowed, "skipResolveInitializerIfAlreadyCalled2", Expression.class, BlockScope.class, LocalDeclaration.class);
- HANDLE_VAL_FOR_LOCAL_DECLARATION = Util.findMethod(shadowed, "handleValForLocalDeclaration", LocalDeclaration.class, BlockScope.class);
- HANDLE_VAL_FOR_FOR_EACH = Util.findMethod(shadowed, "handleValForForEach", ForeachStatement.class, BlockScope.class);
+ HANDLE_VAL_FOR_LOCAL_DECLARATION = Util.findMethod(shadowed, "handleValForLocalDeclaration", LOCAL_DECLARATION_SIG, BLOCK_SCOPE_SIG);
+ HANDLE_VAL_FOR_FOR_EACH = Util.findMethod(shadowed, "handleValForForEach", FOREACH_STATEMENT_SIG, BLOCK_SCOPE_SIG);
}
- public static TypeBinding skipResolveInitializerIfAlreadyCalled(Expression expr, BlockScope scope) {
- return (TypeBinding) Util.invokeMethod(SKIP_RESOLVE_INITIALIZER_IF_ALREADY_CALLED, expr, scope);
+ public static boolean handleValForLocalDeclaration(Object local, Object scope) {
+ return (Boolean) Util.invokeMethod(HANDLE_VAL_FOR_LOCAL_DECLARATION, local, scope);
}
- public static TypeBinding skipResolveInitializerIfAlreadyCalled2(Expression expr, BlockScope scope, LocalDeclaration decl) {
- return (TypeBinding) Util.invokeMethod(SKIP_RESOLVE_INITIALIZER_IF_ALREADY_CALLED2, expr, scope, decl);
+ public static boolean handleValForForEach(Object forEach, Object scope) {
+ return (Boolean) Util.invokeMethod(HANDLE_VAL_FOR_FOR_EACH, forEach, scope);
}
- public static boolean handleValForLocalDeclaration(LocalDeclaration local, BlockScope scope) {
- return (Boolean) Util.invokeMethod(HANDLE_VAL_FOR_LOCAL_DECLARATION, local, scope);
+ /**
+ * Patches local declaration to not call .resolveType() on the initializer expression if we've already done so (calling it twice causes weird errors)
+ * This and the next method must be transplanted so that the return type is loaded in the correct class loader
+ */
+ public static TypeBinding skipResolveInitializerIfAlreadyCalled(Expression expr, BlockScope scope) {
+ if (expr.resolvedType != null) return expr.resolvedType;
+ try {
+ return expr.resolveType(scope);
+ } catch (NullPointerException e) {
+ return null;
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // This will occur internally due to for example 'val x = mth("X");', where mth takes 2 arguments.
+ return null;
+ }
}
- public static boolean handleValForForEach(ForeachStatement forEach, BlockScope scope) {
- return (Boolean) Util.invokeMethod(HANDLE_VAL_FOR_FOR_EACH, forEach, scope);
+ public static TypeBinding skipResolveInitializerIfAlreadyCalled2(Expression expr, BlockScope scope, LocalDeclaration decl) {
+ if (decl != null && LocalDeclaration.class.equals(decl.getClass()) && expr.resolvedType != null) return expr.resolvedType;
+ try {
+ return expr.resolveType(scope);
+ } catch (NullPointerException e) {
+ return null;
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // This will occur internally due to for example 'val x = mth("X");', where mth takes 2 arguments.
+ return null;
+ }
}
}
/** Contains patch code to support {@code @ExtensionMethod} */
public static final class ExtensionMethod {
+ private static final String MESSAGE_SEND_SIG = "org.eclipse.jdt.internal.compiler.ast.MessageSend";
+ private static final String TYPE_BINDING_SIG = "org.eclipse.jdt.internal.compiler.lookup.TypeBinding";
+ private static final String SCOPE_SIG = "org.eclipse.jdt.internal.compiler.lookup.Scope";
+ private static final String BLOCK_SCOPE_SIG = "org.eclipse.jdt.internal.compiler.lookup.BlockScope";
+ private static final String TYPE_BINDINGS_SIG = "[Lorg.eclipse.jdt.internal.compiler.lookup.TypeBinding;";
+ private static final String PROBLEM_REPORTER_SIG = "org.eclipse.jdt.internal.compiler.problem.ProblemReporter";
+ private static final String METHOD_BINDING_SIG = "org.eclipse.jdt.internal.compiler.lookup.MethodBinding";
+ private static final String AST_NODE_SIG = "org.eclipse.jdt.internal.compiler.ast.ASTNode";
+
private static final Method RESOLVE_TYPE;
private static final Method ERROR_NO_METHOD_FOR;
private static final Method INVALID_METHOD, INVALID_METHOD2;
+ private static final Method NON_STATIC_ACCESS_TO_STATIC_METHOD;
static {
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchExtensionMethod");
- RESOLVE_TYPE = Util.findMethod(shadowed, "resolveType", TypeBinding.class, MessageSend.class, BlockScope.class);
- ERROR_NO_METHOD_FOR = Util.findMethod(shadowed, "errorNoMethodFor", ProblemReporter.class, MessageSend.class, TypeBinding.class, TypeBinding[].class);
- INVALID_METHOD = Util.findMethod(shadowed, "invalidMethod", ProblemReporter.class, MessageSend.class, MethodBinding.class);
- INVALID_METHOD2 = Util.findMethod(shadowed, "invalidMethod", ProblemReporter.class, MessageSend.class, MethodBinding.class, Scope.class);
+ RESOLVE_TYPE = Util.findMethod(shadowed, "resolveType", TYPE_BINDING_SIG, MESSAGE_SEND_SIG, BLOCK_SCOPE_SIG);
+ ERROR_NO_METHOD_FOR = Util.findMethod(shadowed, "errorNoMethodFor", PROBLEM_REPORTER_SIG, MESSAGE_SEND_SIG, TYPE_BINDING_SIG, TYPE_BINDINGS_SIG);
+ INVALID_METHOD = Util.findMethod(shadowed, "invalidMethod", PROBLEM_REPORTER_SIG, MESSAGE_SEND_SIG, METHOD_BINDING_SIG);
+ INVALID_METHOD2 = Util.findMethod(shadowed, "invalidMethod", PROBLEM_REPORTER_SIG, MESSAGE_SEND_SIG, METHOD_BINDING_SIG, SCOPE_SIG);
+ NON_STATIC_ACCESS_TO_STATIC_METHOD = Util.findMethod(shadowed, "nonStaticAccessToStaticMethod", PROBLEM_REPORTER_SIG, AST_NODE_SIG, METHOD_BINDING_SIG, MESSAGE_SEND_SIG);
}
- public static TypeBinding resolveType(TypeBinding resolvedType, MessageSend methodCall, BlockScope scope) {
- return (TypeBinding) Util.invokeMethod(RESOLVE_TYPE, resolvedType, methodCall, scope);
+ public static Object resolveType(Object resolvedType, Object methodCall, Object scope) {
+ return Util.invokeMethod(RESOLVE_TYPE, resolvedType, methodCall, scope);
}
- public static void errorNoMethodFor(ProblemReporter problemReporter, MessageSend messageSend, TypeBinding recType, TypeBinding[] params) {
+ public static void errorNoMethodFor(Object problemReporter, Object messageSend, Object recType, Object params) {
Util.invokeMethod(ERROR_NO_METHOD_FOR, problemReporter, messageSend, recType, params);
}
- public static void invalidMethod(ProblemReporter problemReporter, MessageSend messageSend, MethodBinding method) {
+ public static void invalidMethod(Object problemReporter, Object messageSend, Object method) {
Util.invokeMethod(INVALID_METHOD, problemReporter, messageSend, method);
}
- public static void invalidMethod(ProblemReporter problemReporter, MessageSend messageSend, MethodBinding method, Scope scope) {
+ public static void invalidMethod(Object problemReporter, Object messageSend, Object method, Object scope) {
Util.invokeMethod(INVALID_METHOD2, problemReporter, messageSend, method, scope);
}
+
+ public static void nonStaticAccessToStaticMethod(Object problemReporter, Object location, Object method, Object messageSend) {
+ Util.invokeMethod(NON_STATIC_ACCESS_TO_STATIC_METHOD, problemReporter, location, method, messageSend);
+ }
+ }
+
+ /** Contains patch code to support Javadoc for generated methods */
+ public static final class Javadoc {
+ private static final Method GET_HTML;
+ private static final Method PRINT_METHOD;
+
+ static {
+ Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchJavadoc");
+ GET_HTML = Util.findMethod(shadowed, "getHTMLContentFromSource", String.class, Object.class);
+ PRINT_METHOD = Util.findMethod(shadowed, "printMethod", AbstractMethodDeclaration.class, Integer.class, StringBuffer.class, TypeDeclaration.class);
+ }
+
+ public static String getHTMLContentFromSource(String original, IJavaElement member) {
+ return (String) Util.invokeMethod(GET_HTML, original, member);
+ }
+
+ public static StringBuffer printMethod(AbstractMethodDeclaration methodDeclaration, int tab, StringBuffer output, TypeDeclaration type) {
+ return (StringBuffer) Util.invokeMethod(PRINT_METHOD, methodDeclaration, tab, output, type);
+ }
}
/**
@@ -497,6 +584,10 @@ final class PatchFixesHider {
return original == -1 ? end : original;
}
+ public static int fixRetrieveStartBlockPosition(int original, int start) {
+ return original == -1 ? start : original;
+ }
+
public static int fixRetrieveRightBraceOrSemiColonPosition(int original, int end) {
// if (original == -1) {
// Thread.dumpStack();
@@ -506,21 +597,21 @@ final class PatchFixesHider {
public static int fixRetrieveRightBraceOrSemiColonPosition(int retVal, AbstractMethodDeclaration amd) {
if (retVal != -1 || amd == null) return retVal;
- boolean isGenerated = EclipseAugments.ASTNode_generatedBy.get(amd) != null;
+ boolean isGenerated = ASTNode_generatedBy.get(amd) != null;
if (isGenerated) return amd.declarationSourceEnd;
return -1;
}
public static int fixRetrieveRightBraceOrSemiColonPosition(int retVal, FieldDeclaration fd) {
if (retVal != -1 || fd == null) return retVal;
- boolean isGenerated = EclipseAugments.ASTNode_generatedBy.get(fd) != null;
+ boolean isGenerated = ASTNode_generatedBy.get(fd) != null;
if (isGenerated) return fd.declarationSourceEnd;
return -1;
}
- public static int fixRetrieveProperRightBracketPosition(int retVal, ArrayType arrayType) {
- if (retVal != -1 || arrayType == null) return retVal;
- if (isGenerated(arrayType)) return arrayType.getStartPosition() + arrayType.getLength() - 1;
+ public static int fixRetrieveProperRightBracketPosition(int retVal, Type type) {
+ if (retVal != -1 || type == null) return retVal;
+ if (isGenerated(type)) return type.getStartPosition() + type.getLength() - 1;
return -1;
}
@@ -539,13 +630,13 @@ final class PatchFixesHider {
org.eclipse.jdt.internal.compiler.ast.ASTNode internalNode) throws Exception {
if (internalNode == null || domNode == null) return;
- boolean isGenerated = EclipseAugments.ASTNode_generatedBy.get(internalNode) != null;
+ boolean isGenerated = ASTNode_generatedBy.get(internalNode) != null;
if (isGenerated) domNode.getClass().getField("$isGenerated").set(domNode, true);
}
public static void setIsGeneratedFlagForName(org.eclipse.jdt.core.dom.Name name, Object internalNode) throws Exception {
if (internalNode instanceof org.eclipse.jdt.internal.compiler.ast.ASTNode) {
- boolean isGenerated = EclipseAugments.ASTNode_generatedBy.get((org.eclipse.jdt.internal.compiler.ast.ASTNode) internalNode) != null;
+ boolean isGenerated = ASTNode_generatedBy.get((org.eclipse.jdt.internal.compiler.ast.ASTNode) internalNode) != null;
if (isGenerated) name.getClass().getField("$isGenerated").set(name, true);
}
}