Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在运行时更改方法的行为?_Java_Reflection - Fatal编程技术网

Java 在运行时更改方法的行为?

Java 在运行时更改方法的行为?,java,reflection,Java,Reflection,我有以下代码 头等舱 package com.test; public class CustomizeHere { private CustomizeMe customizeMe = new CustomizeMe(); public void setDescription() { customizeMe.setText("How about calling some method before me?"); } } package com.test; pu

我有以下代码

头等舱

package com.test;

public class CustomizeHere
{    
private CustomizeMe customizeMe = new CustomizeMe();    
public void setDescription()
{
    customizeMe.setText("How about calling some method before me?");
}    
}
package com.test;

public final class CustomizeMe
{
private String text = null;    
public String getText()
{
    return text;
}

public void setText(String text)
{
    this.text = text;
}    
}
package com.test;

public class ReflectCustomizer
{    
/**
 * @param args
 */
public static void main(String[] args)
{
    CustomizeHere customizeHere = new CustomizeHere();
    // Requirement here is when customizeMe.setText() before method invocation we want to add some additional behaviour at run time like we should come to
    // know setText() is invoked.
    customizeHere.setDescription();        
}    
}
二等舱

package com.test;

public class CustomizeHere
{    
private CustomizeMe customizeMe = new CustomizeMe();    
public void setDescription()
{
    customizeMe.setText("How about calling some method before me?");
}    
}
package com.test;

public final class CustomizeMe
{
private String text = null;    
public String getText()
{
    return text;
}

public void setText(String text)
{
    this.text = text;
}    
}
package com.test;

public class ReflectCustomizer
{    
/**
 * @param args
 */
public static void main(String[] args)
{
    CustomizeHere customizeHere = new CustomizeHere();
    // Requirement here is when customizeMe.setText() before method invocation we want to add some additional behaviour at run time like we should come to
    // know setText() is invoked.
    customizeHere.setDescription();        
}    
}
三等舱

package com.test;

public class CustomizeHere
{    
private CustomizeMe customizeMe = new CustomizeMe();    
public void setDescription()
{
    customizeMe.setText("How about calling some method before me?");
}    
}
package com.test;

public final class CustomizeMe
{
private String text = null;    
public String getText()
{
    return text;
}

public void setText(String text)
{
    this.text = text;
}    
}
package com.test;

public class ReflectCustomizer
{    
/**
 * @param args
 */
public static void main(String[] args)
{
    CustomizeHere customizeHere = new CustomizeHere();
    // Requirement here is when customizeMe.setText() before method invocation we want to add some additional behaviour at run time like we should come to
    // know setText() is invoked.
    customizeHere.setDescription();        
}    
}
我想知道在上面的场景中,我是否可以知道正在调用
CustomizeMe
上的
setText()
方法?我不能从customizeMe更改代码,但我在运行时使用反射创建了customizeMe的实例


我不能在
CustomizeMe
CustomizeHere
类中更改代码。另外,正如
java.lang.Reflect.Method
类不允许附加任何listner,以便我知道它正在被调用,那么还有其他方法吗?

一种高级方法是定义一个面向setText方法的方面,该方法包含您想要编写的附加逻辑。这将在该方面所针对的类的任何实例上运行该方法的所有调用


Aspect旨在解决横切问题。

如果您的需求是在调用其他方法之前设置对象上的一些参数,那么您可以让构造函数获取这些参数,或者,如果要创建的各种配置选项之间存在相互依赖关系,则遵循生成器模式。

您到底想要什么。。?您正在调用customizeHere.setDescription();这就是callcustomizeme.setText(“在我之前调用某个方法怎么样?”);因此,调用了end setText()|