use of org.objectweb.asm.util.CheckClassAdapter 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.util.CheckClassAdapter in project dex2jar by pxb1988.
the class AsmVerify method doCommandLine.
@Override
protected void doCommandLine() throws Exception {
if (remainingArgs.length < 1) {
usage();
return;
}
List<Path> files = new ArrayList<>();
for (String fn : remainingArgs) {
Path file = new File(fn).toPath();
if (!Files.exists(file)) {
System.err.println(fn + " is not exists");
usage();
return;
}
files.add(file);
}
for (Path file : files) {
System.out.println("verify " + file);
walkJarOrDir(file, new FileVisitorX() {
@Override
public void visitFile(Path file, String relative) throws IOException {
if (file.getFileName().toString().endsWith(".class")) {
ClassReader cr = new ClassReader(Files.readAllBytes(file));
ClassNode cn = new ClassNode();
cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG | ClassReader.EXPAND_FRAMES | ClassReader.SKIP_FRAMES);
for (MethodNode method : cn.methods) {
BasicVerifier verifier = new BasicVerifier();
Analyzer<BasicValue> a = new Analyzer<>(verifier);
try {
a.analyze(cn.name, method);
} catch (Exception ex) {
System.err.println("Error verify method " + cr.getClassName() + "." + method.name + " " + method.desc);
if (detail) {
ex.printStackTrace(System.err);
printAnalyzerResult(method, a, new PrintWriter(new OutputStreamWriter(System.err, StandardCharsets.UTF_8)));
}
}
}
}
}
});
}
}
use of org.objectweb.asm.util.CheckClassAdapter in project RecurrentComplex by Ivorforce.
the class VanillaGenerationClassFactory method createClassBinary.
protected byte[] createClassBinary(String className) {
ClassWriter writer = new ClassWriter(0);
ClassVisitor cw = new CheckClassAdapter(writer);
cw.visit(V1_5, ACC_PUBLIC, className, null, Type.getInternalName(GenericVillagePiece.class), null);
{
// empty constructor
String descriptor = Type.getMethodDescriptor(Type.VOID_TYPE);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(GenericVillagePiece.class), "<init>", descriptor, false);
mv.visitInsn(RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
{
// initial constructor
String descriptor = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(StructureVillagePieces.Start.class), Type.INT_TYPE);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ILOAD, 2);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(GenericVillagePiece.class), "<init>", descriptor, false);
mv.visitInsn(RETURN);
mv.visitMaxs(4, 3);
mv.visitEnd();
}
cw.visitEnd();
return writer.toByteArray();
}
use of org.objectweb.asm.util.CheckClassAdapter in project slab by RichardWarburton.
the class BytecodeGenerator method generate.
@SuppressWarnings("unchecked")
public Class<T> generate() {
ClassWriter out = new ClassWriter(ClassWriter.COMPUTE_MAXS);
CheckClassAdapter writer = new CheckClassAdapter(out);
int offset = 0;
declareClass(writer);
declareConstructor(writer);
for (Method getter : inspector.getters) {
offset = declareField(getter, writer, offset);
}
writer.visitEnd();
return (Class<T>) new GeneratedClassLoader(options).defineClass(implementationName, out);
}
use of org.objectweb.asm.util.CheckClassAdapter in project cdap by caskdata.
the class Debugs method debugByteCode.
public static void debugByteCode(ClassDefinition classDefinition, PrintWriter writer) {
ClassReader reader = new ClassReader(classDefinition.getBytecode());
reader.accept(new CheckClassAdapter(new TraceClassVisitor(writer)), 0);
File file = new File("/tmp/" + classDefinition.getInternalName() + ".class");
file.getParentFile().mkdirs();
writer.println(file);
writer.flush();
try {
ByteStreams.write(classDefinition.getBytecode(), Files.newOutputStreamSupplier(file));
} catch (IOException e) {
e.printStackTrace();
}
}
Aggregations