|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.groovy.contracts.ast; |
| 20 | + |
| 21 | +import org.codehaus.groovy.ast.ASTNode; |
| 22 | +import org.codehaus.groovy.ast.AnnotationNode; |
| 23 | +import org.codehaus.groovy.ast.ClassNode; |
| 24 | +import org.codehaus.groovy.ast.MethodNode; |
| 25 | +import org.codehaus.groovy.ast.Parameter; |
| 26 | +import org.codehaus.groovy.ast.expr.ClosureExpression; |
| 27 | +import org.codehaus.groovy.ast.expr.Expression; |
| 28 | +import org.codehaus.groovy.ast.expr.ListExpression; |
| 29 | +import org.codehaus.groovy.ast.expr.PropertyExpression; |
| 30 | +import org.codehaus.groovy.ast.expr.VariableExpression; |
| 31 | +import org.codehaus.groovy.ast.stmt.BlockStatement; |
| 32 | +import org.codehaus.groovy.ast.stmt.ExpressionStatement; |
| 33 | +import org.codehaus.groovy.ast.stmt.Statement; |
| 34 | +import org.codehaus.groovy.control.CompilePhase; |
| 35 | +import org.codehaus.groovy.control.SourceUnit; |
| 36 | +import org.codehaus.groovy.syntax.SyntaxException; |
| 37 | +import org.codehaus.groovy.transform.ASTTransformation; |
| 38 | +import org.codehaus.groovy.transform.GroovyASTTransformation; |
| 39 | + |
| 40 | +import java.util.LinkedHashSet; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Set; |
| 43 | + |
| 44 | +/** |
| 45 | + * Handles {@link groovy.contracts.Modifies} annotations placed on methods. |
| 46 | + * Extracts the declared modification targets (fields and parameters) from |
| 47 | + * the annotation closure and stores them as node metadata on the {@link MethodNode}. |
| 48 | + * <p> |
| 49 | + * The metadata key is {@value #MODIFIES_FIELDS_KEY} and the value is a |
| 50 | + * {@code Set<String>} of field/parameter names. Downstream processors |
| 51 | + * (such as {@code @Ensures} validation) can read this metadata. |
| 52 | + * |
| 53 | + * @since 6.0.0 |
| 54 | + * @see groovy.contracts.Modifies |
| 55 | + */ |
| 56 | +@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS) |
| 57 | +public class ModifiesASTTransformation implements ASTTransformation { |
| 58 | + |
| 59 | + /** Node metadata key for the set of modifiable field/parameter names. */ |
| 60 | + public static final String MODIFIES_FIELDS_KEY = "groovy.contracts.modifiesFields"; |
| 61 | + |
| 62 | + @Override |
| 63 | + @SuppressWarnings("unchecked") |
| 64 | + public void visit(final ASTNode[] nodes, final SourceUnit source) { |
| 65 | + if (nodes.length != 2) return; |
| 66 | + if (!(nodes[0] instanceof AnnotationNode annotation)) return; |
| 67 | + if (!(nodes[1] instanceof MethodNode methodNode)) return; |
| 68 | + |
| 69 | + // For @Repeatable, each annotation triggers this transform separately, |
| 70 | + // so merge with any existing metadata from prior invocations. |
| 71 | + Set<String> modifiesSet = (Set<String>) methodNode.getNodeMetaData(MODIFIES_FIELDS_KEY); |
| 72 | + if (modifiesSet == null) { |
| 73 | + modifiesSet = new LinkedHashSet<>(); |
| 74 | + } |
| 75 | + |
| 76 | + extractFromAnnotation(annotation, methodNode, modifiesSet, source); |
| 77 | + |
| 78 | + if (!modifiesSet.isEmpty()) { |
| 79 | + methodNode.putNodeMetaData(MODIFIES_FIELDS_KEY, modifiesSet); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static void extractFromAnnotation(AnnotationNode annotation, MethodNode methodNode, Set<String> modifiesSet, SourceUnit source) { |
| 84 | + Expression value = annotation.getMember("value"); |
| 85 | + if (!(value instanceof ClosureExpression closureExpr)) return; |
| 86 | + |
| 87 | + Expression expr = extractExpression(closureExpr); |
| 88 | + if (expr == null) { |
| 89 | + source.addError(new SyntaxException( |
| 90 | + "@Modifies closure must contain a field reference, parameter reference, or list of references", |
| 91 | + annotation.getLineNumber(), annotation.getColumnNumber())); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (expr instanceof ListExpression listExpr) { |
| 96 | + for (Expression element : listExpr.getExpressions()) { |
| 97 | + addValidatedName(element, methodNode, modifiesSet, source); |
| 98 | + } |
| 99 | + } else { |
| 100 | + addValidatedName(expr, methodNode, modifiesSet, source); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private static void addValidatedName(Expression expr, MethodNode methodNode, Set<String> modifiesSet, SourceUnit source) { |
| 105 | + if (expr instanceof PropertyExpression propExpr) { |
| 106 | + Expression objExpr = propExpr.getObjectExpression(); |
| 107 | + if (objExpr instanceof VariableExpression varExpr && "this".equals(varExpr.getName())) { |
| 108 | + String fieldName = propExpr.getPropertyAsString(); |
| 109 | + ClassNode declaringClass = methodNode.getDeclaringClass(); |
| 110 | + if (declaringClass != null && declaringClass.getField(fieldName) == null) { |
| 111 | + source.addError(new SyntaxException( |
| 112 | + "@Modifies references field '" + fieldName + "' which does not exist in " + declaringClass.getName(), |
| 113 | + expr.getLineNumber(), expr.getColumnNumber())); |
| 114 | + } else { |
| 115 | + modifiesSet.add(fieldName); |
| 116 | + } |
| 117 | + return; |
| 118 | + } |
| 119 | + } |
| 120 | + if (expr instanceof VariableExpression varExpr) { |
| 121 | + String name = varExpr.getName(); |
| 122 | + if (!"this".equals(name)) { |
| 123 | + boolean isParam = false; |
| 124 | + for (Parameter param : methodNode.getParameters()) { |
| 125 | + if (param.getName().equals(name)) { |
| 126 | + isParam = true; |
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | + if (!isParam) { |
| 131 | + source.addError(new SyntaxException( |
| 132 | + "@Modifies references '" + name + "' which is not a parameter of " + methodNode.getName() + "()", |
| 133 | + expr.getLineNumber(), expr.getColumnNumber())); |
| 134 | + } else { |
| 135 | + modifiesSet.add(name); |
| 136 | + } |
| 137 | + return; |
| 138 | + } |
| 139 | + } |
| 140 | + source.addError(new SyntaxException( |
| 141 | + "@Modifies elements must be field references (this.field) or parameter references", |
| 142 | + expr.getLineNumber(), expr.getColumnNumber())); |
| 143 | + } |
| 144 | + |
| 145 | + private static Expression extractExpression(ClosureExpression closureExpression) { |
| 146 | + BlockStatement block = (BlockStatement) closureExpression.getCode(); |
| 147 | + List<Statement> statements = block.getStatements(); |
| 148 | + if (statements.size() != 1) return null; |
| 149 | + Statement stmt = statements.get(0); |
| 150 | + if (stmt instanceof ExpressionStatement exprStmt) { |
| 151 | + return exprStmt.getExpression(); |
| 152 | + } |
| 153 | + return null; |
| 154 | + } |
| 155 | +} |
0 commit comments