Debian Codegraph @

src/main/java/net/sf/antcontrib/design/VisitorImpl.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 /*
17 * Created on Jan 9, 2005
18 */
19 package net.sf.antcontrib.design;
20
21 import java.io.File;
22
23 import org.apache.bcel.Constants;
24 import org.apache.bcel.classfile.Code;
25 import org.apache.bcel.classfile.CodeException;
26 import org.apache.bcel.classfile.ConstantPool;
27 import org.apache.bcel.classfile.EmptyVisitor;
28 import org.apache.bcel.classfile.ExceptionTable;
29 import org.apache.bcel.classfile.Field;
30 import org.apache.bcel.classfile.JavaClass;
31 import org.apache.bcel.classfile.LineNumberTable;
32 import org.apache.bcel.classfile.LocalVariable;
33 import org.apache.bcel.classfile.Method;
34 import org.apache.bcel.classfile.Utility;
35 import org.apache.bcel.generic.ConstantPoolGen;
36 import org.apache.bcel.generic.Instruction;
37 import org.apache.bcel.generic.InstructionHandle;
38 import org.apache.bcel.generic.MethodGen;
39 import org.apache.tools.ant.BuildException;
40 import org.apache.tools.ant.Location;
41 import org.apache.tools.ant.Project;
42
43 class VisitorImpl extends EmptyVisitor {
44
45 private ConstantPool pool;
46 private Log log;
47 private Design design;
48 private ConstantPoolGen poolGen;
49 private InstructionVisitor visitor;
50 private Location location;
51
52 public VisitorImpl(ConstantPool pool, Log log, Design d, Location loc) {
53 this.pool = pool;
54 this.log = log;
55 this.design = d;
56 this.location = loc;
57 this.poolGen = new ConstantPoolGen(pool);
58 visitor = new InstructionVisitor(poolGen, log, d);
59 }
60
61 private void log(String s, int level) {
62 log.log(s, level);
63 }
64
65 public void visitJavaClass(JavaClass c) {
66 log(" super=" + c.getSuperclassName(), Project.MSG_VERBOSE);
67 String[] names = c.getInterfaceNames();
68
69 String superClass = c.getSuperclassName();
70
71 design.checkClass(superClass);
72
73 for (int i = 0; i < names.length; i++) {
74 log(" interfaces=" + names[i], Project.MSG_VERBOSE);
75 design.checkClass(names[i]);
76 }
77 }
78
79 /**
80 * @see org.apache.bcel.classfile.Visitor#visitField(org.apache.bcel.classfile.Field)
81 */
82 public void visitField(Field f) {
83 String type = Utility.methodSignatureReturnType(f.getSignature());
84 log(" field type=" + type, Project.MSG_VERBOSE);
85 design.checkClass(type);
86
87 }
88
89 /**
90 * @see org.apache.bcel.classfile.Visitor#visitLocalVariable(org.apache.bcel.classfile.LocalVariable)
91 */
92 public void visitLocalVariable(LocalVariable v) {
93 String type = Utility.methodSignatureReturnType(v.getSignature());
94 log(" localVar type=" + type, Project.MSG_VERBOSE);
95 design.checkClass(type);
96 }
97
98 /**
99 * @see org.apache.bcel.classfile.Visitor#visitMethod(org.apache.bcel.classfile.Method)
100 */
101 public void visitMethod(Method m) {
102 log(" method=" + m.getName(), Project.MSG_VERBOSE);
103 String retType = Utility.methodSignatureReturnType(m.getSignature());
104 log(" method ret type=" + retType, Project.MSG_VERBOSE);
105 if (!"void".equals(retType))
106 design.checkClass(retType);
107
108 String[] types = Utility.methodSignatureArgumentTypes(m.getSignature());
109 for (int i = 0; i < types.length; i++) {
110 log(" method param[" + i + "]=" + types[i],
111 Project.MSG_VERBOSE);
112 design.checkClass(types[i]);
113 }
114
115 ExceptionTable excs = m.getExceptionTable();
116 if (excs != null) {
117 types = excs.getExceptionNames();
118 for (int i = 0; i < types.length; i++) {
119 log(" exc=" + types[i], Project.MSG_VERBOSE);
120 design.checkClass(types[i]);
121 }
122 }
123
124 processInstructions(m);
125 }
126
127 private void processInstructions(Method m) {
128 MethodGen mg = new MethodGen(m, design.getCurrentClass(), poolGen);
129
130 if (!mg.isAbstract() && !mg.isNative()) {
131 InstructionHandle ih = mg.getInstructionList().getStart();
132 for (; ih != null; ih = ih.getNext()) {
133 Instruction i = ih.getInstruction();
134 log(" instr=" + i, Project.MSG_DEBUG);
135 // if (i instanceof BranchInstruction) {
136 // branch_map.put(i, ih); // memorize container
137 // }
138
139 // if (ih.hasTargeters()) {
140 // if (i instanceof BranchInstruction) {
141 // _out.println(" InstructionHandle ih_"
142 // + ih.getPosition() + ";");
143 // } else {
144 // _out.print(" InstructionHandle ih_"
145 // + ih.getPosition() + " = ");
146 // }
147 // } else {
148 // _out.print(" ");
149 // }
150
151 // if (!visitInstruction(i))
152 i.accept(visitor);
153 }
154
155 // CodeExceptionGen[] handlers = mg.getExceptionHandlers();
156 //
157 // log("handlers len="+handlers.length, Project.MSG_DEBUG);
158 // for (int i = 0; i < handlers.length; i++) {
159 // CodeExceptionGen h = handlers[i];
160 // ObjectType t = h.getCatchType();
161 // log("type="+t, Project.MSG_DEBUG);
162 // if(t != null) {
163 // log("type="+t.getClassName(), Project.MSG_DEBUG);
164 // }
165 // }
166 // updateExceptionHandlers();
167 }
168 }
169
170 public void visitCodeException(CodeException c) {
171 String s = c.toString(pool, false);
172
173 int catch_type = c.getCatchType();
174
175 if (catch_type == 0)
176 return;
177
178 String temp = pool.getConstantString(catch_type,
179 Constants.CONSTANT_Class);
180 String str = Utility.compactClassName(temp, false);
181
182 log(" catch=" + str, Project.MSG_DEBUG);
183 design.checkClass(str);
184 }
185
186 //
187 public void visitCode(Code c) {
188 LineNumberTable table = c.getLineNumberTable();
189 // LocalVariableTable table = c.getLocalVariableTable();
190 if (table == null)
191 throw new BuildException(getNoDebugMsg(design.getCurrentClass()), location);
192 }
193
194 public static String getNoDebugMsg(String className) {
195 String s = "Class="+className+" was not compiled with the debug option(-g) and\n" +
196 "therefore verifydesign cannot be used on this jar. Please compile your code\n"+
197 "with -g option in javac or debug=\"true\" in the ant build.xml file";
198 return s;
199 }
200
201 /**
202 * @param jarName
203 * @return
204 */
205 public static String getNoFileMsg(File jarName) {
206 String s = "File you specified in your path(or jar attribute)='"+jarName.getAbsolutePath()+"' does not exist";
207 return s;
208 }
209
210
211 }