You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by "Mark R. Diggory" <md...@latte.harvard.edu> on 2003/01/17 19:17:09 UTC

How to get instance of my new Class.

I've written code using the BCEL packages that produces a new Class.

I can see how to get the byte code or source of the be class and save it 
a file from the examples provided, however, I don't really want to do 
this yet. I'd like to jusat create a new instance of my class I've 
created in the current process in which I generatede it. Does anyone 
have suggestions on how I can do this?

thanks,
Mark


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: How to get instance of my new Class.

Posted by Andreas Schlapbach <sc...@iam.unibe.ch>.
And once the class is loaded into the VM, you can try something
like this:

     private Object instantiate(Class aClass, IForm arg) {
         // Whoever said that writing tests is fun, was wrong.
         Constructor cstr = null;
         Class par1 = null;
         try {
             par1 = Class.forName("ch.unibe.piccola.IForm");
         } catch (ClassNotFoundException cnfe) {cnfe.printStackTrace();}
         Class[] parameterTypes = {par1};
         try {
             cstr= aClass.getConstructor(parameterTypes);
         } catch (NoSuchMethodException nsme) {nsme.printStackTrace();}
         Object[] initArgs = {arg};

         Object obj = null;
         try {
             obj = cstr.newInstance(initArgs);
         } catch (Exception e) {e.printStackTrace();}

         return obj;
     }


Juozas Baliuka wrote:
> use ClassLoader.defineClass( byte[], int, int)
> 
> ----- Original Message -----
> From: "Mark R. Diggory" <md...@latte.harvard.edu>
> To: <bc...@jakarta.apache.org>
> Sent: Friday, January 17, 2003 8:17 PM
> Subject: How to get instance of my new Class.
> 
> 
> 
>>I've written code using the BCEL packages that produces a new Class.
>>
>>I can see how to get the byte code or source of the be class and save it
>>a file from the examples provided, however, I don't really want to do
>>this yet. I'd like to jusat create a new instance of my class I've
>>created in the current process in which I generatede it. Does anyone
>>have suggestions on how I can do this?
>>
>>thanks,
>>Mark
>>
>>
>>--
>>To unsubscribe, e-mail:
> 
> <ma...@jakarta.apache.org>
> 
>>For additional commands, e-mail:
> 
> <ma...@jakarta.apache.org>
> 
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 

-- 
Andreas Schlapbach      schlpbch@iam.unibe.ch
http://www.iam.unibe.ch/~schlpbch



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [OT - sort of] Primitives hurt!

Posted by Juozas Baliuka <ba...@centras.lt>.
paste code from this page, it handles arrays too:
http://cglib.sourceforge.net/xref/net/sf/cglib/ReflectUtils.html#124

