The strange thing is that the code works for some methods and throws
the exception for others. The NullPointerException is because it try
to access a constant that is null.

2008/4/20, Dave Brosius <[EMAIL PROTECTED]>:
> Hmm, maybe i misread your code, and the problem is the other way. Aren't you
> appending statements after the return statement?
>
>  ----- Original Message ----- From: "Eduardo Guerra" <[EMAIL PROTECTED]>
>  To: <bcel-user@jakarta.apache.org>
>  Sent: Friday, April 18, 2008 12:37 PM
>  Subject: NullPointerException in setMaxStack()
>
>
>
> >
> > Hi!
> >
> > I'm trying to put some code for log each line of code: There is the code:
> >
> > ClassParser cp = new ClassParser(new
> > ByteArrayInputStream(classFileBuffer),className+".java");
> >
> > JavaClass jc = cp.parse();
> > ClassGen cg = new ClassGen(jc);
> > InstructionFactory factory = new InstructionFactory(cg);
> > ConstantPoolGen cpg = new
> ConstantPoolGen(jc.getConstantPool());
> >
> > ObjectType pStream = new
> ObjectType("java.io.PrintStream");
> >
> > for(int i = 0; i < cg.getMethods().length; i++){
> > MethodGen mg = new MethodGen(cg.getMethods()[i], jc.getClassName(), cpg);
> > InstructionList il = mg.getInstructionList();
> >
> > for(LineNumberGen lng : mg.getLineNumbers()){
> > InstructionHandle ih = lng.getInstruction();
> > ih =
> il.append(ih,factory.createFieldAccess("java.lang.System",
> > "out", pStream, Constants.GETSTATIC));
> > ih = il.append(ih,new PUSH(cpg,"Classe "+jc.getClassName()+ "
> > metodo " + cg.getMethods()[i].getName() +  " linha " +
> > lng.getSourceLine()));
> > ih =
> il.append(ih,factory.createInvoke("java.io.PrintStream",
> > "println", Type.VOID, new Type[]{Type.STRING},
> > Constants.INVOKEVIRTUAL));
> > }
> >
> > mg.setInstructionList(il);
> > mg.setMaxStack();
> > mg.setMaxLocals();
> > cg.replaceMethod(cg.getMethods()[i], mg.getMethod());
> > }
> >
> > cg.setConstantPool(cpg);
> > JavaClass newClass = cg.getJavaClass();
> >
> > return newClass.getBytes();
> >
> >
> > For some methods, the setMaxStack() throws NullPointerException with
> > this stacktrace:
> >
> > java.lang.NullPointerException
> > at
> org.apache.bcel.generic.FieldOrMethod.getSignature(FieldOrMethod.java:54)
> > at
> org.apache.bcel.generic.FieldInstruction.getFieldType(FieldInstruction.java:71)
> > at
> org.apache.bcel.generic.FieldInstruction.getType(FieldInstruction.java:64)
> > at
> org.apache.bcel.generic.FieldInstruction.getFieldSize(FieldInstruction.java:57)
> > at
> org.apache.bcel.generic.GETSTATIC.produceStack(GETSTATIC.java:47)
> > at
> org.apache.bcel.generic.MethodGen.getMaxStack(MethodGen.java:928)
> > at
> org.apache.bcel.generic.MethodGen.setMaxStack(MethodGen.java:814)
> > at
> teste.IntrumentatorAdaptor.transform(IntrumentatorAdaptor.java:54)
> > at
> sun.instrument.TransformerManager.transform(TransformerManager.java:169)
> > at
> sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:365)
> > at java.lang.ClassLoader.defineClass1(Native Method)
> > at
> java.lang.ClassLoader.defineClass(ClassLoader.java:620)
> > at
> java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
> > at
> java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
> > at
> java.net.URLClassLoader.access$000(URLClassLoader.java:56)
> > at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
> > at java.security.AccessController.doPrivileged(Native
> Method)
> > at
> java.net.URLClassLoader.findClass(URLClassLoader.java:188)
> > at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
> > at
> sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
> > at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
> > at
> java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
> >
> >
> > Thanks in advance for any help!!!
> >
> > Eduardo Guerra
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> > For additional commands, e-mail:
> [EMAIL PROTECTED]
> >
> >
>
>
> ---------------------------------------------------------------------
>  To unsubscribe, e-mail:
> [EMAIL PROTECTED]
>  For additional commands, e-mail:
> [EMAIL PROTECTED]
>
>

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

Reply via email to