public static void printASMifiedCode(byte[] data,PrintWriter out) { ClassReader cr = new ClassReader(data); cr.accept(new TraceClassVisitor(null, new ASMifier(),out),ClassReader.EXPAND_FRAMES); }
@Override public ClassDependency resolveDependentClassNames(Resource resource, byte[] bytes) { ClassReader cr = new ClassReader(bytes); DependencyVisitor visitor = new DependencyVisitor(); cr.accept(visitor,0); String className = Type.getObjectType(cr.getClassName()).getClassName(); String superName = className.equals("java.lang.Object") ? null : Type.getObjectType(cr.getSuperName()).getClassName(); return new SimpleClassDependency(className, superName, visitor.innerClasses, visitor.allClasses); }
classReader.copyPool(this); this.cr = classReader;
final Item[] items, final char[] c) { int u = getAttributes(); boolean found = false; for (int i = readUnsignedShort(u); i > 0; --i) { String attrName = readUTF8(u + 2, c); if ("BootstrapMethods".equals(attrName)) { found = true; break; u += 6 + readInt(u + 4); int boostrapMethodCount = readUnsignedShort(u + 8); for (int j = 0, v = u + 10; j < boostrapMethodCount; j++) { int position = v - u - 10; int hashCode = readConst(readUnsignedShort(v), c).hashCode(); for (int k = readUnsignedShort(v + 2); k > 0; --k) { hashCode ^= readConst(readUnsignedShort(v + 4), c).hashCode(); v += 2; items[index] = item; int attrSize = readInt(u + 4); ByteVector bootstrapMethods = new ByteVector(attrSize + 62); bootstrapMethods.putByteArray(b, u + 10, attrSize - 2);
int maxStack = readUnsignedShort(u); int maxLocals = readUnsignedShort(u + 2); int codeLength = readInt(u + 4); u += 8; int codeEnd = u + codeLength; Label[] labels = context.labels = new Label[codeLength + 2]; readLabel(codeLength + 1, labels); while (u < codeEnd) { int offset = u - codeStart; break; case ClassWriter.LABEL_INSN: readLabel(offset + readShort(u + 1), labels); u += 3; break; case ClassWriter.LABELW_INSN: readLabel(offset + readInt(u + 1), labels); u += 5; break; readLabel(offset + readInt(u), labels); for (int i = readInt(u + 8) - readInt(u + 4) + 1; i > 0; --i) { readLabel(offset + readInt(u + 12), labels); u += 4; readLabel(offset + readInt(u), labels); for (int i = readInt(u + 4); i > 0; --i) { readLabel(offset + readInt(u + 12), labels);
int access = readUnsignedShort(u); String name = readClass(u + 2, c); String superClass = readClass(u + 4, c); String[] interfaces = new String[readUnsignedShort(u + 6)]; u += 8; for (int i = 0; i < interfaces.length; ++i) { interfaces[i] = readClass(u, c); u += 2; u = getAttributes(); for (int i = readUnsignedShort(u); i > 0; --i) { String attrName = readUTF8(u + 2, c); sourceFile = readUTF8(u + 8, c); } else if ("InnerClasses".equals(attrName)) { innerClasses = u + 8; } else if ("EnclosingMethod".equals(attrName)) { enclosingOwner = readClass(u + 8, c); int item = readUnsignedShort(u + 10); if (item != 0) { enclosingName = readUTF8(items[item], c); enclosingDesc = readUTF8(items[item] + 2, c); signature = readUTF8(u + 8, c); } else if (ANNOTATIONS && "RuntimeVisibleAnnotations".equals(attrName)) { | ClassWriter.ACC_SYNTHETIC_ATTRIBUTE; } else if ("SourceDebugExtension".equals(attrName)) {
case ClassWriter.METH: case ClassWriter.IMETH: nameType = items[readUnsignedShort(index + 2)]; item.set(tag, readClass(index, buf), readUTF8(nameType, buf), readUTF8(nameType + 2, buf)); break; case ClassWriter.INT: item.set(readInt(index)); break; case ClassWriter.FLOAT: item.set(Float.intBitsToFloat(readInt(index))); break; case ClassWriter.NAME_TYPE: item.set(tag, readUTF8(index, buf), readUTF8(index + 2, buf), null); break; case ClassWriter.LONG: item.set(readLong(index)); ++i; break; case ClassWriter.DOUBLE: item.set(Double.longBitsToDouble(readLong(index))); ++i; break; if (s == null) { index = items[i]; s = strings[i] = readUTF(index + 2, readUnsignedShort(index), buf);
/** * Makes the given visitor visit the Java class of this {@link ClassReader} * . This class is the one specified in the constructor (see * {@link #ClassReader(byte[]) ClassReader}). * * @param classVisitor * the visitor that must visit this class. * @param flags * option flags that can be used to modify the default behavior * of this class. See {@link #SKIP_DEBUG}, {@link #EXPAND_FRAMES} * , {@link #SKIP_FRAMES}, {@link #SKIP_CODE}. */ public void accept(final ClassVisitor classVisitor, final int flags) { accept(classVisitor, new Attribute[0], flags); }
cr = new ClassReader(is); }catch (Exception e) { String internalClassName = cr.getClassName(); className = internalClassName.replace('/','.');
protected boolean isFrameworkClass(ClassInfo ci) { return ci.cr.getClassName().startsWith("leap/"); }
/** * Reads the internal class of super class. */ protected static String readSuperName(Resource base, String internalClassName){ String classUrl = "classpath:" + internalClassName + Classes.CLASS_FILE_SUFFIX; Resource resource = Resources.getResource(classUrl); if(null != resource && resource.exists()){ InputStream is = null; try{ is = resource.getInputStream(); return new ClassReader(is).getSuperName(); }catch(IOException e) { throw Exceptions.wrap(e); }finally{ IO.close(is); } } return null; }
try (InputStream in = ci.is.getInputStream()) { byte[] bytes = instrumentClass(cn, new ClassReader(in), methods);
@Override protected void processClass(AppInstrumentContext context, AppInstrumentClass ic, ClassInfo ci, boolean methodBodyOnly) { if(ASM.isAnnotationPresent(ci.cn, NonEntity.class)){ return; } boolean isModel = false; String superName = ci.cr.getSuperName(); if(MODEL_CLASS_NAME.equals(superName)){ isModel = true; }else if(!superName.equals("java/lang/Object")){ for(;;){ superName = readSuperName(ci.rs, superName); if(null == superName || superName.equals("java/lang/Object")){ break; } if(superName.equals(MODEL_CLASS_NAME)){ isModel = true; break; } } } if(isModel){ ClassWriter cw = new ClassWriter(ci.cr,ClassWriter.COMPUTE_FRAMES); instrument(context, ic, ci.cr, cw); } }
protected void transformClass(ClassReader cr,ClassWriter cw, ClassNode cn){ ClassVisitor cv = new ModelTransformVisitor(cn, cw, transformers); cr.accept(cv,0); }
ClassReader cr = new ClassReader(bytes); if(context.isInstrumentedBy(cr.getClassName(), this.getClass())) { return; AppInstrumentClass ic = context.getInstrumentedClass(cr.getClassName()); if(null != ic) { is = new ByteArrayInputStreamSource(ic.getClassData()); cr = new ClassReader(ic.getClassData()); }else{ ic = context.newInstrumentedClass(cr.getClassName()); ic = context.getInstrumentedClass(cr.getClassName());
protected void instrumentSetClassName(ClassReader cr,MethodVisitor mv){ mv.visitFieldInsn(Opcodes.GETSTATIC, cr.getClassName(), "className", "Ljava/lang/ThreadLocal;"); mv.visitLdcInsn(cr.getClassName().replace('/', '.')); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/ThreadLocal", "set", "(Ljava/lang/Object;)V",false); }
@Override protected void processClass(AppInstrumentContext context, AppInstrumentClass ic, ClassInfo ci, boolean methodBodyOnly) { ClassNode cn = ci.cn; if(null != cn.methods) { boolean hasTransactionalMethods = false; for(MethodNode mn : cn.methods) { if(ASM.isAnnotationPresent(mn, Transactional.class)) { hasTransactionalMethods = true; break; } } if(hasTransactionalMethods) { log.debug("Instrument Transactional class : {}", ic.getClassName()); Try.throwUnchecked(() -> { try(InputStream in = ci.is.getInputStream()) { context.updateInstrumented(ic, this, instrumentClass(ci.cn, new ClassReader(in), true), true); } }); } } }
if (args[i].endsWith(".class") || args[i].indexOf('\\') > -1 || args[i].indexOf('/') > -1) { cr = new ClassReader(new FileInputStream(args[i])); } else { cr = new ClassReader(args[i]); cr.accept(new TraceClassVisitor(new PrintWriter(System.out)), flags);
public static ClassNode getClassNode(ClassReader cr){ ClassNode cn = new ClassNode(); cr.accept(cn, 0); return cn; }