use of org.objectweb.asm.tree.analysis.Analyzer in project groovy by apache.
the class VerifyClass method readClass.
private boolean readClass(String clazz) throws IOException {
ClassReader cr = new ClassReader(new FileInputStream(clazz));
ClassNode ca = new ClassNode() {
public void visitEnd() {
//accept(cv);
}
};
cr.accept(new CheckClassAdapter(ca), ClassWriter.COMPUTE_MAXS);
boolean failed = false;
List methods = ca.methods;
for (int i = 0; i < methods.size(); ++i) {
MethodNode method = (MethodNode) methods.get(i);
if (method.instructions.size() > 0) {
Analyzer a = new Analyzer(new SimpleVerifier());
try {
a.analyze(ca.name, method);
continue;
} catch (Exception e) {
e.printStackTrace();
}
if (!failed) {
failed = true;
log("verifying of class " + clazz + " failed");
}
if (verbose)
log(method.name + method.desc);
TraceMethodVisitor mv = new TraceMethodVisitor(null);
/*= new TraceMethodVisitor(null) {
public void visitMaxs(int maxStack, int maxLocals) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < text.size(); ++i) {
String s = frames[i] == null ? "null" : frames[i].toString();
while (s.length() < maxStack + maxLocals + 1) {
s += " ";
}
buffer.append(Integer.toString(i + 100000).substring(1));
buffer.append(" ");
buffer.append(s);
buffer.append(" : ");
buffer.append(text.get(i));
}
if (verbose) log(buffer.toString());
}
};*/
for (int j = 0; j < method.instructions.size(); ++j) {
Object insn = method.instructions.get(j);
if (insn instanceof AbstractInsnNode) {
((AbstractInsnNode) insn).accept(mv);
} else {
mv.visitLabel((Label) insn);
}
}
mv.visitMaxs(method.maxStack, method.maxLocals);
}
}
return !failed;
}
use of org.objectweb.asm.tree.analysis.Analyzer in project dex2jar by pxb1988.
the class TestUtils method verify.
@SuppressWarnings("rawtypes")
public static void verify(final ClassReader cr, PrintWriter out) throws AnalyzerException, IllegalArgumentException, IllegalAccessException {
ClassNode cn = new ClassNode();
cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);
List methods = cn.methods;
for (int i = 0; i < methods.size(); ++i) {
MethodNode method = (MethodNode) methods.get(i);
List tryCatchBlocks = method.tryCatchBlocks;
for (int j = 0; j < tryCatchBlocks.size(); j++) {
TryCatchBlockNode tcn = (TryCatchBlockNode) tryCatchBlocks.get(j);
if (tcn.start.equals(tcn.end)) {
throw new DexException("try/catch block %d in %s has same start(%s) and end(%s)", j, method.name, tcn.start.getLabel(), tcn.end.getLabel());
}
}
BasicVerifier verifier = new BasicVerifier();
Analyzer a = new Analyzer(verifier);
try {
a.analyze(cn.name, method);
} catch (Exception e) {
out.println(cr.getClassName() + "." + method.name + method.desc);
printAnalyzerResult(method, a, out);
e.printStackTrace(out);
out.flush();
throw new DexException("method " + method.name + " " + method.desc, e);
}
}
}
use of org.objectweb.asm.tree.analysis.Analyzer in project bytecode-viewer by Konloch.
the class CheckClassAdapter method verify.
/**
* Checks a given class.
*
* @param cr
* a <code>ClassReader</code> that contains bytecode for the
* analysis.
* @param loader
* a <code>ClassLoader</code> which will be used to load
* referenced classes. This is useful if you are verifiying
* multiple interdependent classes.
* @param dump
* true if bytecode should be printed out not only when errors
* are found.
* @param pw
* write where results going to be printed
*/
public static void verify(final ClassReader cr, final ClassLoader loader, final boolean dump, final PrintWriter pw) {
ClassNode cn = new ClassNode();
cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);
Type syperType = cn.superName == null ? null : Type.getObjectType(cn.superName);
List<MethodNode> methods = cn.methods;
List<Type> interfaces = new ArrayList<Type>();
for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext(); ) {
interfaces.add(Type.getObjectType(i.next()));
}
for (int i = 0; i < methods.size(); ++i) {
MethodNode method = methods.get(i);
SimpleVerifier verifier = new SimpleVerifier(Type.getObjectType(cn.name), syperType, interfaces, (cn.access & Opcodes.ACC_INTERFACE) != 0);
Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
if (loader != null) {
verifier.setClassLoader(loader);
}
try {
a.analyze(cn.name, method);
if (!dump) {
continue;
}
} catch (Exception e) {
e.printStackTrace(pw);
}
printAnalyzerResult(method, a, pw);
}
pw.flush();
}
use of org.objectweb.asm.tree.analysis.Analyzer in project groovy-core by groovy.
the class VerifyClass method readClass.
private boolean readClass(String clazz) throws IOException {
ClassReader cr = new ClassReader(new FileInputStream(clazz));
ClassNode ca = new ClassNode() {
public void visitEnd() {
//accept(cv);
}
};
cr.accept(new CheckClassAdapter(ca), ClassWriter.COMPUTE_MAXS);
boolean failed = false;
List methods = ca.methods;
for (int i = 0; i < methods.size(); ++i) {
MethodNode method = (MethodNode) methods.get(i);
if (method.instructions.size() > 0) {
Analyzer a = new Analyzer(new SimpleVerifier());
try {
a.analyze(ca.name, method);
continue;
} catch (Exception e) {
e.printStackTrace();
}
final Frame[] frames = a.getFrames();
if (!failed) {
failed = true;
log("verifying of class " + clazz + " failed");
}
if (verbose)
log(method.name + method.desc);
TraceMethodVisitor mv = new TraceMethodVisitor(null);
/*= new TraceMethodVisitor(null) {
public void visitMaxs(int maxStack, int maxLocals) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < text.size(); ++i) {
String s = frames[i] == null ? "null" : frames[i].toString();
while (s.length() < maxStack + maxLocals + 1) {
s += " ";
}
buffer.append(Integer.toString(i + 100000).substring(1));
buffer.append(" ");
buffer.append(s);
buffer.append(" : ");
buffer.append(text.get(i));
}
if (verbose) log(buffer.toString());
}
};*/
for (int j = 0; j < method.instructions.size(); ++j) {
Object insn = method.instructions.get(j);
if (insn instanceof AbstractInsnNode) {
((AbstractInsnNode) insn).accept(mv);
} else {
mv.visitLabel((Label) insn);
}
}
mv.visitMaxs(method.maxStack, method.maxLocals);
}
}
return !failed;
}
use of org.objectweb.asm.tree.analysis.Analyzer in project enumerable by hraberg.
the class LambdaExpressionTrees method parseExpressionFromMethod.
public static Expression parseExpressionFromMethod(Method method, String... parameters) {
try {
MethodNode mn = findMethodNode(method);
LocalVariableNode[] parameterLocals = new LocalVariableNode[parameters.length];
Type[] argumentTypes = getArgumentTypes(mn.desc);
int realIndex = 1;
for (int i = 0; i < parameters.length; i++) {
parameterLocals[i] = new LocalVariableNode(parameters[i], argumentTypes[i].getDescriptor(), null, null, null, realIndex);
realIndex += argumentTypes[i].getSize();
}
final ExpressionInterpreter interpreter = new ExpressionInterpreter(mn, parameterLocals);
Analyzer analyzer = new Analyzer(interpreter) {
protected Frame newFrame(Frame src) {
Frame frame = super.newFrame(src);
interpreter.setCurrentFrame(frame);
return frame;
}
protected void newControlFlowEdge(int insn, int successor) {
interpreter.newControlFlowEdge(insn, successor);
}
};
interpreter.analyzer = analyzer;
analyzer.analyze(getInternalName(method.getDeclaringClass()), mn);
return interpreter.expression;
} catch (Exception e) {
throw uncheck(e);
}
}
Aggregations