gpt4 book ai didi

Java:是否可以在调用其他函数之前始终执行某个函数? (就像 JUnit 中的@Before)

转载 作者:搜寻专家 更新时间:2023-11-01 01:32:23 24 4
gpt4 key购买 nike

有没有办法总是在调用一个类的任何其他函数之前执行一个函数?

我有一个类,我需要在调用任何函数之前总是刷新一些字段:

public class Example {

private int data;

public void function1(){

}

public void function2(){

}

//@BeforeOtherFunction
private void refresh(){
// refresh data
}
}

因为这似乎是糟糕的编程,我不想在每个其他函数的开头调用 refresh。由于其他人也将在这个项目上工作,因此会有危险,有人会扩展调用但不会调用 refresh

JUnit 使用 @Before 注释来解决这个问题。有没有办法在其他类(class)中也这样做?

顺便说一句:如果您知道一种编程模式可以用另一种方式解决这个问题,而不是每次调用任何函数时都执行一个函数,那也会非常有帮助!

最佳答案

使用动态代理,您可以在其中过滤到应调用特定“之前”方法的那些方法。并在发送调用之前在这些情况下调用它。请看How do I intercept a method invocation with standard java features (no AspectJ etc)?的回答

更新:

代理需要分离一个接口(interface)。 refresh() 方法不能保持私有(private)。它必须是公共(public)的并且是接口(interface)的一部分(这在这里不太好)才能从代理调用。

package CallBefore;

public interface ExampleInterface {
void function1();

void function2();

void otherFunction();

void refresh();
}

您的类实现了该接口(interface):

package CallBefore;

public class Example implements ExampleInterface {

@Override
public void function1() {
System.out.println("function1() has been called");
}

@Override
public void function2() {
System.out.println("function2() has been called");
}

@Override
public void otherFunction() {
System.out.println("otherFunction() has been called");
}

@Override
public void refresh() {
System.out.println("refresh() has been called");
}
}

发挥作用的代理。它过滤所需的方法并调用 refresh()。

package CallBefore;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ExampleProxy implements InvocationHandler {

private ExampleInterface obj;

public static ExampleInterface newInstance(ExampleInterface obj) {
return (ExampleInterface) java.lang.reflect.Proxy.newProxyInstance(obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(), new ExampleProxy(obj));
}

private ExampleProxy(ExampleInterface obj) {
this.obj = obj;
}

@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
Object result;
try {
if (m.getName().startsWith("function")) {
obj.refresh();
}
result = m.invoke(obj, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
} catch (Exception e) {
throw new RuntimeException("unexpected invocation exception: " + e.getMessage());
}
return result;
}
}

用法:

package CallBefore;

public class Main {

public static void main(String[] args) {

ExampleInterface proxy = ExampleProxy.newInstance(new Example());
proxy.function1();
proxy.function2();
proxy.otherFunction();
proxy.refresh();
}
}

输出:

refresh() has been called
function1() has been called
refresh() has been called
function2() has been called
otherFunction() has been called
refresh() has been called

关于Java:是否可以在调用其他函数之前始终执行某个函数? (就像 JUnit 中的@Before),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37701020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com