001package ball.annotation.processing;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: TagletProcessor.java 5901 2020-05-08 20:03:30Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/annotation/processing/TagletProcessor.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 com.sun.tools.doclets.Taglet;
025import java.lang.reflect.Method;
026import java.util.Map;
027import javax.annotation.processing.Processor;
028import javax.annotation.processing.RoundEnvironment;
029import javax.lang.model.element.Element;
030import javax.lang.model.element.ExecutableElement;
031import javax.lang.model.element.TypeElement;
032import lombok.NoArgsConstructor;
033import lombok.ToString;
034
035import static javax.lang.model.element.ElementKind.CLASS;
036import static javax.lang.model.element.Modifier.ABSTRACT;
037import static javax.tools.Diagnostic.Kind.WARNING;
038
039/**
040 * {@link Processor} implementation to verify concrete implementations of
041 * {@link Taglet} implement the required static {@code register()} method.
042 * See
043 * {@link.uri https://docs.oracle.com/javase/8/docs/technotes/guides/javadoc/taglet/overview.html Taglet Overview}
044 * and
045 * {@link.uri https://docs.oracle.com/javase/8/docs/jdk/api/javadoc/taglet/com/sun/tools/doclets/Taglet.html Taglet}.
046 *
047 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
048 * @version $Revision: 5901 $
049 */
050@ServiceProviderFor({ Processor.class })
051@ForElementKinds({ CLASS })
052@ForSubclassesOf(Taglet.class)
053@WithoutModifiers({ ABSTRACT })
054@NoArgsConstructor @ToString
055public class TagletProcessor extends AnnotatedNoAnnotationProcessor {
056    private static abstract class PROTOTYPE {
057        public static void register(Map<Object,Object> map) { }
058    }
059
060    private static final Method PROTOTYPE =
061        PROTOTYPE.class.getDeclaredMethods()[0];
062
063    @Override
064    protected void process(RoundEnvironment roundEnv, Element element) {
065        TypeElement type = (TypeElement) element;
066        ExecutableElement method = getMethod(type, PROTOTYPE);
067
068        if (! (method != null
069               && method.getModifiers().containsAll(getModifiers(PROTOTYPE)))) {
070            print(WARNING, element,
071                  "%s implements %s but does not implement '%s'",
072                  element.getKind(), Taglet.class.getName(),
073                  declaration(PROTOTYPE));
074        }
075    }
076}