groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject incubator-groovy git commit: GROOVY-7503: Builder with Initializer strategy and no properties results in ClassFormatError (side effect: closes #60)
Date Thu, 16 Jul 2015 03:10:50 GMT
Repository: incubator-groovy
Updated Branches:
  refs/heads/master 653690c1a -> 2e5288ccf


GROOVY-7503: Builder with Initializer strategy and no properties results in ClassFormatError
(side effect: closes #60)


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

Branch: refs/heads/master
Commit: 2e5288ccf7ae5c119b9fc646546e10a0e8a41b5f
Parents: 653690c
Author: paulk <paulk@asert.com.au>
Authored: Thu Jul 16 13:10:32 2015 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Thu Jul 16 13:10:32 2015 +1000

----------------------------------------------------------------------
 gradle/pomconfigurer.gradle                     |  3 +++
 .../transform/builder/InitializerStrategy.java  | 13 ++++++----
 .../transform/BuilderTransformTest.groovy       | 25 ++++++++++++++++++--
 3 files changed, 35 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/2e5288cc/gradle/pomconfigurer.gradle
----------------------------------------------------------------------
diff --git a/gradle/pomconfigurer.gradle b/gradle/pomconfigurer.gradle
index 43e2f56..bafedb0 100644
--- a/gradle/pomconfigurer.gradle
+++ b/gradle/pomconfigurer.gradle
@@ -564,6 +564,9 @@ project.ext.pomConfigureClosureWithoutTweaks = {
             contributor {
                 name 'Nick Grealy'
             }
+            contributor {
+                name 'Marcin Grzejszczak'
+            }
         }
         mailingLists {
             mailingList {

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/2e5288cc/src/main/groovy/transform/builder/InitializerStrategy.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/transform/builder/InitializerStrategy.java b/src/main/groovy/transform/builder/InitializerStrategy.java
index 46de3f8..a69f7f0 100644
--- a/src/main/groovy/transform/builder/InitializerStrategy.java
+++ b/src/main/groovy/transform/builder/InitializerStrategy.java
@@ -112,9 +112,6 @@ import static org.objectweb.asm.Opcodes.ACC_SYNTHETIC;
  * to call your constructor. Any parameters to your constructor become the properties expected
by the initializer.
  * If you use such a builder on a constructor as well as on the class or on more than one
constructor, then it is up to you
  * to define unique values for 'builderClassName' and 'builderMethodName' for each annotation.
- *
- * @author Paul King
- * @author Marcin Grzejszczak
  */
 public class InitializerStrategy extends BuilderASTTransformation.AbstractBuilderStrategy
{
 
@@ -149,6 +146,10 @@ public class InitializerStrategy extends BuilderASTTransformation.AbstractBuilde
         if (!getIncludeExclude(transform, anno, buildee, excludes, includes)) return;
         List<FieldNode> fields = getInstancePropertyFields(buildee);
         List<FieldNode> filteredFields = filterFields(fields, includes, excludes);
+        if (filteredFields.isEmpty()) {
+            transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME +
+                    " processing: at least one property is required for this strategy", anno);
+        }
         ClassNode builder = createInnerHelperClass(buildee, getBuilderClassName(buildee,
anno), filteredFields.size());
         addFields(buildee, filteredFields, builder);
 
@@ -157,7 +158,7 @@ public class InitializerStrategy extends BuilderASTTransformation.AbstractBuilde
     }
 
     private void createBuilderForAnnotatedMethod(BuilderASTTransformation transform, MethodNode
mNode, AnnotationNode anno, boolean useSetters) {
-        if (transform.getMemberValue(anno, "includes") != null || transform.getMemberValue(anno,
"includes") != null) {
+        if (transform.getMemberValue(anno, "includes") != null || transform.getMemberValue(anno,
"excludes") != null) {
             transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME +
                     " processing: includes/excludes only allowed on classes", anno);
         }
@@ -172,6 +173,10 @@ public class InitializerStrategy extends BuilderASTTransformation.AbstractBuilde
         }
         ClassNode buildee = mNode.getDeclaringClass();
         Parameter[] parameters = mNode.getParameters();
+        if (parameters.length == 0) {
+            transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME +
+                    " processing: at least one parameter is required for this strategy",
anno);
+        }
         ClassNode builder = createInnerHelperClass(buildee, getBuilderClassName(buildee,
anno), parameters.length);
         List<FieldNode> convertedFields = convertParamsToFields(builder, parameters);
 

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/2e5288cc/src/test/org/codehaus/groovy/transform/BuilderTransformTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/BuilderTransformTest.groovy b/src/test/org/codehaus/groovy/transform/BuilderTransformTest.groovy
index ff66bb0..8beb8e2 100644
--- a/src/test/org/codehaus/groovy/transform/BuilderTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/BuilderTransformTest.groovy
@@ -21,8 +21,7 @@ package org.codehaus.groovy.transform
 import gls.CompilableTestSupport
 
 /**
- * @author Marcin Grzejszczak
- * @author Paul King
+ * Tests for {@code @Builder} transform.
  */
 class BuilderTransformTest extends CompilableTestSupport {
 
@@ -569,4 +568,26 @@ class BuilderTransformTest extends CompilableTestSupport {
         '''
     }
 
+    void testInitializerStrategyEmptyCases_GROOVY7503() {
+        def message = shouldNotCompile '''
+            import groovy.transform.builder.*
+            @Builder(builderStrategy=InitializerStrategy) class Foo { }
+        '''
+        assert message.contains('at least one property is required for this strategy')
+        message = shouldNotCompile '''
+            import groovy.transform.builder.*
+            @Builder(builderStrategy=InitializerStrategy, excludes='bar') class Foo { String
bar }
+        '''
+        assert message.contains('at least one property is required for this strategy')
+        message = shouldNotCompile '''
+            import groovy.transform.builder.*
+            class Foo {
+              @Builder(builderStrategy=InitializerStrategy)
+              Foo() {
+              }
+            }
+        '''
+        assert message.contains('at least one parameter is required for this strategy')
+    }
+
 }


Mime
View raw message