D
D
Daniel2018-03-25 17:19:17
Java
Daniel, 2018-03-25 17:19:17

How to modify a method in java at RUNTIME?

There is a method

class Foo{
            public void printName(){
                    System.out.println( "Foo");
                  // Сюда должна добавиться System.out.println( "Время вызова метода"
                //                 +System.currentTimeMillis() );
           }
        }

How can you call and receive using reflection using basic means without a decorator and calling through a Proxy object
Foo f=new Foo();
     f.getName();           // "Foo"
                                  // "Время вызова метода" ....
// Всегда работаем только с базовым классом Foo не обертывая его.

Those. you need to modify (or what other solutions) the method and add one more instruction to it, something like creating a handler as in the Observer pattern.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Alexandrov, 2018-03-25
@jamakasi666

It is possible, but extremely hemorrhoids and most likely the game will not cost the candle. See javasist.

P
piatachki, 2018-03-26
@piatachki

Proxying, but, as you rightly noted, doing it for academic purposes is a so-so idea.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question