[Redacted
package pascal.taie.analysis.pta.plugin.spring;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import pascal.taie.analysis.graph.callgraph.Edge;
import pascal.taie.analysis.pta.core.cs.context.Context;
import pascal.taie.analysis.pta.core.cs.element.CSCallSite;
import pascal.taie.analysis.pta.core.cs.element.CSMethod;
import pascal.taie.analysis.pta.core.cs.element.CSObj;
import pascal.taie.analysis.pta.core.cs.element.CSVar;
import pascal.taie.analysis.pta.core.solver.Solver;
import pascal.taie.analysis.pta.plugin.Plugin;
import pascal.taie.analysis.pta.pts.PointsToSet;
import pascal.taie.ir.exp.InvokeExp;
import pascal.taie.ir.stmt.Invoke;
import pascal.taie.ir.stmt.Stmt;
import pascal.taie.language.annotation.Annotation;
import pascal.taie.language.classes.JClass;
import pascal.taie.language.classes.JField;
import pascal.taie.language.classes.JMethod;
import pascal.taie.language.type.ClassType;
import pascal.taie.language.type.Type;
import pascal.taie.util.collection.Maps;
import pascal.taie.util.collection.Sets;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Spring框架分析插件的完整实现
*/
public class SpringAnalysisPlugin implements Plugin {
private static final Logger logger = LogManager.getLogger(SpringAnalysisPlugin.class);
private Solver solver;
// 记录所有Spring Bean类
private final Set<JClass> springBeans = new HashSet<>();
// 记录[Redacted Person]名称到类的映射
private final Map<String, JClass> beanNameToClass = new HashMap<>();
// 记录类到Bean实例的映射
private final Map<JClass, Set<CSObj>> beanInstances = Maps.newMap();
// 记录需要注入的依赖
private final Map<JField, Set<JClass>> injectionPoints = Maps.newMap();
// Spring相关注解集合
private static final Set<String> SPRING_ANNOTATIONS = Set.of(
"org.springframework.stereotype.Component",
"org.springframework.stereotype.Service",
"org.springframework.stereotype.Repository",
"org.springframework.stereotype.Controller"
);
@[Redacted Person]
public void setSolver(Solver solver) {
this.solver = solver;
}
@[Redacted Person]
public void onStart() {
logger.info("Starting Spring analysis plugin...");
scanSpringAnnotations();
}
@[Redacted Person]
public void onNewMethod(JMethod method) {
// 检查方法上的[Redacted Person]注解
checkSpringAnnotations(method);
// 检查方法参数上的注解
checkParameterAnnotations(method);
}
@[Redacted Person]
public void onNewCallEdge(Edge<CSCallSite, CSMethod> edge) {
analyzeDependencyInjection(edge);
}
@[Redacted Person]
public void onNewPointsToSet(CSVar csVar, PointsToSet pts) {
analyzeBeanPointsTo(csVar, pts);
}
@[Redacted Person]
public void onFinish() {
// 输出分析结果
outputAnalysisResults();
}
private void scanSpringAnnotations() {
solver.getHierarchy().allClasses().forEach(jclass -> {
if (hasSpringAnnotation(jclass)) {
springBeans.add(jclass);
// 获取[Redacted Person]名称
String beanName = extractBeanName(jclass);
beanNameToClass.put(beanName, jclass);
logger.info("Found Spring Bean: {} ({})", beanName, jclass.getName());
}
});
}
private boolean hasSpringAnnotation(JClass jclass) {
return jclass.getAnnotationHolder()
.getAnnotations()
.stream()
.map(Annotation::getType)
.anyMatch(SPRING_ANNOTATIONS::contains);
}
private String extractBeanName(JClass jclass) {
// 从@Component等注解中提取bean名称
for (Annotation annotation : jclass.getAnnotationHolder().getAnnotations()) {
if (SPRING_ANNOTATIONS.contains(annotation.getType())) {
String value = annotation.getElementValue("value");
if (value != null && !value.isEmpty()) {
return value;
}
}
}
// 默认使用类名首字母小写
String className = jclass.getSimpleName();
return Character.toLowerCase(className.charAt(0)) + className.substring(1);
}
private void checkSpringAnnotations(JMethod method) {
// 检查@Autowired注解
if (method.getAnnotationHolder().hasAnnotation(
"org.springframework.beans.factory.annotation.Autowired")) {
handleAutowired(method);
}
// 检查@[Redacted Person]注解
if (method.getAnnotationHolder().hasAnnotation(
"org.springframework.context.annotation.Bean")) {
handleBeanMethod(method);
}
}
private void checkParameterAnnotations(JMethod method) {
if (method.getParamAnnotations() != null) {
for (int i = 0; i < method.getParamAnnotations().size(); i++) {
var annotations = method.getParamAnnotations().get(i);
if (annotations.hasAnnotation(
"org.springframework.beans.factory.annotation.Autowired")) {
Type paramType = method.getParameterTypes().get(i);
handleParameterInjection(method, paramType, i);
}
}
}
}
private void handleAutowired(JMethod method) {
// 处理构造方法注入
if (method.isConstructor()) {
handleConstructorInjection(method);
}
// 处理setter方法注入
else {
handleSetterInjection(method);
}
}
private void handleConstructorInjection(JMethod constructor) {
JClass declaringClass = constructor.getDeclaringClass();
for (Type paramType : constructor.getParameterTypes()) {
if (paramType instanceof ClassType classType) {
// 记录构造方法注入点
Set<JClass> candidates = findInjectableBeans(classType);
for (JClass candidate : candidates) {
addInjectionPoint(declaringClass, candidate);
}
}
}
}
private void handleSetterInjection(JMethod setter) {
if (!setter.getParameterTypes().isEmpty()) {
Type paramType = setter.getParameterTypes().get(0);
if (paramType instanceof ClassType classType) {
// 记录setter注入点
Set<JClass> candidates = findInjectableBeans(classType);
for (JClass candidate : candidates) {
addInjectionPoint(setter.getDeclaringClass(), candidate);
}
}
}
}
private void handleParameterInjection(JMethod method, Type paramType, int paramIndex) {
if (paramType instanceof ClassType classType) {
Set<JClass> candidates = findInjectableBeans(classType);
for (JClass candidate : candidates) {
addInjectionPoint(method.getDeclaringClass(), candidate);
}