[prev in list] [next in list] [prev in thread] [next in thread] 

List:       bcel-user
Subject:    Re: Generating method Code for setters
From:       "Mark R. Diggory" <mdiggory () latte ! harvard ! edu>
Date:       2003-04-01 21:39:53
[Download RAW message or body]

Mark Proctor (mproctor) wrote:

>Ok now I understand that its not possible to use new, like it isn't
>possible to call the methods directory - it all has to be done with
>newInstance and reflection; due to the need to compile first.
>
>I've gone over the example at:
>http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/repast-jellytag/repast-je
>lly-taglibrary/src/org/apache/commons/jelly/tags/bcel/ClassExtender.java
>?rev=1.1&content-type=text/vnd.viewcvs-markup
>
>The class loader stuff seems pretty complicated there, and I don't
>understand a word of it. However I found the following to work:
>    try {
>      Class Revision = Class.forName(className);   
>      Object revision = Revision.newInstance();
>      revision.setDescription("Mark");
>      System.out.println(revision.getDescription());      
>    } catch(Exception e) {
>      e.printStackTrace();
>    }    
>
>Is that ok, is there a reason why I should be doing it as in the linked
>example?
>  
>

Yes, My example is setup to handle situations where security won't let 
you modify the system classloader directly. The BCELClassLoader just 
provides an alternate means to load classes if thatcase ever arose. The 
defineClass() method provides a default behavior that uses the:

*this*.getClass().getClassLoader()

see

*public* Class defineClass()
    *throws*  java.lang.ClassNotFoundException,
    java.lang.IllegalAccessException,
    java.lang.InstantiationException,
    java.lang.reflect.InvocationTargetException {
        *return* defineClass(*this*.getClass().getClassLoader());

    }

 the

One thing you can do is if you know the "methods" your going to add 
beforehand (ie your just implementing the methods on the "fly") then you 
can define them in a regular Java interface, then have your bcel 
generated class implement that interface, After that step you can then 
cast to that interface and call the methods directly through it.

Hope that helps,
-MRD


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

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic