Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X fd8ccf4e7 -> fd81ec1fe


GROOVY-7599: @Immutable should recognize JSR-310 classes


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/fd81ec1f
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/fd81ec1f
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/fd81ec1f

Branch: refs/heads/GROOVY_2_6_X
Commit: fd81ec1fe527bdf03577cee29f4f2dd470524791
Parents: fd8ccf4
Author: paulk <pa...@asert.com.au>
Authored: Fri Feb 2 15:45:20 2018 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Fri Feb 2 16:35:34 2018 +1000

----------------------------------------------------------------------
 .../transform/ImmutableASTTransformation.java   | 43 +++++++++++++++++---
 .../transform/ImmutableTransformTest.groovy     | 18 ++++++++
 2 files changed, 56 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/fd81ec1f/src/main/java/org/codehaus/groovy/transform/ImmutableASTTransformation.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/codehaus/groovy/transform/ImmutableASTTransformation.java 
b/src/main/java/org/codehaus/groovy/transform/ImmutableASTTransformation.java
index 0d271ed..9e25e18 100644
--- 
a/src/main/java/org/codehaus/groovy/transform/ImmutableASTTransformation.java
+++ 
b/src/main/java/org/codehaus/groovy/transform/ImmutableASTTransformation.java
@@ -59,6 +59,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -118,7 +119,7 @@ public class ImmutableASTTransformation extends 
AbstractASTTransformation {
       This list can by extended by providing "known immutable" classes
       via Immutable.knownImmutableClasses
      */
-    private static List<String> immutableList = Arrays.asList(
+    private static Set<String> builtinImmutables = new HashSet(Arrays.asList(
             "java.lang.Class",
             "java.lang.Boolean",
             "java.lang.Byte",
@@ -133,8 +134,39 @@ public class ImmutableASTTransformation extends 
AbstractASTTransformation {
             "java.math.BigDecimal",
             "java.awt.Color",
             "java.net.URI",
-            "java.util.UUID"
-    );
+            "java.util.UUID",
+            "java.time.DayOfWeek",
+            "java.time.Duration",
+            "java.time.Instant",
+            "java.time.LocalDate",
+            "java.time.LocalDateTime",
+            "java.time.LocalTime",
+            "java.time.Month",
+            "java.time.MonthDay",
+            "java.time.OffsetDateTime",
+            "java.time.OffsetTime",
+            "java.time.Period",
+            "java.time.Year",
+            "java.time.YearMonth",
+            "java.time.ZonedDateTime",
+            "java.time.ZoneOffset",
+            "java.time.ZoneRegion",
+            "java.time.chrono.ChronoLocalDate",
+            "java.time.chrono.ChronoLocalDateTime",
+            "java.time.chrono.Chronology",
+            "java.time.chrono.ChronoPeriod",
+            "java.time.chrono.ChronoZonedDateTime",
+            "java.time.chrono.Era",
+            "java.time.format.DecimalStyle",
+            "java.time.format.FormatStyle",
+            "java.time.format.ResolverStyle",
+            "java.time.format.SignStyle",
+            "java.time.format.TextStyle",
+            "java.time.temporal.IsoFields",
+            "java.time.temporal.JulianFields",
+            "java.time.temporal.ValueRange",
+            "java.time.temporal.WeekFields"
+    ));
     private static final Class MY_CLASS = ImmutableBase.class;
     private static final Class<? extends Annotation> KNOWN_IMMUTABLE_CLASS = 
KnownImmutable.class;
     private static final Class<? extends Annotation> IMMUTABLE_BASE_CLASS = 
ImmutableBase.class;
@@ -578,7 +610,7 @@ public class ImmutableASTTransformation extends 
AbstractASTTransformation {
     }
 
     private static boolean inImmutableList(String typeName) {
-        return immutableList.contains(typeName);
+        return builtinImmutables.contains(typeName);
     }
 
     private static Statement 
createConstructorStatementArrayOrCloneable(FieldNode fNode, boolean namedArgs) {
@@ -650,7 +682,8 @@ public class ImmutableASTTransformation extends 
AbstractASTTransformation {
                 "Immutable classes only support properties with effectively 
immutable types including:\n" +
                 "- Strings, primitive types, wrapper types, Class, BigInteger 
and BigDecimal, enums\n" +
                 "- classes annotated with @KnownImmutable and known immutables 
(java.awt.Color, java.net.URI)\n" +
-                "- Cloneable classes, collections, maps and arrays, and other 
classes with special handling (java.util.Date)\n" +
+                "- Cloneable classes, collections, maps and arrays, and other 
classes with special handling\n" +
+                "  (java.util.Date and various java.time.* classes and 
interfaces)\n" +
                 "Other restrictions apply, please see the groovydoc for " + 
MY_TYPE_NAME + " for further details";
     }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/fd81ec1f/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy
----------------------------------------------------------------------
diff --git 
a/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy 
b/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy
index 4d47351..fb73e88 100644
--- a/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy
@@ -1037,4 +1037,22 @@ class ImmutableTransformTest extends GroovyShellTestCase 
{
             '''
         }
     }
+
+    // GROOVY-7599
+    @Test
+    void testImmutableWithJSR310_vm8() {
+        assertScript '''
+            import groovy.transform.Immutable
+            import java.time.*
+
+            @Immutable
+            class Person {
+              String first, last
+              LocalDate born
+            }
+
+            def mmm = new Person('Fred', 'Brooks', LocalDate.of(1931, 
Month.APRIL, 19))
+            assert mmm.toString() == 'Person(Fred, Brooks, 1931-04-19)'
+        '''
+    }
 }

Reply via email to