Mark Proctor (mproctor | 1 Apr 2003 01:25
Picon
Favicon

Re: Generating method Code for setters

I've downloaded org.apache.commons.jelly.tags.bcel; and successfully
used that as the basis of my own setter and getter generators. I'm
having problems understanding the Class loader stuff. There are a lot of
methods there and I don't understand why you can't got to using
Class.forName for the class you've just created, why all the other
stuff?

public Object newInstance()
public Object newInstance(ClassLoader loader)
public Class defineClass()
public Class defineClass(ClassLoader loader)

I would really appreciate it if someone could enlighten me.

Regards

Mark
gaurav shankar | 1 Apr 2003 04:46
Picon
Favicon

Re: class to JavaClass file

What I am trying to do is change in constant pool of every class my 
application loads (not all but that also includes some library classes ) for 
that im trying to change instance of Class that findSystemClass() returns 
into that of JavaClass so that I can change in the constant pool and then 
use defineClass on it. ClassParser converts DataInputStream into JavaClass, 
how can I make it to change Class into JavaClass.

Thanks
Gaurav Shankar
CS Deptt , Rutgers University

>From: "Juozas Baliuka" <baliuka <at> mwm.lt>
>Reply-To: "BCEL Users List" <bcel-user <at> jakarta.apache.org>
>To: "BCEL Users List" <bcel-user <at> jakarta.apache.org>
>Subject: Re: class to JavaClass file
>Date: Mon, 31 Mar 2003 17:25:44 +0200
>
>
>Sometimes it can work on system classes too,
>classloader can not define system class, but you can try depricated
>"defineClass(byte[] b, int pos, int len)" method, I am not sure this will
>work for classes like Object or String,
>and it is no way to define classes for arrays.
>
>
> > Hi,
> > Im trying my class loader with BCEL, Im looking Constant pool of each
>class
> > used by my application, even system classes, for that I need to convert
> > class file ( which I get as result of findSystemClass() ) and convert it
(Continue reading)

Mark R. Diggory | 1 Apr 2003 04:50
Picon

Re: Generating method Code for setters

Search the BCEL mail list archives for previous emails by me. I had some 
discussions with the developers concerning ClassLoaders and BCEL back in 
the Fall/Early winter. They may help you.

-Mark Diggory

Mark Proctor (mproctor) wrote:

>I've downloaded org.apache.commons.jelly.tags.bcel; and successfully
>used that as the basis of my own setter and getter generators. I'm
>having problems understanding the Class loader stuff. There are a lot of
>methods there and I don't understand why you can't got to using
>Class.forName for the class you've just created, why all the other
>stuff?
> 
>public Object newInstance()
>public Object newInstance(ClassLoader loader)
>public Class defineClass()
>public Class defineClass(ClassLoader loader)
> 
>I would really appreciate it if someone could enlighten me.
> 
>Regards
> 
>Mark
>
>  
>
Juozas Baliuka | 1 Apr 2003 08:11
Picon

Re: class to JavaClass file


You can load class file as resource with system classloader, but you can not
to define it twice in the same classloader:

 JavaClass jclass =  new
ClassParser(Object.class.getResource(Object.class.getName().replace('.','/')
+ ".class")),Object.class.getName());

> What I am trying to do is change in constant pool of every class my
> application loads (not all but that also includes some library classes )
for
> that im trying to change instance of Class that findSystemClass() returns
> into that of JavaClass so that I can change in the constant pool and then
> use defineClass on it. ClassParser converts DataInputStream into
JavaClass,
> how can I make it to change Class into JavaClass.
>
> Thanks
> Gaurav Shankar
> CS Deptt , Rutgers University
>
>
>
>
>
>
> >From: "Juozas Baliuka" <baliuka <at> mwm.lt>
> >Reply-To: "BCEL Users List" <bcel-user <at> jakarta.apache.org>
> >To: "BCEL Users List" <bcel-user <at> jakarta.apache.org>
> >Subject: Re: class to JavaClass file
(Continue reading)

Mark R. Diggory | 1 Apr 2003 16:33
Picon

Re: Generating method Code for setters


