Search in sources :

Example 1 with FieldNode

use of jdk.internal.org.objectweb.asm.tree.FieldNode in project openj9 by eclipse.

the class Transformer method transform.

public byte[] transform(ClassLoader loader, String className, Class<?> classToTransform, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    try {
        if (flag == MODIFY_EXISTING_METHOD) {
            // for fast HCR
            AgentMain.logger.debug("Instrumenting " + className + " by modifying existing method.");
            ClassReader reader = new ClassReader(classToTransform.getCanonicalName());
            ClassWriter cw = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
            ClassVisitor visitor = new CustomClassVisitor(cw, classToTransform.getCanonicalName());
            reader.accept(visitor, ClassReader.EXPAND_FRAMES);
            return cw.toByteArray();
        } else if (flag == ADD_NEW_METHOD) {
            // for extended HCR
            AgentMain.logger.debug("Instrumenting " + className + " by adding a new method.");
            ClassReader reader = new ClassReader(classToTransform.getCanonicalName());
            ClassWriter cw = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
            ClassVisitor cv = new MethodAddAdapter(new CheckClassAdapter(cw));
            reader.accept(cv, ClassReader.SKIP_FRAMES);
            return cw.toByteArray();
        } else if (flag == ADD_NEW_FIELD) {
            // for GCRetransform test
            AgentMain.logger.debug("Instrumenting " + className + " by adding a new field.");
            ClassReader reader = new ClassReader(classToTransform.getCanonicalName());
            ClassWriter cw = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
            FieldNode newField = new FieldNode(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "new_static_int_array", "[I", null, null);
            ClassVisitor cv = new FieldAdder(new CheckClassAdapter(cw), newField, className, _methodName);
            reader.accept(cv, ClassReader.SKIP_FRAMES);
            return cw.toByteArray();
        }
        return null;
    } catch (IOException e) {
        return null;
    }
}
Also used : FieldNode(jdk.internal.org.objectweb.asm.tree.FieldNode) CheckClassAdapter(jdk.internal.org.objectweb.asm.util.CheckClassAdapter) ClassReader(jdk.internal.org.objectweb.asm.ClassReader) ClassVisitor(jdk.internal.org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) ClassWriter(jdk.internal.org.objectweb.asm.ClassWriter)

Aggregations

IOException (java.io.IOException)1 ClassReader (jdk.internal.org.objectweb.asm.ClassReader)1 ClassVisitor (jdk.internal.org.objectweb.asm.ClassVisitor)1 ClassWriter (jdk.internal.org.objectweb.asm.ClassWriter)1 FieldNode (jdk.internal.org.objectweb.asm.tree.FieldNode)1 CheckClassAdapter (jdk.internal.org.objectweb.asm.util.CheckClassAdapter)1