You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by temp temp <mi...@yahoo.com> on 2007/04/27 17:26:20 UTC

[BEAN-UITLS]setting super class using bean retrospection

Is there any way I can set super class using bean retrospection ?
  I create a instance of a class  is there a possible way to   to add a super class using bean retrospection ?
  Thansk & Regards
  miro
  
  
       
---------------------------------
Ahhh...imagining that irresistible "new car" smell?
 Check outnew cars at Yahoo! Autos.

Re: [Very OT] [BEER] Re: [BEAN-UITLS]setting super class using bean retrospection

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Miro,

I've done heavy editing to make the quoted post make sense. Forgive me
if I have inaccurately portrayed the question.

temp temp wrote:
> I want a wrapper class for an object. I do not want to write methods 
> which calls [the superclass] method. Assume I have class.

Ok, you want a wrapper class. Generally, this is done like this:

public interface Wrapped
{
   int foo();
}

public class WrappedWrapper
   implements Wrapped
{
    private Wrapped _wrapped;
    public WrappedWrapper(Wrapped wrapped)
    {
        _wrapped = wrapped;
    }

    public int foo()
    {
        return _wrapped.foo();
    }
}

It sounds like you don't want to go through all that trouble to actually
implement the methods.

> I have [an] interface [which includes the methods] getString(); [and]
> getLength(); Some class is implementing this [interface]. At runtime
> I want to update this object by overriding method getLength(). One
> way is create a new implementation. This implementation is just a
> wrapper to actual implementation so in this wrapper I will say
> 
> getLength(){ actualImplementation.getLength();

Right... this is exactly what I have above.

> instead of doing this  is there a way I create a class override  only
>  method getLength nad rest  methods comes from super class . Thanks &
>  Regards Miro

Well... that would be a subclass, not a wrapper class. Often, interfaces
and wrappers are accompanied with "adapters" which are implementations
of interfaces that don't do anything. They allow you to override some
methods and leave the others unchanged.

What I'd recommend in your case is a combination of a wrapper and an
adapter. In fact, you don't even need another class for the adapter...
just use the wrapper itself as the adapter. You can either formally
subclass WrappedWrapper to include something specific, or use an
anonymous class. Something like this:

Wrapped a = ...; // however you want to get this instance
WrappedWrapper b = new WrappedWrapper(a)
   {
        public int foo() {  // overrides WrappedWrapper.foo()
           doSomethingElse();
        }
   };

You could also look into using the java.lang.reflect.Proxy class, which
is super cool and will blow your mind.

To answer your original question: no, you cannot change the superclass
of a class at runtime. You'll just have to suck it up and write classes
like the rest of us.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGMjvN9CaO5/Lv0PARAuLHAJ9RAfmao1PkRu8Wh9GSuhAMPJOa+gCgkmrm
ll1PFM1UDLUQASiRDw5z2Jg=
=r29/
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [BEER] Re: [BEAN-UITLS]setting super class using bean retrospection

Posted by temp temp <mi...@yahoo.com>.
I want a wrapper class for an object I do not want to write methods which calls method 
  Assume I have class
   
  I have inteface 
   getString();
  getLength();
  Some class is implementing this.
  At runtime I uwant to update this object by overriding method getLenth().
  one way is create a new implementation this implemtation is just a wrapper to actual implementation 
so in this wrapper I will say 
  
  getLenght(){
  actualImplementation.getLength();
  
  instead of doing this  is there a way I create a class override  only method getLength nad rest  methods comes from super class .
  Thanks & Regards
  Miro
  
  
  Dave Newton <ne...@yahoo.com> wrote:  --- temp temp  wrote:
> Is there any way I can set super class using bean
> retrospection ?
> I create a instance of a class  is there a
> possible way to   to add a super class using bean
> retrospection ?

In a day of surprisingly accurate typos, retrospection
is *exactly* what you'd want to use.

What do you mean by "add a super class"? A class can
only have a single superclass, which is determined at
compile-time (AFAIK).

d.


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



       
---------------------------------
Ahhh...imagining that irresistible "new car" smell?
 Check outnew cars at Yahoo! Autos.

[BEER] Re: [BEAN-UITLS]setting super class using bean retrospection

Posted by Dave Newton <ne...@yahoo.com>.
--- temp temp <mi...@yahoo.com> wrote:
> Is there any way I can set super class using bean
> retrospection ?
> I create a instance of a class  is there a
> possible way to   to add a super class using bean
> retrospection ?

In a day of surprisingly accurate typos, retrospection
is *exactly* what you'd want to use.

What do you mean by "add a super class"? A class can
only have a single superclass, which is determined at
compile-time (AFAIK).

d.


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org