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
  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
		    JavaClass clazz = cparse.parse();
		    ClassGen cg = new ClassGen(clazz);
		    ConstantPool cp1 = clazz.getConstantPool();
		    ConstantPoolGen cp = new ConstantPoolGen(cp1); 
		    InstructionFactory factory = new
		    Method addmeth = addMethod(clazz,cp,factory);

		 try {
ool()); // i think it is wrong here..
		    cg.getJavaClass().dump(clazz.getClassName() +
		    }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));
			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

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

View raw message