001package ball.annotation.processing;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: ObjectToStringProcessor.java 8244 2021-07-13 10:08:17Z 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 - 2021 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 javax.lang.model.type.TypeMirror;
036import lombok.NoArgsConstructor;
037import lombok.ToString;
038
039import static javax.lang.model.element.ElementKind.CLASS;
040import static javax.lang.model.element.Modifier.ABSTRACT;
041import static javax.lang.model.type.TypeKind.NONE;
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: 8244 $
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            PROTOTYPE.setAccessible(true);
068        } catch (Exception exception) {
069            throw new ExceptionInInitializerError(exception);
070        }
071    }
072
073    private static final Set<String> ANNOTATIONS =
074        ResourceBundle.getBundle(ObjectToStringProcessor.class.getName())
075        .keySet();
076
077    private ExecutableElement METHOD = null;
078
079    @Override
080    public void init(ProcessingEnvironment processingEnv) {
081        super.init(processingEnv);
082
083        try {
084            METHOD = asExecutableElement(PROTOTYPE);
085        } catch (Exception exception) {
086            print(ERROR, exception);
087        }
088    }
089
090    @Override
091    protected void process(RoundEnvironment roundEnv, Element element) {
092        if (! isGenerated(element)) {
093            TypeElement type = (TypeElement) element;
094
095            if (! isAnnotated(type)) {
096                ExecutableElement implementation = implementationOf(METHOD, type);
097
098                if (implementation == null || METHOD.equals(implementation)) {
099                    print(WARNING, type,
100                          "%s does not override '%s'",
101                          type.getKind(), declaration(PROTOTYPE));
102                }
103            }
104        }
105    }
106
107    private boolean isAnnotated(TypeElement element) {
108        boolean found =
109            element.getAnnotationMirrors()
110            .stream()
111            .map(AnnotationMirror::getAnnotationType)
112            .map(Objects::toString)
113            .anyMatch(ANNOTATIONS::contains);
114
115        if (! found) {
116            TypeMirror mirror = element.getSuperclass();
117
118            if (mirror != null && (! mirror.getKind().equals(NONE))) {
119                found |= isAnnotated((TypeElement) types.asElement(mirror));
120            }
121        }
122
123        return found;
124    }
125}