在批注另一个方法之前调用方法

我正在尝试通过使用注释将功能添加到接口方法签名中。


这个想法是为每个带注释的方法调用一些其他方法。


例如,如果我有此方法签名:


public interface IMyInterface{


    @Entity(visibileName = "Name")

    public TextField getName();

}

我需要调用一个方法,该方法在此方法之前,之后打印字符串名称。如果有任何方法可以在运行时定义此方法的功能,也可以。


我也对结构性变化持开放态度。


慕勒3428872
浏览 84回答 1
1回答

慕桂英546537

如果你想要的是批注方法,那么在没有AOP的情况下是可能的。只需使用动态代理!interface实现代理的基础是interfaceInvocationHandler调用处理程序是由代理实例的调用处理程序实现的接口。遵循代码内注释。static class MyInterfaceProxy implements InvocationHandler {&nbsp; &nbsp; private static final Map<String, Method> METHODS = new HashMap<>(16);&nbsp; &nbsp; static {&nbsp; &nbsp; &nbsp; &nbsp; // Scan each interface method for the specific annotation&nbsp; &nbsp; &nbsp; &nbsp; // and save each compatible method&nbsp; &nbsp; &nbsp; &nbsp; for (final Method m : IMyInterface.class.getDeclaredMethods()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (m.getAnnotation(YourAnnotation.class) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; METHODS.put(m.getName(), m);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private final IMyInterface toBeProxied;&nbsp; &nbsp; private MyInterfaceProxy(final IMyInterface toBeProxied) {&nbsp; &nbsp; &nbsp; &nbsp; // Accept the real implementation to be proxied&nbsp; &nbsp; &nbsp; &nbsp; this.toBeProxied = toBeProxied;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Object invoke(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Object proxy,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Method method,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Object[] args) throws InvocationTargetException, IllegalAccessException {&nbsp; &nbsp; &nbsp; &nbsp; // A method on MyInterface has been called!&nbsp; &nbsp; &nbsp; &nbsp; // Check if we need to call it directly or if we need to&nbsp; &nbsp; &nbsp; &nbsp; // execute something else before!&nbsp; &nbsp; &nbsp; &nbsp; final Method found = METHODS.get(method.getName());&nbsp; &nbsp; &nbsp; &nbsp; if (found != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // The method exist in our to-be-proxied list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Execute something and the call it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ... some other things&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Something else");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Invoke original method&nbsp; &nbsp; &nbsp; &nbsp; return method.invoke(toBeProxied, args);&nbsp; &nbsp; }}要使用此实现,您需要一个要代理的对象的真实实例。InvocationHandler假设您有一个用于实现的工厂MyInterfaceMyInterface getMyInsterface() {&nbsp; &nbsp;...&nbsp; &nbsp;final MyInterface instance = ...&nbsp; &nbsp;// Create the proxy and inject the real implementation&nbsp; &nbsp;final IMyInterface proxy = (IMyInterface) Proxy.newProxyInstance(&nbsp; &nbsp; &nbsp; &nbsp; MyInterfaceProxy.class.getClassLoader(),&nbsp; &nbsp; &nbsp; &nbsp; new Class[] {IMyInterface.class},&nbsp; &nbsp; &nbsp; &nbsp; new MyInterfaceProxy(instance) // Inject the real instance&nbsp; &nbsp; );&nbsp; &nbsp;// Return the proxy!&nbsp; &nbsp;return proxy;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java