|
| 1 | +package org.jddd.spring; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import net.bytebuddy.build.Plugin.NoOp; |
| 5 | +import net.bytebuddy.description.annotation.AnnotationDescription; |
| 6 | +import net.bytebuddy.description.type.TypeDescription; |
| 7 | +import net.bytebuddy.dynamic.ClassFileLocator; |
| 8 | +import net.bytebuddy.dynamic.DynamicType.Builder; |
| 9 | + |
| 10 | +import java.lang.annotation.Annotation; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.Set; |
| 14 | + |
| 15 | +import org.jddd.core.annotation.Repository; |
| 16 | +import org.jddd.core.annotation.Service; |
| 17 | +import org.springframework.stereotype.Component; |
| 18 | + |
| 19 | +@Slf4j |
| 20 | +public class JDddSpringPlugin extends NoOp { |
| 21 | + |
| 22 | + private static Set<Class<?>> ANNOTATIONS = new HashSet<>( |
| 23 | + Arrays.asList(Service.class, Repository.class, Factory.class)); |
| 24 | + |
| 25 | + @Override |
| 26 | + public boolean matches(TypeDescription target) { |
| 27 | + return ANNOTATIONS.stream().anyMatch(it -> isAnnotatedWith(target, it)); |
| 28 | + } |
| 29 | + |
| 30 | + /* |
| 31 | + * (non-Javadoc) |
| 32 | + * @see net.bytebuddy.build.Plugin#apply(net.bytebuddy.dynamic.DynamicType.Builder, net.bytebuddy.description.type.TypeDescription, net.bytebuddy.dynamic.ClassFileLocator) |
| 33 | + */ |
| 34 | + @Override |
| 35 | + public Builder<?> apply(Builder<?> builder, TypeDescription type, ClassFileLocator classFileLocator) { |
| 36 | + |
| 37 | + if (isAnnotatedWith(type, Service.class)) { |
| 38 | + builder = addAnnotationIfMissing(org.springframework.stereotype.Service.class, builder, type); |
| 39 | + } |
| 40 | + |
| 41 | + if (isAnnotatedWith(type, Repository.class)) { |
| 42 | + builder = addAnnotationIfMissing(org.springframework.stereotype.Repository.class, builder, type); |
| 43 | + } |
| 44 | + |
| 45 | + if (isAnnotatedWith(type, org.jddd.core.annotation.Factory.class)) { |
| 46 | + builder = addAnnotationIfMissing(Component.class, builder, type); |
| 47 | + } |
| 48 | + |
| 49 | + return builder; |
| 50 | + } |
| 51 | + |
| 52 | + private static Builder<?> addAnnotationIfMissing(Class<? extends Annotation> annotation, Builder<?> builder, |
| 53 | + TypeDescription type) { |
| 54 | + |
| 55 | + if (isAnnotatedWith(type, annotation)) { |
| 56 | + return builder; |
| 57 | + } |
| 58 | + |
| 59 | + log.info("jDDD Spring Plugin - Annotating {} with {}.", type.getName(), annotation.getName()); |
| 60 | + |
| 61 | + return builder.annotateType(getAnnotation(annotation)); |
| 62 | + } |
| 63 | + |
| 64 | + private static AnnotationDescription getAnnotation(Class<? extends Annotation> type) { |
| 65 | + return AnnotationDescription.Builder.ofType(type).build(); |
| 66 | + } |
| 67 | + |
| 68 | + private static boolean isAnnotatedWith(TypeDescription type, Class<?> annotationType) { |
| 69 | + |
| 70 | + return type.getDeclaredAnnotations() // |
| 71 | + .asTypeList() // |
| 72 | + .stream() // |
| 73 | + .anyMatch(it -> it.isAssignableTo(annotationType)); // || isAnnotatedWith(it, annotationType)); |
| 74 | + } |
| 75 | +} |
0 commit comments