From 91cd859b72c89a47bbe145c00a9e7c1b20a5e657 Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Tue, 14 Apr 2020 09:15:31 +0200 Subject: [fixes #2286] Set source range for vararg arrays --- src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/eclipseAgent/lombok/eclipse/agent') diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index 756c23fa..95d42c2c 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -103,6 +103,7 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { patchIdentifierEndReparse(sm); patchRetrieveEllipsisStartPosition(sm); patchRetrieveRightBraceOrSemiColonPosition(sm); + patchRetrieveProperRightBracketPosition(sm); patchSetGeneratedFlag(sm); patchDomAstReparseIssues(sm); patchHideGeneratedNodes(sm); @@ -445,6 +446,16 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { // .transplant().request(StackRequest.RETURN_VALUE, StackRequest.PARAM2).build()); } + private static void patchRetrieveProperRightBracketPosition(ScriptManager sm) { + sm.addScript(ScriptBuilder.wrapMethodCall() + .target(new MethodTarget("org.eclipse.jdt.core.dom.ASTConverter", "extractSubArrayType", "org.eclipse.jdt.core.dom.ArrayType", "org.eclipse.jdt.core.dom.ArrayType", "int", "int")) + .methodToWrap(new Hook("org.eclipse.jdt.core.dom.ASTConverter", "retrieveProperRightBracketPosition", "int", "int", "int")) + .wrapMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "fixRetrieveProperRightBracketPosition", "int", "int", "org.eclipse.jdt.core.dom.ArrayType")) + .requestExtra(StackRequest.PARAM1) + .transplant() + .build()); + } + private static void patchSetGeneratedFlag(ScriptManager sm) { sm.addScript(ScriptBuilder.addField() .targetClass("org.eclipse.jdt.internal.compiler.ast.ASTNode") -- cgit From a2a194f32da2058e8783cd664d74799cf418d03d Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Sat, 16 May 2020 11:54:56 +0200 Subject: [fixes #2006] Delegate now excludes already implemented methods --- src/core/lombok/javac/handlers/HandleDelegate.java | 13 ++-- .../lombok/eclipse/agent/PatchDelegate.java | 73 +++++++++++++++++++++- .../after-delombok/DelegateAlreadyImplemented.java | 43 +++++++++++++ .../after-ecj/DelegateAlreadyImplemented.java | 29 +++++++++ .../before/DelegateAlreadyImplemented.java | 45 +++++++++++++ 5 files changed, 194 insertions(+), 9 deletions(-) create mode 100644 test/transform/resource/after-delombok/DelegateAlreadyImplemented.java create mode 100644 test/transform/resource/after-ecj/DelegateAlreadyImplemented.java create mode 100644 test/transform/resource/before/DelegateAlreadyImplemented.java (limited to 'src/eclipseAgent/lombok/eclipse/agent') diff --git a/src/core/lombok/javac/handlers/HandleDelegate.java b/src/core/lombok/javac/handlers/HandleDelegate.java index c19540fa..d6e76ab1 100644 --- a/src/core/lombok/javac/handlers/HandleDelegate.java +++ b/src/core/lombok/javac/handlers/HandleDelegate.java @@ -48,6 +48,7 @@ import org.mangosdk.spi.ProviderFor; import com.sun.tools.javac.code.Attribute.Compound; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.code.Symbol; +import com.sun.tools.javac.code.Symbol.MethodSymbol; import com.sun.tools.javac.code.Symbol.TypeSymbol; import com.sun.tools.javac.code.Type; import com.sun.tools.javac.code.Type.ClassType; @@ -175,14 +176,14 @@ public class HandleDelegate extends JavacAnnotationHandler { List signaturesToExclude = new ArrayList(); Set banList = new HashSet(); banList.addAll(METHODS_IN_OBJECT); - /* To exclude all methods in the class itself, try this: - for (Symbol member : ((JCClassDecl)typeNode.get()).sym.getEnclosedElements()) { - if (member instanceof MethodSymbol) { - MethodSymbol method = (MethodSymbol) member; - banList.add(printSig((ExecutableType) method.asType(), method.name, annotationNode.getTypesUtil())); + + // Add already implemented methods to ban list + JavacNode typeNode = upToTypeNode(annotationNode); + for (Symbol m : ((JCClassDecl)typeNode.get()).sym.getEnclosedElements()) { + if (m instanceof MethodSymbol) { + banList.add(printSig((ExecutableType) m.asType(), m.name, annotationNode.getTypesUtil())); } } - */ try { for (Type t : toExclude) { diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java index 02760e35..ebd60ce9 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java @@ -29,11 +29,14 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import lombok.core.AST.Kind; +import lombok.eclipse.Eclipse; import lombok.eclipse.EclipseAST; import lombok.eclipse.EclipseNode; import lombok.eclipse.TransformEclipseAST; @@ -67,6 +70,7 @@ import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding; import org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding; import org.eclipse.jdt.internal.compiler.lookup.Binding; import org.eclipse.jdt.internal.compiler.lookup.ClassScope; +import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers; import org.eclipse.jdt.internal.compiler.lookup.FieldBinding; import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; import org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding; @@ -214,7 +218,7 @@ public class PatchDelegate { addAllMethodBindings(methodsToExclude, cla.type.resolveType(decl.initializerScope), new HashSet(), field.name, ann); } - Set banList = new HashSet(); + Set banList = findAlreadyImplementedMethods(decl); for (BindingTuple excluded : methodsToExclude) banList.add(printSig(excluded.parameterized)); if (rawTypes.isEmpty()) { @@ -283,8 +287,8 @@ public class PatchDelegate { for (ClassLiteralAccess cla : excludedRawTypes) { addAllMethodBindings(methodsToExclude, cla.type.resolveType(decl.initializerScope), new HashSet(), method.selector, ann); } - - Set banList = new HashSet(); + + Set banList = findAlreadyImplementedMethods(decl); for (BindingTuple excluded : methodsToExclude) banList.add(printSig(excluded.parameterized)); if (rawTypes.isEmpty()) { @@ -764,6 +768,21 @@ public class PatchDelegate { } } + private static Set findAlreadyImplementedMethods(TypeDeclaration decl) { + Set sigs = new HashSet(); + for (AbstractMethodDeclaration md : decl.methods) { + if (md.isStatic()) continue; + if ((md.modifiers & ClassFileConstants.AccBridge) != 0) continue; + if (md.isConstructor()) continue; + if ((md.modifiers & ExtraCompilerModifiers.AccDefaultAbstract) != 0) continue; + if ((md.modifiers & ClassFileConstants.AccPublic) == 0) continue; + if ((md.modifiers & ClassFileConstants.AccSynthetic) != 0) continue; + + sigs.add(printSig(md, decl.scope)); + } + return sigs; + } + private static final char[] STRING_LOMBOK = new char[] {'l', 'o', 'm', 'b', 'o', 'k'}; private static final char[] STRING_EXPERIMENTAL = new char[] {'e', 'x', 'p', 'e', 'r', 'i', 'm', 'e', 'n', 't', 'a', 'l'}; private static final char[] STRING_DELEGATE = new char[] {'D', 'e', 'l', 'e', 'g', 'a', 't', 'e'}; @@ -839,6 +858,54 @@ public class PatchDelegate { return signature.toString(); } + private static String printSig(AbstractMethodDeclaration md, ClassScope scope) { + StringBuilder signature = new StringBuilder(); + + signature.append(md.selector); + signature.append("("); + boolean first = true; + if (md.arguments != null) { + TypeParameter[] typeParameters = md.typeParameters(); + Map typeParametersMap = new HashMap(); + if (typeParameters != null) { + for (TypeParameter typeParameter : typeParameters) { + typeParametersMap.put(new String(typeParameter.name), typeParameter); + } + } + + for (Argument argument : md.arguments) { + TypeBinding typeBinding = makeTypeBinding(argument.type, typeParametersMap, scope); + + if (!first) signature.append(", "); + first = false; + signature.append(typeBindingToSignature(typeBinding)); + } + } + signature.append(")"); + + return signature.toString(); + } + + private static TypeBinding makeTypeBinding(TypeReference typeReference, Map typeParametersMap, ClassScope scope) { + char[][] typeName = typeReference.getTypeName(); + String typeNameString = Eclipse.toQualifiedName(typeName); + + TypeParameter typeParameter = typeParametersMap.get(typeNameString); + if (typeParameter != null) { + if (typeParameter.type != null) { + typeName = typeParameter.type.getTypeName(); + } else { + typeName = TypeConstants.JAVA_LANG_OBJECT; + } + } + + TypeBinding typeBinding = scope.getType(typeName, typeName.length); + if (typeReference.dimensions() > 0) { + typeBinding = scope.createArrayType(typeBinding, typeReference.dimensions()); + } + return typeBinding; + } + private static String typeBindingToSignature(TypeBinding binding) { binding = binding.erasure(); if (binding != null && binding.isBaseType()) { diff --git a/test/transform/resource/after-delombok/DelegateAlreadyImplemented.java b/test/transform/resource/after-delombok/DelegateAlreadyImplemented.java new file mode 100644 index 00000000..c876d8ce --- /dev/null +++ b/test/transform/resource/after-delombok/DelegateAlreadyImplemented.java @@ -0,0 +1,43 @@ +public class DelegateAlreadyImplemented { + private A a; + + public void a() { + } + + public void b(java.util.List l) { + } + + public void c(java.util.List l, String[] a, Integer... varargs) { + } + + public void d(String[][][][] d) { + } + + public void e(Y x) { + } + + @SuppressWarnings("unchecked") + public void f(T s, java.util.List l, T[] a, T... varargs) { + } + + public void g(Number g) { + } +} + +interface A { + void a(); + + void b(java.util.List l); + + @SuppressWarnings("unchecked") + void c(java.util.List l, String[] a, T... varargs); + + void d(String[][][][] d); + + X e(X x); + + @SuppressWarnings("unchecked") + void f(T2 s, java.util.List l, T2[] a, T2... varargs); + + void g(G g); +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/DelegateAlreadyImplemented.java b/test/transform/resource/after-ecj/DelegateAlreadyImplemented.java new file mode 100644 index 00000000..1e12f405 --- /dev/null +++ b/test/transform/resource/after-ecj/DelegateAlreadyImplemented.java @@ -0,0 +1,29 @@ +public class DelegateAlreadyImplemented { + private @lombok.experimental.Delegate A a; + public DelegateAlreadyImplemented() { + super(); + } + public void a() { + } + public void b(java.util.List l) { + } + public void c(java.util.List l, String[] a, Integer... varargs) { + } + public void d(String[][][][] d) { + } + public void e(Y x) { + } + public @SuppressWarnings("unchecked") void f(T s, java.util.List l, T[] a, T... varargs) { + } + public void g(Number g) { + } +} +interface A { + public void a(); + public void b(java.util.List l); + public @SuppressWarnings("unchecked") void c(java.util.List l, String[] a, T... varargs); + public void d(String[][][][] d); + public X e(X x); + public @SuppressWarnings("unchecked") void f(T2 s, java.util.List l, T2[] a, T2... varargs); + public void g(G g); +} \ No newline at end of file diff --git a/test/transform/resource/before/DelegateAlreadyImplemented.java b/test/transform/resource/before/DelegateAlreadyImplemented.java new file mode 100644 index 00000000..c43c1949 --- /dev/null +++ b/test/transform/resource/before/DelegateAlreadyImplemented.java @@ -0,0 +1,45 @@ +public class DelegateAlreadyImplemented { + + @lombok.experimental.Delegate + private A a; + + public void a() { + } + + public void b(java.util.List l) { + } + + public void c(java.util.List l, String[] a, Integer... varargs) { + } + + public void d(String[][][][] d) { + } + + public void e(Y x) { + } + + @SuppressWarnings("unchecked") + public void f(T s, java.util.List l, T[] a, T... varargs) { + } + + public void g(Number g) { + } +} + +interface A { + public void a(); + + public void b(java.util.List l); + + @SuppressWarnings("unchecked") + public void c(java.util.List l, String[] a, T... varargs); + + public void d(String[][][][] d); + + public X e(X x); + + @SuppressWarnings("unchecked") + public void f(T2 s, java.util.List l, T2[] a, T2... varargs); + + public void g(G g); +} \ No newline at end of file -- cgit From faa682b41620d76796a4196e51fd2e2603c349a6 Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Sun, 21 Jun 2020 13:59:37 +0200 Subject: Do not resolve type if the name does not match --- src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/eclipseAgent/lombok/eclipse/agent') diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java index ebd60ce9..1a287d93 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java @@ -321,6 +321,8 @@ public class PatchDelegate { private static boolean isDelegate(Annotation ann, TypeDeclaration decl) { if (ann.type == null) return false; + if (!charArrayEquals("Delegate", ann.type.getLastToken())) return false; + TypeBinding tb = ann.type.resolveType(decl.initializerScope); if (tb == null) return false; if (!charArrayEquals("lombok", tb.qualifiedPackageName()) && !charArrayEquals("lombok.experimental", tb.qualifiedPackageName())) return false; -- cgit From ce92389da76b17b2c8d9f8757d67b4ea3edc8cb5 Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Sun, 12 Jul 2020 01:01:18 +0200 Subject: [fixes #977] Set generated flag for annotation names --- .../lombok/eclipse/agent/EclipsePatcher.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/eclipseAgent/lombok/eclipse/agent') diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index 95d42c2c..c560f002 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -575,6 +575,22 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { .wrapMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "setIsGeneratedFlagForName", "void", "org.eclipse.jdt.core.dom.Name", "java.lang.Object")) .transplant().build()); + + sm.addScript(ScriptBuilder.wrapMethodCall() + .target(new MethodTarget("org.eclipse.jdt.core.dom.ASTConverter", "setTypeNameForAnnotation", "void", "org.eclipse.jdt.internal.compiler.ast.Annotation", "org.eclipse.jdt.core.dom.Annotation")) + .methodToWrap(new Hook("org.eclipse.jdt.core.dom.SimpleName", "", "void", "org.eclipse.jdt.core.dom.AST")) + .requestExtra(StackRequest.PARAM1) + .wrapMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "setIsGeneratedFlagForName", "void", + "org.eclipse.jdt.core.dom.Name", "java.lang.Object")) + .transplant().build()); + + sm.addScript(ScriptBuilder.wrapMethodCall() + .target(new MethodTarget("org.eclipse.jdt.core.dom.ASTConverter", "setTypeNameForAnnotation", "void", "org.eclipse.jdt.internal.compiler.ast.Annotation", "org.eclipse.jdt.core.dom.Annotation")) + .methodToWrap(new Hook("org.eclipse.jdt.core.dom.QualifiedName", "", "void", "org.eclipse.jdt.core.dom.AST")) + .requestExtra(StackRequest.PARAM1) + .wrapMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "setIsGeneratedFlagForName", "void", + "org.eclipse.jdt.core.dom.Name", "java.lang.Object")) + .transplant().build()); } private static void patchAvoidReparsingGeneratedCode(ScriptManager sm) { -- cgit From 74211eb0ae63a916c6ecc7be4b92ba521e0cef34 Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Thu, 27 Aug 2020 09:46:41 +0200 Subject: [fixes #2566] Reset constant value set during type resolution --- .../lombok/eclipse/agent/PatchVal.java | 25 +++++++++++++++------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'src/eclipseAgent/lombok/eclipse/agent') diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java index 056852c8..f22e78a8 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java @@ -35,11 +35,13 @@ import org.eclipse.jdt.internal.compiler.ast.FunctionalExpression; import org.eclipse.jdt.internal.compiler.ast.ImportReference; import org.eclipse.jdt.internal.compiler.ast.LambdaExpression; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; +import org.eclipse.jdt.internal.compiler.ast.MessageSend; import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference; import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; +import org.eclipse.jdt.internal.compiler.impl.Constant; import org.eclipse.jdt.internal.compiler.impl.ReferenceContext; import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding; import org.eclipse.jdt.internal.compiler.lookup.Binding; @@ -227,14 +229,16 @@ public class PatchVal { boolean var = isVar(local, scope); if (!(val || var)) return false; - StackTraceElement[] st = new Throwable().getStackTrace(); - for (int i = 0; i < st.length - 2 && i < 10; i++) { - if (st[i].getClassName().equals("lombok.launch.PatchFixesHider$Val")) { - boolean valInForStatement = val && - st[i + 1].getClassName().equals("org.eclipse.jdt.internal.compiler.ast.LocalDeclaration") && - st[i + 2].getClassName().equals("org.eclipse.jdt.internal.compiler.ast.ForStatement"); - if (valInForStatement) return false; - break; + if (val) { + StackTraceElement[] st = new Throwable().getStackTrace(); + for (int i = 0; i < st.length - 2 && i < 10; i++) { + if (st[i].getClassName().equals("lombok.launch.PatchFixesHider$Val")) { + boolean valInForStatement = + st[i + 1].getClassName().equals("org.eclipse.jdt.internal.compiler.ast.LocalDeclaration") && + st[i + 2].getClassName().equals("org.eclipse.jdt.internal.compiler.ast.ForStatement"); + if (valInForStatement) return false; + break; + } } } @@ -264,6 +268,7 @@ public class PatchVal { } TypeBinding resolved = null; + Constant oldConstant = init.constant; try { resolved = decomponent ? getForEachComponentType(init, scope) : resolveForExpression(init, scope); } catch (NullPointerException e) { @@ -280,6 +285,10 @@ public class PatchVal { } catch (Exception e) { // Some type thing failed. } + } else { + if (init instanceof MessageSend && ((MessageSend) init).actualReceiverType == null) { + init.constant = oldConstant; + } } } -- cgit