groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: GROOVY-7599: @Immutable should recognize JSR-310 classes
Date Fri, 02 Feb 2018 06:36:09 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 3e15e03b0 -> fb439b259


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/fb439b25
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/fb439b25
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/fb439b25

Branch: refs/heads/GROOVY_2_5_X
Commit: fb439b259fc353eb03546f8e7ba877efb597c716
Parents: 3e15e03
Author: paulk <paulk@asert.com.au>
Authored: Fri Feb 2 15:45:20 2018 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Fri Feb 2 16:35:56 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/fb439b25/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/fb439b25/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)'
+        '''
+    }
 }


Mime
View raw message