提交 97c1fc3b 编写于 作者: Z zhangxin10

1.添加拷贝类上的注解以及方法和参数注解

2.修改jdbc插件的编译级别
上级 9d47acbf
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version> <version>5.1.36</version>
<scope>test</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -40,6 +40,12 @@ ...@@ -40,6 +40,12 @@
<version>3.2.0.RELEASE</version> <version>3.2.0.RELEASE</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId> <artifactId>spring-context</artifactId>
...@@ -64,5 +70,10 @@ ...@@ -64,5 +70,10 @@
<version>${project.version}</version> <version>${project.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -3,6 +3,7 @@ package com.ai.cloud.skywalking.plugin.spring; ...@@ -3,6 +3,7 @@ package com.ai.cloud.skywalking.plugin.spring;
import com.ai.cloud.skywalking.buriedpoint.LocalBuriedPointSender; import com.ai.cloud.skywalking.buriedpoint.LocalBuriedPointSender;
import com.ai.cloud.skywalking.model.Identification; import com.ai.cloud.skywalking.model.Identification;
import javassist.*; import javassist.*;
import javassist.bytecode.*;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
...@@ -10,12 +11,17 @@ import java.lang.reflect.Constructor; ...@@ -10,12 +11,17 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TracingEnhanceProcessor implements BeanPostProcessor { public class TracingEnhanceProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { private Logger logger = Logger.getLogger(TracingEnhanceProcessor.class.getName());
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
List<Method> methods = new ArrayList<Method>(); List<Method> methods = new ArrayList<Method>();
for (Method method : bean.getClass().getMethods()) { for (Method method : bean.getClass().getMethods()) {
if (method.isAnnotationPresent(Tracing.class)) { if (method.isAnnotationPresent(Tracing.class)) {
...@@ -24,9 +30,12 @@ public class TracingEnhanceProcessor implements BeanPostProcessor { ...@@ -24,9 +30,12 @@ public class TracingEnhanceProcessor implements BeanPostProcessor {
} }
if (methods.size() > 0) { if (methods.size() > 0) {
try { try {
ClassPool mPool = new ClassPool(true); ClassPool mPool = ClassPool.getDefault();
mPool.appendClassPath(new ClassClassPath(bean.getClass()));
//创建代理类 //创建代理类
CtClass mCtc = createProxyClass(bean, mPool); CtClass mCtc = createProxyClass(bean, mPool);
//拷贝类上注解
copyClassesAnnotations(bean, mPool, mCtc);
//代理类继承所有的被代理的接口 //代理类继承所有的被代理的接口
inheritanceAllInterfaces(bean, mPool, mCtc); inheritanceAllInterfaces(bean, mPool, mCtc);
// //
...@@ -45,9 +54,13 @@ public class TracingEnhanceProcessor implements BeanPostProcessor { ...@@ -45,9 +54,13 @@ public class TracingEnhanceProcessor implements BeanPostProcessor {
result.append(generateException(method)); result.append(generateException(method));
// 生成方法体 // 生成方法体
result.append(generateMethodBody(bean, method)); result.append(generateMethodBody(bean, method));
// TODO 注解 CtMethod dest = CtMethod.make(result.toString(), mCtc);
// TODO 参数注解
mCtc.addMethod(CtMethod.make(result.toString(), mCtc)); // 拷贝方法上的注解
CtMethod origin = convertMethod2CtMethod(bean, mPool, method);
copyAllAnnotation(origin.getMethodInfo(), dest.getMethodInfo());
mCtc.addMethod(dest);
} }
mCtc.addConstructor(CtNewConstructor.make("public " + mCtc.getSimpleName() + "(" + LocalBuriedPointSender.class mCtc.addConstructor(CtNewConstructor.make("public " + mCtc.getSimpleName() + "(" + LocalBuriedPointSender.class
.getName() + " buriedPoint, " + bean.getClass().getName() + " realBean){" + .getName() + " buriedPoint, " + bean.getClass().getName() + " realBean){" +
...@@ -57,23 +70,69 @@ public class TracingEnhanceProcessor implements BeanPostProcessor { ...@@ -57,23 +70,69 @@ public class TracingEnhanceProcessor implements BeanPostProcessor {
Constructor<?> constructor = classes.getConstructor(LocalBuriedPointSender.class, bean.getClass()); Constructor<?> constructor = classes.getConstructor(LocalBuriedPointSender.class, bean.getClass());
return constructor.newInstance(new LocalBuriedPointSender(), bean); return constructor.newInstance(new LocalBuriedPointSender(), bean);
} catch (CannotCompileException e) { } catch (CannotCompileException e) {
e.printStackTrace(); logger.log(Level.ALL, "Failed to create the instance of the class[" + beanName + "]", e);
} catch (InstantiationException e) { } catch (InstantiationException e) {
e.printStackTrace(); logger.log(Level.ALL, "Failed to create the instance of the class[" + beanName + "]", e);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); logger.log(Level.ALL, "Failed to create the instance of the class[" + beanName + "]", e);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
e.printStackTrace(); logger.log(Level.ALL, "Failed to create the instance of the class[" + beanName + "]", e);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
e.printStackTrace(); logger.log(Level.ALL, "Failed to create the instance of the class[" + beanName + "]", e);
} catch (NotFoundException e) { } catch (NotFoundException e) {
e.printStackTrace(); logger.log(Level.ALL, "Failed to create the instance of the class[" + beanName + "]", e);
} }
} }
return bean; return bean;
} }
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
private CtMethod convertMethod2CtMethod(Object bean, ClassPool mPool, Method method) throws NotFoundException {
int i = 0;
CtClass ctClass = mPool.get(bean.getClass().getName());
CtClass[] parameterClass = new CtClass[method.getParameterTypes().length];
for (Class methodClass : method.getParameterTypes()) {
parameterClass[i++] = mPool.get(methodClass.getName());
}
return ctClass.getDeclaredMethod(method.getName(), parameterClass);
}
private void copyClassesAnnotations(Object bean, ClassPool mPool, CtClass mCtc) throws NotFoundException {
// 拷贝类上注解
CtClass originCtClass = mPool.get(bean.getClass().getName());
AnnotationsAttribute annotations = (AnnotationsAttribute) originCtClass.getClassFile().
getAttribute(AnnotationsAttribute.visibleTag);
AttributeInfo newAnnotations = annotations.copy(mCtc.getClassFile().getConstPool(), Collections.EMPTY_MAP);
mCtc.getClassFile().addAttribute(newAnnotations);
}
private void copyAllAnnotation(MethodInfo origin, MethodInfo dest) {
AnnotationsAttribute annotations = (AnnotationsAttribute) origin.getAttribute(AnnotationsAttribute.visibleTag);
ParameterAnnotationsAttribute pannotations = (ParameterAnnotationsAttribute) origin.getAttribute(ParameterAnnotationsAttribute.visibleTag);
ExceptionsAttribute exAt = (ExceptionsAttribute) origin.getAttribute(ExceptionsAttribute.tag);
SignatureAttribute sigAt = (SignatureAttribute) origin.getAttribute(SignatureAttribute.tag);
if (annotations != null) {
AttributeInfo newAnnotations = annotations.copy(dest.getConstPool(), Collections.EMPTY_MAP);
dest.addAttribute(newAnnotations);
}
if (pannotations != null) {
AttributeInfo newAnnotations = pannotations.copy(dest.getConstPool(), Collections.EMPTY_MAP);
dest.addAttribute(newAnnotations);
}
if (sigAt != null) {
AttributeInfo newAnnotations = sigAt.copy(dest.getConstPool(), Collections.EMPTY_MAP);
dest.addAttribute(newAnnotations);
}
if (exAt != null) {
AttributeInfo newAnnotations = exAt.copy(dest.getConstPool(), Collections.EMPTY_MAP);
dest.addAttribute(newAnnotations);
}
}
private String generateMethodBody(Object bean, Method method) { private String generateMethodBody(Object bean, Method method) {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
result.append("{"); result.append("{");
...@@ -160,7 +219,7 @@ public class TracingEnhanceProcessor implements BeanPostProcessor { ...@@ -160,7 +219,7 @@ public class TracingEnhanceProcessor implements BeanPostProcessor {
} }
private CtClass createProxyClass(Object bean, ClassPool mPool) { private CtClass createProxyClass(Object bean, ClassPool mPool) {
return mPool.makeClass(bean.getClass().getSimpleName() + "$EnhanceBySWTracing$" + ThreadLocalRandom.current().nextInt(100)); return mPool.makeClass(bean.getClass().getName() + "$EnhanceBySWTracing$" + ThreadLocalRandom.current().nextInt(100));
} }
private void inheritanceAllInterfaces(Object bean, ClassPool mPool, CtClass mCtc) throws NotFoundException { private void inheritanceAllInterfaces(Object bean, ClassPool mPool, CtClass mCtc) throws NotFoundException {
...@@ -168,8 +227,4 @@ public class TracingEnhanceProcessor implements BeanPostProcessor { ...@@ -168,8 +227,4 @@ public class TracingEnhanceProcessor implements BeanPostProcessor {
mCtc.addInterface(mPool.get(classes.getClass().getName())); mCtc.addInterface(mPool.get(classes.getClass().getName()));
} }
} }
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册