001package ball.annotation.processing;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: NoOverrideProcessor.java 5903 2020-05-08 22:19:33Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/annotation/processing/NoOverrideProcessor.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.ServiceProviderFor;
024import javax.annotation.processing.ProcessingEnvironment;
025import javax.annotation.processing.Processor;
026import javax.annotation.processing.RoundEnvironment;
027import javax.lang.model.element.Element;
028import javax.lang.model.element.ExecutableElement;
029import lombok.NoArgsConstructor;
030import lombok.ToString;
031
032import static javax.lang.model.element.ElementKind.METHOD;
033import static javax.lang.model.element.Modifier.PRIVATE;
034import static javax.lang.model.element.Modifier.STATIC;
035import static javax.tools.Diagnostic.Kind.ERROR;
036import static javax.tools.Diagnostic.Kind.WARNING;
037
038/**
039 * {@link Processor} implementation to identify overriding
040 * {@link java.lang.reflect.Method}s that are not marked with the
041 * {@link Override} {@link java.lang.annotation.Annotation}.
042 *
043 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
044 * @version $Revision: 5903 $
045 */
046@ServiceProviderFor({ Processor.class })
047@ForElementKinds({ METHOD })
048@WithoutModifiers({ PRIVATE, STATIC })
049@NoArgsConstructor @ToString
050public class NoOverrideProcessor extends AnnotatedNoAnnotationProcessor {
051    @Override
052    public void init(ProcessingEnvironment processingEnv) {
053        super.init(processingEnv);
054
055        try {
056            criteria.add(t -> t.getAnnotation(Override.class) == null);
057        } catch (Exception exception) {
058            print(ERROR, exception);
059        }
060    }
061
062    @Override
063    protected void process(RoundEnvironment roundEnv, Element element) {
064        super.process(roundEnv, element);
065
066        ExecutableElement method = (ExecutableElement) element;
067        ExecutableElement specification = specifiedBy(method);
068
069        if (specification != null) {
070            print(WARNING, method,
071                  "Missing @%s annotation (specified by %s)",
072                  Override.class.getSimpleName(),
073                  specification.getEnclosingElement());
074        }
075    }
076}