You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@harmony.apache.org by Andrew Zhang <zh...@gmail.com> on 2007/11/15 05:58:14 UTC

[classlib][lang] Is it possible to new an object without invoking any constructor?

Hi,

Is it possible to new an object without invoking any constructor? I know the
question is a bit weird. What I want is to create an object with all
null/default value for its fields.
Reseting all fields to null by reflection is a possible solution, but is
there any existing special method (no matter it's low level or Harmony
specific) which can achieve this goal?
Thanks!


-- 
Best regards,
Andrew Zhang

http://zhanghuangzhu.blogspot.com/

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Alexey Varlamov <al...@gmail.com>.
2007/11/15, Nathan Beyer <nd...@apache.org>:
> Do you mean this?
>
> class MyObject {
>  String field;
>  MyObject(){
>      super();
>  }
> }

or simply "class MyObject { String field; }" - just as you wanted, no
any constructor ;)

>
> MyObject mo = new MyObject();
>
> With regard to fields being in their default state, that's what
> happens by default. The only thing that changes this is if you inline
> field assigment with the declaration or assign the fields in the
> constructor.
>
> As for creating a new instance without calling a constructor, that's
> what happens when an instance of something is de-serialized. No
> constructor is called; just the readObject() and related methods are
> invoked.
>
> -Nathan
>
> On Nov 14, 2007 11:58 PM, Andrew Zhang <zh...@gmail.com> wrote:
> > Hi,
> >
> > Is it possible to new an object without invoking any constructor? I know the
> > question is a bit weird. What I want is to create an object with all
> > null/default value for its fields.
> > Reseting all fields to null by reflection is a possible solution, but is
> > there any existing special method (no matter it's low level or Harmony
> > specific) which can achieve this goal?
> > Thanks!
> >
> >
> > --
> > Best regards,
> > Andrew Zhang
> >
> > http://zhanghuangzhu.blogspot.com/
> >
>

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Andrew Zhang <zh...@gmail.com>.
On Nov 15, 2007 1:18 PM, Andrew Zhang <zh...@gmail.com> wrote:

>
>
> On Nov 15, 2007 1:07 PM, Nathan Beyer <nd...@apache.org> wrote:
>
> > Do you mean this?
> >
> > class MyObject {
> >  String field;
> >  MyObject(){
> >      super();
> >  }
> > }
> >
> > MyObject mo = new MyObject();
> >
> > With regard to fields being in their default state, that's what
> > happens by default. The only thing that changes this is if you inline
> > field assigment with the declaration or assign the fields in the
> > constructor.
>
>
> Hi Nathan,
>
> I want an object with all null values for all reference fields, and
> default value(0/false) for primitive fields, even there's a constructor:
>
> public class MyObject {
>   int i;
>   String s;
>   B b;
>   MyObject() { i = 100; s = "hello";  b= new B(); }
> }
>
> I want to have a MyObject myObject whose i = 0, s = null and b = null.
> Yes, it's very like the initial state of de-serialized. What I have is the
> class definition (e.g class MyObject above), and I want to "new" an
> instance with all null/0 value of MyObject.class by some way. Is it
> possible? Thanks!
>
> >
> >
> > As for creating a new instance without calling a constructor, that's
> > what happens when an instance of something is de-serialized. No
> > constructor is called; just the readObject() and related methods are
>
>
> But I don't have an FileInputStream instance...
>

Sorry, I mean ObjectInputStream...

>
>
> > invoked.
> >
> > -Nathan
> >
> > On Nov 14, 2007 11:58 PM, Andrew Zhang <zh...@gmail.com> wrote:
> > > Hi,
> > >
> > > Is it possible to new an object without invoking any constructor? I
> > know the
> > > question is a bit weird. What I want is to create an object with all
> > > null/default value for its fields.
> > > Reseting all fields to null by reflection is a possible solution, but
> > is
> > > there any existing special method (no matter it's low level or Harmony
> > > specific) which can achieve this goal?
> > > Thanks!
> > >
> > >
> > > --
> > > Best regards,
> > > Andrew Zhang
> > >
> > > http://zhanghuangzhu.blogspot.com/
> > >
> >
>
>
>
> --
>
> Best regards,
> Andrew Zhang
>
> http://zhanghuangzhu.blogspot.com/
>



-- 
Best regards,
Andrew Zhang

http://zhanghuangzhu.blogspot.com/

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Andrew Zhang <zh...@gmail.com>.
On Nov 15, 2007 1:26 PM, Alexey Varlamov <al...@gmail.com>
wrote:

> 2007/11/15, Andrew Zhang <zh...@gmail.com>:
> > On Nov 15, 2007 1:07 PM, Nathan Beyer <nd...@apache.org> wrote:
> >
> > > Do you mean this?
> > >
> > > class MyObject {
> > >  String field;
> > >  MyObject(){
> > >      super();
> > >  }
> > > }
> > >
> > > MyObject mo = new MyObject();
> > >
> > > With regard to fields being in their default state, that's what
> > > happens by default. The only thing that changes this is if you inline
> > > field assigment with the declaration or assign the fields in the
> > > constructor.
>

