提取相同的建议代码

时间:2015-08-05 08:30:52

标签: java aspectj

我有两个建议:

let localPlayer = GKLocalPlayer() 

有很复杂的代码重复。我尝试将同一个主体提取到一个分开的方法中,如下所示

public aspect LogAspect {
    before() : execution(@Log * *(..)) {
        System.out.println(thisJoinPoint.getClass());
        Logger logger = Logger.getLogger(thisJoinPoint.getClass());
        String className = thisJoinPoint.getClass().getSimpleName();
        System.out.println(thisJoinPoint.getSignature().getName());
        String methodName = thisJoinPoint.getSignature().getName();
        String startInfoMessage = String.format("%s.%s: execution started", className, methodName);
        logger.info(startInfoMessage);
    }

    after() : execution(@Log * *(..)) {
        System.out.println(thisJoinPoint.getClass());
        Logger logger = Logger.getLogger(thisJoinPoint.getClass());
        String className = thisJoinPoint.getClass().getSimpleName();
        System.out.println(thisJoinPoint.getSignature().getName());
        String methodName = thisJoinPoint.getSignature().getName();
        String endInfoMessage = String.format("%s.%s: execution's finished", className, methodName);
        logger.info(startInfoMessage);
    }
}

但它不起作用,因为编译器在建议之外不知道 private String getInfoMessage(String pattern){ System.out.println(thisJoinPoint.getClass()); Logger logger = Logger.getLogger(thisJoinPoint.getClass()); String className = thisJoinPoint.getClass().getSimpleName(); System.out.println(thisJoinPoint.getSignature().getName()); String methodName = thisJoinPoint.getSignature().getName(); String infoMessage = String.format(pattern, className, methodName); return infoMessage; } 。这些事情的解决方案是什么?

0 个答案:

没有答案