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