According to the spec description of  AllocObject,  it may fulfill my
requirement?
"jobject AllocObject(JNIEnv *env, jclass clazz);
Allocates a new Java object without invoking any of the constructors for the
object. Returns a reference to the object.
The clazz argument must not refer to an array class. "


> >
> >
> > Hi Nathan,
> >
> > I want an object with all null values for all reference fields, and
> default
> > value(0/false) for primitive fields, even there's a constructor:
> >
> > public class MyObject {
> >  int i;
> >  String s;
> >  B b;
> >  MyObject() { i = 100; s = "hello";  b= new B(); }
> > }
> >
> > I want to have a MyObject myObject whose i = 0, s = null and b = null.
> > Yes, it's very like the initial state of de-serialized. What I have is
> the
> > class definition (e.g class MyObject above), and I want to "new" an
> instance
> > with all null/0 value of MyObject.class by some way. Is it possible?
> Thanks!
>
> Only via reflection in general case. Deserialization would work only
> if explicitly supported by the class, and no less tricky to use.
> Moreover custom readObject() may reject nullified object, say, due to
> constraints/invariant violation - no real difference to normal c-tor.
>
> >
> > >
> > >
> > > As for creating a new instance without calling a constructor, that's
> > > what happens when an instance of something is de-serialized. No
> > > constructor is called; just the readObject() and related methods are
> >
> >
> > But I don't have an FileInputStream instance...
> >
> > >
> > > invoked.
> > >
> > > -Nathan
> > >
> > > On Nov 14, 2007 11:58 PM, Andrew Zhang <zh...@gmail.com>
> wrote:
> > > > Hi,
> > > >
> > > > Is it possible to new an object without invoking any constructor? I
> know
> > > the
> > > > question is a bit weird. What I want is to create an object with all
> > > > null/default value for its fields.
> > > > Reseting all fields to null by reflection is a possible solution,
> but is
> > > > there any existing special method (no matter it's low level or
> Harmony
> > > > specific) which can achieve this goal?
> > > > Thanks!
> > > >
> > > >
> > > > --
> > > > Best regards,
> > > > Andrew Zhang
> > > >
> > > > http://zhanghuangzhu.blogspot.com/
> > > >
> > >
> >
> >
> >
> > --
> > Best regards,
> > Andrew Zhang
> >
> > http://zhanghuangzhu.blogspot.com/
> >
>



-- 
Best regards,
Andrew Zhang

http://zhanghuangzhu.blogspot.com/

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Alexey Varlamov <al...@gmail.com>.
2007/11/15, Andrew Zhang <zh...@gmail.com>:
> On Nov 15, 2007 1:07 PM, Nathan Beyer <nd...@apache.org> wrote:
>
> > Do you mean this?
> >
> > class MyObject {
> >  String field;
> >  MyObject(){
> >      super();
> >  }
> > }
> >
> > MyObject mo = new MyObject();
> >
> > With regard to fields being in their default state, that's what
> > happens by default. The only thing that changes this is if you inline
> > field assigment with the declaration or assign the fields in the
> > constructor.
>
>
> Hi Nathan,
>
> I want an object with all null values for all reference fields, and default
> value(0/false) for primitive fields, even there's a constructor:
>
> public class MyObject {
>  int i;
>  String s;
>  B b;
>  MyObject() { i = 100; s = "hello";  b= new B(); }
> }
>
> I want to have a MyObject myObject whose i = 0, s = null and b = null.
> Yes, it's very like the initial state of de-serialized. What I have is the
> class definition (e.g class MyObject above), and I want to "new" an instance
> with all null/0 value of MyObject.class by some way. Is it possible? Thanks!

Only via reflection in general case. Deserialization would work only
if explicitly supported by the class, and no less tricky to use.
Moreover custom readObject() may reject nullified object, say, due to
constraints/invariant violation - no real difference to normal c-tor.

>
> >
> >
> > As for creating a new instance without calling a constructor, that's
> > what happens when an instance of something is de-serialized. No
> > constructor is called; just the readObject() and related methods are
>
>
> But I don't have an FileInputStream instance...
>
> >
> > invoked.
> >
> > -Nathan
> >
> > On Nov 14, 2007 11:58 PM, Andrew Zhang <zh...@gmail.com> wrote:
> > > Hi,
> > >
> > > Is it possible to new an object without invoking any constructor? I know
> > the
> > > question is a bit weird. What I want is to create an object with all
> > > null/default value for its fields.
> > > Reseting all fields to null by reflection is a possible solution, but is
> > > there any existing special method (no matter it's low level or Harmony
> > > specific) which can achieve this goal?
> > > Thanks!
> > >
> > >
> > > --
> > > Best regards,
> > > Andrew Zhang
> > >
> > > http://zhanghuangzhu.blogspot.com/
> > >
> >
>
>
>
> --
> Best regards,
> Andrew Zhang
>
> http://zhanghuangzhu.blogspot.com/
>

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Andrew Zhang <zh...@gmail.com>.
On Nov 15, 2007 1:07 PM, Nathan Beyer <nd...@apache.org> wrote:

