001package ball.annotation.processing;
002/*-
003 * ##########################################################################
004 * Utilities
005 * %%
006 * Copyright (C) 2008 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import ball.annotation.MatcherGroup;
022import ball.annotation.ServiceProviderFor;
023import javax.annotation.processing.Processor;
024import javax.annotation.processing.RoundEnvironment;
025import javax.lang.model.element.AnnotationMirror;
026import javax.lang.model.element.AnnotationValue;
027import javax.lang.model.element.Element;
028import javax.lang.model.element.ExecutableElement;
029import javax.lang.model.element.TypeElement;
030import lombok.NoArgsConstructor;
031import lombok.ToString;
032
033import static javax.lang.model.element.Modifier.PRIVATE;
034import static javax.tools.Diagnostic.Kind.ERROR;
035
036/**
037 * {@link Processor} implementation to check {@link MatcherGroup}
038 * annotations.
039 *
040 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
041 */
042@ServiceProviderFor({ Processor.class })
043@For({ MatcherGroup.class })
044@NoArgsConstructor @ToString
045public class MatcherGroupProcessor extends AnnotatedProcessor {
046    @Override
047    protected void process(RoundEnvironment roundEnv, TypeElement annotation, Element element) {
048        super.process(roundEnv, annotation, element);
049
050        AnnotationMirror mirror = getAnnotationMirror(element, annotation);
051        AnnotationValue value = getAnnotationValue(mirror, "value");
052        int group = (Integer) value.getValue();
053
054        if (group < 0) {
055            print(ERROR, element, mirror, value, "value() must be non-negative");
056        }
057
058        switch (element.getKind()) {
059        case ANNOTATION_TYPE:
060        case FIELD:
061        default:
062            break;
063
064        case METHOD:
065            ExecutableElement executable = (ExecutableElement) element;
066
067            if (withoutModifiers(PRIVATE).test(element)) {
068                if (executable.isVarArgs() || executable.getParameters().size() != 1) {
069                    print(ERROR, element,
070                          "@%s: %s must take exactly one argument",
071                          annotation.getSimpleName(), element.getKind());
072                }
073            } else {
074                print(ERROR, element, "@%s: %s is %s", annotation.getSimpleName(), element.getKind(), PRIVATE);
075            }
076            break;
077        }
078    }
079}