From 3edac649118dff4a48d7218dc31a727f13537fea Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 15 Mar 2020 03:04:03 +0100 Subject: [fixes #2386] [checkerframework] Now generating checkerframework `@Pure` instead of `@SideEffectFree` where appropriate. --- src/core/lombok/core/LombokNode.java | 3 ++- .../core/configuration/CheckerFrameworkVersion.java | 16 +++++++++++----- src/core/lombok/eclipse/EclipseNode.java | 18 +++++++++++++++++- .../eclipse/handlers/HandleEqualsAndHashCode.java | 2 +- src/core/lombok/eclipse/handlers/HandleGetter.java | 6 +++++- .../lombok/eclipse/handlers/HandleSuperBuilder.java | 4 ++-- src/core/lombok/javac/JavacNode.java | 16 +++++++++++++++- .../lombok/javac/handlers/HandleEqualsAndHashCode.java | 4 ++-- src/core/lombok/javac/handlers/HandleGetter.java | 6 +++++- src/core/lombok/javac/handlers/HandleSuperBuilder.java | 4 ++-- 10 files changed, 62 insertions(+), 17 deletions(-) (limited to 'src/core/lombok') diff --git a/src/core/lombok/core/LombokNode.java b/src/core/lombok/core/LombokNode.java index 843a78f0..e52cd5b3 100644 --- a/src/core/lombok/core/LombokNode.java +++ b/src/core/lombok/core/LombokNode.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2018 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 @@ -286,6 +286,7 @@ public abstract class LombokNode, L extends LombokNode AnnotationValues findAnnotation(Class type); public abstract boolean isStatic(); + public abstract boolean isFinal(); public abstract boolean isTransient(); public abstract boolean isEnumMember(); public abstract boolean isEnumType(); diff --git a/src/core/lombok/core/configuration/CheckerFrameworkVersion.java b/src/core/lombok/core/configuration/CheckerFrameworkVersion.java index 587c5fec..5805678f 100644 --- a/src/core/lombok/core/configuration/CheckerFrameworkVersion.java +++ b/src/core/lombok/core/configuration/CheckerFrameworkVersion.java @@ -26,9 +26,11 @@ import java.util.regex.Pattern; public final class CheckerFrameworkVersion implements ConfigurationValueType { private final int version; - private static final int MAX_SUPPORTED = 3000; + private static final int DEFAULT = 3200; + private static final int MAX_SUPPORTED = 4000; public static final String NAME__SIDE_EFFECT_FREE = "org.checkerframework.dataflow.qual.SideEffectFree"; + public static final String NAME__PURE = "org.checkerframework.dataflow.qual.Pure"; public static final String NAME__UNIQUE = "org.checkerframework.common.aliasing.qual.Unique"; public static final String NAME__RETURNS_RECEIVER = "org.checkerframework.checker.builder.qual.ReturnsReceiver"; public static final String NAME__NOT_CALLED = "org.checkerframework.checker.builder.qual.NotCalledMethods"; @@ -47,21 +49,25 @@ public final class CheckerFrameworkVersion implements ConfigurationValueType { } public boolean generateUnique() { + return version > 2899; + } + + public boolean generatePure() { return version > 0; } public boolean generateReturnsReceiver() { - return version > 2999; + return version > 3999; } public boolean generateCalledMethods() { - return version > 2999; + return version > 3999; } public static CheckerFrameworkVersion valueOf(String versionString) { if (versionString != null) versionString = versionString.trim(); if (versionString == null || versionString.equalsIgnoreCase("false") || versionString.equals("0")) return new CheckerFrameworkVersion(0); - if (versionString.equalsIgnoreCase("true")) return new CheckerFrameworkVersion(MAX_SUPPORTED); + if (versionString.equalsIgnoreCase("true")) return new CheckerFrameworkVersion(DEFAULT); Matcher m = VERSION.matcher(versionString); if (!m.matches()) throw new IllegalArgumentException("Expected 'true' or 'false' or a major/minor version, such as '2.9'"); int major = Integer.parseInt(m.group(1)); @@ -81,7 +87,7 @@ public final class CheckerFrameworkVersion implements ConfigurationValueType { public static String exampleValue() { String s = (MAX_SUPPORTED / 1000) + "." + (MAX_SUPPORTED % 1000); - return "major.minor (example: 2.9 - and no higher than " + s + ") or true or false"; + return "major.minor (example: 3.2 - and no higher than " + s + ") or true or false"; } @Override public boolean equals(Object obj) { diff --git a/src/core/lombok/eclipse/EclipseNode.java b/src/core/lombok/eclipse/EclipseNode.java index 4c7f4eac..9db491f5 100644 --- a/src/core/lombok/eclipse/EclipseNode.java +++ b/src/core/lombok/eclipse/EclipseNode.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 @@ -248,6 +248,22 @@ public class EclipseNode extends lombok.core.LombokNode { /* Generate annotations that must be put on the generated method, and attach them. */ { Annotation[] deprecated = null, checkerFramework = null; if (isFieldDeprecated(fieldNode)) deprecated = new Annotation[] { generateDeprecatedAnnotation(source) }; - if (getCheckerFrameworkVersion(fieldNode).generateSideEffectFree()) checkerFramework = new Annotation[] { generateNamedAnnotation(source, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE) }; + if (fieldNode.isFinal()) { + if (getCheckerFrameworkVersion(fieldNode).generatePure()) checkerFramework = new Annotation[] { generateNamedAnnotation(source, CheckerFrameworkVersion.NAME__PURE) }; + } else { + if (getCheckerFrameworkVersion(fieldNode).generateSideEffectFree()) checkerFramework = new Annotation[] { generateNamedAnnotation(source, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE) }; + } method.annotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), diff --git a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java index 82af39fa..6634d1c8 100644 --- a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java @@ -785,7 +785,7 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { out.modifiers = ClassFileConstants.AccAbstract | ClassFileConstants.AccProtected | ExtraCompilerModifiers.AccSemicolonBody; Annotation overrideAnn = override ? makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, tdParent.get()) : null; Annotation rrAnn = cfv.generateReturnsReceiver() ? generateNamedAnnotation(tdParent.get(), CheckerFrameworkVersion.NAME__RETURNS_RECEIVER): null; - Annotation sefAnn = cfv.generateSideEffectFree() ? generateNamedAnnotation(tdParent.get(), CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE): null; + Annotation sefAnn = cfv.generatePure() ? generateNamedAnnotation(tdParent.get(), CheckerFrameworkVersion.NAME__PURE): null; if (overrideAnn != null && rrAnn != null && sefAnn != null) out.annotations = new Annotation[] {overrideAnn, rrAnn, sefAnn}; else if (overrideAnn != null && rrAnn != null) out.annotations = new Annotation[] {overrideAnn, rrAnn}; else if (overrideAnn != null && sefAnn != null) out.annotations = new Annotation[] {overrideAnn, sefAnn}; @@ -804,7 +804,7 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { out.modifiers = ClassFileConstants.AccProtected; Annotation overrideAnn = makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, builderImplType.get()); Annotation rrAnn = cfv.generateReturnsReceiver() ? generateNamedAnnotation(builderImplType.get(), CheckerFrameworkVersion.NAME__RETURNS_RECEIVER): null; - Annotation sefAnn = cfv.generateSideEffectFree() ? generateNamedAnnotation(builderImplType.get(), CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE): null; + Annotation sefAnn = cfv.generatePure() ? generateNamedAnnotation(builderImplType.get(), CheckerFrameworkVersion.NAME__PURE): null; if (rrAnn != null && sefAnn != null) out.annotations = new Annotation[] {overrideAnn, rrAnn, sefAnn}; else if (rrAnn != null) out.annotations = new Annotation[] {overrideAnn, rrAnn}; else if (sefAnn != null) out.annotations = new Annotation[] {overrideAnn, sefAnn}; diff --git a/src/core/lombok/javac/JavacNode.java b/src/core/lombok/javac/JavacNode.java index 191ab3c0..19bbeae3 100644 --- a/src/core/lombok/javac/JavacNode.java +++ b/src/core/lombok/javac/JavacNode.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 @@ -319,6 +319,20 @@ public class JavacNode extends lombok.core.LombokNode annsOnMethod = List.nil(); CheckerFrameworkVersion checkerFramework = getCheckerFrameworkVersion(typeNode); - if (checkerFramework.generateSideEffectFree()) { - annsOnMethod = annsOnMethod.prepend(maker.Annotation(genTypeRef(typeNode, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.nil())); + if (checkerFramework.generatePure()) { + annsOnMethod = annsOnMethod.prepend(maker.Annotation(genTypeRef(typeNode, CheckerFrameworkVersion.NAME__PURE), List.nil())); } JCModifiers mods = maker.Modifiers(Flags.PROTECTED, annsOnMethod); JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN); diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 8615d572..985873c1 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -251,7 +251,11 @@ public class HandleGetter extends JavacAnnotationHandler { List copyableAnnotations = findCopyableAnnotations(field); List delegates = findDelegatesAndRemoveFromField(field); List annsOnMethod = copyAnnotations(onMethod).appendList(copyableAnnotations); - if (getCheckerFrameworkVersion(field).generateSideEffectFree()) annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genTypeRef(field, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.nil())); + if (field.isFinal()) { + if (getCheckerFrameworkVersion(field).generatePure()) annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genTypeRef(field, CheckerFrameworkVersion.NAME__PURE), List.nil())); + } else { + if (getCheckerFrameworkVersion(field).generateSideEffectFree()) annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genTypeRef(field, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.nil())); + } if (isFieldDeprecated(field)) annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.nil())); JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, diff --git a/src/core/lombok/javac/handlers/HandleSuperBuilder.java b/src/core/lombok/javac/handlers/HandleSuperBuilder.java index cc70b333..2dc3247d 100644 --- a/src/core/lombok/javac/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/javac/handlers/HandleSuperBuilder.java @@ -750,7 +750,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler { List annotations = List.nil(); JCAnnotation overrideAnnotation = override ? maker.Annotation(genJavaLangTypeRef(type, "Override"), List.nil()) : null; JCAnnotation rrAnnotation = cfv.generateReturnsReceiver() ? maker.Annotation(genTypeRef(type, CheckerFrameworkVersion.NAME__RETURNS_RECEIVER), List.nil()) : null; - JCAnnotation sefAnnotation = cfv.generateSideEffectFree() ? maker.Annotation(genTypeRef(type, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.nil()) : null; + JCAnnotation sefAnnotation = cfv.generatePure() ? maker.Annotation(genTypeRef(type, CheckerFrameworkVersion.NAME__PURE), List.nil()) : null; if (sefAnnotation != null) annotations = annotations.prepend(sefAnnotation); if (rrAnnotation != null) annotations = annotations.prepend(rrAnnotation); if (overrideAnnotation != null) annotations = annotations.prepend(overrideAnnotation); @@ -766,7 +766,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler { JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(builderImplType, "Override"), List.nil()); JCAnnotation rrAnnotation = cfv.generateReturnsReceiver() ? maker.Annotation(genTypeRef(builderImplType, CheckerFrameworkVersion.NAME__RETURNS_RECEIVER), List.nil()) : null; - JCAnnotation sefAnnotation = cfv.generateSideEffectFree() ? maker.Annotation(genTypeRef(builderImplType, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.nil()) : null; + JCAnnotation sefAnnotation = cfv.generatePure() ? maker.Annotation(genTypeRef(builderImplType, CheckerFrameworkVersion.NAME__PURE), List.nil()) : null; List annsOnMethod = List.nil(); if (sefAnnotation != null) annsOnMethod = annsOnMethod.prepend(sefAnnotation); if (rrAnnotation != null) annsOnMethod = annsOnMethod.prepend(rrAnnotation); -- cgit