diff options
author | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2020-02-06 23:57:30 +0100 |
---|---|---|
committer | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2020-02-06 23:57:30 +0100 |
commit | 8943b49dfed95228e324422435a77597a95943c0 (patch) | |
tree | eeaadb526286cb8947abe06fb49379b8f3309b55 /src/core/lombok | |
parent | 2e06cb3ff19fb25b709ddfe5513a953c3d658b2e (diff) | |
download | lombok-8943b49dfed95228e324422435a77597a95943c0.tar.gz lombok-8943b49dfed95228e324422435a77597a95943c0.tar.bz2 lombok-8943b49dfed95228e324422435a77597a95943c0.zip |
[fixes #880] get rid of an unchecked cast warning for `@Getter(lazy=true)`
Diffstat (limited to 'src/core/lombok')
4 files changed, 39 insertions, 7 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index bdb88299..25999e85 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -1904,7 +1904,8 @@ public class EclipseHandlerUtil { return typeNode.add(type, Kind.TYPE); } - private static final char[] ALL = "all".toCharArray(); + static final char[] ALL = "all".toCharArray(); + static final char[] UNCHECKED = "unchecked".toCharArray(); private static final char[] JUSTIFICATION = "justification".toCharArray(); private static final char[] GENERATED_CODE = "generated code".toCharArray(); private static final char[] LOMBOK = "lombok".toCharArray(); @@ -1934,7 +1935,7 @@ public class EclipseHandlerUtil { return result; } - private static Annotation[] addAnnotation(ASTNode source, Annotation[] originalAnnotationArray, char[][] annotationTypeFqn, ASTNode arg) { + static Annotation[] addAnnotation(ASTNode source, Annotation[] originalAnnotationArray, char[][] annotationTypeFqn, ASTNode arg) { char[] simpleName = annotationTypeFqn[annotationTypeFqn.length - 1]; if (originalAnnotationArray != null) for (Annotation ann : originalAnnotationArray) { diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index 7ba84cdc..58401ac1 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2019 The Project Lombok Authors. + * Copyright (C) 2009-2020 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 @@ -46,6 +46,7 @@ import lombok.eclipse.agent.PatchDelegate; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.AllocationExpression; import org.eclipse.jdt.internal.compiler.ast.Annotation; +import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer; import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference; import org.eclipse.jdt.internal.compiler.ast.Assignment; import org.eclipse.jdt.internal.compiler.ast.BinaryExpression; @@ -66,6 +67,7 @@ import org.eclipse.jdt.internal.compiler.ast.ReturnStatement; 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.SynchronizedStatement; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.eclipse.jdt.internal.compiler.ast.TypeReference; @@ -241,8 +243,10 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> { TypeReference returnType = copyType(((FieldDeclaration) fieldNode.get()).type, source); Statement[] statements; + boolean addSuppressWarningsUnchecked = false; if (lazy) { statements = createLazyGetterBody(source, fieldNode); + addSuppressWarningsUnchecked = true; } else { statements = createSimpleGetterBody(source, fieldNode); } @@ -276,6 +280,14 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> { deprecated); } + if (addSuppressWarningsUnchecked) { + ArrayInitializer arr = new ArrayInitializer(); + arr.expressions = new Expression[2]; + arr.expressions[0] = new StringLiteral(ALL, 0, 0, 0); + arr.expressions[1] = new StringLiteral(UNCHECKED, 0, 0, 0); + method.annotations = addAnnotation(source, method.annotations, TypeConstants.JAVA_LANG_SUPPRESSWARNINGS, arr); + } + method.traverse(new SetGeneratedByVisitor(source), parent.scope); return method; } diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 681865df..8615d572 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2019 The Project Lombok Authors. + * Copyright (C) 2009-2020 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 @@ -38,6 +38,7 @@ import lombok.Getter; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; import lombok.core.configuration.CheckerFrameworkVersion; +import lombok.delombok.LombokOptionsFactory; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacNode; import lombok.javac.JavacTreeMaker; @@ -231,9 +232,11 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { List<JCStatement> statements; JCTree toClearOfMarkers = null; + boolean addSuppressWarningsUnchecked = false; if (lazy && !inNetbeansEditor(field)) { toClearOfMarkers = fieldNode.init; statements = createLazyGetterBody(treeMaker, field, source); + addSuppressWarningsUnchecked = LombokOptionsFactory.getDelombokOptions(field.getContext()).getFormatPreferences().generateSuppressWarnings(); } else { statements = createSimpleGetterBody(treeMaker, field); } @@ -252,11 +255,15 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { if (isFieldDeprecated(field)) annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil())); JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, - methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source, field.getContext()); + methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source, field.getContext()); if (toClearOfMarkers != null) recursiveSetGeneratedBy(toClearOfMarkers, null, null); decl.mods.annotations = decl.mods.annotations.appendList(delegates); + if (addSuppressWarningsUnchecked) { + addAnnotation(decl.mods, field, source.pos, source, field.getContext(), "java.lang.SuppressWarnings", treeMaker.NewArray(null, List.<JCExpression>nil(), List.<JCExpression>of(treeMaker.Literal("all"), treeMaker.Literal("unchecked")))); + } + copyJavadoc(field, decl, CopyJavadoc.GETTER); return decl; } @@ -418,7 +425,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { /* private final java.util.concurrent.atomic.AtomicReference<Object> fieldName = new java.util.concurrent.atomic.AtomicReference<Object>(); */ { field.vartype = recursiveSetGeneratedBy( - maker.TypeApply(chainDotsString(fieldNode, AR), List.<JCExpression>of(genJavaLangTypeRef(fieldNode, "Object"))), source, fieldNode.getContext()); + maker.TypeApply(chainDotsString(fieldNode, AR), List.<JCExpression>of(genJavaLangTypeRef(fieldNode, "Object"))), source, fieldNode.getContext()); field.init = recursiveSetGeneratedBy(maker.NewClass(null, NIL_EXPRESSION, copyType(maker, field), NIL_EXPRESSION, null), source, fieldNode.getContext()); } diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 3ec9f159..2fbc32b6 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1247,7 +1247,19 @@ public class JavacHandlerUtil { public static void addSuppressWarningsAll(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) { if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateSuppressWarnings()) return; - addAnnotation(mods, node, pos, source, context, "java.lang.SuppressWarnings", node.getTreeMaker().Literal("all")); + + boolean addJLSuppress = true; + + for (JCAnnotation ann : mods.annotations) { + JCTree type = ann.getAnnotationType(); + Name n = null; + if (type instanceof JCIdent) n = ((JCIdent) type).name; + else if (type instanceof JCFieldAccess) n = ((JCFieldAccess) type).name; + if (n != null && n.contentEquals("SuppressWarnings")) { + addJLSuppress = false; + } + } + if (addJLSuppress) addAnnotation(mods, node, pos, source, context, "java.lang.SuppressWarnings", node.getTreeMaker().Literal("all")); if (Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_FINDBUGS_SUPPRESSWARNINGS_ANNOTATIONS))) { JavacTreeMaker maker = node.getTreeMaker(); |