> Do you mean this?
>
> class MyObject {
>  String field;
>  MyObject(){
>      super();
>  }
> }
>
> MyObject mo = new MyObject();
>
> With regard to fields being in their default state, that's what
> happens by default. The only thing that changes this is if you inline
> field assigment with the declaration or assign the fields in the
> constructor.


Hi Nathan,

I want an object with all null values for all reference fields, and default
value(0/false) for primitive fields, even there's a constructor:

public class MyObject {
  int i;
  String s;
  B b;
  MyObject() { i = 100; s = "hello";  b= new B(); }
}

I want to have a MyObject myObject whose i = 0, s = null and b = null.
Yes, it's very like the initial state of de-serialized. What I have is the
class definition (e.g class MyObject above), and I want to "new" an instance
with all null/0 value of MyObject.class by some way. Is it possible? Thanks!

>
>
> As for creating a new instance without calling a constructor, that's
> what happens when an instance of something is de-serialized. No
> constructor is called; just the readObject() and related methods are


But I don't have an FileInputStream instance...

>
> invoked.
>
> -Nathan
>
> On Nov 14, 2007 11:58 PM, Andrew Zhang <zh...@gmail.com> wrote:
> > Hi,
> >
> > Is it possible to new an object without invoking any constructor? I know
> the
> > question is a bit weird. What I want is to create an object with all
> > null/default value for its fields.
> > Reseting all fields to null by reflection is a possible solution, but is
> > there any existing special method (no matter it's low level or Harmony
> > specific) which can achieve this goal?
> > Thanks!
> >
> >
> > --
> > Best regards,
> > Andrew Zhang
> >
> > http://zhanghuangzhu.blogspot.com/
> >
>



-- 
Best regards,
Andrew Zhang

http://zhanghuangzhu.blogspot.com/

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Nathan Beyer <nd...@apache.org>.
Do you mean this?

class MyObject {
  String field;
  MyObject(){
      super();
  }
}

MyObject mo = new MyObject();

With regard to fields being in their default state, that's what
happens by default. The only thing that changes this is if you inline
field assigment with the declaration or assign the fields in the
constructor.

As for creating a new instance without calling a constructor, that's
what happens when an instance of something is de-serialized. No
constructor is called; just the readObject() and related methods are
invoked.

-Nathan

On Nov 14, 2007 11:58 PM, Andrew Zhang <zh...@gmail.com> wrote:
> Hi,
>
> Is it possible to new an object without invoking any constructor? I know the
> question is a bit weird. What I want is to create an object with all
> null/default value for its fields.
> Reseting all fields to null by reflection is a possible solution, but is
> there any existing special method (no matter it's low level or Harmony
> specific) which can achieve this goal?
> Thanks!
>
>
> --
> Best regards,
> Andrew Zhang
>
> http://zhanghuangzhu.blogspot.com/
>

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Andrew Zhang <zh...@gmail.com>.
On Nov 15, 2007 1:08 PM, Tony Wu <wu...@gmail.com> wrote:

> If you have an object already and it is cloneable, you can clone it
> without invoking any constructor :)


Hi Tony,

No, I don't have an object... I only have the definition of the class... I'm
looking for a method whose behavior is similar as

Object newNullObject(Class c)  {
   get a constructor
   new an object
   set all fields to null/0/false
   return the instance; // the object is like the initial state of
de-serialize as Nathan pointed out.
}

>
>
> On 11/15/07, Andrew Zhang <zh...@gmail.com> wrote:
> > Hi,
> >
> > Is it possible to new an object without invoking any constructor? I know
> the
> > question is a bit weird. What I want is to create an object with all
> > null/default value for its fields.
> > Reseting all fields to null by reflection is a possible solution, but is
> > there any existing special method (no matter it's low level or Harmony
> > specific) which can achieve this goal?
> > Thanks!
> >
> >
> > --
> > Best regards,
> > Andrew Zhang
> >
> > http://zhanghuangzhu.blogspot.com/
> >
>
>
> --
> Tony Wu
> China Software Development Lab, IBM
>



-- 
Best regards,
Andrew Zhang

http://zhanghuangzhu.blogspot.com/

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Tony Wu <wu...@gmail.com>.
If you have an object already and it is cloneable, you can clone it
without invoking any constructor :)

On 11/15/07, Andrew Zhang <zh...@gmail.com> wrote:
> Hi,
>
> Is it possible to new an object without invoking any constructor? I know the
> question is a bit weird. What I want is to create an object with all
> null/default value for its fields.
> Reseting all fields to null by reflection is a possible solution, but is
> there any existing special method (no matter it's low level or Harmony
> specific) which can achieve this goal?
> Thanks!
>
>
> --
> Best regards,
> Andrew Zhang
>
> http://zhanghuangzhu.blogspot.com/
>


