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