静态分析工具,用于捕获绕过Spring cache@Cacheable方法的自调用



我理解这是因为在Spring中创建代理以处理缓存、事务相关功能的方式。解决它的方法是使用AspectJ,但我不想走这条路,因为它有自己的问题我可以使用任何静态分析工具检测自调用吗

@Cacheable(value = "defaultCache", key = "#id")
public Person findPerson(int id) {
return getSession().getPerson(id);
} 
public List<Person> findPersons(int[] ids) {
List<Person> list = new ArrayList<Person>();
for (int id : ids) {
list.add(findPerson(id));
}
return list;
} 

如果检测内部调用就足够了,那么可以使用本机AspectJ而不是Spring AOP,然后在每次发生这种情况时抛出运行时异常或日志警告。这不是静态分析,但总比什么都没有好。另一方面,如果您使用本机AspectJ,那么无论如何都不局限于Spring代理,这些方面也可以用于自调用。

不管怎样,以下是一个方面的样子,包括一个展示它如何工作的MCVE。我是在Spring之外完成的,这就是为什么我使用代理@Component注释进行演示的原因。

更新:很抱歉针对@Component类而不是@Cacheable类/方法,但如果您只需稍微调整切入点,我在这里展示的基本相同的通用方法也适用于您的特定情况

组件注释:

package de.scrum_master.app;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(TYPE)
public @interface Component {}

样本类(组件和非组件(:

该组件将由其他组件调用,不应导致异常/警告:

package de.scrum_master.app;
@Component
public class AnotherComponent {
public void doSomething() {
System.out.println("Doing something in another component");
}
}

这个类不是@Component,所以方面应该忽略它内部的自调用:

package de.scrum_master.app;
public class NotAComponent {
public void doSomething() {
System.out.println("Doing something in non-component");
new AnotherComponent().doSomething();
internallyCalled("foo");
}
public int internallyCalled(String text ) {
return 11;
}
}

这个类是@Component。方面应该标记internallyCalled("foo"),而不是new AnotherComponent().doSomething()

package de.scrum_master.app;
@Component
public class AComponent {
public void doSomething() {
System.out.println("Doing something in component");
new AnotherComponent().doSomething();
internallyCalled("foo");
}
public int internallyCalled(String text ) {
return 11;
}
}

驱动程序应用程序:

请注意,我在整个示例代码中使用new创建组件实例,而不是像在Spring中那样从应用程序上下文请求bean。但你可以忽略这一点,这只是一个例子。

package de.scrum_master.app;
public class Application {
public static void main(String[] args) {
new NotAComponent().doSomething();
new AComponent().doSomething();
}
}

在没有方面的情况下运行时的控制台日志:

Doing something in non-component
Doing something in another component
Doing something in component
Doing something in another component

现在,对于方面,我们期望的不是最后一条消息,而是异常或记录的警告。以下是如何做到这一点:

方面:

很抱歉在此处使用本机AspectJ语法。当然,您也可以使用基于注释的语法。

package de.scrum_master.aspect;
import de.scrum_master.app.*;
public aspect SelfInvocationInterceptor {
Object around(Object caller, Object callee) :
@within(Component) &&
call(* (@Component *).*(..)) &&
this(caller) &&
target(callee)
{
if (caller == callee)
throw new RuntimeException(
"Self-invocation in component detected from "  + thisEnclosingJoinPointStaticPart.getSignature() +
" to "+ thisJoinPointStaticPart.getSignature()
);
return proceed(caller, callee);
}
}

使用方面运行时的控制台日志:

Doing something in non-component
Doing something in another component
Doing something in component
Doing something in another component
Exception in thread "main" java.lang.RuntimeException: Self-invocation in component detected from void de.scrum_master.app.AComponent.doSomething() to int de.scrum_master.app.AComponent.internallyCalled(String)
at de.scrum_master.app.AComponent.internallyCalled_aroundBody3$advice(AComponent.java:8)
at de.scrum_master.app.AComponent.doSomething(AComponent.java:8)
at de.scrum_master.app.Application.main(Application.java:6)

我认为,您可以使用这个解决方案,也许可以记录警告,而不是抛出异常,以便温和地指导您的同事检查和改进他们依赖AOP的Spring组件。有时,他们可能不希望自调用触发某个方面,这取决于情况。您可以在完整的AspectJ模式下运行Spring应用程序,然后在评估日志后,切换回SpringAOP。但是,也许从一开始就使用本机AspectJ并完全避免自调用问题会更简单。


更新:在AspectJ中,如果满足某些条件,您也可以让编译器抛出警告或错误。在这种情况下,您只能静态地确定从组件到其他组件的调用,但不能区分自调用和对其他组件的其他方法的调用。所以这对你没有帮助。

还请注意,此解决方案仅限于由@Component注释的类。如果您的Springbean是以其他方式实例化的,例如通过XML配置或@Bean工厂方法,那么这个简单的方面就不起作用。但是,通过检查被拦截的类是否是代理实例,然后决定标记自调用,可以很容易地扩展它。不幸的是,您将不得不将方面代码编织到所有应用程序类中,因为检查只能在运行时进行。

我可以解释更多的事情,比如使用自注入,并在注入的代理实例上调用内部方法,而不是通过this.internallyCalled(..)。然后,自调用问题也将得到解决,这种方法也适用于SpringAOP。

我可以使用任何静态分析工具检测自调用吗?

理论上你可以,但要注意Rice定理。任何这样的工具有时都会发出假警报。

你可以使用抽象的解释技术来开发这样一个工具。你可能需要一年以上的工作。

您可以将此类工具的开发分包给Frama-C团队。然后给我发电子邮件到basile.starynkevitch@cea.fr

最新更新