From cc9a78ecff4511f4f2ab12a59d6377831808ad1f Mon Sep 17 00:00:00 2001 From: Yuyan <845058547@qq.com> Date: Tue, 29 Oct 2019 18:46:12 +0800 Subject: [PATCH 1/2] useless commit --- .../Useless/AnnotationPractise.java | 13 ++++ .../github/hcsp/annotation/Useless/Log.java | 13 ++++ .../github/hcsp/annotation/Useless/Main.java | 67 +++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 src/main/java/com/github/hcsp/annotation/Useless/AnnotationPractise.java create mode 100644 src/main/java/com/github/hcsp/annotation/Useless/Log.java create mode 100644 src/main/java/com/github/hcsp/annotation/Useless/Main.java diff --git a/src/main/java/com/github/hcsp/annotation/Useless/AnnotationPractise.java b/src/main/java/com/github/hcsp/annotation/Useless/AnnotationPractise.java new file mode 100644 index 0000000..616f110 --- /dev/null +++ b/src/main/java/com/github/hcsp/annotation/Useless/AnnotationPractise.java @@ -0,0 +1,13 @@ +package com.github.hcsp.annotation.Useless; + +public class AnnotationPractise { + @Log + void queryDatabase() { + System.out.println("queryDatabase..."); + } + + @Log + void selectDatabase() { + System.out.println("selectDatabase..."); + } +} diff --git a/src/main/java/com/github/hcsp/annotation/Useless/Log.java b/src/main/java/com/github/hcsp/annotation/Useless/Log.java new file mode 100644 index 0000000..6a4783b --- /dev/null +++ b/src/main/java/com/github/hcsp/annotation/Useless/Log.java @@ -0,0 +1,13 @@ +package com.github.hcsp.annotation.Useless; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Log { + +} diff --git a/src/main/java/com/github/hcsp/annotation/Useless/Main.java b/src/main/java/com/github/hcsp/annotation/Useless/Main.java new file mode 100644 index 0000000..b21bdf4 --- /dev/null +++ b/src/main/java/com/github/hcsp/annotation/Useless/Main.java @@ -0,0 +1,67 @@ +package com.github.hcsp.annotation.Useless; + +import net.bytebuddy.ByteBuddy; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.implementation.MethodDelegation; +import net.bytebuddy.implementation.bind.annotation.SuperCall; +import net.bytebuddy.matcher.ElementMatcher; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Main { + public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + AnnotationPractise annotationPractise = enhanceAnnotation(); + annotationPractise.selectDatabase(); + annotationPractise.queryDatabase(); + } + + + + private static AnnotationPractise enhanceAnnotation() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { + return new ByteBuddy() + //动态的生成AnnotationPractise的子类 + .subclass(AnnotationPractise.class) + //匹配带Log注解的方法 + .method(new FilterAnnotationWithLog()) + //将方法拦截并委托给LoggerInterceptor + .intercept(MethodDelegation.to(LoggerInterceptor.class)) + .make() + .load(Main.class.getClassLoader()) + .getLoaded() + .getConstructor() + .newInstance(); + } + + public static class FilterAnnotationWithLog implements ElementMatcher { + + @Override + public boolean matches(MethodDescription target) { + //获取自身所有的方法,并将其转换为流 + List collect = Stream.of(AnnotationPractise.class.getDeclaredMethods()) + //过滤出方法的注解为Log的方法 + .filter(method -> method.getAnnotation(Log.class) != null) + .map(Method::getName) + .collect(Collectors.toList()); + System.out.println(target.getName()); + return collect.contains(target.getName()); + } + } + + public static class LoggerInterceptor { + public static void log(@SuperCall Callable zuper) + throws Exception { + System.out.println("Calling database"); + try { + zuper.call(); + } finally { + System.out.println("Returned from database"); + } + } + } + +} From 21344c670e455d8725c2fef9e6c0c919dc403082 Mon Sep 17 00:00:00 2001 From: Yuyan <845058547@qq.com> Date: Tue, 29 Oct 2019 18:49:39 +0800 Subject: [PATCH 2/2] useless pr --- .../java/com/github/hcsp/annotation/CacheClassDecorator.java | 1 + .../com/github/hcsp/annotation/Useless/AnnotationPractise.java | 1 + src/main/java/com/github/hcsp/annotation/Useless/Log.java | 1 + 3 files changed, 3 insertions(+) diff --git a/src/main/java/com/github/hcsp/annotation/CacheClassDecorator.java b/src/main/java/com/github/hcsp/annotation/CacheClassDecorator.java index fb5d531..4503bf7 100644 --- a/src/main/java/com/github/hcsp/annotation/CacheClassDecorator.java +++ b/src/main/java/com/github/hcsp/annotation/CacheClassDecorator.java @@ -9,6 +9,7 @@ public class CacheClassDecorator { // 注意,缓存的实现需要是线程安全的 public static Class decorate(Class klass) { return klass; + } public static void main(String[] args) throws Exception { diff --git a/src/main/java/com/github/hcsp/annotation/Useless/AnnotationPractise.java b/src/main/java/com/github/hcsp/annotation/Useless/AnnotationPractise.java index 616f110..e2e5f07 100644 --- a/src/main/java/com/github/hcsp/annotation/Useless/AnnotationPractise.java +++ b/src/main/java/com/github/hcsp/annotation/Useless/AnnotationPractise.java @@ -10,4 +10,5 @@ void queryDatabase() { void selectDatabase() { System.out.println("selectDatabase..."); } + } diff --git a/src/main/java/com/github/hcsp/annotation/Useless/Log.java b/src/main/java/com/github/hcsp/annotation/Useless/Log.java index 6a4783b..a26d7f5 100644 --- a/src/main/java/com/github/hcsp/annotation/Useless/Log.java +++ b/src/main/java/com/github/hcsp/annotation/Useless/Log.java @@ -10,4 +10,5 @@ @Retention(RetentionPolicy.RUNTIME) public @interface Log { + }