Hi,

I'm trying to add a set of instructions before a
particular instruction in a class file.  The class
file I'm trying to modify appears as

public class CorrectiveClass {
        
        public void printString()
        {
                String str = null;
                System.out.println(str.length());
        }

}

All I'm doing is adding the statement
System.out.println("Before null initialization"),
before the statment String str = null.

Here is the code that does that.

public class ModifyClass {

        public static void main(String[] args) {
                
                ObjectType p_stream = new
ObjectType("java.io.PrintStream");
                CorrectiveClass cclazz = new CorrectiveClass();
                JavaClass clazz =
Repository.lookupClass(cclazz.getClass());
                ClassGen classgen = new ClassGen(clazz);
                System.out.println("Repository look up succeeded");
                
                //System.out.println("Fields in this class ");
                //Field [] fields = clazz.getFields();
                //System.out.println(fields.length);
                
                System.out.println("Methods in this class ");
                Method [] methods = classgen.getMethods();
                System.out.println(methods.length);

                for (int mindex = 0; mindex < methods.length;
mindex++){
                        System.out.println(methods[mindex].toString());
                        if
(methods[mindex].getName().equals("printString"))
                        {
                                ConstantPoolGen cpool = new
ConstantPoolGen(clazz.getConstantPool());
                                //To modify the method, construct a MethodGen
object
                                
                                MethodGen methodGen = new
MethodGen(methods[mindex], classgen.getClassName(),
cpool);
                                InstructionList il =
methodGen.getInstructionList();
                                InstructionFinder insf = new
InstructionFinder(il);
                                System.out.println("****Code for method
printString********");
                                Code code = methods[mindex].getCode();
                                System.out.println(code);
                                Iterator i = insf.search("ACONST_NULL");
                        
                                InstructionFactory ifact = new
InstructionFactory(classgen);
                                InstructionList instr_to_be_inserted = new
InstructionList();
                        
instr_to_be_inserted.append(ifact.createFieldAccess("java.lang.System",
"out", p_stream, Constants.GETSTATIC));
                                instr_to_be_inserted.append(new PUSH(cpool,
"Before null initialization"));
                        
instr_to_be_inserted.append(ifact.createInvoke("java.io.PrintStream",
"println", Type.VOID, 
                        new Type[] { Type.STRING },
                        Constants.INVOKEVIRTUAL));
                                
                                while (i.hasNext())
                                {
                                        System.out.println("Search fetched 
result");
                                        InstructionHandle [] matches =
(InstructionHandle[])i.next();
                                        System.out.println("****No. of 
instructions
matched is ***** " + matches.length);
                                        Instruction instruction =
matches[0].getInstruction();
                                        il.insert(instruction, 
instr_to_be_inserted);
                                        il.dispose();
                                }
                                        classgen.removeMethod(methods[mindex]);
                                        
classgen.addMethod(methodGen.getMethod());
                                        il.dispose();
                                try
                                {
                                        FileOutputStream fpstream = new
FileOutputStream("C:\\Eshwari\\MSProject\\Workspace\\BCELTrial\\bcelPackage\\CorrectiveClass.class");
                                        classgen.getJavaClass().dump(fpstream);
                                        fpstream.close();
                                        System.out.println("Class file 
generated");
                                }
                                catch(Exception e)
                                {
                                        e.printStackTrace();
                                }
                        }
                }
                
                CorrectiveClass cclass = new CorrectiveClass();
                cclass.printString();
        }
}

But when I run this class for the first time, I'm not
getting the System.out.println("Before null
initialization") statment in the output before the
null pointer exception.  Subsequent runs give me the
following error.

java.lang.VerifyError: (class:
bcelPackage/CorrectiveClass, method: printString
signature: ()V) Illegal type in constant pool
        at ModifyClass.main(ModifyClass.java:45)
Exception in thread "main" 

I'm really stuck here. Any help will be highly
appreciated.

Thanks, 
Eshwari

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to