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