-- 
Tony Wu
China Software Development Lab, IBM

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Alexey Varlamov <al...@gmail.com>.
2007/11/15, Andrew Zhang <zh...@gmail.com>:
> On Nov 15, 2007 2:31 PM, Jimmy,Jing Lv <fi...@gmail.com> wrote:
>
> > 2007/11/15, Andrew Zhang <zh...@gmail.com>:
> > > On Nov 15, 2007 1:22 PM, Jimmy,Jing Lv <fi...@gmail.com> wrote:
> > >
> > > > A simple thought is using the JNI call AllocObject(JNIEnv *env, jclass
> > > > clazz);
> > > > however we need to implement a native api then :)
> > > >
> > >
> > > Thanks Jimmy!
> > >
> >
> > :)
> >
> > > AllocObject looks like what I want. ObjectInputStream#newInstance is
> > even
> > > closer (it has defined a native method). Is it possible to refactor?
> > > Comments? Thanks!
> > >
> >
> > Hmm, it seems the current native method newInstance is used for
> > ObjectInputStream itself, so refactor it may not be a good idea (will
> > surely break Harmony function) .
>
>
> No, I mean add a similar method in the ObjectInputStream, e.g private static
> native allocObject(Class <?> c);
>
> Or put it in some util class in o.a.h package. Any comments or objections?

But why do you need this in Harmony?

>
> I suggest define it in some utility
> > class or your own class :)
> >
> > > Java_java_io_ObjectInputStream_newInstance (JNIEnv * env, jclass clazz,
> > >                                             jobject instantiationClass,
> > >                                             jobject constructorClass)
> > > {
> > >   jmethodID mid =
> > >     (*env)->GetMethodID (env, constructorClass, "<init>", "()V");
> > >
> > >   if (mid == 0)
> > >     {
> > >       /* Cant newInstance,No empty constructor... */
> > >       return (jobject) 0;
> > >     }
> > >   else
> > >     {
> > >       /* Instantiate an object of a given class and constuct it using
> > >          the constructor of the other class. */
> > >       jobject obj;
> > >       obj = (*env)->AllocObject(env, instantiationClass);  // I want
> > this
> > > method :)
> > >       if (obj != NULL) {
> > >         (*env)->CallNonvirtualVoidMethod(env, obj, constructorClass,
> > mid);
> > >       }
> > >       return obj;
> > >     }
> > >
> > > }
> > >
> > >
> > > > 2007/11/15, Andrew Zhang <zh...@gmail.com>:
> > > > > Hi,
> > > > >
> > > > > Is it possible to new an object without invoking any constructor? I
> > know
> > > > the
> > > > > question is a bit weird. What I want is to create an object with all
> > > > > null/default value for its fields.
> > > > > Reseting all fields to null by reflection is a possible solution,
> > but is
> > > > > there any existing special method (no matter it's low level or
> > Harmony
> > > > > specific) which can achieve this goal?
> > > > > Thanks!
> > > > >
> > > > >
> > > > > --
> > > > > Best regards,
> > > > > Andrew Zhang
> > > > >
> > > > > http://zhanghuangzhu.blogspot.com/
> > > > >
> > > >
> > > >
> > > > --
> > > >
> > > > Best Regards!
> > > >
> > > > Jimmy, Jing Lv
> > > > China Software Development Lab, IBM
> > > >
> > >
> > >
> > >
> > > --
> > > Best regards,
> > > Andrew Zhang
> > >
> > > http://zhanghuangzhu.blogspot.com/
> > >
> >
> >
> > --
> >
> > Best Regards!
> >
> > Jimmy, Jing Lv
> > China Software Development Lab, IBM
> >
>
>
>
> --
> Best regards,
> Andrew Zhang
>
> http://zhanghuangzhu.blogspot.com/
>

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Nathan Beyer <nb...@gmail.com>.
On Nov 15, 2007 3:08 AM, Andrew Zhang <zh...@gmail.com> wrote:
>
> Hi,
>
> Can we make ObjectAccessor.getInstance() public? It's package friend now...

Why? What's the use case for this?

-Nathan

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Andrew Zhang <zh...@gmail.com>.
On Nov 15, 2007 3:37 PM, Andrew Zhang <zh...@gmail.com> wrote:

>
>
> On Nov 15, 2007 2:46 PM, Andrew Zhang <zh...@gmail.com> wrote:
>
> >
> >
> > On Nov 15, 2007 2:31 PM, Jimmy,Jing Lv <fi...@gmail.com> wrote:
> >
> > > 2007/11/15, Andrew Zhang <zh...@gmail.com>:
> > > > On Nov 15, 2007 1:22 PM, Jimmy,Jing Lv < firepure@gmail.com> wrote:
> > > >
> > > > > A simple thought is using the JNI call AllocObject(JNIEnv *env,
> > > jclass
> > > > > clazz);
> > > > > however we need to implement a native api then :)
> > > > >
> > > >
> > > > Thanks Jimmy!
> > > >
> > >
> > > :)
> > >
> > > > AllocObject looks like what I want. ObjectInputStream#newInstance is
> > > even
> > > > closer (it has defined a native method). Is it possible to refactor?
> > > > Comments? Thanks!
> > > >
> > >
> > > Hmm, it seems the current native method newInstance is used for
> > > ObjectInputStream itself, so refactor it may not be a good idea (will
> > > surely break Harmony function) .
> >
> >
> > No, I mean add a similar method in the ObjectInputStream, e.g private
> > static native allocObject(Class <?> c);
> >
> > Or put it in some util class in o.a.h package. Any comments or
> > objections?
> >
>
> Found it! It's already in misc module.
>
>
> working_classlib\modules\misc\src\main\java\org\apache\harmony\misc\accessors\ObjectAccessor.java
>
> public final native Object allocateObject(Class c);
>

