AstVisitor.java

biz/hammurapi/antlr/AstVisitor.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

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.antlr;
24
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27import java.lang.reflect.Modifier;
28
29import antlr.collections.AST;
30import biz.hammurapi.util.DispatchException;
31import biz.hammurapi.util.PoliteVisitor;
32
33
34/**
35 * Visits AST and dispatches invocations by
36 * node names.
37 * @author Pavel Vlasov
38 * @version $Revision: 1.2 $
39 */
40public class AstVisitor implements PoliteVisitor {
41 private Method[] visitMethods;
42 private Method[] leaveMethods;
43
44 /**
45 * @param tokenTypeNames - can be obtained from Parser.
46 * @param visitPrefix - visit method name prefix. E.g. if prefix is <code>visit_</code>
47 * then visit of AST with type name <code>LITERAL_throws</code> will be dispatched
48 * to method <code>visit_LITERAL_throws</code> if such method is present.
49 * @param leavePrefix - leave method name prefix.
50 */
51 public AstVisitor(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 && AST.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 && AST.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 AST) {
107 Method m=visitMethods[((AST) 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