diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java index fe1a57256f..3038cd3f71 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java @@ -6327,4 +6327,25 @@ public void testCompileStatic10089() { runConformTest(sources, "[[id:x, name:null, count:1]]"); } + + @Test + public void testCompileStatic10197() { + for (String override : new String[]{"int getBaz() {1}", "final int baz = 1"}) { + //@formatter:off + String[] sources = { + "Main.groovy", + "@groovy.transform.CompileStatic\n" + + "enum Foo {\n" + + " BAR {\n" + + " " + override + "\n" + + " }\n" + + " int getBaz() { -1 }\n" + + "}\n" + + "print Foo.BAR.baz\n", + }; + //@formatter:on + + runConformTest(sources, "1"); + } + } } diff --git a/base/org.codehaus.groovy25/.checkstyle b/base/org.codehaus.groovy25/.checkstyle index 09531b9287..30b2484f44 100644 --- a/base/org.codehaus.groovy25/.checkstyle +++ b/base/org.codehaus.groovy25/.checkstyle @@ -79,6 +79,7 @@ + diff --git a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java new file mode 100644 index 0000000000..5eb53dedfc --- /dev/null +++ b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java @@ -0,0 +1,360 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.codehaus.groovy.transform; + +import groovy.lang.GroovyClassLoader; +import groovy.transform.CompilationUnitAware; +import groovy.transform.TupleConstructor; +import groovy.transform.options.PropertyHandler; +import org.apache.groovy.ast.tools.AnnotatedNodeUtils; +import org.codehaus.groovy.ast.ASTNode; +import org.codehaus.groovy.ast.AnnotatedNode; +import org.codehaus.groovy.ast.AnnotationNode; +import org.codehaus.groovy.ast.ClassHelper; +import org.codehaus.groovy.ast.ClassNode; +import org.codehaus.groovy.ast.ConstructorNode; +import org.codehaus.groovy.ast.FieldNode; +import org.codehaus.groovy.ast.Parameter; +import org.codehaus.groovy.ast.PropertyNode; +import org.codehaus.groovy.ast.expr.ClosureExpression; +import org.codehaus.groovy.ast.expr.ConstantExpression; +import org.codehaus.groovy.ast.expr.Expression; +import org.codehaus.groovy.ast.expr.MethodCallExpression; +import org.codehaus.groovy.ast.expr.VariableExpression; +import org.codehaus.groovy.ast.stmt.BlockStatement; +import org.codehaus.groovy.ast.stmt.EmptyStatement; +import org.codehaus.groovy.ast.stmt.ExpressionStatement; +import org.codehaus.groovy.ast.stmt.IfStatement; +import org.codehaus.groovy.ast.stmt.Statement; +import org.codehaus.groovy.classgen.VariableScopeVisitor; +import org.codehaus.groovy.control.CompilationUnit; +import org.codehaus.groovy.control.CompilePhase; +import org.codehaus.groovy.control.SourceUnit; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.markAsGenerated; +import static org.apache.groovy.ast.tools.ClassNodeUtils.hasExplicitConstructor; +import static org.apache.groovy.ast.tools.VisibilityUtils.getVisibility; +import static org.codehaus.groovy.ast.ClassHelper.make; +import static org.codehaus.groovy.ast.ClassHelper.makeWithoutCaching; +import static org.codehaus.groovy.ast.tools.GeneralUtils.args; +import static org.codehaus.groovy.ast.tools.GeneralUtils.assignS; +import static org.codehaus.groovy.ast.tools.GeneralUtils.block; +import static org.codehaus.groovy.ast.tools.GeneralUtils.callX; +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.getAllProperties; +import static org.codehaus.groovy.ast.tools.GeneralUtils.ifElseS; +import static org.codehaus.groovy.ast.tools.GeneralUtils.ifS; +import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX; +import static org.codehaus.groovy.ast.tools.GeneralUtils.params; +import static org.codehaus.groovy.ast.tools.GeneralUtils.propX; +import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt; +import static org.codehaus.groovy.ast.tools.GeneralUtils.throwS; +import static org.codehaus.groovy.ast.tools.GeneralUtils.varX; +import static org.codehaus.groovy.transform.ImmutableASTTransformation.makeImmutable; + +/** + * Handles generation of code for the @TupleConstructor annotation. + */ +@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION) +public class TupleConstructorASTTransformation extends AbstractASTTransformation implements CompilationUnitAware { + + private CompilationUnit compilationUnit; + static final Class MY_CLASS = TupleConstructor.class; + static final ClassNode MY_TYPE = make(MY_CLASS); + static final String MY_TYPE_NAME = "@" + MY_TYPE.getNameWithoutPackage(); + private static final ClassNode LHMAP_TYPE = makeWithoutCaching(LinkedHashMap.class, false); + private static final ClassNode CHECK_METHOD_TYPE = make(ImmutableASTTransformation.class); + private static final Map, Expression> primitivesInitialValues; + + static { + final ConstantExpression zero = constX(0); + final ConstantExpression zeroDecimal = constX(.0); + primitivesInitialValues = new HashMap, Expression>(); + primitivesInitialValues.put(int.class, zero); + primitivesInitialValues.put(long.class, zero); + primitivesInitialValues.put(short.class, zero); + primitivesInitialValues.put(byte.class, zero); + primitivesInitialValues.put(char.class, zero); + primitivesInitialValues.put(float.class, zeroDecimal); + primitivesInitialValues.put(double.class, zeroDecimal); + primitivesInitialValues.put(boolean.class, ConstantExpression.FALSE); + } + + @Override + public String getAnnotationName() { + return MY_TYPE_NAME; + } + + public void visit(ASTNode[] nodes, SourceUnit source) { + init(nodes, source); + AnnotatedNode parent = (AnnotatedNode) nodes[1]; + AnnotationNode anno = (AnnotationNode) nodes[0]; + if (!MY_TYPE.equals(anno.getClassNode())) return; + + if (parent instanceof ClassNode) { + ClassNode cNode = (ClassNode) parent; + if (!checkNotInterface(cNode, MY_TYPE_NAME)) return; + boolean includeFields = memberHasValue(anno, "includeFields", true); + boolean includeProperties = !memberHasValue(anno, "includeProperties", false); + boolean includeSuperFields = memberHasValue(anno, "includeSuperFields", true); + boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true); + boolean allProperties = memberHasValue(anno, "allProperties", true); + List excludes = getMemberStringList(anno, "excludes"); + List includes = getMemberStringList(anno, "includes"); + boolean allNames = memberHasValue(anno, "allNames", true); + if (!checkIncludeExcludeUndefinedAware(anno, excludes, includes, MY_TYPE_NAME)) return; + if (!checkPropertyList(cNode, includes, "includes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties, includeSuperFields, false)) + return; + if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties, includeSuperFields, false)) + return; + final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader(); + final PropertyHandler handler = PropertyHandler.createPropertyHandler(this, classLoader, cNode); + if (handler == null) return; + if (!handler.validateAttributes(this, anno)) return; + + Expression pre = anno.getMember("pre"); + if (pre != null && !(pre instanceof ClosureExpression)) { + addError("Expected closure value for annotation parameter 'pre'. Found " + pre, cNode); + return; + } + Expression post = anno.getMember("post"); + if (post != null && !(post instanceof ClosureExpression)) { + addError("Expected closure value for annotation parameter 'post'. Found " + post, cNode); + return; + } + + createConstructor(this, anno, cNode, includeFields, includeProperties, includeSuperFields, includeSuperProperties, + excludes, includes, allNames, allProperties, + sourceUnit, handler, (ClosureExpression) pre, (ClosureExpression) post); + + if (pre != null) { + anno.setMember("pre", new ClosureExpression(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE)); + } + if (post != null) { + anno.setMember("post", new ClosureExpression(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE)); + } + } + } + + private static void createConstructor(AbstractASTTransformation xform, AnnotationNode anno, ClassNode cNode, boolean includeFields, + boolean includeProperties, boolean includeSuperFields, boolean includeSuperProperties, + List excludes, final List includes, boolean allNames, boolean allProperties, + SourceUnit sourceUnit, PropertyHandler handler, ClosureExpression pre, ClosureExpression post) { + boolean callSuper = xform.memberHasValue(anno, "callSuper", true); + boolean force = xform.memberHasValue(anno, "force", true); + boolean defaults = !xform.memberHasValue(anno, "defaults", false); + Set names = new HashSet(); + List superList; + if (includeSuperProperties || includeSuperFields) { + superList = getAllProperties(names, cNode.getSuperClass(), includeSuperProperties, includeSuperFields, false, allProperties, true, true); + } else { + superList = new ArrayList(); + } + + List list = getAllProperties(names, cNode, includeProperties, includeFields, false, allProperties, false, true); + + boolean makeImmutable = makeImmutable(cNode); + boolean specialNamedArgCase = (ImmutableASTTransformation.isSpecialNamedArgCase(list, !defaults) && superList.isEmpty()) || + (ImmutableASTTransformation.isSpecialNamedArgCase(superList, !defaults) && list.isEmpty()); + + // no processing if existing constructors found unless forced or ImmutableBase in play + if (hasExplicitConstructor(null, cNode) && !force && !makeImmutable) return; + + final List params = new ArrayList(); + final List superParams = new ArrayList(); + final BlockStatement preBody = new BlockStatement(); + boolean superInPre = false; + if (pre != null) { + superInPre = copyStatementsWithSuperAdjustment(pre, preBody); + if (superInPre && callSuper) { + xform.addError("Error during " + MY_TYPE_NAME + " processing, can't have a super call in 'pre' " + + "closure and also 'callSuper' enabled", cNode); + } + } + + final BlockStatement body = new BlockStatement(); + + List tempList = new ArrayList(list); + tempList.addAll(superList); + if (!handler.validateProperties(xform, body, cNode, tempList)) { + return; + } + + for (PropertyNode pNode : superList) { + String name = pNode.getName(); + FieldNode fNode = pNode.getField(); + if (shouldSkipUndefinedAware(name, excludes, includes, allNames)) continue; + params.add(createParam(fNode, name, defaults, xform, makeImmutable)); + if (callSuper) { + superParams.add(varX(name)); + } else if (!superInPre && !specialNamedArgCase) { + Statement propInit = handler.createPropInit(xform, anno, cNode, pNode, null); + if (propInit != null) { + body.addStatement(propInit); + } + } + } + if (callSuper) { + body.addStatement(stmt(ctorX(ClassNode.SUPER, args(superParams)))); + } + if (!preBody.isEmpty()) { + body.addStatements(preBody.getStatements()); + } + + for (PropertyNode pNode : list) { + String name = pNode.getName(); + FieldNode fNode = pNode.getField(); + if (shouldSkipUndefinedAware(name, excludes, includes, allNames)) continue; + Parameter nextParam = createParam(fNode, name, defaults, xform, makeImmutable); + params.add(nextParam); + Statement propInit = handler.createPropInit(xform, anno, cNode, pNode, null); + if (propInit != null) { + body.addStatement(propInit); + } + } + + if (post != null) { + body.addStatement(post.getCode()); + } + + if (includes != null) { + Comparator includeComparator = new Comparator() { + public int compare(Parameter p1, Parameter p2) { + return Integer.compare(includes.indexOf(p1.getName()), includes.indexOf(p2.getName())); + } + }; + Collections.sort(params, includeComparator); + } + + boolean hasMapCons = AnnotatedNodeUtils.hasAnnotation(cNode, MapConstructorASTTransformation.MY_TYPE); + int modifiers = getVisibility(anno, cNode, ConstructorNode.class, ACC_PUBLIC); + ConstructorNode consNode = new ConstructorNode(modifiers, params.toArray(Parameter.EMPTY_ARRAY), ClassNode.EMPTY_ARRAY, body); + markAsGenerated(cNode, consNode); + cNode.addConstructor(consNode); + + if (sourceUnit != null && !body.isEmpty()) { + VariableScopeVisitor scopeVisitor = new VariableScopeVisitor(sourceUnit); + scopeVisitor.visitClass(cNode); + } + + // GROOVY-8868 don't want an empty body to cause the constructor to be deleted later + if (body.isEmpty()) { + body.addStatement(new ExpressionStatement(ConstantExpression.EMPTY_EXPRESSION)); + } + + // If the first param is def or a Map, named args might not work as expected so we add a hard-coded map constructor in this case + // we don't do it for LinkedHashMap for now (would lead to duplicate signature) + // or if there is only one Map property (for backwards compatibility) + // or if there is already a @MapConstructor annotation + if (!params.isEmpty() && defaults && !hasMapCons && specialNamedArgCase) { + ClassNode firstParamType = params.get(0).getType(); + if (params.size() > 1 || firstParamType.equals(ClassHelper.OBJECT_TYPE)) { + String message = "The class " + cNode.getName() + " was incorrectly initialized via the map constructor with null."; + addSpecialMapConstructors(modifiers, cNode, message, false); + } + } + } + + private static Parameter createParam(FieldNode fNode, String name, boolean defaults, AbstractASTTransformation xform, boolean makeImmutable) { + Parameter param = new Parameter(fNode.getType(), name); + if (defaults) { + param.setInitialExpression(providedOrDefaultInitialValue(fNode)); + } else if (!makeImmutable) { + // TODO we could support some default vals provided they were listed last + if (fNode.getInitialExpression() != null) { + xform.addError("Error during " + MY_TYPE_NAME + " processing, default value processing disabled but default value found for '" + fNode.getName() + "'", fNode); + } + } + return param; + } + + private static Expression providedOrDefaultInitialValue(FieldNode fNode) { + Expression initialExp = fNode.getInitialExpression() != null ? fNode.getInitialExpression() : nullX(); + final ClassNode paramType = fNode.getType(); + if (ClassHelper.isPrimitiveType(paramType) && isNull(initialExp)) { + initialExp = primitivesInitialValues.get(paramType.getTypeClass()); + } + return initialExp; + } + + private static boolean isNull(Expression exp) { + return exp instanceof ConstantExpression && ((ConstantExpression) exp).isNullExpression(); + } + + public static void addSpecialMapConstructors(int modifiers, ClassNode cNode, String message, boolean addNoArg) { + Parameter[] parameters = params(new Parameter(LHMAP_TYPE, "__namedArgs")); + BlockStatement code = new BlockStatement(); + VariableExpression namedArgs = varX("__namedArgs"); + namedArgs.setAccessedVariable(parameters[0]); + code.addStatement(ifElseS(equalsNullX(namedArgs), + illegalArgumentBlock(message), + processArgsBlock(cNode, namedArgs))); + ConstructorNode init = new ConstructorNode(modifiers, parameters, ClassNode.EMPTY_ARRAY, code); + markAsGenerated(cNode, init); + cNode.addConstructor(init); + // potentially add a no-arg constructor too + if (addNoArg) { + code = new BlockStatement(); + code.addStatement(stmt(ctorX(ClassNode.THIS, ctorX(LHMAP_TYPE)))); + init = new ConstructorNode(modifiers, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code); + markAsGenerated(cNode, init); + cNode.addConstructor(init); + } + } + + private static BlockStatement illegalArgumentBlock(String message) { + return block(throwS(ctorX(make(IllegalArgumentException.class), args(constX(message))))); + } + + private static BlockStatement processArgsBlock(ClassNode cNode, VariableExpression namedArgs) { + BlockStatement block = new BlockStatement(); + for (PropertyNode pNode : cNode.getProperties()) { + if (pNode.isStatic()) continue; + + // if namedArgs.containsKey(propertyName) setProperty(propertyName, namedArgs.get(propertyName)); + IfStatement ifStatement = ifS( + callX(namedArgs, "containsKey", constX(pNode.getName())), + assignS(varX(pNode), propX(namedArgs, pNode.getName()))); + // GRECLIPSE add -- GROOVY-10197 + ((MethodCallExpression) ifStatement.getBooleanExpression().getExpression()).setImplicitThis(false); + // GRECLIPSE end + block.addStatement(ifStatement); + } + block.addStatement(stmt(callX(CHECK_METHOD_TYPE, "checkPropNames", args(varX("this"), namedArgs)))); + return block; + } + + @Override + public void setCompilationUnit(CompilationUnit unit) { + this.compilationUnit = unit; + } +} diff --git a/base/org.codehaus.groovy30/.checkstyle b/base/org.codehaus.groovy30/.checkstyle index 57f8e8495e..1b9116f4f1 100644 --- a/base/org.codehaus.groovy30/.checkstyle +++ b/base/org.codehaus.groovy30/.checkstyle @@ -66,6 +66,7 @@ + diff --git a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java new file mode 100644 index 0000000000..e85ce7aca5 --- /dev/null +++ b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java @@ -0,0 +1,349 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.codehaus.groovy.transform; + +import groovy.lang.GroovyClassLoader; +import groovy.transform.CompilationUnitAware; +import groovy.transform.TupleConstructor; +import groovy.transform.options.PropertyHandler; +import org.apache.groovy.ast.tools.AnnotatedNodeUtils; +import org.codehaus.groovy.ast.ASTNode; +import org.codehaus.groovy.ast.AnnotatedNode; +import org.codehaus.groovy.ast.AnnotationNode; +import org.codehaus.groovy.ast.ClassHelper; +import org.codehaus.groovy.ast.ClassNode; +import org.codehaus.groovy.ast.ConstructorNode; +import org.codehaus.groovy.ast.FieldNode; +import org.codehaus.groovy.ast.Parameter; +import org.codehaus.groovy.ast.PropertyNode; +import org.codehaus.groovy.ast.expr.ClosureExpression; +import org.codehaus.groovy.ast.expr.ConstantExpression; +import org.codehaus.groovy.ast.expr.Expression; +import org.codehaus.groovy.ast.expr.MethodCallExpression; +import org.codehaus.groovy.ast.expr.VariableExpression; +import org.codehaus.groovy.ast.stmt.BlockStatement; +import org.codehaus.groovy.ast.stmt.EmptyStatement; +import org.codehaus.groovy.ast.stmt.ExpressionStatement; +import org.codehaus.groovy.ast.stmt.IfStatement; +import org.codehaus.groovy.ast.stmt.Statement; +import org.codehaus.groovy.classgen.VariableScopeVisitor; +import org.codehaus.groovy.control.CompilationUnit; +import org.codehaus.groovy.control.CompilePhase; +import org.codehaus.groovy.control.SourceUnit; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedConstructor; +import static org.apache.groovy.ast.tools.ClassNodeUtils.hasExplicitConstructor; +import static org.apache.groovy.ast.tools.VisibilityUtils.getVisibility; +import static org.codehaus.groovy.ast.ClassHelper.make; +import static org.codehaus.groovy.ast.ClassHelper.makeWithoutCaching; +import static org.codehaus.groovy.ast.tools.GeneralUtils.args; +import static org.codehaus.groovy.ast.tools.GeneralUtils.assignS; +import static org.codehaus.groovy.ast.tools.GeneralUtils.block; +import static org.codehaus.groovy.ast.tools.GeneralUtils.callX; +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.getAllProperties; +import static org.codehaus.groovy.ast.tools.GeneralUtils.ifElseS; +import static org.codehaus.groovy.ast.tools.GeneralUtils.ifS; +import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX; +import static org.codehaus.groovy.ast.tools.GeneralUtils.params; +import static org.codehaus.groovy.ast.tools.GeneralUtils.propX; +import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt; +import static org.codehaus.groovy.ast.tools.GeneralUtils.throwS; +import static org.codehaus.groovy.ast.tools.GeneralUtils.varX; +import static org.codehaus.groovy.transform.ImmutableASTTransformation.makeImmutable; + +/** + * Handles generation of code for the @TupleConstructor annotation. + */ +@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION) +public class TupleConstructorASTTransformation extends AbstractASTTransformation implements CompilationUnitAware { + + private CompilationUnit compilationUnit; + static final Class MY_CLASS = TupleConstructor.class; + static final ClassNode MY_TYPE = make(MY_CLASS); + static final String MY_TYPE_NAME = "@" + MY_TYPE.getNameWithoutPackage(); + private static final ClassNode LHMAP_TYPE = makeWithoutCaching(LinkedHashMap.class, false); + private static final ClassNode CHECK_METHOD_TYPE = make(ImmutableASTTransformation.class); + private static final Map, Expression> primitivesInitialValues; + + static { + final ConstantExpression zero = constX(0); + final ConstantExpression zeroDecimal = constX(.0); + primitivesInitialValues = new HashMap, Expression>(); + primitivesInitialValues.put(int.class, zero); + primitivesInitialValues.put(long.class, zero); + primitivesInitialValues.put(short.class, zero); + primitivesInitialValues.put(byte.class, zero); + primitivesInitialValues.put(char.class, zero); + primitivesInitialValues.put(float.class, zeroDecimal); + primitivesInitialValues.put(double.class, zeroDecimal); + primitivesInitialValues.put(boolean.class, ConstantExpression.FALSE); + } + + @Override + public String getAnnotationName() { + return MY_TYPE_NAME; + } + + public void visit(ASTNode[] nodes, SourceUnit source) { + init(nodes, source); + AnnotatedNode parent = (AnnotatedNode) nodes[1]; + AnnotationNode anno = (AnnotationNode) nodes[0]; + if (!MY_TYPE.equals(anno.getClassNode())) return; + + if (parent instanceof ClassNode) { + ClassNode cNode = (ClassNode) parent; + if (!checkNotInterface(cNode, MY_TYPE_NAME)) return; + boolean includeFields = memberHasValue(anno, "includeFields", true); + boolean includeProperties = !memberHasValue(anno, "includeProperties", false); + boolean includeSuperFields = memberHasValue(anno, "includeSuperFields", true); + boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true); + boolean allProperties = memberHasValue(anno, "allProperties", true); + List excludes = getMemberStringList(anno, "excludes"); + List includes = getMemberStringList(anno, "includes"); + boolean allNames = memberHasValue(anno, "allNames", true); + if (!checkIncludeExcludeUndefinedAware(anno, excludes, includes, MY_TYPE_NAME)) return; + if (!checkPropertyList(cNode, includes, "includes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties, includeSuperFields, false)) + return; + if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties, includeSuperFields, false)) + return; + final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader(); + final PropertyHandler handler = PropertyHandler.createPropertyHandler(this, classLoader, cNode); + if (handler == null) return; + if (!handler.validateAttributes(this, anno)) return; + + Expression pre = anno.getMember("pre"); + if (pre != null && !(pre instanceof ClosureExpression)) { + addError("Expected closure value for annotation parameter 'pre'. Found " + pre, cNode); + return; + } + Expression post = anno.getMember("post"); + if (post != null && !(post instanceof ClosureExpression)) { + addError("Expected closure value for annotation parameter 'post'. Found " + post, cNode); + return; + } + + createConstructor(this, anno, cNode, includeFields, includeProperties, includeSuperFields, includeSuperProperties, + excludes, includes, allNames, allProperties, + sourceUnit, handler, (ClosureExpression) pre, (ClosureExpression) post); + + if (pre != null) { + anno.setMember("pre", new ClosureExpression(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE)); + } + if (post != null) { + anno.setMember("post", new ClosureExpression(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE)); + } + } + } + + private static void createConstructor(AbstractASTTransformation xform, AnnotationNode anno, ClassNode cNode, boolean includeFields, + boolean includeProperties, boolean includeSuperFields, boolean includeSuperProperties, + List excludes, final List includes, boolean allNames, boolean allProperties, + SourceUnit sourceUnit, PropertyHandler handler, ClosureExpression pre, ClosureExpression post) { + boolean callSuper = xform.memberHasValue(anno, "callSuper", true); + boolean force = xform.memberHasValue(anno, "force", true); + boolean defaults = !xform.memberHasValue(anno, "defaults", false); + Set names = new HashSet(); + List superList; + if (includeSuperProperties || includeSuperFields) { + superList = getAllProperties(names, cNode.getSuperClass(), includeSuperProperties, includeSuperFields, false, allProperties, true, true); + } else { + superList = new ArrayList(); + } + + List list = getAllProperties(names, cNode, includeProperties, includeFields, false, allProperties, false, true); + + boolean makeImmutable = makeImmutable(cNode); + boolean specialNamedArgCase = (ImmutableASTTransformation.isSpecialNamedArgCase(list, !defaults) && superList.isEmpty()) || + (ImmutableASTTransformation.isSpecialNamedArgCase(superList, !defaults) && list.isEmpty()); + + // no processing if existing constructors found unless forced or ImmutableBase in play + if (hasExplicitConstructor(null, cNode) && !force && !makeImmutable) return; + + final List params = new ArrayList(); + final List superParams = new ArrayList(); + final BlockStatement preBody = new BlockStatement(); + boolean superInPre = false; + if (pre != null) { + superInPre = copyStatementsWithSuperAdjustment(pre, preBody); + if (superInPre && callSuper) { + xform.addError("Error during " + MY_TYPE_NAME + " processing, can't have a super call in 'pre' " + + "closure and also 'callSuper' enabled", cNode); + } + } + + final BlockStatement body = new BlockStatement(); + + List tempList = new ArrayList(list); + tempList.addAll(superList); + if (!handler.validateProperties(xform, body, cNode, tempList)) { + return; + } + + for (PropertyNode pNode : superList) { + String name = pNode.getName(); + FieldNode fNode = pNode.getField(); + if (shouldSkipUndefinedAware(name, excludes, includes, allNames)) continue; + params.add(createParam(fNode, name, defaults, xform, makeImmutable)); + if (callSuper) { + superParams.add(varX(name)); + } else if (!superInPre && !specialNamedArgCase) { + Statement propInit = handler.createPropInit(xform, anno, cNode, pNode, null); + if (propInit != null) { + body.addStatement(propInit); + } + } + } + if (callSuper) { + body.addStatement(stmt(ctorX(ClassNode.SUPER, args(superParams)))); + } + if (!preBody.isEmpty()) { + body.addStatements(preBody.getStatements()); + } + + for (PropertyNode pNode : list) { + String name = pNode.getName(); + FieldNode fNode = pNode.getField(); + if (shouldSkipUndefinedAware(name, excludes, includes, allNames)) continue; + Parameter nextParam = createParam(fNode, name, defaults, xform, makeImmutable); + params.add(nextParam); + Statement propInit = handler.createPropInit(xform, anno, cNode, pNode, null); + if (propInit != null) { + body.addStatement(propInit); + } + } + + if (post != null) { + body.addStatement(post.getCode()); + } + + if (includes != null) { + Comparator includeComparator = Comparator.comparingInt(p -> includes.indexOf(p.getName())); + params.sort(includeComparator); + } + + boolean hasMapCons = AnnotatedNodeUtils.hasAnnotation(cNode, MapConstructorASTTransformation.MY_TYPE); + int modifiers = getVisibility(anno, cNode, ConstructorNode.class, ACC_PUBLIC); + addGeneratedConstructor(cNode, modifiers, params.toArray(Parameter.EMPTY_ARRAY), ClassNode.EMPTY_ARRAY, body); + + if (sourceUnit != null && !body.isEmpty()) { + VariableScopeVisitor scopeVisitor = new VariableScopeVisitor(sourceUnit); + scopeVisitor.visitClass(cNode); + } + + // GROOVY-8868 don't want an empty body to cause the constructor to be deleted later + if (body.isEmpty()) { + body.addStatement(new ExpressionStatement(ConstantExpression.EMPTY_EXPRESSION)); + } + + // If the first param is def or a Map, named args might not work as expected so we add a hard-coded map constructor in this case + // we don't do it for LinkedHashMap for now (would lead to duplicate signature) + // or if there is only one Map property (for backwards compatibility) + // or if there is already a @MapConstructor annotation + if (!params.isEmpty() && defaults && !hasMapCons && specialNamedArgCase) { + ClassNode firstParamType = params.get(0).getType(); + if (params.size() > 1 || firstParamType.equals(ClassHelper.OBJECT_TYPE)) { + String message = "The class " + cNode.getName() + " was incorrectly initialized via the map constructor with null."; + addSpecialMapConstructors(modifiers, cNode, message, false); + } + } + } + + private static Parameter createParam(FieldNode fNode, String name, boolean defaults, AbstractASTTransformation xform, boolean makeImmutable) { + Parameter param = new Parameter(fNode.getType(), name); + if (defaults) { + param.setInitialExpression(providedOrDefaultInitialValue(fNode)); + } else if (!makeImmutable) { + // TODO we could support some default vals provided they were listed last + if (fNode.getInitialExpression() != null) { + xform.addError("Error during " + MY_TYPE_NAME + " processing, default value processing disabled but default value found for '" + fNode.getName() + "'", fNode); + } + } + return param; + } + + private static Expression providedOrDefaultInitialValue(FieldNode fNode) { + Expression initialExp = fNode.getInitialExpression() != null ? fNode.getInitialExpression() : nullX(); + final ClassNode paramType = fNode.getType(); + if (ClassHelper.isPrimitiveType(paramType) && isNull(initialExp)) { + initialExp = primitivesInitialValues.get(paramType.getTypeClass()); + } + return initialExp; + } + + private static boolean isNull(Expression exp) { + return exp instanceof ConstantExpression && ((ConstantExpression) exp).isNullExpression(); + } + + public static void addSpecialMapConstructors(int modifiers, ClassNode cNode, String message, boolean addNoArg) { + Parameter[] parameters = params(new Parameter(LHMAP_TYPE, "__namedArgs")); + BlockStatement code = new BlockStatement(); + VariableExpression namedArgs = varX("__namedArgs"); + namedArgs.setAccessedVariable(parameters[0]); + code.addStatement(ifElseS(equalsNullX(namedArgs), + illegalArgumentBlock(message), + processArgsBlock(cNode, namedArgs))); + addGeneratedConstructor(cNode, modifiers, parameters, ClassNode.EMPTY_ARRAY, code); + // potentially add a no-arg constructor too + if (addNoArg) { + code = new BlockStatement(); + code.addStatement(stmt(ctorX(ClassNode.THIS, ctorX(LHMAP_TYPE)))); + addGeneratedConstructor(cNode, modifiers, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code); + } + } + + private static BlockStatement illegalArgumentBlock(String message) { + return block(throwS(ctorX(make(IllegalArgumentException.class), args(constX(message))))); + } + + private static BlockStatement processArgsBlock(ClassNode cNode, VariableExpression namedArgs) { + BlockStatement block = new BlockStatement(); + for (PropertyNode pNode : cNode.getProperties()) { + if (pNode.isStatic()) continue; + + // if (namedArgs.containsKey(propertyName)) propertyNode= namedArgs.get(propertyName); + IfStatement ifStatement = ifS( + callX(namedArgs, "containsKey", constX(pNode.getName())), + assignS(varX(pNode), propX(namedArgs, pNode.getName()))); + // GRECLIPSE add -- GROOVY-10197 + ((MethodCallExpression) ifStatement.getBooleanExpression().getExpression()).setImplicitThis(false); + // GRECLIPSE end + block.addStatement(ifStatement); + } + block.addStatement(stmt(callX(CHECK_METHOD_TYPE, "checkPropNames", args(varX("this"), namedArgs)))); + return block; + } + + @Override + public void setCompilationUnit(CompilationUnit unit) { + this.compilationUnit = unit; + } +} diff --git a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java index abd3e58b77..1caf33f3c9 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java @@ -631,6 +631,12 @@ private void visitEnumConstBody(final ClassNode node) { visitMethodInternal(method); } } + for (FieldNode field : node.getFields()) { + if (field.getEnd() > 0) { + enclosingElement = ((IType) enumConstant.getChildren()[0]).getField(field.getName()); + visitFieldInternal(field); + } + } } catch (JavaModelException e) { log(e, "Error visiting children of %s", enclosingDeclarationNode); } finally { diff --git a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy index 491ce601a9..e9ab8b3401 100644 --- a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy +++ b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy @@ -1773,7 +1773,7 @@ final class SemanticHighlightingTests extends GroovyEclipseTestSuite { } @Test - void testEnumInner() { + void testEnumInner1() { String contents = '''\ |import groovy.transform.* |@CompileStatic @@ -1806,6 +1806,26 @@ final class SemanticHighlightingTests extends GroovyEclipseTestSuite { new HighlightedTypedPosition(contents.lastIndexOf('meth'), 4, METHOD)) } + @Test + void testEnumInner2() { + String contents = '''\ + |enum X { + | WHY { + | final int value = 1 + | } + | def getValue() { -1 } + |} + |'''.stripMargin() + + assertHighlighting(contents, + new HighlightedTypedPosition(contents.indexOf('X'), 1, ENUMERATION), + new HighlightedTypedPosition(contents.indexOf('WHY'), 3, STATIC_VALUE), + new HighlightedTypedPosition(contents.indexOf('value'), 5, FIELD), + new HighlightedTypedPosition(contents.indexOf('1'), 1, NUMBER), + new HighlightedTypedPosition(contents.indexOf('getValue'), 8, METHOD), + new HighlightedTypedPosition(contents.indexOf('-1'), 2, NUMBER)) + } + @Test // https://github.com/groovy/groovy-eclipse/issues/1004 void testEnumMethod() { String contents = '''\