001package ball.tools.javac;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: JavacPluginName.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/javac/JavacPluginName.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.source.util.Plugin}s with their name.
048 *
049 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
050 * @version $Revision: 6104 $
051 */
052@Documented
053@Retention(RUNTIME)
054@Target({ TYPE })
055@TargetMustExtend(AnnotatedJavacPlugin.class)
056public @interface JavacPluginName {
057    String value();
058
059    /**
060     * {@link Processor} implementation.
061     */
062    @ServiceProviderFor({ Processor.class })
063    @For({ JavacPluginName.class })
064    @NoArgsConstructor @ToString
065    public class ProcessorImpl extends AnnotatedProcessor {
066        @Override
067        protected void process(RoundEnvironment roundEnv,
068                               TypeElement annotation, Element element) {
069            super.process(roundEnv, annotation, element);
070
071            AnnotationMirror mirror = getAnnotationMirror(element, annotation);
072            AnnotationValue value = getAnnotationValue(mirror, "value");
073            String name = (String) value.getValue();
074
075            if (! name.isEmpty()) {
076                if (element.getModifiers().contains(ABSTRACT)) {
077                    print(ERROR, element, mirror,
078                          "%s is %s", element.getKind(), ABSTRACT);
079                }
080            } else {
081                print(ERROR, element, mirror, value,
082                      "value() must be a non-empty String");
083            }
084        }
085    }
086}