Overriding a method in an instantiated Java object

前端 未结 9 1408
陌清茗
陌清茗 2021-02-05 01:55

I would like to override a method in an object that\'s handed to me by a factory that I have little control over.

My specific problem is that I want to override the

相关标签:
9条回答
  • 2021-02-05 02:02

    Another proxying-related solution: you could use Aspects to override a method on a given object without subclassing it yourself. This is especially appropriate and common for logging. This example uses spring-aop.

    class Example {
    
        final Foo foo;
    
        Example(Foo original) {
            AspectJProxyFactory factory = new AspectJProxyFactory();
            factory.setTarget(original);
            factory.addAspect(FooAspect.class);
            foo = (Foo) factory.getProxy();
        }
    
        @Aspect
        static class FooAspect {
    
            @Before("execution(Foo.doBar())")
            Object beforeDoBar() {
                // My own activity
            }
        }
    
    0 讨论(0)
  • 2021-02-05 02:09

    You can't really change an object on the fly in java.

    You could have something which do what you want by wrapping your Foo into another similar objet which will delegate every call to Foo and at the same log everything you want. (see Proxy)

    But if you want to do logging, maybe aspect is a better choice. (see AspectJ)

    0 讨论(0)
  • 2021-02-05 02:11

    You can't replace methods in existing objects - you can't change an existing object's type, for one thing.

    You could create a new instance of another class which delegated to the existing instance, but that has limitations too.

    In your real world case is there no way you can simply make a separate call to wrap the streams returned by the socket? Can you give more details.

    0 讨论(0)
  • 2021-02-05 02:13

    Using a decorator is the right way to go:

    Some very similar code to the requirement you have with sockets is here:

    http://www.javaspecialists.eu/archive/Issue058.html

    0 讨论(0)
  • 2021-02-05 02:15

    If Socket was an interface then you could create a dynamic proxy. Below is an example. I put this here in case other people need to do this and the target instance is an instance of an interface.

    The main reason this will not work for Socket is because java.lang.reflect.Proxy.newProxyInstance requires an array of interfaces for its second argument, so classes won't work here. As such for this example I had to create an interface called ParentInterface, which just has the three print methods.

    public class Parent implements ParentInterface {
    
        @Override
        public void print1() {
            System.out.println("parent 1");
        }
    
        @Override
        public void print2() {
            System.out.println("parent 2");
        }
    
        @Override
        public void print3() {
            System.out.println("parent 3");
        }
    
        public static void main(String[] args) {
            Parent originalInstance = new Parent();
            ParentInterface proxied = (ParentInterface) java.lang.reflect.Proxy.newProxyInstance(
                    Parent.class.getClassLoader(),
                    new Class[]{ParentInterface.class},
                    new ParentProxy(originalInstance));
    
            proxied.print1();
            proxied.print2();
            proxied.print3();
    
        }
    
        static class ParentProxy implements InvocationHandler {
    
            final Object realObject;
    
            public ParentProxy(Object real) {
                realObject = real;
            }
    
            @Override
            public Object invoke(Object target, Method m, Object[] args) throws Throwable {
                try {
                    if (m.getName().equals("print2")) {
                        print2();
                        return null;
                    } else {
                        return m.invoke(realObject, args);
                    }
                } catch (java.lang.reflect.InvocationTargetException e) {
                    throw e.getTargetException();
                }
            }
    
            public void print2() {
                System.out.println("wrapper 2");
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2021-02-05 02:18

    I think there is a way to achieve the effect you want. I saw it orriginally used in swing with buttons to allow the programmer to make the button do something when it is clicked.

    Say you have your Foo class:

    public class Foo {
      public Bar doBar() {
        // Some activity
      }
    }
    

    Then you have a runner class or something similar. You can override the doBar() method at the point of instantiation and it will only affect that specific object.

    that class may look like this:

    public class FooInstance{
      Foo F1 = new Foo(){
        public Bar doBar(){
          //new activity
        }
      }
    
      Foo F2 = new Foo();
    
      F1.doBar(); //does the new activity
      F2.doBar(); //does the original activity found in the class
    }
    

    I'm not entirely sure that will do the trick for you but maybe it'll set you in the right direction. If nothing else it is possible to override a method outside of the class, maybe that will help you.

    0 讨论(0)
提交回复
热议问题