> I'm stuck at an issue that maybe someone on the list can help
>
>
> The nice thing about the Type class is that I can hand it a "Class"
> object and get back the appropriate type.
>
> public static getType(Class cls)...
>
> I need to keep generic-ness in my application (which is a small BCEL
> taglibrary for Jelly to manipulate Beans). This means I have something
like
>
> *
> <bcel:property name="foo" type="java.lang.String" ...>
> *
>
> so I'm passing around String representations of Classes to methods like
>
> *
> public void addProperty(String name, String type,...){
>
> Type propType = Type.getType(Class.forName(type));
> ...
> *
>
> which works great for regular Classes, unfortunately it doesn't work for
>   primitive types. Calls like:
>
> *
> Class.forName("int")
> *
>
> just don't work, which is frustrating because for the reciprical case:
>
> *
> java.lang.Object.class.toString() returns "java.lang.Object"
> java.lang.Object.getClass().getName() returns "java.lang.Object"
> Class.forName("java.lang.Object").toString() returns "java.lang.Object"
> *
>
> while
>
> *
> int.class.toString() returns "int"
> int.getClass().getName() returns "int"
> *
>
> but
>
> *
> Class.forName("int").toString()
> *
>
> fails with a ClassNotFoundException.
>
> This is something that sucks about primitive type names in the Java API.
> My question, is there a simple way in BCEL to get around this issue, I
> could require the type parameter of my method to require a signature,
> but that is hard for users (who don't know what they are) to use my
> tools, I'd rather be able to just capture the Class object for any
> String representation of a primitive type ("int", "double", "byte" ...)
> and do it simply and elegantly.
>
> Thanks for any comments,
> Mark
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


[OT - sort of] Primitives hurt!

Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
I'm stuck at an issue that maybe someone on the list can help


The nice thing about the Type class is that I can hand it a "Class" 
object and get back the appropriate type.

public static getType(Class cls)...

I need to keep generic-ness in my application (which is a small BCEL 
taglibrary for Jelly to manipulate Beans). This means I have something like

*
<bcel:property name="foo" type="java.lang.String" ...>
*

so I'm passing around String representations of Classes to methods like

*
public void addProperty(String name, String type,...){

	Type propType = Type.getType(Class.forName(type));
	...
*

which works great for regular Classes, unfortunately it doesn't work for 
  primitive types. Calls like:

*
Class.forName("int")
*

just don't work, which is frustrating because for the reciprical case:

*
java.lang.Object.class.toString() returns "java.lang.Object"
java.lang.Object.getClass().getName() returns "java.lang.Object"
Class.forName("java.lang.Object").toString() returns "java.lang.Object"
*

while

*
int.class.toString() returns "int"
int.getClass().getName() returns "int"
*

but

*
Class.forName("int").toString()
*

fails with a ClassNotFoundException.

This is something that sucks about primitive type names in the Java API. 
My question, is there a simple way in BCEL to get around this issue, I 
could require the type parameter of my method to require a signature, 
but that is hard for users (who don't know what they are) to use my 
tools, I'd rather be able to just capture the Class object for any 
String representation of a primitive type ("int", "double", "byte" ...) 
and do it simply and elegantly.

Thanks for any comments,
Mark


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Which version of regexp is suggested.

Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
I'm using ant to retrieve the dependencies for my project which is using 
bcel, is there a "recommended version" of regexp that you are currently 
using? If its available from http://www.ibiblio.org/maven/regexp/jars/ 
I'll grab it there.

-Thanks,
Mark



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: How to get instance of my new Class.

Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
Ok, that was alot of fun, this package will definitly do what I need it 
to! Here's my experiment classes if your interested.

thanks for the help Juozas and Andreas,
Mark


Juozas Baliuka wrote:

>use "Method.setAccessible(true)"
>sample can be faund in this code:
>http://cglib.sourceforge.net/xref/net/sf/cglib/CodeGenerator.html#166
>
>----- Original Message -----
>From: "Mark R. Diggory" <md...@latte.harvard.edu>
>To: "BCEL Users List" <bc...@jakarta.apache.org>
>Sent: Friday, January 17, 2003 8:51 PM
>Subject: Re: How to get instance of my new Class.
>
>
>  
>
>>Thank you for the info, however, I'm slightly confused,
>>ClassLoader.defineClass(...) seems to be protected in both
>>java.lang.ClassLoader and org.apache.bcel.util.ClassLoader. Is there a
>>chance theres a decent example you could point me at in the bcel library
>>examples to outline how to do this.
>>
>>-Mark
>>
>>
>>Juozas Baliuka wrote:
>>
>>    
>>
>>>use ClassLoader.defineClass( byte[], int, int)
>>>
>>>----- Original Message -----
>>>From: "Mark R. Diggory" <md...@latte.harvard.edu>
>>>To: <bc...@jakarta.apache.org>
>>>Sent: Friday, January 17, 2003 8:17 PM
>>>Subject: How to get instance of my new Class.
>>>
>>>
>>>
>>>
>>>      
>>>
>>>>I've written code using the BCEL packages that produces a new Class.
>>>>
>>>>I can see how to get the byte code or source of the be class and save it
>>>>a file from the examples provided, however, I don't really want to do
>>>>this yet. I'd like to jusat create a new instance of my class I've
>>>>created in the current process in which I generatede it. Does anyone
>>>>have suggestions on how I can do this?
>>>>
>>>>thanks,
>>>>Mark
>>>>
>>>>
>>>>--
>>>>To unsubscribe, e-mail:
>>>>
>>>>
>>>>        
>>>>
>>><ma...@jakarta.apache.org>
>>>
>>>
>>>      
>>>
>>>>For additional commands, e-mail:
>>>>
>>>>
>>>>        
>>>>
>>><ma...@jakarta.apache.org>
>>>
>>>
>>>
>>>
>>>--
>>>To unsubscribe, e-mail:
>>>      
>>>
><ma...@jakarta.apache.org>
>  
>
>>>For additional commands, e-mail:
>>>      
>>>
><ma...@jakarta.apache.org>
>  
>
>>>
>>>      
>>>
>>
>>--
>>To unsubscribe, e-mail:
>>    
>>
><ma...@jakarta.apache.org>
>  
>
>>For additional commands, e-mail:
>>    
>>
><ma...@jakarta.apache.org>
>  
>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>  
>


Re: How to get instance of my new Class.

Posted by Juozas Baliuka <ba...@centras.lt>.
use "Method.setAccessible(true)"
sample can be faund in this code:
http://cglib.sourceforge.net/xref/net/sf/cglib/CodeGenerator.html#166

----- Original Message -----
From: "Mark R. Diggory" <md...@latte.harvard.edu>
To: "BCEL Users List" <bc...@jakarta.apache.org>
Sent: Friday, January 17, 2003 8:51 PM
Subject: Re: How to get instance of my new Class.


> Thank you for the info, however, I'm slightly confused,
> ClassLoader.defineClass(...) seems to be protected in both
> java.lang.ClassLoader and org.apache.bcel.util.ClassLoader. Is there a
> chance theres a decent example you could point me at in the bcel library
> examples to outline how to do this.
>
> -Mark
>
>
> Juozas Baliuka wrote:
>
> >use ClassLoader.defineClass( byte[], int, int)
> >
> >----- Original Message -----
> >From: "Mark R. Diggory" <md...@latte.harvard.edu>
> >To: <bc...@jakarta.apache.org>
> >Sent: Friday, January 17, 2003 8:17 PM
> >Subject: How to get instance of my new Class.
> >
> >
> >
> >
> >>I've written code using the BCEL packages that produces a new Class.
> >>
> >>I can see how to get the byte code or source of the be class and save it
> >>a file from the examples provided, however, I don't really want to do
> >>this yet. I'd like to jusat create a new instance of my class I've
> >>created in the current process in which I generatede it. Does anyone
> >>have suggestions on how I can do this?
> >>
> >>thanks,
> >>Mark
> >>
> >>
> >>--
> >>To unsubscribe, e-mail:
> >>
> >>
> ><ma...@jakarta.apache.org>
> >
> >
> >>For additional commands, e-mail:
> >>
> >>
> ><ma...@jakarta.apache.org>
> >
> >
> >
> >
> >--
> >To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> >For additional commands, e-mail:
<ma...@jakarta.apache.org>
> >
> >
> >
>
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: How to get instance of my new Class.

Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
Thank you for the info, however, I'm slightly confused, 
ClassLoader.defineClass(...) seems to be protected in both 
java.lang.ClassLoader and org.apache.bcel.util.ClassLoader. Is there a 
chance theres a decent example you could point me at in the bcel library 
examples to outline how to do this.

-Mark


Juozas Baliuka wrote:

>use ClassLoader.defineClass( byte[], int, int)
>
>----- Original Message -----
>From: "Mark R. Diggory" <md...@latte.harvard.edu>
>To: <bc...@jakarta.apache.org>
>Sent: Friday, January 17, 2003 8:17 PM
>Subject: How to get instance of my new Class.
>
>
>  
>
>>I've written code using the BCEL packages that produces a new Class.
>>
>>I can see how to get the byte code or source of the be class and save it
>>a file from the examples provided, however, I don't really want to do
>>this yet. I'd like to jusat create a new instance of my class I've
>>created in the current process in which I generatede it. Does anyone
>>have suggestions on how I can do this?
>>
>>thanks,
>>Mark
>>
>>
>>--
>>To unsubscribe, e-mail:
>>    
>>
><ma...@jakarta.apache.org>
>  
>
>>For additional commands, e-mail:
>>    
>>
><ma...@jakarta.apache.org>
>  
>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>  
>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: How to get instance of my new Class.

Posted by Juozas Baliuka <ba...@centras.lt>.
use ClassLoader.defineClass( byte[], int, int)

----- Original Message -----
From: "Mark R. Diggory" <md...@latte.harvard.edu>
To: <bc...@jakarta.apache.org>
Sent: Friday, January 17, 2003 8:17 PM
Subject: How to get instance of my new Class.


> I've written code using the BCEL packages that produces a new Class.
>
> I can see how to get the byte code or source of the be class and save it
> a file from the examples provided, however, I don't really want to do
> this yet. I'd like to jusat create a new instance of my class I've
> created in the current process in which I generatede it. Does anyone
> have suggestions on how I can do this?
>
> thanks,
> Mark
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>