1 46 package org.codehaus.groovy.classgen; 47 48 import org.codehaus.groovy.ast.ASTNode; 49 import org.codehaus.groovy.ast.CodeVisitorSupport; 50 import org.codehaus.groovy.ast.stmt.ForStatement; 51 import org.codehaus.groovy.ast.expr.BinaryExpression; 52 import org.codehaus.groovy.ast.expr.MethodCallExpression; 53 import org.codehaus.groovy.ast.expr.PropertyExpression; 54 import org.codehaus.groovy.ast.expr.FieldExpression; 55 import org.codehaus.groovy.ast.expr.VariableExpression; 56 import org.codehaus.groovy.syntax.parser.RuntimeParserException; 57 import org.objectweb.asm.Constants; 58 59 65 public class VerifierCodeVisitor extends CodeVisitorSupport implements Constants { 66 67 private Verifier verifier; 68 69 VerifierCodeVisitor(Verifier verifier) { 70 this.verifier = verifier; 71 } 72 73 public void visitMethodCallExpression(MethodCallExpression call) { 74 assertValidIdentifier(call.getMethod(), "method name", call); 75 super.visitMethodCallExpression(call); 76 } 77 78 public void visitForLoop(ForStatement expression) { 79 assertValidIdentifier(expression.getVariable(), "for loop variable name", expression); 80 super.visitForLoop(expression); 81 } 82 83 public void visitPropertyExpression(PropertyExpression expression) { 84 assertValidIdentifier(expression.getProperty(), "property name", expression); 85 super.visitPropertyExpression(expression); 86 } 87 88 public void visitFieldExpression(FieldExpression expression) { 89 assertValidIdentifier(expression.getFieldName(), "field name", expression); 90 super.visitFieldExpression(expression); 91 } 92 93 public void visitVariableExpression(VariableExpression expression) { 94 assertValidIdentifier(expression.getVariable(), "variable name", expression); 95 super.visitVariableExpression(expression); 96 } 97 98 public void visitBinaryExpression(BinaryExpression expression) { 99 114 super.visitBinaryExpression(expression); 115 } 116 117 public static void assertValidIdentifier(String name, String message, ASTNode node) { 118 int size = name.length(); 119 if (size <= 0) { 120 throw new RuntimeParserException("Invalid " + message + ". Identifier must not be empty", node); 121 } 122 char firstCh = name.charAt(0); 123 if (!Character.isJavaIdentifierStart(firstCh) || firstCh == '$') { 124 throw new RuntimeParserException("Invalid " + message + ". Must start with a letter but was: " + name, node); 125 } 126 127 for (int i = 1; i < size; i++) { 128 char ch = name.charAt(i); 129 if (!Character.isJavaIdentifierPart(ch)) { 130 throw new RuntimeParserException("Invalid " + message + ". Invalid character at position: " + (i + 1) + " of value: " + ch + " in name: " + name, node); 131 } 132 } 133 } 134 } 135 | Popular Tags |