Groups | Blog | Home
all groups > vj# > april 2004 >

vj# : Runtime instances creating


Pakhomenko Dmitry
4/28/2004 2:46:01 AM
Hello, all. Anybody could help me
I want to create insatnces of any classes in runtime. I know two ways
1) using reflection and getting constructo
2) using maging string "Class.forName(<className>).newInstance()

Both methods work well, but... If class doesnt' have any declared cunstructors, instance creation will fail

Thanks
Lars-Inge Tønnessen
4/29/2004 4:23:47 PM

[quoted text, click to view]
will fail.

Make a wrapper. =:o)


Regards,
Lars-Inge Tønnessen
www.larsinge.com

Pakhomenko Dmitry
4/29/2004 9:06:03 PM
[quoted text, click to view]
Very helpful :) But it should be transparently for end user. Let me introduce my problem better. I've written classes for j# for supporting standard java RMI mechanism. So, now j# client could talk with java server using standart sun RMI technology. And back to front, j# server could serve java RMI clients. It was not easy, but now it is working :) There wer
many problems, for example j# serialize/deserialize stream a bit different from sun stream. So, I implemented custo
java like classes ObjectOutputStream, ObjectInputStream, and ObjectStreamClass in j#. Besides, .Net and sun have different ideologies so classes java.lang.Throwable and their inherited members have very different structures. So I added special processing in these classes for converting j# exceptions to java exceptions and back. But I haven't decided yet one little problem. Java class which will be serialized from j# or deserialized by j# have to contain DECLARED and PUBLIC constructor without any parameters. It's not problem, most likely it's just inconvenience

PS: Sorry for my terrible English, I'm from wild country - Russia :

Best wishes
Pakhomenko Dmitr

George Birbilis [MVP J#] [9880]
6/18/2004 12:30:22 AM
[quoted text, click to view]
contain DECLARED and PUBLIC constructor without any parameters. It's not
problem,
[quoted text, click to view]

isn't that the case with Sun Java too?
or do you find that just j# need the default public constructor available at
the class to have it deserialized?

at the Sun JVM side, maybe the "Proxy" class can help with such things, or
else maybe JNI has some useful call

btw, the idea is that you shouldn't be able to trick the JVM to instantiate
an object (make a class instance) for a class that has no default public
constructor, or has a private default constructor (for example it might have
a "private" default constructor to force all descendent classes not be
instantiatable too! [if it had "protected" or no default constructor a
descendent class could add a "public" constructor])

if you're interested to support Double, Long, etc. classes hardcode their
marshalling/unmarshalling instead or even better make a lookup hash, where
one passes a fully qualified class name (like "java.lang.Double") and gets
back a marshaller/unmarshaller class interface implementation (that takes an
"Object" [instance of the wanted class] and writes it to a stream [or
vice-versa])

e.g.

public interface Marshaller {
public void marshal(Object x, SomeOutputStream s);
}

public interface Unmarshaller {
public Object unmarshal(SomeInputStream s);
}

public class DoubleMarshallingSupport
implements Marshaller, Unmarshaller
{

public void marshal(Object x, SomeOutputStream s){
s.writedouble(Double.doubleValue());
}

public Object unmarshal(SomeInputStream s){
return new Double(s.readdouble());
}
}

or something like that

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
George Birbilis <birbilis@kagi.com> [J# MVP9880]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ QuickTime VCL and ActiveX controls (for PowerPoint/VB/Delphi etc.)
+ Plugs VCL and ActiveX controls (InterProcess/Internet communication)
+ TransFormations, VB6 forms to ASP.net WebForms convertion
http://www.kagi.com/birbilis
+ Robotics
http://www.mech.upatras.gr/~robgroup
.........................................................................

AddThis Social Bookmark Button