001package ball.tools.javadoc;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: TagletName.java 6104 2020-06-03 18:26:30Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/tools/javadoc/TagletName.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 ball.annotation.processing.AnnotatedProcessor;
025import ball.annotation.processing.For;
026import ball.annotation.processing.TargetMustExtend;
027import java.lang.annotation.Documented;
028import java.lang.annotation.Retention;
029import java.lang.annotation.Target;
030import javax.annotation.processing.Processor;
031import javax.annotation.processing.RoundEnvironment;
032import javax.lang.model.element.AnnotationMirror;
033import javax.lang.model.element.AnnotationValue;
034import javax.lang.model.element.Element;
035import javax.lang.model.element.TypeElement;
036import lombok.NoArgsConstructor;
037import lombok.ToString;
038
039import static java.lang.annotation.ElementType.TYPE;
040import static java.lang.annotation.RetentionPolicy.RUNTIME;
041import static javax.lang.model.element.Modifier.ABSTRACT;
042import static javax.tools.Diagnostic.Kind.ERROR;
043import static org.apache.commons.lang3.StringUtils.isEmpty;
044
045/**
046 * {@link java.lang.annotation.Annotation} to mark
047 * {@link com.sun.tools.doclets.internal.toolkit.taglets.Taglet}s
048 * with their name.
049 *
050 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
051 * @version $Revision: 6104 $
052 */
053@Documented
054@Retention(RUNTIME)
055@Target({ TYPE })
056@TargetMustExtend(AnnotatedTaglet.class)
057public @interface TagletName {
058    String value();
059
060    /**
061     * {@link Processor} implementation.
062     */
063    @ServiceProviderFor({ Processor.class })
064    @For({ TagletName.class })
065    @NoArgsConstructor @ToString
066    public class ProcessorImpl extends AnnotatedProcessor {
067        @Override
068        protected void process(RoundEnvironment roundEnv,
069                               TypeElement annotation, Element element) {
070            super.process(roundEnv, annotation, element);
071
072            AnnotationMirror mirror = getAnnotationMirror(element, annotation);
073            AnnotationValue value = getAnnotationValue(mirror, "value");
074            String name = (String) value.getValue();
075
076            if (! name.isEmpty()) {
077                if (element.getModifiers().contains(ABSTRACT)) {
078                    print(ERROR, element, mirror,
079                          "%s is %s", element.getKind(), ABSTRACT);
080                }
081            } else {
082                print(ERROR, element, mirror, value,
083                      "value() must be a non-empty String");
084            }
085        }
086    }
087}