001package ball.annotation.processing;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: ObjectToStringProcessor.java 7486 2021-02-15 00:02:29Z 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: 7486 $
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        TypeElement type = (TypeElement) element;
093
094        if (! isAnnotated(type)) {
095            ExecutableElement implementation = implementationOf(METHOD, type);
096
097            if (implementation == null || METHOD.equals(implementation)) {
098                print(WARNING, type,
099                      "%s does not override '%s'",
100                      type.getKind(), declaration(PROTOTYPE));
101            }
102        }
103    }
104
105    private boolean isAnnotated(TypeElement element) {
106        boolean found =
107            element.getAnnotationMirrors()
108            .stream()
109            .map(AnnotationMirror::getAnnotationType)
110            .map(Objects::toString)
111            .anyMatch(ANNOTATIONS::contains);
112
113        if (! found) {
114            TypeMirror mirror = element.getSuperclass();
115
116            if (mirror != null && (! mirror.getKind().equals(NONE))) {
117                found |= isAnnotated((TypeElement) types.asElement(mirror));
118            }
119        }
120
121        return found;
122    }
123}