|
| 1 | +/* |
| 2 | + * Copyright 2018 National Bank of Belgium |
| 3 | + * |
| 4 | + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved |
| 5 | + * by the European Commission - subsequent versions of the EUPL (the "Licence"); |
| 6 | + * You may not use this work except in compliance with the Licence. |
| 7 | + * You may obtain a copy of the Licence at: |
| 8 | + * |
| 9 | + * http://ec.europa.eu/idabc/eupl |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the Licence is distributed on an "AS IS" basis, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the Licence for the specific language governing permissions and |
| 15 | + * limitations under the Licence. |
| 16 | + */ |
| 17 | +package internal.nbbrd.design; |
| 18 | + |
| 19 | +import internal.nbbrd.design.proc.Elements2; |
| 20 | +import internal.nbbrd.design.proc.Processing; |
| 21 | +import internal.nbbrd.design.proc.Processors; |
| 22 | +import internal.nbbrd.design.proc.Rule; |
| 23 | +import nbbrd.design.DecoratorPattern; |
| 24 | +import nbbrd.service.ServiceProvider; |
| 25 | + |
| 26 | +import javax.annotation.processing.*; |
| 27 | +import javax.lang.model.SourceVersion; |
| 28 | +import javax.lang.model.element.ElementKind; |
| 29 | +import javax.lang.model.element.TypeElement; |
| 30 | +import javax.lang.model.type.NoType; |
| 31 | +import javax.lang.model.type.TypeKind; |
| 32 | +import javax.lang.model.type.TypeMirror; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Optional; |
| 35 | +import java.util.Set; |
| 36 | + |
| 37 | +import static internal.nbbrd.design.proc.Processors.extractResultType; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author Philippe Charles |
| 41 | + */ |
| 42 | +@ServiceProvider(Processor.class) |
| 43 | +@SupportedAnnotationTypes("nbbrd.design.DecoratorPattern") |
| 44 | +public final class DecoratorPatternProcessor extends AbstractProcessor { |
| 45 | + |
| 46 | + @Override |
| 47 | + public SourceVersion getSupportedSourceVersion() { |
| 48 | + return SourceVersion.latestSupported(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 53 | + return Processing.of(IS_DECORATOR_PATTERN).process(annotations, roundEnv, processingEnv); |
| 54 | + } |
| 55 | + |
| 56 | + private static final Rule<TypeElement> IS_DECORATOR_PATTERN = |
| 57 | + Rule.on(TypeElement.class) |
| 58 | + .and(Rule.is(ElementKind.CLASS)) |
| 59 | + .and(Rule.of(DecoratorPatternProcessor::isExtendingDecoratedType, "'%s' doesn't extend a class")) |
| 60 | + .and(Rule.of(DecoratorPatternProcessor::hasConstructorWithDecoratedType, "'%s' doesn't have a constructor with the decorated type")); |
| 61 | + |
| 62 | + private static boolean isExtendingDecoratedType(ProcessingEnvironment env, TypeElement type) { |
| 63 | + return getDecoratedType(env, type) |
| 64 | + .filter(decoratedType -> isExtendingDecoratedType(env, type, decoratedType)) |
| 65 | + .isPresent(); |
| 66 | + } |
| 67 | + |
| 68 | + private static boolean isExtendingDecoratedType(ProcessingEnvironment env, TypeElement type, TypeMirror decoratedType) { |
| 69 | + return env.getTypeUtils().isAssignable(type.getSuperclass(), decoratedType) || |
| 70 | + type.getInterfaces().stream().anyMatch(x -> env.getTypeUtils().isAssignable(x, decoratedType)); |
| 71 | + } |
| 72 | + |
| 73 | + private static boolean hasConstructorWithDecoratedType(ProcessingEnvironment env, TypeElement type) { |
| 74 | + return getDecoratedType(env, type) |
| 75 | + .filter(decoratedType -> hasConstructorWithDecoratedType(env, type, decoratedType)) |
| 76 | + .isPresent(); |
| 77 | + } |
| 78 | + |
| 79 | + private static boolean hasConstructorWithDecoratedType(ProcessingEnvironment env, TypeElement type, TypeMirror decoratedType) { |
| 80 | + return Elements2.constructorsIn(type) |
| 81 | + .flatMap(method -> method.getParameters().stream()) |
| 82 | + .anyMatch(var -> env.getTypeUtils().isSameType(decoratedType, var.asType())); |
| 83 | + } |
| 84 | + |
| 85 | + private static boolean isNullType(ProcessingEnvironment env, TypeMirror expected) { |
| 86 | + return env.getTypeUtils().isSameType(Processors.getTypeMirror(env, Void.class), expected); |
| 87 | + } |
| 88 | + |
| 89 | + private static boolean isDirectSuperClass(TypeMirror found) { |
| 90 | + return !(found instanceof NoType && found.getKind() == TypeKind.NONE) && !(found.toString().equals("java.lang.Object")); |
| 91 | + } |
| 92 | + |
| 93 | + private static Optional<TypeMirror> inferDecoratedType(TypeElement type) { |
| 94 | + TypeMirror superclass = type.getSuperclass(); |
| 95 | + List<? extends TypeMirror> parents = type.getInterfaces(); |
| 96 | + return isDirectSuperClass(superclass) && parents.isEmpty() |
| 97 | + ? Optional.of(superclass) |
| 98 | + : (!isDirectSuperClass(superclass) && parents.size() == 1 ? Optional.of(parents.get(0)) : Optional.empty()); |
| 99 | + } |
| 100 | + |
| 101 | + private static Optional<TypeMirror> getDecoratedType(ProcessingEnvironment env, TypeElement type) { |
| 102 | + TypeMirror result = extractResultType(type.getAnnotation(DecoratorPattern.class)::value); |
| 103 | + return isNullType(env, result) ? inferDecoratedType(type) : Optional.of(result); |
| 104 | + } |
| 105 | +} |
0 commit comments