Hi,

Can we make ObjectAccessor.getInstance() public? It's package friend now...


>
>
> >
> > I suggest define it in some utility
> > > class or your own class :)
> > >
> > > > Java_java_io_ObjectInputStream_newInstance (JNIEnv * env, jclass
> > > clazz,
> > > >                                             jobject
> > > instantiationClass,
> > > >                                             jobject
> > > constructorClass)
> > > > {
> > > >   jmethodID mid =
> > > >     (*env)->GetMethodID (env, constructorClass, "<init>", "()V");
> > > >
> > > >   if (mid == 0)
> > > >     {
> > > >       /* Cant newInstance,No empty constructor... */
> > > >       return (jobject) 0;
> > > >     }
> > > >   else
> > > >     {
> > > >       /* Instantiate an object of a given class and constuct it
> > > using
> > > >          the constructor of the other class. */
> > > >       jobject obj;
> > > >       obj = (*env)->AllocObject(env, instantiationClass);  // I want
> > > this
> > > > method :)
> > > >       if (obj != NULL) {
> > > >         (*env)->CallNonvirtualVoidMethod(env, obj, constructorClass,
> > > mid);
> > > >       }
> > > >       return obj;
> > > >     }
> > > >
> > > > }
> > > >
> > > >
> > > > > 2007/11/15, Andrew Zhang < zhanghuangzhu@gmail.com>:
> > > > > > Hi,
> > > > > >
> > > > > > Is it possible to new an object without invoking any
> > > constructor? I know
> > > > > the
> > > > > > question is a bit weird. What I want is to create an object with
> > > all
> > > > > > null/default value for its fields.
> > > > > > Reseting all fields to null by reflection is a possible
> > > solution, but is
> > > > > > there any existing special method (no matter it's low level or
> > > Harmony
> > > > > > specific) which can achieve this goal?
> > > > > > Thanks!
> > > > > >
> > > > > >
> > > > > > --
> > > > > > Best regards,
> > > > > > Andrew Zhang
> > > > > >
> > > > > > http://zhanghuangzhu.blogspot.com/
> > > > > >
> > > > >
> > > > >
> > > > > --
> > > > >
> > > > > Best Regards!
> > > > >
> > > > > Jimmy, Jing Lv
> > > > > China Software Development Lab, IBM
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Best regards,
> > > > Andrew Zhang
> > > >
> > > > http://zhanghuangzhu.blogspot.com/
> > > >
> > >
> > >
> > > --
> > >
> > > Best Regards!
> > >
> > > Jimmy, Jing Lv
> > > China Software Development Lab, IBM
> > >
> >
> >
> >
> > --
> >
> > Best regards,
> > Andrew Zhang
> >
> > http://zhanghuangzhu.blogspot.com/
> >
>
>
>
> --
>
> Best regards,
> Andrew Zhang
>
> http://zhanghuangzhu.blogspot.com/
>



-- 
Best regards,
Andrew Zhang

http://zhanghuangzhu.blogspot.com/

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Sean Qiu <se...@gmail.com>.
Interesting subject.
It seems harmony has so many functionalities to explore.