Mark Proctor (mproctor) wrote:
> Mark,
> 
> I'm having to read up on classloaders and security - as it still doesn't
> mean much to me :( - I don't understand why you have to do
> myclass.newInstance - how can I get it to operate like a normal class so
> I can just do MyClass myclass = new MyClass().
> 
I think that may be tough to do because using "new" really requires that 
class to be present at "compile time", while the BCEL based class you've 
generated is only available during the "runtime" of JVM instance your 
created it in. Thats why you need to use newInstance at that point.

Now, if your creating the BCEL class and then saving it to a class file 
for future use in another "JVM" instance, I think you can use it with 
"new" because it really is just like any other class.

I could be incorrect on this, I will cross post tot he list to get some 
critique.

-MRD

> Do you use IRC or ICQ? 
> 

Nope, sorry, I pretty much stick to email.
Mark Proctor (mproctor | 1 Apr 2003 18:01
Picon
Favicon

RE: Generating method Code for setters

Could a variation on Class.forName be used - like with jdbc driver
loading - to then allow normal instance creation with "new". If the file
has to be in the classpath, could you "dump" the class and then use
Class.forName?

Mark

-----Original Message-----
From: Mark R. Diggory [mailto:mdiggory <at> latte.harvard.edu] 
Sent: 01 April 2003 15:34
To: Mark Proctor (mproctor); bcel-user <at> jakarta.apache.org
Subject: Re: Generating method Code for setters

Mark Proctor (mproctor) wrote:
> Mark,
> 
> I'm having to read up on classloaders and security - as it still
doesn't
> mean much to me :( - I don't understand why you have to do
> myclass.newInstance - how can I get it to operate like a normal class
so
> I can just do MyClass myclass = new MyClass().
> 
I think that may be tough to do because using "new" really requires that

class to be present at "compile time", while the BCEL based class you've

generated is only available during the "runtime" of JVM instance your 
created it in. Thats why you need to use newInstance at that point.

(Continue reading)

Mark Proctor (mproctor | 1 Apr 2003 22:17
Picon
Favicon

RE: Generating method Code for setters

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?

Mark
-----Original Message-----
From: Mark Proctor (mproctor) 
Sent: 01 April 2003 17:01
To: BCEL Users List; mdiggory <at> latte.harvard.edu
Subject: RE: Generating method Code for setters

(Continue reading)

Mark R. Diggory | 1 Apr 2003 23:39
Picon

Re: Generating method Code for setters

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 
(Continue reading)

gaurav shankar | 2 Apr 2003 04:41
Picon
Favicon

Re: class to JavaClass file

Hi,
This is a part of my ClassLoader , at  the end of this section I have result 
( an instance of Class ) which gets bytecode of system classes, I want to 
convert this into JavaClass. "object" in your example is an instance of 
Class or shall I say object and result of same type ..

public synchronized Class loadClass(String className, boolean resolveIt)
    	throws ClassNotFoundException {
        Class result;
        byte  classData[];
	JavaClass java_class;

        System.out.println("        >>>>>> Load class : "+className);

        /* Check our local cache of classes */
        result = (Class)classes.get(className);
        if (result != null) {
            System.out.println("        >>>>>> returning cached result.");
            return result;
        }

        /* Check with the primordial class loader */
        try {
            result = super.findSystemClass(className);
// >> I WANT TO CONVERT THIS result TO JavaClass <<<<

        } catch (ClassNotFoundException e) {
            System.out.println("        >>>>>> Not a system class.");
        }

(Continue reading)

Juozas Baliuka | 2 Apr 2003 12:39
Picon

Re: class to JavaClass file


----- Original Message -----
From: "gaurav shankar" <shankargaurav <at> hotmail.com>
To: <bcel-user <at> jakarta.apache.org>
Sent: Wednesday, April 02, 2003 4:41 AM
Subject: Re: class to JavaClass file

> Hi,
> This is a part of my ClassLoader , at  the end of this section I have
result
> ( an instance of Class ) which gets bytecode of system classes, I want to
> convert this into JavaClass. "object" in your example is an instance of
> Class or shall I say object and result of same type ..
>
>
> public synchronized Class loadClass(String className, boolean resolveIt)
>     throws ClassNotFoundException {
>         Class result;
>         byte  classData[];
> JavaClass java_class;
>
>         System.out.println("        >>>>>> Load class : "+className);
>
>         /* Check our local cache of classes */
>         result = (Class)classes.get(className);
>         if (result != null) {
>             System.out.println("        >>>>>> returning cached result.");
>             return result;
>         }
>
(Continue reading)


Gmane