| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
i.accept(visitor); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 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 |
|
| 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 |
|
| 203 |
|
| 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 |
} |