This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new d026fd2  GROOVY-9669: Enhance immutability check (port to 3_0_X)
d026fd2 is described below

commit d026fd263ce31d46bd0569b6156abe22a89fb4ac
Author: Daniel Sun <[email protected]>
AuthorDate: Sun Aug 2 17:57:32 2020 +0800

    GROOVY-9669: Enhance immutability check (port to 3_0_X)
---
 build.gradle                                            |  2 ++
 .../apache/groovy/ast/tools/ImmutablePropertyUtils.java | 17 +++++++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/build.gradle b/build.gradle
index 3e6cd05..8f317e6 100644
--- a/build.gradle
+++ b/build.gradle
@@ -157,6 +157,7 @@ ext {
     checkstyleVersion = '8.34'
     junit5Version = '5.6.2'
     junit5PlatformVersion = '1.6.2'
+    jcipAnnotationsVersion = '1.0'
 }
 
 dependencies {
@@ -213,6 +214,7 @@ dependencies {
     testImplementation project(':groovy-test')
     testImplementation project(':groovy-macro')
     spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.10.1'
+    testImplementation "net.jcip:jcip-annotations:$jcipAnnotationsVersion"
 }
 
 ext.generatedDirectory = "${buildDir}/generated/sources"
diff --git 
a/src/main/java/org/apache/groovy/ast/tools/ImmutablePropertyUtils.java 
b/src/main/java/org/apache/groovy/ast/tools/ImmutablePropertyUtils.java
index c89b484..190f259 100644
--- a/src/main/java/org/apache/groovy/ast/tools/ImmutablePropertyUtils.java
+++ b/src/main/java/org/apache/groovy/ast/tools/ImmutablePropertyUtils.java
@@ -19,7 +19,6 @@
 package org.apache.groovy.ast.tools;
 
 import groovy.transform.ImmutableOptions;
-import groovy.transform.KnownImmutable;
 import org.codehaus.groovy.ast.AnnotationNode;
 import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
@@ -54,7 +53,6 @@ public class ImmutablePropertyUtils {
     private static final ClassNode CLONEABLE_TYPE = make(Cloneable.class);
     private static final ClassNode DATE_TYPE = make(Date.class);
     private static final ClassNode REFLECTION_INVOKER_TYPE = 
make(ReflectionMethodInvoker.class);
-    private static final String KNOWN_IMMUTABLE_NAME = 
KnownImmutable.class.getName();
     private static final Class<? extends Annotation> IMMUTABLE_OPTIONS_CLASS = 
ImmutableOptions.class;
     public static final ClassNode IMMUTABLE_OPTIONS_TYPE = 
makeWithoutCaching(IMMUTABLE_OPTIONS_CLASS, false);
     private static final String MEMBER_KNOWN_IMMUTABLE_CLASSES = 
"knownImmutableClasses";
@@ -127,6 +125,13 @@ public class ImmutablePropertyUtils {
             "java.io.File"
     ));
 
+    private static final Set<String> BUILTIN_IMMUTABLE_ANNOTATIONS = new 
HashSet<String>(Arrays.asList(
+            "groovy.transform.Immutable",
+            "groovy.transform.KnownImmutable",
+//            "javax.annotation.concurrent.Immutable", // its RetentionPolicy 
is CLASS, can not be got via reflection
+            "net.jcip.annotations.Immutable" // supported by Findbugs and 
IntelliJ IDEA
+    ));
+
     private ImmutablePropertyUtils() { }
 
     public static Expression cloneArrayOrCloneableExpr(Expression fieldExpr, 
ClassNode type) {
@@ -194,13 +199,13 @@ public class ImmutablePropertyUtils {
         List<AnnotationNode> annotations = type.getAnnotations();
         for (AnnotationNode next : annotations) {
             String name = next.getClassNode().getName();
-            if (matchingMarkerName(name)) return true;
+            if (matchingImmutableMarkerName(name)) return true;
         }
         return false;
     }
 
-    private static boolean matchingMarkerName(String name) {
-        return name.equals("groovy.transform.Immutable") || 
name.equals(KNOWN_IMMUTABLE_NAME);
+    private static boolean matchingImmutableMarkerName(String name) {
+        return BUILTIN_IMMUTABLE_ANNOTATIONS.contains(name);
     }
 
     public static boolean isBuiltinImmutable(String typeName) {
@@ -211,7 +216,7 @@ public class ImmutablePropertyUtils {
         Annotation[] annotations = clazz.getAnnotations();
         for (Annotation next : annotations) {
             String name = next.annotationType().getName();
-            if (matchingMarkerName(name)) return true;
+            if (matchingImmutableMarkerName(name)) return true;
         }
         return false;
     }

Reply via email to