001package ball.annotation.processing;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: ObjectToStringProcessor.java 6062 2020-05-28 05:35:23Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/annotation/processing/ObjectToStringProcessor.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 java.lang.reflect.Method;
025import java.util.Objects;
026import java.util.ResourceBundle;
027import java.util.Set;
028import javax.annotation.processing.ProcessingEnvironment;
029import javax.annotation.processing.Processor;
030import javax.annotation.processing.RoundEnvironment;
031import javax.lang.model.element.AnnotationMirror;
032import javax.lang.model.element.Element;
033import javax.lang.model.element.ExecutableElement;
034import javax.lang.model.element.TypeElement;
035import lombok.NoArgsConstructor;
036import lombok.ToString;
037
038import static java.util.Collections.disjoint;
039import static java.util.stream.Collectors.toSet;
040import static javax.lang.model.element.ElementKind.CLASS;
041import static javax.lang.model.element.Modifier.ABSTRACT;
042import static javax.tools.Diagnostic.Kind.ERROR;
043import static javax.tools.Diagnostic.Kind.WARNING;
044
045/**
046 * {@link Processor} implementation to check {@link Class}es to verify:
047 * <ol>
048 *   <li value="1">
049 *     The implementing {@link Class} also overrides {@link Object#toString()}
050 *   </li>
051 * </ol>
052 *
053 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
054 * @version $Revision: 6062 $
055 */
056@ServiceProviderFor({ Processor.class })
057@ForElementKinds({ CLASS })
058@ForSubclassesOf(Object.class)
059@WithoutModifiers({ ABSTRACT })
060@NoArgsConstructor @ToString
061public class ObjectToStringProcessor extends AnnotatedNoAnnotationProcessor {
062    private static final Method PROTOTYPE;
063
064    static {
065        try {
066            PROTOTYPE = Object.class.getDeclaredMethod("toString");
067        } catch (Exception exception) {
068            throw new ExceptionInInitializerError(exception);
069        }
070    }
071
072    private static final Set<String> ANNOTATIONS =
073        ResourceBundle.getBundle(ObjectToStringProcessor.class.getName())
074        .keySet();
075
076    private ExecutableElement METHOD = null;
077
078    @Override
079    public void init(ProcessingEnvironment processingEnv) {
080        super.init(processingEnv);
081
082        try {
083            METHOD = getMethod(PROTOTYPE);
084        } catch (Exception exception) {
085            print(ERROR, exception);
086        }
087    }
088
089    @Override
090    protected void process(RoundEnvironment roundEnv, Element element) {
091        TypeElement type = (TypeElement) element;
092        Set<String> annotations =
093            type.getAnnotationMirrors()
094            .stream()
095            .map(AnnotationMirror::getAnnotationType)
096            .map(Objects::toString)
097            .collect(toSet());
098
099        if (disjoint(ANNOTATIONS, annotations)) {
100            ExecutableElement implementation = implementationOf(METHOD, type);
101
102            if (implementation == null || METHOD.equals(implementation)) {
103                print(WARNING, type,
104                      "%s does not override '%s'",
105                      type.getKind(), declaration(PROTOTYPE));
106            }
107        }
108    }
109}