|
| 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.codehaus.groovy.transform; |
| 20 | + |
| 21 | +import groovy.transform.NullCheck; |
| 22 | +import org.apache.groovy.ast.tools.ConstructorNodeUtils; |
| 23 | +import org.codehaus.groovy.ast.ASTNode; |
| 24 | +import org.codehaus.groovy.ast.AnnotatedNode; |
| 25 | +import org.codehaus.groovy.ast.AnnotationNode; |
| 26 | +import org.codehaus.groovy.ast.ClassHelper; |
| 27 | +import org.codehaus.groovy.ast.ClassNode; |
| 28 | +import org.codehaus.groovy.ast.ConstructorNode; |
| 29 | +import org.codehaus.groovy.ast.MethodNode; |
| 30 | +import org.codehaus.groovy.ast.Parameter; |
| 31 | +import org.codehaus.groovy.ast.expr.ConstantExpression; |
| 32 | +import org.codehaus.groovy.ast.expr.ConstructorCallExpression; |
| 33 | +import org.codehaus.groovy.ast.expr.Expression; |
| 34 | +import org.codehaus.groovy.ast.stmt.BlockStatement; |
| 35 | +import org.codehaus.groovy.ast.stmt.ThrowStatement; |
| 36 | +import org.codehaus.groovy.classgen.BytecodeSequence; |
| 37 | +import org.codehaus.groovy.control.CompilePhase; |
| 38 | +import org.codehaus.groovy.control.SourceUnit; |
| 39 | + |
| 40 | +import java.util.List; |
| 41 | + |
| 42 | +import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.isGenerated; |
| 43 | +import static org.apache.groovy.ast.tools.MethodNodeUtils.getCodeAsBlock; |
| 44 | +import static org.codehaus.groovy.ast.tools.GeneralUtils.constX; |
| 45 | +import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX; |
| 46 | +import static org.codehaus.groovy.ast.tools.GeneralUtils.ifS; |
| 47 | +import static org.codehaus.groovy.ast.tools.GeneralUtils.isNullX; |
| 48 | +import static org.codehaus.groovy.ast.tools.GeneralUtils.param; |
| 49 | +import static org.codehaus.groovy.ast.tools.GeneralUtils.params; |
| 50 | +import static org.codehaus.groovy.ast.tools.GeneralUtils.throwS; |
| 51 | +import static org.codehaus.groovy.ast.tools.GeneralUtils.varX; |
| 52 | +import static org.codehaus.groovy.transform.stc.StaticTypesMarker.DIRECT_METHOD_CALL_TARGET; |
| 53 | + |
| 54 | +/** |
| 55 | + * Handles generation of code for the @NullCheck annotation. |
| 56 | + */ |
| 57 | +@GroovyASTTransformation(phase = CompilePhase.INSTRUCTION_SELECTION) |
| 58 | +public class NullCheckASTTransformation extends AbstractASTTransformation { |
| 59 | + public static final ClassNode NULL_CHECK_TYPE = ClassHelper.make(NullCheck.class); |
| 60 | + private static final String NULL_CHECK_NAME = "@" + NULL_CHECK_TYPE.getNameWithoutPackage(); |
| 61 | + private static final ClassNode EXCEPTION = ClassHelper.make(IllegalArgumentException.class); |
| 62 | + private static final String NULL_CHECK_IS_PROCESSED = "NullCheck.isProcessed"; |
| 63 | + |
| 64 | + @Override |
| 65 | + public void visit(ASTNode[] nodes, SourceUnit source) { |
| 66 | + init(nodes, source); |
| 67 | + AnnotatedNode parent = (AnnotatedNode) nodes[1]; |
| 68 | + AnnotationNode anno = (AnnotationNode) nodes[0]; |
| 69 | + if (!NULL_CHECK_TYPE.equals(anno.getClassNode())) return; |
| 70 | + boolean includeGenerated = isIncludeGenerated(anno); |
| 71 | + |
| 72 | + if (parent instanceof ClassNode) { |
| 73 | + ClassNode cNode = (ClassNode) parent; |
| 74 | + if (!checkNotInterface(cNode, NULL_CHECK_NAME)) return; |
| 75 | + for (ConstructorNode cn : cNode.getDeclaredConstructors()) { |
| 76 | + adjustMethod(cn, includeGenerated); |
| 77 | + } |
| 78 | + for (MethodNode mn : cNode.getAllDeclaredMethods()) { |
| 79 | + adjustMethod(mn, includeGenerated); |
| 80 | + } |
| 81 | + } else if (parent instanceof MethodNode) { |
| 82 | + // handles constructor case too |
| 83 | + adjustMethod((MethodNode) parent, false); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private boolean isIncludeGenerated(AnnotationNode anno) { |
| 88 | + return memberHasValue(anno, "includeGenerated", true); |
| 89 | + } |
| 90 | + |
| 91 | + public static boolean hasIncludeGenerated(ClassNode cNode) { |
| 92 | + List<AnnotationNode> annotations = cNode.getAnnotations(NULL_CHECK_TYPE); |
| 93 | + if (annotations.isEmpty()) return false; |
| 94 | + return hasIncludeGenerated(annotations.get(0)); |
| 95 | + } |
| 96 | + |
| 97 | + private static boolean hasIncludeGenerated(AnnotationNode node) { |
| 98 | + final Expression member = node.getMember("includeGenerated"); |
| 99 | + return member instanceof ConstantExpression && ((ConstantExpression) member).getValue().equals(true); |
| 100 | + } |
| 101 | + |
| 102 | + private void adjustMethod(MethodNode mn, boolean includeGenerated) { |
| 103 | + BlockStatement newCode = getCodeAsBlock(mn); |
| 104 | + if (mn.getParameters().length == 0) return; |
| 105 | + boolean generated = isGenerated(mn); |
| 106 | + int startingIndex = 0; |
| 107 | + if (!includeGenerated && generated) return; |
| 108 | + if (isMarkedAsProcessed(mn)) return; |
| 109 | + if (mn instanceof ConstructorNode) { |
| 110 | + // some transform has been here already and we assume it knows what it is doing |
| 111 | + if (mn.getFirstStatement() instanceof BytecodeSequence) return; |
| 112 | + // ignore any constructors calling this(...) or super(...) |
| 113 | + ConstructorCallExpression cce = ConstructorNodeUtils.getFirstIfSpecialConstructorCall(mn.getCode()); |
| 114 | + if (cce != null) { |
| 115 | + if (generated) { |
| 116 | + return; |
| 117 | + } else { |
| 118 | + startingIndex = 1; // skip over this/super() call |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + for (Parameter p : mn.getParameters()) { |
| 123 | + if (ClassHelper.isPrimitiveType(p.getType())) continue; |
| 124 | + newCode.getStatements().add(startingIndex, ifS(isNullX(varX(p)), makeThrowStmt(p.getName()))); |
| 125 | + } |
| 126 | + mn.setCode(newCode); |
| 127 | + } |
| 128 | + |
| 129 | + public static ThrowStatement makeThrowStmt(String name) { |
| 130 | + /* GRECLIPSE edit -- GROOVY-10178 |
| 131 | + return throwS(ctorX(EXCEPTION, constX(name + " cannot be null"))); |
| 132 | + */ |
| 133 | + ConstructorCallExpression newException = ctorX(EXCEPTION, constX(name + " cannot be null")); |
| 134 | + newException.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, |
| 135 | + EXCEPTION.getDeclaredConstructor(params(param(ClassHelper.STRING_TYPE, "s")))); |
| 136 | + return throwS(newException); |
| 137 | + // GRECLIPSE end |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Mark a method as already processed. |
| 142 | + * |
| 143 | + * @param mn the method node to be considered already processed |
| 144 | + */ |
| 145 | + public static void markAsProcessed(MethodNode mn) { |
| 146 | + mn.setNodeMetaData(NULL_CHECK_IS_PROCESSED, Boolean.TRUE); |
| 147 | + } |
| 148 | + |
| 149 | + private static boolean isMarkedAsProcessed(MethodNode mn) { |
| 150 | + Boolean r = mn.getNodeMetaData(NULL_CHECK_IS_PROCESSED); |
| 151 | + return null != r && r; |
| 152 | + } |
| 153 | +} |
0 commit comments