use of org.objectweb.asm.ClassReader in project LogisticsPipes by RS485.
the class ParamProfiler method handleClass.
public static byte[] handleClass(byte[] bytes) {
if (!isActive)
return bytes;
final ClassReader reader = new ClassReader(bytes);
final ClassNode node = new ClassNode();
reader.accept(node, 0);
final String className = node.name;
for (final MethodNode m : node.methods) {
final String methodName = m.name;
final String methodDesc = m.desc;
final boolean isConst = methodName.contains("<") || methodName.contains(">");
if (isConst)
continue;
final long methodId = minMethodId++;
final List<String> varList = new ArrayList<>();
if (!methodDesc.startsWith("("))
throw new UnsupportedOperationException(methodDesc);
outer: for (int i = 1; i < methodDesc.length(); i++) {
switch(methodDesc.charAt(i)) {
case ')':
break outer;
case 'L':
int startA = i;
while (methodDesc.charAt(i) != ';') i++;
varList.add(methodDesc.substring(startA, i + 1));
break;
case '[':
int startB = i;
while (methodDesc.charAt(i) == '[') i++;
if (methodDesc.charAt(i) == 'L') {
while (methodDesc.charAt(i) != ';') i++;
}
varList.add(methodDesc.substring(startB, i + 1));
break;
default:
varList.add(String.valueOf(methodDesc.charAt(i)));
}
}
final List<Label> catchStatement = new ArrayList<>();
MethodNode mv = new MethodNode(Opcodes.ASM4, m.access, m.name, m.desc, m.signature, m.exceptions.toArray(new String[0])) {
@Override
public void visitCode() {
super.visitCode();
Label l0 = new Label();
visitLabel(l0);
visitLdcInsn(new Long(methodId));
visitLdcInsn(className + "+" + methodName + "+" + methodDesc);
if ((m.access & Opcodes.ACC_STATIC) != 0) {
visitInsn(Opcodes.ACONST_NULL);
} else {
visitVarInsn(Opcodes.ALOAD, 0);
}
visitIntInsn(Opcodes.BIPUSH, varList.size());
visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
int count = 0;
int pos = 0;
if ((m.access & Opcodes.ACC_STATIC) == 0) {
pos = 1;
}
for (String varNode : varList) {
visitInsn(Opcodes.DUP);
visitIntInsn(Opcodes.BIPUSH, count++);
if (!varNode.startsWith("L") && !varNode.startsWith("[")) {
String primitiveType = varNode;
switch(primitiveType.charAt(0)) {
case 'I':
visitVarInsn(Opcodes.ILOAD, pos);
pos += 1;
visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
break;
case 'J':
visitVarInsn(Opcodes.LLOAD, pos);
pos += 2;
visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
break;
case 'Z':
visitVarInsn(Opcodes.ILOAD, pos);
pos += 1;
visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
break;
case 'B':
visitVarInsn(Opcodes.ILOAD, pos);
pos += 1;
visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false);
break;
case 'C':
visitVarInsn(Opcodes.ILOAD, pos);
pos += 1;
visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;", false);
break;
case 'S':
visitVarInsn(Opcodes.ILOAD, pos);
pos += 1;
visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false);
break;
case 'F':
visitVarInsn(Opcodes.FLOAD, pos);
pos += 1;
visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false);
break;
case 'D':
visitVarInsn(Opcodes.DLOAD, pos);
pos += 2;
visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false);
break;
default:
throw new UnsupportedOperationException("'" + primitiveType + "'");
}
} else {
visitVarInsn(Opcodes.ALOAD, pos);
pos += 1;
}
visitInsn(Opcodes.AASTORE);
}
visitMethodInsn(Opcodes.INVOKESTATIC, "logisticspipes/asm/ParamProfiler", "methodStart", "(JLjava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;)V", false);
}
@Override
public void visitInsn(int opcode) {
if (opcode == Opcodes.RETURN || opcode == Opcodes.IRETURN || opcode == Opcodes.LRETURN || opcode == Opcodes.FRETURN || opcode == Opcodes.DRETURN || opcode == Opcodes.ARETURN) {
visitLdcInsn(new Long(methodId));
visitMethodInsn(Opcodes.INVOKESTATIC, "logisticspipes/asm/ParamProfiler", "methodEnd", "(J)V", false);
}
super.visitInsn(opcode);
}
@Override
public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
catchStatement.add(handler);
super.visitTryCatchBlock(start, end, handler, type);
}
boolean watchForHandling = false;
@Override
public void visitLabel(Label label) {
watchForHandling = false;
super.visitLabel(label);
if (catchStatement.contains(label)) {
watchForHandling = true;
}
}
@Override
public void visitVarInsn(int opcode, int var) {
super.visitVarInsn(opcode, var);
if (watchForHandling) {
watchForHandling = false;
Label l = new Label();
visitLabel(l);
visitVarInsn(Opcodes.ALOAD, var);
visitMethodInsn(Opcodes.INVOKESTATIC, "logisticspipes/asm/ParamProfiler", "handleException", "(Ljava/lang/Throwable;)V", false);
}
}
};
m.accept(mv);
node.methods.set(node.methods.indexOf(m), mv);
}
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
node.accept(writer);
return writer.toByteArray();
}
use of org.objectweb.asm.ClassReader in project pinpoint by naver.
the class JavassistVerifyErrorTest method asm_stackmapframe_check.
@Test
public void asm_stackmapframe_check() throws Exception {
CustomURLClassLoader classLoader = new CustomURLClassLoader(new URL[] {}, Thread.currentThread().getContextClassLoader());
final InputStream stream = classLoader.getResourceAsStream(JavaAssistUtils.javaNameToJvmName(INVALID_STACK_MAP_FRAME) + ".class");
ClassReader cr = new ClassReader(stream);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
ClassVisitor cv = new BytecodeVerifyTestClassVisitor(cw);
cr.accept(cv, ClassReader.EXPAND_FRAMES | ClassReader.SKIP_DEBUG);
byte[] bytecode = cw.toByteArray();
classLoader.defineClass0(INVALID_STACK_MAP_FRAME, bytecode);
final Class<?> aClass = Class.forName(INVALID_STACK_MAP_FRAME, true, classLoader);
Assert.assertSame(aClass.getClassLoader(), classLoader);
final ASMBytecodeDisassembler bytecodeDisassembler = new ASMBytecodeDisassembler();
final String dumpBytecode = bytecodeDisassembler.dumpBytecode(bytecode);
logger.debug("dumpBytecode:{}", dumpBytecode);
final String verify = bytecodeDisassembler.dumpVerify(bytecode, classLoader);
logger.debug("dumpVerify:{}", verify);
// final String dumpAsm = bytecodeDisassembler.dumpASM(bytecode);
// logger.debug("dumpAsm :{}", dumpAsm);
}
use of org.objectweb.asm.ClassReader in project neo4j by neo4j.
the class ByteCodeVerifier method classNode.
private static ClassNode classNode(ByteBuffer bytecode) {
byte[] bytes;
if (bytecode.hasArray()) {
bytes = bytecode.array();
} else {
bytes = new byte[bytecode.limit()];
bytecode.get(bytes);
}
ClassNode classNode = new ClassNode();
new ClassReader(bytes).accept(new CheckClassAdapter(classNode, false), SKIP_DEBUG);
return classNode;
}
use of org.objectweb.asm.ClassReader in project retrolambda by orfjackal.
the class LambdaClassesTest method does_not_contain_references_to_JDK_lambda_classes.
@Test
public void does_not_contain_references_to_JDK_lambda_classes() throws IOException {
ClassReader cr = new ClassReader("net/orfjackal/retrolambda/test/LambdaClassesTest$Dummy1$$Lambda$1");
// XXX: fix visitConstantPool and assert the constant pool entries instead of this hack
// TestUtil.visitConstantPool(cr, (item, constant) -> {
// });
String bytecode = new String(cr.b);
assertThat(bytecode, not(containsString("java/lang/invoke/LambdaForm")));
}
use of org.objectweb.asm.ClassReader in project pinpoint by naver.
the class ASMBytecodeDisassembler method dumpVerify.
public String dumpVerify(byte[] bytecode, ClassLoader classLoader) {
if (bytecode == null) {
throw new NullPointerException("bytecode must not be null");
}
if (classLoader == null) {
throw new NullPointerException("classLoader must not be null");
}
final StringWriter out = new StringWriter();
final PrintWriter writer = new PrintWriter(out);
final ClassReader cr = new ClassReader(bytecode);
CheckClassAdapter.verify(cr, classLoader, true, writer);
return out.toString();
}
Aggregations