2007/11/15, Andrew Zhang <zh...@gmail.com>:
>
> On Nov 15, 2007 2:46 PM, Andrew Zhang <zh...@gmail.com> wrote:
>
> >
> >
> > On Nov 15, 2007 2:31 PM, Jimmy,Jing Lv <fi...@gmail.com> wrote:
> >
> > > 2007/11/15, Andrew Zhang <zh...@gmail.com>:
> > > > On Nov 15, 2007 1:22 PM, Jimmy,Jing Lv < firepure@gmail.com> wrote:
> > > >
> > > > > A simple thought is using the JNI call AllocObject(JNIEnv *env,
> > > jclass
> > > > > clazz);
> > > > > however we need to implement a native api then :)
> > > > >
> > > >
> > > > Thanks Jimmy!
> > > >
> > >
> > > :)
> > >
> > > > AllocObject looks like what I want. ObjectInputStream#newInstance is
> > > even
> > > > closer (it has defined a native method). Is it possible to refactor?
> > > > Comments? Thanks!
> > > >
> > >
> > > Hmm, it seems the current native method newInstance is used for
> > > ObjectInputStream itself, so refactor it may not be a good idea (will
> > > surely break Harmony function) .
> >
> >
> > No, I mean add a similar method in the ObjectInputStream, e.g private
> > static native allocObject(Class <?> c);
> >
> > Or put it in some util class in o.a.h package. Any comments or
> objections?
> >
>
> Found it! It's already in misc module.
>
>
> working_classlib\modules\misc\src\main\java\org\apache\harmony\misc\accessors\ObjectAccessor.java
>
> public final native Object allocateObject(Class c);
>
>
> >
> > I suggest define it in some utility
> > > class or your own class :)
> > >
> > > > Java_java_io_ObjectInputStream_newInstance (JNIEnv * env, jclass
> > > clazz,
> > > >                                             jobject
> > > instantiationClass,
> > > >                                             jobject
> constructorClass)
> > > > {
> > > >   jmethodID mid =
> > > >     (*env)->GetMethodID (env, constructorClass, "<init>", "()V");
> > > >
> > > >   if (mid == 0)
> > > >     {
> > > >       /* Cant newInstance,No empty constructor... */
> > > >       return (jobject) 0;
> > > >     }
> > > >   else
> > > >     {
> > > >       /* Instantiate an object of a given class and constuct it
> using
> > > >          the constructor of the other class. */
> > > >       jobject obj;
> > > >       obj = (*env)->AllocObject(env, instantiationClass);  // I want
> > > this
> > > > method :)
> > > >       if (obj != NULL) {
> > > >         (*env)->CallNonvirtualVoidMethod(env, obj, constructorClass,
> > > mid);
> > > >       }
> > > >       return obj;
> > > >     }
> > > >
> > > > }
> > > >
> > > >
> > > > > 2007/11/15, Andrew Zhang < zhanghuangzhu@gmail.com>:
> > > > > > Hi,
> > > > > >
> > > > > > Is it possible to new an object without invoking any
> constructor?
> > > I know
> > > > > the
> > > > > > question is a bit weird. What I want is to create an object with
> > > all
> > > > > > null/default value for its fields.
> > > > > > Reseting all fields to null by reflection is a possible
> solution,
> > > but is
> > > > > > there any existing special method (no matter it's low level or
> > > Harmony
> > > > > > specific) which can achieve this goal?
> > > > > > Thanks!
> > > > > >
> > > > > >
> > > > > > --
> > > > > > Best regards,
> > > > > > Andrew Zhang
> > > > > >
> > > > > > http://zhanghuangzhu.blogspot.com/
> > > > > >
> > > > >
> > > > >
> > > > > --
> > > > >
> > > > > Best Regards!
> > > > >
> > > > > Jimmy, Jing Lv
> > > > > China Software Development Lab, IBM
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Best regards,
> > > > Andrew Zhang
> > > >
> > > > http://zhanghuangzhu.blogspot.com/
> > > >
> > >
> > >
> > > --
> > >
> > > Best Regards!
> > >
> > > Jimmy, Jing Lv
> > > China Software Development Lab, IBM
> > >
> >
> >
> >
> > --
> >
> > Best regards,
> > Andrew Zhang
> >
> > http://zhanghuangzhu.blogspot.com/
> >
>
>
>
> --
> Best regards,
> Andrew Zhang
>
> http://zhanghuangzhu.blogspot.com/
>



-- 
Sean Qiu
http://xiaoxia.turendui.com

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Andrew Zhang <zh...@gmail.com>.
On Nov 15, 2007 2:46 PM, Andrew Zhang <zh...@gmail.com> wrote:

>
>
> On Nov 15, 2007 2:31 PM, Jimmy,Jing Lv <fi...@gmail.com> wrote:
>
> > 2007/11/15, Andrew Zhang <zh...@gmail.com>:
> > > On Nov 15, 2007 1:22 PM, Jimmy,Jing Lv < firepure@gmail.com> wrote:
> > >
> > > > A simple thought is using the JNI call AllocObject(JNIEnv *env,
> > jclass
> > > > clazz);
> > > > however we need to implement a native api then :)
> > > >
> > >
> > > Thanks Jimmy!
> > >
> >
> > :)
> >
> > > AllocObject looks like what I want. ObjectInputStream#newInstance is
> > even
> > > closer (it has defined a native method). Is it possible to refactor?
> > > Comments? Thanks!
> > >
> >
> > Hmm, it seems the current native method newInstance is used for
> > ObjectInputStream itself, so refactor it may not be a good idea (will
> > surely break Harmony function) .
>
>
> No, I mean add a similar method in the ObjectInputStream, e.g private
> static native allocObject(Class <?> c);
>
> Or put it in some util class in o.a.h package. Any comments or objections?
>

Found it! It's already in misc module.

working_classlib\modules\misc\src\main\java\org\apache\harmony\misc\accessors\ObjectAccessor.java

public final native Object allocateObject(Class c);


