I would like to be able to create a subclass instance from an existing base class in a generic way. The generic part is where I'm having trouble.
The problem is that JavaUtil.setObjectFields() is not part of the same package as the objects so it will not have access to any non-public fields. Of course I'm not going to declare all my instance fields public, but I don't want to have to copy/paste the setObjectFields() code into each one of my objects either.
Suggestions?
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1865
posted
0
Hi Michael,
I'm not sure why you would need such a functionality but if you put the setObjectFields() method in the Base Class as,
Shouldn't it work? This is because the second argument "always" is THIS object as you define it in the BaseClass's constructor so if you put the method inthe BaseClass it would be automatically inherited to child classes and you can call it by giving any arguments of valid BaseClass Child from any other valid BaseClass's child.
e.g. if you had two children of BaseClass, Child1 and Child2 you can call, Child1.setObjectFields(Child2); which will copy all BaseClass defined fields from Child2 to Child1 object.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1865
posted
0
Hi Ilja,
Can you see what I am suggesting would work or not? Its interesting question.
And I would love to know if I got the picture correctly and if we can avoid even creating JavaUtil's method setObjectFields() when we can really use inheritance power.
Regards, Maulin
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1865
posted
0
Okay...finally I had to write the code,
And the output is, ch1=i=100,j=200 ch21=i=100,j=200 ch22=i=1000,j=2000
Is this correct behavior as expectations?
Regards, Maulin
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1865
posted
0
okay..okay...don't scream at me. I get the problem
I guess what Ilja suggested would be the only way out given that we have Security Permissions to call setAccessible() on the Field object..
Regards, Maulin
Michael Diamond
Greenhorn
Joined: Oct 14, 2004
Posts: 3
posted
0
Originally posted by Ilja Preuss: fields[i].setAccessible(true);
Isn't this equivalent to setting the fields public?
Maulin: I like the idea of putting the function in the base class, but I have several object heirarchies I would like to use this with and I don't like having duplicate code. The project I inherited suffers greatly from this already so I was trying to avoid putting this function into each base class.
Michael Diamond
Greenhorn
Joined: Oct 14, 2004
Posts: 3
posted
0
I am surprised that this doesn't seem to be a common pattern. Let me describe the problem I am trying to solve.
I have two objects, PurchaseRequest and PurchaseOrder. These objects share the same base class, OrderObject. When a request is approved, it is turned into a PurchaseOrder. I would like to create a new PurchaseOrder based upon the PurchaseRequest.
The other option I thought about was cloning, but that does not allow me to create a new subclass from the cloned object. So this is essentially a deep copy of the superclass in the constructor of the subclass.
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1865
posted
0
but I have several object heirarchies I would like to use this with and I don't like having duplicate code.
Thats why I said in my latest post that "I get the problem" I understand what you want but as you think, its difficult to do it in clean way w/o using setAccessible() thing
Regards, Maulin
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
Hmmm, maybe they shouldn't have a common superclass. Your constructor idea looks fine:
Maybe PurchaseOrder class keeps the existing PurchaseRequest object as a variable.
If you need to work at an abstract level where you can look at either type, you might make the common methods in an interface:
Now you could have methods work on the abstract type:
Any of that sound tempting? [ October 14, 2004: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi