All pastes #624691 Raw Edit

Mine

public text v1 · immutable
·published 2007-07-18 13:38 UTC
rendered paste body
import com.sun.org.apache.bcel.internal.classfile.ClassParser;
import com.sun.org.apache.bcel.internal.classfile.JavaClass;
import com.sun.org.apache.bcel.internal.classfile.Method;
import com.sun.org.apache.bcel.internal.classfile.Field;
import com.sun.org.apache.bcel.internal.classfile.Attribute;
import com.sun.org.apache.bcel.internal.generic.*;
import com.sun.org.apache.bcel.internal.util.InstructionFinder;
import com.sun.org.apache.bcel.internal.Constants;

import java.lang.instrument.ClassFileTransformer;
import java.security.ProtectionDomain;
import java.io.ByteArrayInputStream;
import java.util.Iterator;
import java.io.*;

public class Transformer implements ClassFileTransformer
{

 gui mainFrame	= new gui();

    public byte[] transform(
            ClassLoader loader,
            String className,
            Class<?> classBeingRedefined,
            ProtectionDomain protectionDomain,
            byte[] classFileBuffer)
    {

        // Apparently our classes don't have that sign
        if(className.contains("/"))
        {
            return classFileBuffer;
        }

        if (true)
        {
            try
            {
                // Let's get a class generic so that we can modify / examine the class
                ClassGen clazz = new ClassGen(new ClassParser(new ByteArrayInputStream(classFileBuffer), null).parse());
                ConstantPoolGen cpg = clazz.getConstantPool();


                // Iterate through every method in the class
                for (Method m : clazz.getMethods())
                {

                    // For now we'll have to iterate through all the instructions, because
                    // we don't know the name of the class.
                    if(true)
                    {
                        MethodGen methodGen = new MethodGen(m, className,cpg);
                        InstructionList il = methodGen.getInstructionList();
                        instructions = il.getInstructions();
                        for(int i = 0; i < instructions.length; i++) {
                            if (instructions[i] instanceof LDC2_W)
                            {
                                //do this blah blah
                            }
                        }
                    }
                }       // !for
            }        // !try
            catch (Exception e)
            {
                e.printStackTrace();
                return classFileBuffer;
            }
        }
        return classFileBuffer;
    }   // !function



}   // !class