>
> I suggest define it in some utility
> > class or your own class :)
> >
> > > Java_java_io_ObjectInputStream_newInstance (JNIEnv * env, jclass
> > clazz,
> > >                                             jobject
> > instantiationClass,
> > >                                             jobject constructorClass)
> > > {
> > >   jmethodID mid =
> > >     (*env)->GetMethodID (env, constructorClass, "<init>", "()V");
> > >
> > >   if (mid == 0)
> > >     {
> > >       /* Cant newInstance,No empty constructor... */
> > >       return (jobject) 0;
> > >     }
> > >   else
> > >     {
> > >       /* Instantiate an object of a given class and constuct it using
> > >          the constructor of the other class. */
> > >       jobject obj;
> > >       obj = (*env)->AllocObject(env, instantiationClass);  // I want
> > this
> > > method :)
> > >       if (obj != NULL) {
> > >         (*env)->CallNonvirtualVoidMethod(env, obj, constructorClass,
> > mid);
> > >       }
> > >       return obj;
> > >     }
> > >
> > > }
> > >
> > >
> > > > 2007/11/15, Andrew Zhang < zhanghuangzhu@gmail.com>:
> > > > > Hi,
> > > > >
> > > > > Is it possible to new an object without invoking any constructor?
> > I know
> > > > the
> > > > > question is a bit weird. What I want is to create an object with
> > all
> > > > > null/default value for its fields.
> > > > > Reseting all fields to null by reflection is a possible solution,
> > but is
> > > > > there any existing special method (no matter it's low level or
> > Harmony
> > > > > specific) which can achieve this goal?
> > > > > Thanks!
> > > > >
> > > > >
> > > > > --
> > > > > Best regards,
> > > > > Andrew Zhang
> > > > >
> > > > > http://zhanghuangzhu.blogspot.com/
> > > > >
> > > >
> > > >
> > > > --
> > > >
> > > > Best Regards!
> > > >
> > > > Jimmy, Jing Lv
> > > > China Software Development Lab, IBM
> > > >
> > >
> > >
> > >
> > > --
> > > Best regards,
> > > Andrew Zhang
> > >
> > > http://zhanghuangzhu.blogspot.com/
> > >
> >
> >
> > --
> >
> > Best Regards!
> >
> > Jimmy, Jing Lv
> > China Software Development Lab, IBM
> >
>
>
>
> --
>
> Best regards,
> Andrew Zhang
>
> http://zhanghuangzhu.blogspot.com/
>



-- 
Best regards,
Andrew Zhang

http://zhanghuangzhu.blogspot.com/

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Andrew Zhang <zh...@gmail.com>.
On Nov 15, 2007 2:31 PM, Jimmy,Jing Lv <fi...@gmail.com> wrote:

> 2007/11/15, Andrew Zhang <zh...@gmail.com>:
> > On Nov 15, 2007 1:22 PM, Jimmy,Jing Lv <fi...@gmail.com> wrote:
> >
> > > A simple thought is using the JNI call AllocObject(JNIEnv *env, jclass
> > > clazz);
> > > however we need to implement a native api then :)
> > >
> >
> > Thanks Jimmy!
> >
>
> :)
>
> > AllocObject looks like what I want. ObjectInputStream#newInstance is
> even
> > closer (it has defined a native method). Is it possible to refactor?
> > Comments? Thanks!
> >
>
> Hmm, it seems the current native method newInstance is used for
> ObjectInputStream itself, so refactor it may not be a good idea (will
> surely break Harmony function) .


No, I mean add a similar method in the ObjectInputStream, e.g private static
native allocObject(Class <?> c);

Or put it in some util class in o.a.h package. Any comments or objections?

I suggest define it in some utility
> class or your own class :)
>
> > Java_java_io_ObjectInputStream_newInstance (JNIEnv * env, jclass clazz,
> >                                             jobject instantiationClass,
> >                                             jobject constructorClass)
> > {
> >   jmethodID mid =
> >     (*env)->GetMethodID (env, constructorClass, "<init>", "()V");
> >
> >   if (mid == 0)
> >     {
> >       /* Cant newInstance,No empty constructor... */
> >       return (jobject) 0;
> >     }
> >   else
> >     {
> >       /* Instantiate an object of a given class and constuct it using
> >          the constructor of the other class. */
> >       jobject obj;
> >       obj = (*env)->AllocObject(env, instantiationClass);  // I want
> this
> > method :)
> >       if (obj != NULL) {
> >         (*env)->CallNonvirtualVoidMethod(env, obj, constructorClass,
> mid);
> >       }
> >       return obj;
> >     }
> >
> > }
> >
> >
> > > 2007/11/15, Andrew Zhang <zh...@gmail.com>:
> > > > Hi,
> > > >
> > > > Is it possible to new an object without invoking any constructor? I
> know
> > > the
> > > > question is a bit weird. What I want is to create an object with all
> > > > null/default value for its fields.
> > > > Reseting all fields to null by reflection is a possible solution,
> but is
> > > > there any existing special method (no matter it's low level or
> Harmony
> > > > specific) which can achieve this goal?
> > > > Thanks!
> > > >
> > > >
> > > > --
> > > > Best regards,
> > > > Andrew Zhang
> > > >
> > > > http://zhanghuangzhu.blogspot.com/
> > > >
> > >
> > >
> > > --
> > >
> > > Best Regards!
> > >
> > > Jimmy, Jing Lv
> > > China Software Development Lab, IBM
> > >
> >
> >
> >
> > --
> > Best regards,
> > Andrew Zhang
> >
> > http://zhanghuangzhu.blogspot.com/
> >
>
>
> --
>
> Best Regards!
>
> Jimmy, Jing Lv
> China Software Development Lab, IBM
>



-- 
Best regards,
Andrew Zhang

http://zhanghuangzhu.blogspot.com/

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by "Jimmy,Jing Lv" <fi...@gmail.com>.
2007/11/15, Andrew Zhang <zh...@gmail.com>:
> On Nov 15, 2007 1:22 PM, Jimmy,Jing Lv <fi...@gmail.com> wrote:
>
> > A simple thought is using the JNI call AllocObject(JNIEnv *env, jclass
> > clazz);
> > however we need to implement a native api then :)
> >
>
> Thanks Jimmy!
>

