From 92bc41ae6dce8e16fd9da64c08ad085822fd33ed Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 4 Sep 2018 01:48:49 +0200 Subject: [debugging] Improved the ecj AST printer --- src/core/lombok/eclipse/EclipseASTVisitor.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/core') diff --git a/src/core/lombok/eclipse/EclipseASTVisitor.java b/src/core/lombok/eclipse/EclipseASTVisitor.java index b2fd4b2f..37bda5e3 100644 --- a/src/core/lombok/eclipse/EclipseASTVisitor.java +++ b/src/core/lombok/eclipse/EclipseASTVisitor.java @@ -34,9 +34,14 @@ import org.eclipse.jdt.internal.compiler.ast.Argument; import org.eclipse.jdt.internal.compiler.ast.Block; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration; +import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.Initializer; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; +import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation; +import org.eclipse.jdt.internal.compiler.ast.MemberValuePair; +import org.eclipse.jdt.internal.compiler.ast.NormalAnnotation; +import org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation; import org.eclipse.jdt.internal.compiler.ast.Statement; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.eclipse.jdt.internal.compiler.ast.TypeReference; @@ -351,7 +356,23 @@ public interface EclipseASTVisitor { } public void visitAnnotationOnMethod(AbstractMethodDeclaration method, EclipseNode node, Annotation annotation) { - forcePrint("", isGenerated(method) ? " (GENERATED)" : "", annotation, position(node)); + forcePrint("", isGenerated(method) ? " (GENERATED)" : "", annotation, position(node)); + if (annotation instanceof MarkerAnnotation || disablePrinting != 0) { + forcePrint("", isGenerated(method) ? " (GENERATED)" : "", annotation, position(node)); + } else { + forcePrint("", isGenerated(method) ? " (GENERATED)" : "", annotation, position(node)); + indent++; + if (annotation instanceof SingleMemberAnnotation) { + Expression expr = ((SingleMemberAnnotation) annotation).memberValue; + print(" %s", expr.getClass(), expr); + } + if (annotation instanceof NormalAnnotation) { + for (MemberValuePair mvp : ((NormalAnnotation) annotation).memberValuePairs) { + print(" %s", new String(mvp.name), mvp.value.getClass(), mvp.value); + } + } + indent--; + } } public void endVisitMethod(EclipseNode node, AbstractMethodDeclaration method) { -- cgit From af55bd0a328aa05f3ffb53a57a37306d1372a478 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 4 Sep 2018 01:53:30 +0200 Subject: [wip] Random fixes to various tests that are failing, especially against Java7. --- src/core/lombok/javac/handlers/HandleBuilder.java | 3 +- src/utils/lombok/core/ClassLiteral.java | 39 +++++++++++++++++----- src/utils/lombok/core/FieldSelect.java | 39 +++++++++++++++++----- src/utils/lombok/eclipse/Eclipse.java | 4 +-- .../BuilderSingularToBuilderWithNull.java | 2 +- .../after-delombok/BuilderWithToBuilder.java | 2 +- .../BuilderSingularToBuilderWithNull.java | 2 +- 7 files changed, 67 insertions(+), 24 deletions(-) (limited to 'src/core') diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index 63697691..1a471029 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -516,7 +516,8 @@ public class HandleBuilder extends JavacAnnotationHandler { arg = tgt[0]; } else { JCExpression eqNull = maker.Binary(CTC_EQUAL, tgt[0], maker.Literal(CTC_BOT, null)); - JCExpression emptyList = maker.Apply(List.nil(), chainDots(type, "java", "util", "Collections", "emptyList"), List.nil()); + List tas = cloneTypes(maker, bfd.singularData.getTypeArgs(), ast, type.getContext()); + JCExpression emptyList = maker.Apply(tas, chainDots(type, "java", "util", "Collections", "emptyList"), List.nil()); arg = maker.Conditional(eqNull, emptyList, tgt[1]); } diff --git a/src/utils/lombok/core/ClassLiteral.java b/src/utils/lombok/core/ClassLiteral.java index 077ead31..2008484b 100644 --- a/src/utils/lombok/core/ClassLiteral.java +++ b/src/utils/lombok/core/ClassLiteral.java @@ -1,13 +1,34 @@ +/* + * Copyright (C) 2018 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 + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.core; public class ClassLiteral { - private final String className; - - public ClassLiteral(String className) { - this.className = className; - } - - public String getClassName() { - return className; - } + private final String className; + + public ClassLiteral(String className) { + this.className = className; + } + + public String getClassName() { + return className; + } } diff --git a/src/utils/lombok/core/FieldSelect.java b/src/utils/lombok/core/FieldSelect.java index ab784401..4c055105 100644 --- a/src/utils/lombok/core/FieldSelect.java +++ b/src/utils/lombok/core/FieldSelect.java @@ -1,13 +1,34 @@ +/* + * Copyright (C) 2018 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 + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.core; public class FieldSelect { - private final String finalPart; - - public FieldSelect(String finalPart) { - this.finalPart = finalPart; - } - - public String getFinalPart() { - return finalPart; - } + private final String finalPart; + + public FieldSelect(String finalPart) { + this.finalPart = finalPart; + } + + public String getFinalPart() { + return finalPart; + } } diff --git a/src/utils/lombok/eclipse/Eclipse.java b/src/utils/lombok/eclipse/Eclipse.java index 943a7a7a..5dbe3e2d 100644 --- a/src/utils/lombok/eclipse/Eclipse.java +++ b/src/utils/lombok/eclipse/Eclipse.java @@ -186,11 +186,11 @@ public class Eclipse { default: return null; } } else if (e instanceof ClassLiteralAccess) { - return new ClassLiteral(Eclipse.toQualifiedName(((ClassLiteralAccess)e).type.getTypeName())); + return new ClassLiteral(Eclipse.toQualifiedName(((ClassLiteralAccess) e).type.getTypeName())); } else if (e instanceof SingleNameReference) { return new FieldSelect(new String(((SingleNameReference)e).token)); } else if (e instanceof QualifiedNameReference) { - String qName = Eclipse.toQualifiedName(((QualifiedNameReference)e).tokens); + String qName = Eclipse.toQualifiedName(((QualifiedNameReference) e).tokens); int idx = qName.lastIndexOf('.'); return new FieldSelect(idx == -1 ? qName : qName.substring(idx+1)); } else if (e instanceof UnaryExpression) { diff --git a/test/transform/resource/after-delombok/BuilderSingularToBuilderWithNull.java b/test/transform/resource/after-delombok/BuilderSingularToBuilderWithNull.java index 1f472438..b0563858 100644 --- a/test/transform/resource/after-delombok/BuilderSingularToBuilderWithNull.java +++ b/test/transform/resource/after-delombok/BuilderSingularToBuilderWithNull.java @@ -58,6 +58,6 @@ class BuilderSingularToBuilderWithNull { } @java.lang.SuppressWarnings("all") public BuilderSingularToBuilderWithNullBuilder toBuilder() { - return new BuilderSingularToBuilderWithNullBuilder().elems(this.elems == null ? java.util.Collections.emptyList() : this.elems); + return new BuilderSingularToBuilderWithNullBuilder().elems(this.elems == null ? java.util.Collections.emptyList() : this.elems); } } diff --git a/test/transform/resource/after-delombok/BuilderWithToBuilder.java b/test/transform/resource/after-delombok/BuilderWithToBuilder.java index b644a16f..8615a40b 100644 --- a/test/transform/resource/after-delombok/BuilderWithToBuilder.java +++ b/test/transform/resource/after-delombok/BuilderWithToBuilder.java @@ -86,7 +86,7 @@ class BuilderWithToBuilder { } @java.lang.SuppressWarnings("all") public BuilderWithToBuilderBuilder toBuilder() { - return new BuilderWithToBuilderBuilder().one(this.mOne).two(this.mTwo).foo(BuilderWithToBuilder.rrr(this)).bars(this.bars == null ? java.util.Collections.emptyList() : this.bars); + return new BuilderWithToBuilderBuilder().one(this.mOne).two(this.mTwo).foo(BuilderWithToBuilder.rrr(this)).bars(this.bars == null ? java.util.Collections.emptyList() : this.bars); } } class ConstructorWithToBuilder { diff --git a/test/transform/resource/after-ecj/BuilderSingularToBuilderWithNull.java b/test/transform/resource/after-ecj/BuilderSingularToBuilderWithNull.java index 7265e17a..7feff6e9 100644 --- a/test/transform/resource/after-ecj/BuilderSingularToBuilderWithNull.java +++ b/test/transform/resource/after-ecj/BuilderSingularToBuilderWithNull.java @@ -52,6 +52,6 @@ import lombok.Singular; return new BuilderSingularToBuilderWithNullBuilder(); } public @java.lang.SuppressWarnings("all") BuilderSingularToBuilderWithNullBuilder toBuilder() { - return new BuilderSingularToBuilderWithNullBuilder().elems(((this.elems == null) ? java.util.Collections.emptyList() : this.elems)); + return new BuilderSingularToBuilderWithNullBuilder().elems(((this.elems == null) ? java.util.Collections.emptyList() : this.elems)); } } -- cgit From a39ef3ba0cae1fd6038c4407ec7e66f9bf0f5ea2 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 4 Sep 2018 01:53:07 +0200 Subject: [issue #1851] Lombok needs to generate type-use annotations when copying over types from, say, a field, to getter/setter/constructor/builder/wither/etc. --- .../eclipse/handlers/EclipseHandlerUtil.java | 189 ++++++++++++++++++++- .../lombok/javac/handlers/JavacHandlerUtil.java | 8 + .../after-delombok/TypeUseAnnotations.java | 14 ++ .../resource/after-ecj/TypeUseAnnotations.java | 15 ++ .../resource/before/TypeUseAnnotations.java | 11 ++ 5 files changed, 229 insertions(+), 8 deletions(-) create mode 100644 test/transform/resource/after-delombok/TypeUseAnnotations.java create mode 100644 test/transform/resource/after-ecj/TypeUseAnnotations.java create mode 100644 test/transform/resource/before/TypeUseAnnotations.java (limited to 'src/core') diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index f89ef061..1e29764a 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -24,8 +24,10 @@ package lombok.eclipse.handlers; import static lombok.core.handlers.HandlerUtil.*; import static lombok.eclipse.Eclipse.*; import static lombok.eclipse.EclipseAugments.*; +import static lombok.eclipse.handlers.EclipseHandlerUtil.EclipseReflectiveMembers.*; import java.lang.reflect.Constructor; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -64,14 +66,22 @@ import org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference; import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference; import org.eclipse.jdt.internal.compiler.ast.Block; import org.eclipse.jdt.internal.compiler.ast.CastExpression; +import org.eclipse.jdt.internal.compiler.ast.CharLiteral; +import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration; +import org.eclipse.jdt.internal.compiler.ast.DoubleLiteral; import org.eclipse.jdt.internal.compiler.ast.EqualExpression; import org.eclipse.jdt.internal.compiler.ast.Expression; +import org.eclipse.jdt.internal.compiler.ast.ExtendedStringLiteral; +import org.eclipse.jdt.internal.compiler.ast.FalseLiteral; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.FieldReference; +import org.eclipse.jdt.internal.compiler.ast.FloatLiteral; import org.eclipse.jdt.internal.compiler.ast.IfStatement; import org.eclipse.jdt.internal.compiler.ast.IntLiteral; +import org.eclipse.jdt.internal.compiler.ast.Literal; +import org.eclipse.jdt.internal.compiler.ast.LongLiteral; import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation; import org.eclipse.jdt.internal.compiler.ast.MemberValuePair; import org.eclipse.jdt.internal.compiler.ast.MessageSend; @@ -89,8 +99,10 @@ import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; import org.eclipse.jdt.internal.compiler.ast.Statement; import org.eclipse.jdt.internal.compiler.ast.StringLiteral; +import org.eclipse.jdt.internal.compiler.ast.StringLiteralConcatenation; import org.eclipse.jdt.internal.compiler.ast.ThisReference; import org.eclipse.jdt.internal.compiler.ast.ThrowStatement; +import org.eclipse.jdt.internal.compiler.ast.TrueLiteral; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.eclipse.jdt.internal.compiler.ast.TypeParameter; import org.eclipse.jdt.internal.compiler.ast.TypeReference; @@ -250,6 +262,9 @@ public class EclipseHandlerUtil { MarkerAnnotation ann = new MarkerAnnotation(copyType(annotation.type, source), pS); setGeneratedBy(ann, source); ann.declarationSourceEnd = ann.sourceEnd = ann.statementEnd = pE; + try { + reflectSet(ANNOTATION__MEMBER_VALUE_PAIR_NAME, ann, reflect(ANNOTATION__MEMBER_VALUE_PAIR_NAME, annotation)); + } catch (Exception ignore) { /* Various eclipse versions don't have it */ } return ann; } @@ -257,8 +272,10 @@ public class EclipseHandlerUtil { SingleMemberAnnotation ann = new SingleMemberAnnotation(copyType(annotation.type, source), pS); setGeneratedBy(ann, source); ann.declarationSourceEnd = ann.sourceEnd = ann.statementEnd = pE; - //TODO memberValue(s) need to be copied as well (same for copying a NormalAnnotation as below). - ann.memberValue = ((SingleMemberAnnotation)annotation).memberValue; + ann.memberValue = copyAnnotationMemberValue(((SingleMemberAnnotation) annotation).memberValue); + try { + reflectSet(ANNOTATION__MEMBER_VALUE_PAIR_NAME, ann, reflect(ANNOTATION__MEMBER_VALUE_PAIR_NAME, annotation)); + } catch (Exception ignore) { /* Various eclipse versions don't have it */ } return ann; } @@ -266,13 +283,138 @@ public class EclipseHandlerUtil { NormalAnnotation ann = new NormalAnnotation(copyType(annotation.type, source), pS); setGeneratedBy(ann, source); ann.declarationSourceEnd = ann.statementEnd = ann.sourceEnd = pE; - ann.memberValuePairs = ((NormalAnnotation)annotation).memberValuePairs; + MemberValuePair[] inPairs = ((NormalAnnotation) annotation).memberValuePairs; + if (inPairs == null) { + ann.memberValuePairs = null; + } else { + ann.memberValuePairs = new MemberValuePair[inPairs.length]; + for (int i = 0; i < inPairs.length; i++) ann.memberValuePairs[i] = + new MemberValuePair(inPairs[i].name, inPairs[i].sourceStart, inPairs[i].sourceEnd, copyAnnotationMemberValue(inPairs[i].value)); + } + try { + reflectSet(ANNOTATION__MEMBER_VALUE_PAIR_NAME, ann, reflect(ANNOTATION__MEMBER_VALUE_PAIR_NAME, annotation)); + } catch (Exception ignore) { /* Various eclipse versions don't have it */ } return ann; } return annotation; } + static class EclipseReflectiveMembers { + public static final Field STRING_LITERAL__LINE_NUMBER; + public static final Field ANNOTATION__MEMBER_VALUE_PAIR_NAME; + public static final Field TYPE_REFERENCE__ANNOTATIONS; + static { + STRING_LITERAL__LINE_NUMBER = getField(StringLiteral.class, "lineNumber"); + ANNOTATION__MEMBER_VALUE_PAIR_NAME = getField(Annotation.class, "memberValuePairName"); + TYPE_REFERENCE__ANNOTATIONS = getField(TypeReference.class, "annotations"); + } + + public static int reflectInt(Field f, Object o) { + try { + return ((Number) f.get(o)).intValue(); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + public static void reflectSet(Field f, Object o, Object v) { + try { + f.set(o, v); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + public static Object reflect(Field f, Object o) { + try { + return f.get(o); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + private static Field getField(Class c, String fName) { + try { + Field f = c.getDeclaredField(fName); + f.setAccessible(true); + return f; + } catch (Exception e) { + return null; + } + } + } + + private static Expression copyAnnotationMemberValue(Expression in) { + Expression out = copyAnnotationMemberValue0(in); + out.constant = in.constant; + return out; + } + + private static Expression copyAnnotationMemberValue0(Expression in) { + int s = in.sourceStart, e = in.sourceEnd; + + // literals + + if (in instanceof FalseLiteral) return new FalseLiteral(s, e); + if (in instanceof TrueLiteral) return new TrueLiteral(s, e); + if (in instanceof NullLiteral) return new NullLiteral(s, e); + + if (in instanceof CharLiteral) return new CharLiteral(((Literal) in).source(), s, e); + if (in instanceof DoubleLiteral) return new DoubleLiteral(((Literal) in).source(), s, e); + if (in instanceof FloatLiteral) return new FloatLiteral(((Literal) in).source(), s, e); + if (in instanceof IntLiteral) return IntLiteral.buildIntLiteral(((Literal) in).source(), s, e); + if (in instanceof LongLiteral) return LongLiteral.buildLongLiteral(((Literal) in).source(), s, e); + + if (in instanceof StringLiteral) return new StringLiteral(((Literal) in).source(), s, e, reflectInt(STRING_LITERAL__LINE_NUMBER, in) + 1); + if (in instanceof ExtendedStringLiteral) { + StringLiteral str = new StringLiteral(((Literal) in).source(), s, e, reflectInt(STRING_LITERAL__LINE_NUMBER, in) + 1); + StringLiteral empty = new StringLiteral(new char[0], s, e, reflectInt(STRING_LITERAL__LINE_NUMBER, in) + 1); + return new ExtendedStringLiteral(str, empty); + } + if (in instanceof StringLiteralConcatenation) { + Expression[] literals = ((StringLiteralConcatenation) in).literals; + // 0 and 1 len shouldn't happen. + if (literals.length == 0) return new StringLiteral(new char[0], s, e, 0); + if (literals.length == 1) return copyAnnotationMemberValue0(literals[0]); + StringLiteralConcatenation c = new StringLiteralConcatenation((StringLiteral) literals[0], (StringLiteral) literals[1]); + for (int i = 2; i < literals.length; i++) c = c.extendsWith((StringLiteral) literals[i]); + return c; + } + + // enums and field accesses (as long as those are references to compile time constant literals that's also acceptable) + + if (in instanceof SingleNameReference) { + SingleNameReference snr = (SingleNameReference) in; + long p = (long) s << 32 | e; + return new SingleNameReference(snr.token, p); + } + if (in instanceof QualifiedNameReference) { + QualifiedNameReference qnr = (QualifiedNameReference) in; + return new QualifiedNameReference(qnr.tokens, qnr.sourcePositions, s, e); + } + + // class refs + if (in instanceof ClassLiteralAccess) return new ClassLiteralAccess(e, copyType(((ClassLiteralAccess) in).type)); + + // arrays + if (in instanceof ArrayInitializer) { + Expression[] exprs = ((ArrayInitializer) in).expressions; + Expression[] copy = new Expression[exprs.length]; + for (int i = 0; i < exprs.length; i++) copy[i] = copyAnnotationMemberValue(exprs[i]); + ArrayInitializer out = new ArrayInitializer(); + out.sourceStart = s; + out.sourceEnd = e; + out.bits = in.bits; + out.implicitConversion = in.implicitConversion; + out.statementEnd = in.statementEnd; + out.expressions = copy; + return out; + } + + return in; + } + /** * You can't share TypeParameter objects or bad things happen; for example, one 'T' resolves differently * from another 'T', even for the same T in a single class file. Unfortunately the TypeParameter type hierarchy @@ -368,6 +510,7 @@ public class EclipseHandlerUtil { } TypeReference typeRef = new ParameterizedQualifiedTypeReference(iRef.tokens, args, iRef.dimensions(), copy(iRef.sourcePositions)); + copyTypeAnns(ref, typeRef); if (source != null) setGeneratedBy(typeRef, source); return typeRef; } @@ -375,6 +518,7 @@ public class EclipseHandlerUtil { if (ref instanceof ArrayQualifiedTypeReference) { ArrayQualifiedTypeReference iRef = (ArrayQualifiedTypeReference) ref; TypeReference typeRef = new ArrayQualifiedTypeReference(iRef.tokens, iRef.dimensions(), copy(iRef.sourcePositions)); + copyTypeAnns(ref, typeRef); if (source != null) setGeneratedBy(typeRef, source); return typeRef; } @@ -382,6 +526,7 @@ public class EclipseHandlerUtil { if (ref instanceof QualifiedTypeReference) { QualifiedTypeReference iRef = (QualifiedTypeReference) ref; TypeReference typeRef = new QualifiedTypeReference(iRef.tokens, copy(iRef.sourcePositions)); + copyTypeAnns(ref, typeRef); if (source != null) setGeneratedBy(typeRef, source); return typeRef; } @@ -398,32 +543,36 @@ public class EclipseHandlerUtil { } } - TypeReference typeRef = new ParameterizedSingleTypeReference(iRef.token, args, iRef.dimensions(), (long)iRef.sourceStart << 32 | iRef.sourceEnd); + TypeReference typeRef = new ParameterizedSingleTypeReference(iRef.token, args, iRef.dimensions(), (long) iRef.sourceStart << 32 | iRef.sourceEnd); + copyTypeAnns(ref, typeRef); if (source != null) setGeneratedBy(typeRef, source); return typeRef; } if (ref instanceof ArrayTypeReference) { ArrayTypeReference iRef = (ArrayTypeReference) ref; - TypeReference typeRef = new ArrayTypeReference(iRef.token, iRef.dimensions(), (long)iRef.sourceStart << 32 | iRef.sourceEnd); + TypeReference typeRef = new ArrayTypeReference(iRef.token, iRef.dimensions(), (long) iRef.sourceStart << 32 | iRef.sourceEnd); + copyTypeAnns(ref, typeRef); if (source != null) setGeneratedBy(typeRef, source); return typeRef; } if (ref instanceof Wildcard) { - Wildcard original = (Wildcard)ref; + Wildcard original = (Wildcard) ref; Wildcard wildcard = new Wildcard(original.kind); wildcard.sourceStart = original.sourceStart; wildcard.sourceEnd = original.sourceEnd; if (original.bound != null) wildcard.bound = copyType(original.bound, source); + copyTypeAnns(ref, wildcard); if (source != null) setGeneratedBy(wildcard, source); return wildcard; } if (ref instanceof SingleTypeReference) { SingleTypeReference iRef = (SingleTypeReference) ref; - TypeReference typeRef = new SingleTypeReference(iRef.token, (long)iRef.sourceStart << 32 | iRef.sourceEnd); + TypeReference typeRef = new SingleTypeReference(iRef.token, (long) iRef.sourceStart << 32 | iRef.sourceEnd); + copyTypeAnns(ref, typeRef); if (source != null) setGeneratedBy(typeRef, source); return typeRef; } @@ -431,6 +580,30 @@ public class EclipseHandlerUtil { return ref; } + private static void copyTypeAnns(TypeReference in, TypeReference out) { + Annotation[][] a; + try { + a = (Annotation[][]) reflect(TYPE_REFERENCE__ANNOTATIONS, in); + } catch (Exception e) { + return; + } + + if (a == null) { + reflectSet(TYPE_REFERENCE__ANNOTATIONS, out, null); + return; + } + + Annotation[][] b = new Annotation[a.length][]; + for (int i = 0; i < a.length; i++) { + b[i] = new Annotation[a[i].length]; + for (int j = 0 ; j < a[i].length; j++) { + b[i][j] = copyAnnotation(a[i][j], a[i][j]); + } + } + + reflectSet(TYPE_REFERENCE__ANNOTATIONS, out, b); + } + public static Annotation[] copyAnnotations(ASTNode source, Annotation[]... allAnnotations) { List result = null; for (Annotation[] annotations : allAnnotations) { @@ -525,7 +698,7 @@ public class EclipseHandlerUtil { public static TypeReference cloneSelfType(EclipseNode context, ASTNode source) { int pS = source == null ? 0 : source.sourceStart, pE = source == null ? 0 : source.sourceEnd; - long p = (long)pS << 32 | pE; + long p = (long) pS << 32 | pE; EclipseNode type = context; TypeReference result = null; while (type != null && type.getKind() != Kind.TYPE) type = type.up(); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 570d1a7e..92c642d4 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1583,6 +1583,14 @@ public class JavacHandlerUtil { return node; } + public static List cloneTypes(JavacTreeMaker maker, List in, JCTree source, Context context) { + if (in.isEmpty()) return List.nil(); + if (in.size() == 1) return List.of(cloneType(maker, in.get(0), source, context)); + ListBuffer lb = new ListBuffer(); + for (JCExpression expr : in) lb.append(cloneType(maker, expr, source, context)); + return lb.toList(); + } + /** * Creates a full clone of a given javac AST type node. Every part is cloned (every identifier, every select, every wildcard, every type apply). * diff --git a/test/transform/resource/after-delombok/TypeUseAnnotations.java b/test/transform/resource/after-delombok/TypeUseAnnotations.java new file mode 100644 index 00000000..1e1536bf --- /dev/null +++ b/test/transform/resource/after-delombok/TypeUseAnnotations.java @@ -0,0 +1,14 @@ +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; +import java.util.List; +@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) +@interface TA { + int x(); +} +class TypeUseAnnotations { + List<@TA(x = 5) String> foo; + @java.lang.SuppressWarnings("all") + public List<@TA(x = 5) String> getFoo() { + return this.foo; + } +} diff --git a/test/transform/resource/after-ecj/TypeUseAnnotations.java b/test/transform/resource/after-ecj/TypeUseAnnotations.java new file mode 100644 index 00000000..156643b9 --- /dev/null +++ b/test/transform/resource/after-ecj/TypeUseAnnotations.java @@ -0,0 +1,15 @@ +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; +import java.util.List; +@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TA { + int x(); +} +class TypeUseAnnotations { + @lombok.Getter List<@TA(x = 5) String> foo; + TypeUseAnnotations() { + super(); + } + public @java.lang.SuppressWarnings("all") List<@TA(x = 5) String> getFoo() { + return this.foo; + } +} \ No newline at end of file diff --git a/test/transform/resource/before/TypeUseAnnotations.java b/test/transform/resource/before/TypeUseAnnotations.java new file mode 100644 index 00000000..c09a291d --- /dev/null +++ b/test/transform/resource/before/TypeUseAnnotations.java @@ -0,0 +1,11 @@ +//version 8: +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; +import java.util.List; +@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) +@interface TA { + int x(); +} +class TypeUseAnnotations { + @lombok.Getter List<@TA(x=5) String> foo; +} -- cgit From 6a42bccb9730b7072b1219a03642074ab96fbe61 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 10 Sep 2018 22:24:06 +0200 Subject: toBuilder() with singular should force the typeargs on the Collections.emptyList call. --- src/core/lombok/eclipse/handlers/HandleBuilder.java | 1 + test/transform/resource/after-ecj/BuilderWithToBuilder.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'src/core') diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index 9a069c58..4d20f052 100644 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -541,6 +541,7 @@ public class HandleBuilder extends EclipseAnnotationHandler { MessageSend emptyList = new MessageSend(); emptyList.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.UTIL, "Collections".toCharArray()); emptyList.selector = EMPTY_LIST; + emptyList.typeArguments = copyTypes(bfd.singularData.getTypeArgs().toArray(new TypeReference[0])); ms.arguments = new Expression[] {new ConditionalExpression(ifNull, emptyList, tgt[1])}; } ms.receiver = receiver; diff --git a/test/transform/resource/after-ecj/BuilderWithToBuilder.java b/test/transform/resource/after-ecj/BuilderWithToBuilder.java index b9cc27dd..a8935107 100644 --- a/test/transform/resource/after-ecj/BuilderWithToBuilder.java +++ b/test/transform/resource/after-ecj/BuilderWithToBuilder.java @@ -74,7 +74,7 @@ import lombok.Builder; return new BuilderWithToBuilderBuilder(); } public @java.lang.SuppressWarnings("all") BuilderWithToBuilderBuilder toBuilder() { - return new BuilderWithToBuilderBuilder().one(this.mOne).two(this.mTwo).foo(BuilderWithToBuilder.rrr(this)).bars(((this.bars == null) ? java.util.Collections.emptyList() : this.bars)); + return new BuilderWithToBuilderBuilder().one(this.mOne).two(this.mTwo).foo(BuilderWithToBuilder.rrr(this)).bars(((this.bars == null) ? java.util.Collections.emptyList() : this.bars)); } } @lombok.experimental.Accessors(prefix = "m") class ConstructorWithToBuilder { -- cgit