jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Koduru, Rajendra Kumar Reddy" <rajendra.kumar.reddy.kod...@sap.com>
Subject Hi! adding a method to already existing class
Date Mon, 20 Dec 2004 19:09:52 GMT
Hi 
  i am trying to add a method to already existing class and update the
class file with the method.

  My code is follows

import java.io.*;
import org.apache.bcel.*;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;

public class addMethodBCEL{

	 public static void main( String args[] ) throws IOException {

		    ClassParser cparse = new
ClassParser("HelloBCEL.class");
		    JavaClass clazz = cparse.parse();
		    ClassGen cg = new ClassGen(clazz);
		    ConstantPool cp1 = clazz.getConstantPool();
		    ConstantPoolGen cp = new ConstantPoolGen(cp1); 
		    InstructionFactory factory = new
InstructionFactory(cp);
		    Method addmeth = addMethod(clazz,cp,factory);

		    cg.addMethod(addmeth);
		 try {
	
cg.getJavaClass().setConstantPool(cg.getConstantPool().getFinalConstantP
ool()); // i think it is wrong here..
		    cg.getJavaClass().dump(clazz.getClassName() +
".class");
		    }catch(Exception e){System.out.println(e);} 

		
	}


	public static Method addMethod(JavaClass clazz, ConstantPoolGen
cp, InstructionFactory factory){
			InstructionList il1 = new InstructionList();
			MethodGen mg = new
MethodGen(Constants.ACC_STATIC | Constants.ACC_PUBLIC, Type.INT,
Type.NO_ARGS, new String[]{},"getMarks",clazz.getClassName(), il1, cp) ;
		    	il1.append(new PUSH(cp,10));
			il1.append(factory.createReturn(Type.INT));
			mg.setMaxStack();
			return mg.getMethod();
		}
}


    and when i am trying to execute HelloBCEL.class, the class that is
updated i the above code, it gives me an exception stating 

Exception in thread "main" java.lang.ClassFormatError: HelloBCEL
(Illegal consta
nt pool index)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)


 could you please let me know, where i am wrong. 

Hope that you can help me in this regard.

Thank you
Reddy

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Mime
View raw message