TokenVisitor.java
biz/hammurapi/antlr/TokenVisitor.java
Violations
Inspector |
Message |
Severity |
Location |
Java Inspector 048 |
Copyrights information should be present in each file. |
1 |
|
Java Inspector 049 |
Use a Collection instead of arrays Object[] |
2 |
41:9
|
Java Inspector 049 |
Use a Collection instead of arrays Object[] |
2 |
42:9
|
Java Inspector 089 |
Constructor is not properly documented |
2 |
51:9
|
Java Inspector 089 |
Undocumented method |
2 |
83:9
|
Java Inspector 089 |
Undocumented method |
2 |
105:9
|
Java Inspector 051 |
It is good practice to call in any case super() in a constructor. |
3 |
51:9
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package biz.hammurapi.antlr;
24
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27import java.lang.reflect.Modifier;
28
29import antlr.Token;
30import biz.hammurapi.util.DispatchException;
31import biz.hammurapi.util.PoliteVisitor;
32
33
34
35
36
37
38
39
40public class TokenVisitor implements PoliteVisitor {
41 private Method[] visitMethods;
42 private Method[] leaveMethods;
43
44
45
46
47
48
49
50
51 public TokenVisitor(String[] tokenTypeNames, String visitPrefix, String leavePrefix) {
52 Class thisClass=this.getClass();
53 for (int i=0, mc=thisClass.getMethods().length; i<mc; i++) {
54 Method m=thisClass.getMethods()[i];
55 if (!m.getName().equals(visitPrefix)
56 && Modifier.isPublic(m.getModifiers())
57 && m.getName().startsWith(visitPrefix)
58 && m.getParameterTypes().length==1
59 && Token.class.isAssignableFrom(m.getParameterTypes()[0])
60 && (boolean.class.equals(m.getReturnType()) || void.class.equals(m.getReturnType()))) {
61 for (int j=0; j<tokenTypeNames.length; j++) {
62 if (m.getName().substring(visitPrefix.length()).equals(tokenTypeNames[j])) {
63 visitMethods[j]=m;
64 }
65 }
66 }
67
68 if (!m.getName().equals(leavePrefix)
69 && Modifier.isPublic(m.getModifiers())
70 && m.getName().startsWith(leavePrefix)
71 && m.getParameterTypes().length==1
72 && Token.class.isAssignableFrom(m.getParameterTypes()[0])
73 && void.class.equals(m.getReturnType())) {
74 for (int j=0; j<tokenTypeNames.length; j++) {
75 if (m.getName().substring(leavePrefix.length()).equals(tokenTypeNames[j])) {
76 leaveMethods[j]=m;
77 }
78 }
79 }
80 }
81 }
82
83 public boolean visit(Object target) {
84 if (target instanceof AST) {
85 Method m=visitMethods[((AST) target).getType()];
86 if (m==null) {
87 return true;
88 }
89
90 try {
91 Object ret = m.invoke(this, new Object[] {target});
92 return ret instanceof Boolean ? ((Boolean) ret).booleanValue() : true;
93 } catch (IllegalArgumentException e) {
94 throw new DispatchException(e);
95 } catch (IllegalAccessException e) {
96 throw new DispatchException(e);
97 } catch (InvocationTargetException e) {
98 throw new DispatchException(e);
99 }
100 }
101
102 return false;
103 }
104
105 public void leave(Object target) {
106 if (target instanceof Token) {
107 Method m=visitMethods[((Token) target).getType()];
108 if (m!=null) {
109 try {
110 m.invoke(this, new Object[] {target});
111 } catch (IllegalArgumentException e) {
112 throw new DispatchException(e);
113 } catch (IllegalAccessException e) {
114 throw new DispatchException(e);
115 } catch (InvocationTargetException e) {
116 throw new DispatchException(e);
117 }
118 }
119 }
120 }
121}
122