1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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.RuntimeParserException;
57 import org.objectweb.asm.Opcodes;
58
59 /***
60 * Verifies the method code
61 *
62 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
63 * @version $Revision: 1.15 $
64 */
65 public class VerifierCodeVisitor extends CodeVisitorSupport implements Opcodes {
66
67 private Verifier verifier;
68
69 VerifierCodeVisitor(Verifier verifier) {
70 this.verifier = verifier;
71 }
72
73 public void visitMethodCallExpression(MethodCallExpression call) {
74 super.visitMethodCallExpression(call);
75 }
76
77 public void visitForLoop(ForStatement expression) {
78 assertValidIdentifier(expression.getVariable(), "for loop variable name", expression);
79 super.visitForLoop(expression);
80 }
81
82 public void visitPropertyExpression(PropertyExpression expression) {
83
84 super.visitPropertyExpression(expression);
85 }
86
87 public void visitFieldExpression(FieldExpression expression) {
88 assertValidIdentifier(expression.getFieldName(), "field name", expression);
89 super.visitFieldExpression(expression);
90 }
91
92 public void visitVariableExpression(VariableExpression expression) {
93 assertValidIdentifier(expression.getName(), "variable name", expression);
94 super.visitVariableExpression(expression);
95 }
96
97 public void visitBinaryExpression(BinaryExpression expression) {
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 super.visitBinaryExpression(expression);
114 }
115
116 public static void assertValidIdentifier(String name, String message, ASTNode node) {
117 int size = name.length();
118 if (size <= 0) {
119 throw new RuntimeParserException("Invalid " + message + ". Identifier must not be empty", node);
120 }
121 char firstCh = name.charAt(0);
122 if (!Character.isJavaIdentifierStart(firstCh) || firstCh == '$') {
123 throw new RuntimeParserException("Invalid " + message + ". Must start with a letter but was: " + name, node);
124 }
125
126 for (int i = 1; i < size; i++) {
127 char ch = name.charAt(i);
128 if (!Character.isJavaIdentifierPart(ch)) {
129 throw new RuntimeParserException("Invalid " + message + ". Invalid character at position: " + (i + 1) + " of value: " + ch + " in name: " + name, node);
130 }
131 }
132 }
133 }