Repository: groovy Updated Branches: refs/heads/GROOVY_2_5_X 07bcbcf43 -> e9d8dffca
minor refactor: prepare to remove some duplicated code Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/19b011e4 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/19b011e4 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/19b011e4 Branch: refs/heads/GROOVY_2_5_X Commit: 19b011e460f9255b0627d75d1a23f427ac19f5e5 Parents: 07bcbcf Author: paulk <pa...@asert.com.au> Authored: Thu Jan 25 21:13:01 2018 +1000 Committer: paulk <pa...@asert.com.au> Committed: Wed Jan 31 22:41:27 2018 +1000 ---------------------------------------------------------------------- .../codehaus/groovy/ast/tools/GeneralUtils.java | 8 ++-- .../TupleConstructorASTTransformation.java | 48 ++++++++++---------- 2 files changed, 28 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/19b011e4/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java b/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java index 84196e3..986a439 100644 --- a/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java +++ b/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java @@ -463,16 +463,16 @@ public class GeneralUtils { return result; } - public static List<PropertyNode> getAllFields(Set<String> names, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean allProperties, boolean traverseSuperClasses) { - return getAllFields(names, cNode, cNode, includeProperties, includeFields, allProperties, traverseSuperClasses); + public static List<PropertyNode> getAllProperties(Set<String> names, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean allProperties, boolean traverseSuperClasses) { + return getAllProperties(names, cNode, cNode, includeProperties, includeFields, allProperties, traverseSuperClasses); } - private static List<PropertyNode> getAllFields(Set<String> names, ClassNode origType, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean allProperties, boolean traverseSuperClasses) { + private static List<PropertyNode> getAllProperties(Set<String> names, ClassNode origType, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean allProperties, boolean traverseSuperClasses) { final List<PropertyNode> result; if (cNode == ClassHelper.OBJECT_TYPE || !traverseSuperClasses) { result = new ArrayList<PropertyNode>(); } else { - result = getAllFields(names, origType, cNode.getSuperClass(), includeProperties, includeFields, allProperties, true); + result = getAllProperties(names, origType, cNode.getSuperClass(), includeProperties, includeFields, allProperties, true); } if (includeProperties) { for (PropertyNode pNode : cNode.getProperties()) { http://git-wip-us.apache.org/repos/asf/groovy/blob/19b011e4/src/main/java/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java index c253d93..2815cb1 100644 --- a/src/main/java/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java +++ b/src/main/java/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java @@ -61,7 +61,7 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.constX; import static org.codehaus.groovy.ast.tools.GeneralUtils.copyStatementsWithSuperAdjustment; import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX; import static org.codehaus.groovy.ast.tools.GeneralUtils.equalsNullX; -import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllFields; +import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties; import static org.codehaus.groovy.ast.tools.GeneralUtils.getSetterName; import static org.codehaus.groovy.ast.tools.GeneralUtils.ifElseS; import static org.codehaus.groovy.ast.tools.GeneralUtils.ifS; @@ -136,18 +136,9 @@ public class TupleConstructorASTTransformation extends AbstractASTTransformation return; } - // TODO remove duplication between various paths below - List<PropertyNode> list = ImmutableASTTransformation.getProperties(cNode, includeSuperProperties, allProperties); - boolean specialHashMapCase = ImmutableASTTransformation.isSpecialHashMapCase(list); - if (makeImmutable) { - if (!specialHashMapCase) { - ImmutableASTTransformation.createConstructorOrdered(cNode, list); - } - } else { - createConstructor(this, cNode, includeFields, includeProperties, includeSuperFields, includeSuperProperties, - callSuper, force, excludes, includes, useSetters, defaults, allNames, allProperties, sourceUnit, - (ClosureExpression) pre, (ClosureExpression) post); - } + createConstructor(this, cNode, includeFields, includeProperties, includeSuperFields, includeSuperProperties, + callSuper, force, excludes, includes, useSetters, defaults, allNames, allProperties, makeImmutable, + sourceUnit, (ClosureExpression) pre, (ClosureExpression) post); if (pre != null) { anno.setMember("pre", new ClosureExpression(new Parameter[0], new EmptyStatement())); @@ -177,27 +168,36 @@ public class TupleConstructorASTTransformation extends AbstractASTTransformation List<String> excludes, final List<String> includes, boolean useSetters, boolean defaults, boolean allNames, SourceUnit sourceUnit, ClosureExpression pre, ClosureExpression post) { - createConstructor(xform, cNode, includeFields, includeProperties, includeSuperFields, includeSuperProperties, callSuper, force, excludes, includes, useSetters, defaults, allNames, false, sourceUnit, pre, post); + createConstructor(xform, cNode, includeFields, includeProperties, includeSuperFields, includeSuperProperties, callSuper, force, excludes, includes, useSetters, defaults, allNames, false, false, sourceUnit, pre, post); } public static void createConstructor(AbstractASTTransformation xform, ClassNode cNode, boolean includeFields, - boolean includeProperties, boolean includeSuperFields, boolean - includeSuperProperties, boolean callSuper, boolean force, - List<String> excludes, final List<String> includes, boolean useSetters, boolean - defaults, boolean allNames, boolean allProperties, SourceUnit sourceUnit, ClosureExpression - pre, ClosureExpression post) { - // no processing if existing constructors found - if (!cNode.getDeclaredConstructors().isEmpty() && !force) return; - + boolean includeProperties, boolean includeSuperFields, boolean includeSuperProperties, + boolean callSuper, boolean force, List<String> excludes, final List<String> includes, + boolean useSetters, boolean defaults, boolean allNames, boolean allProperties, boolean makeImmutable, + SourceUnit sourceUnit, ClosureExpression pre, ClosureExpression post) { Set<String> names = new HashSet<String>(); List<PropertyNode> superList; if (includeSuperProperties || includeSuperFields) { - superList = getAllFields(names, cNode.getSuperClass(), includeSuperProperties, includeSuperFields, allProperties, true); + superList = getAllProperties(names, cNode.getSuperClass(), includeSuperProperties, includeSuperFields, allProperties, true); } else { superList = new ArrayList<PropertyNode>(); } - List<PropertyNode> list = getAllFields(names, cNode, true, includeFields, allProperties, false); + List<PropertyNode> list = getAllProperties(names, cNode, true, includeFields, allProperties, false); + + if (makeImmutable) { + boolean specialHashMapCase = (ImmutableASTTransformation.isSpecialHashMapCase(list) && superList.isEmpty()) || + (ImmutableASTTransformation.isSpecialHashMapCase(superList) && list.isEmpty()); + if (!specialHashMapCase) { + superList.addAll(list); + ImmutableASTTransformation.createConstructorOrdered(cNode, superList); + } + return; + } + + // no processing if existing constructors found + if (!cNode.getDeclaredConstructors().isEmpty() && !force) return; final List<Parameter> params = new ArrayList<Parameter>(); final List<Expression> superParams = new ArrayList<Expression>();