:)

> AllocObject looks like what I want. ObjectInputStream#newInstance is even
> closer (it has defined a native method). Is it possible to refactor?
> Comments? Thanks!
>

Hmm, it seems the current native method newInstance is used for
ObjectInputStream itself, so refactor it may not be a good idea (will
surely break Harmony function) . I suggest define it in some utility
class or your own class :)

> Java_java_io_ObjectInputStream_newInstance (JNIEnv * env, jclass clazz,
>                                             jobject instantiationClass,
>                                             jobject constructorClass)
> {
>   jmethodID mid =
>     (*env)->GetMethodID (env, constructorClass, "<init>", "()V");
>
>   if (mid == 0)
>     {
>       /* Cant newInstance,No empty constructor... */
>       return (jobject) 0;
>     }
>   else
>     {
>       /* Instantiate an object of a given class and constuct it using
>          the constructor of the other class. */
>       jobject obj;
>       obj = (*env)->AllocObject(env, instantiationClass);  // I want this
> method :)
>       if (obj != NULL) {
>         (*env)->CallNonvirtualVoidMethod(env, obj, constructorClass, mid);
>       }
>       return obj;
>     }
>
> }
>
>
> > 2007/11/15, Andrew Zhang <zh...@gmail.com>:
> > > Hi,
> > >
> > > Is it possible to new an object without invoking any constructor? I know
> > the
> > > question is a bit weird. What I want is to create an object with all
> > > null/default value for its fields.
> > > Reseting all fields to null by reflection is a possible solution, but is
> > > there any existing special method (no matter it's low level or Harmony
> > > specific) which can achieve this goal?
> > > Thanks!
> > >
> > >
> > > --
> > > Best regards,
> > > Andrew Zhang
> > >
> > > http://zhanghuangzhu.blogspot.com/
> > >
> >
> >
> > --
> >
> > Best Regards!
> >
> > Jimmy, Jing Lv
> > China Software Development Lab, IBM
> >
>
>
>
> --
> Best regards,
> Andrew Zhang
>
> http://zhanghuangzhu.blogspot.com/
>


-- 

Best Regards!

Jimmy, Jing Lv
China Software Development Lab, IBM

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by Andrew Zhang <zh...@gmail.com>.
On Nov 15, 2007 1:22 PM, Jimmy,Jing Lv <fi...@gmail.com> wrote:

> A simple thought is using the JNI call AllocObject(JNIEnv *env, jclass
> clazz);
> however we need to implement a native api then :)
>

Thanks Jimmy!

AllocObject looks like what I want. ObjectInputStream#newInstance is even
closer (it has defined a native method). Is it possible to refactor?
Comments? Thanks!

Java_java_io_ObjectInputStream_newInstance (JNIEnv * env, jclass clazz,
                                            jobject instantiationClass,
                                            jobject constructorClass)
{
  jmethodID mid =
    (*env)->GetMethodID (env, constructorClass, "<init>", "()V");

  if (mid == 0)
    {
      /* Cant newInstance,No empty constructor... */
      return (jobject) 0;
    }
  else
    {
      /* Instantiate an object of a given class and constuct it using
         the constructor of the other class. */
      jobject obj;
      obj = (*env)->AllocObject(env, instantiationClass);  // I want this
method :)
      if (obj != NULL) {
        (*env)->CallNonvirtualVoidMethod(env, obj, constructorClass, mid);
      }
      return obj;
    }

}


> 2007/11/15, Andrew Zhang <zh...@gmail.com>:
> > Hi,
> >
> > Is it possible to new an object without invoking any constructor? I know
> the
> > question is a bit weird. What I want is to create an object with all
> > null/default value for its fields.
> > Reseting all fields to null by reflection is a possible solution, but is
> > there any existing special method (no matter it's low level or Harmony
> > specific) which can achieve this goal?
> > Thanks!
> >
> >
> > --
> > Best regards,
> > Andrew Zhang
> >
> > http://zhanghuangzhu.blogspot.com/
> >
>
>
> --
>
> Best Regards!
>
> Jimmy, Jing Lv
> China Software Development Lab, IBM
>



-- 
Best regards,
Andrew Zhang

http://zhanghuangzhu.blogspot.com/

Re: [classlib][lang] Is it possible to new an object without invoking any constructor?

Posted by "Jimmy,Jing Lv" <fi...@gmail.com>.
A simple thought is using the JNI call AllocObject(JNIEnv *env, jclass clazz);
however we need to implement a native api then :)

2007/11/15, Andrew Zhang <zh...@gmail.com>:
> Hi,
>
> Is it possible to new an object without invoking any constructor? I know the
> question is a bit weird. What I want is to create an object with all
> null/default value for its fields.
> Reseting all fields to null by reflection is a possible solution, but is
> there any existing special method (no matter it's low level or Harmony
> specific) which can achieve this goal?
> Thanks!
>
>
> --
> Best regards,
> Andrew Zhang
>
> http://zhanghuangzhu.blogspot.com/
>


-- 

Best Regards!

Jimmy, Jing Lv
China Software Development Lab, IBM