|
| 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.Rule; |
| 22 | +import nbbrd.service.ServiceProvider; |
| 23 | + |
| 24 | +import javax.annotation.processing.AbstractProcessor; |
| 25 | +import javax.annotation.processing.Processor; |
| 26 | +import javax.annotation.processing.RoundEnvironment; |
| 27 | +import javax.annotation.processing.SupportedAnnotationTypes; |
| 28 | +import javax.lang.model.SourceVersion; |
| 29 | +import javax.lang.model.element.TypeElement; |
| 30 | +import java.util.Set; |
| 31 | + |
| 32 | +import static internal.nbbrd.design.proc.Rule.*; |
| 33 | +import static javax.lang.model.element.ElementKind.INTERFACE; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author Philippe Charles |
| 37 | + */ |
| 38 | +@ServiceProvider(Processor.class) |
| 39 | +@SupportedAnnotationTypes("nbbrd.design.Trait") |
| 40 | +public final class TraitProcessor extends AbstractProcessor { |
| 41 | + |
| 42 | + @Override |
| 43 | + public SourceVersion getSupportedSourceVersion() { |
| 44 | + return SourceVersion.latestSupported(); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 49 | + return Processing.of(IS_TRAIT).process(annotations, roundEnv, processingEnv); |
| 50 | + } |
| 51 | + |
| 52 | + private static boolean hasAtLeastOneMethod(TypeElement type) { |
| 53 | + return Elements2.methodsIn(type).findAny().isPresent(); |
| 54 | + } |
| 55 | + |
| 56 | + static boolean isValidName(String name) { |
| 57 | + return name.endsWith("ble") || name.startsWith("Has"); |
| 58 | + } |
| 59 | + |
| 60 | + private static final Rule<TypeElement> IS_TRAIT = Rule.on(TypeElement.class) |
| 61 | + .and(is(INTERFACE)) |
| 62 | + .and(isNamedTesting(TraitProcessor::isValidName, "must end with 'ble' or start with 'Has'")) |
| 63 | + .and(it(TraitProcessor::hasAtLeastOneMethod, "'%s' must have at least one method")); |
| 64 | +} |
0 commit comments