001package ball.tools.javac;
002/*-
003 * ##########################################################################
004 * Utilities
005 * %%
006 * Copyright (C) 2008 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import ball.annotation.ServiceProviderFor;
022import ball.annotation.processing.AnnotatedProcessor;
023import ball.annotation.processing.For;
024import ball.annotation.processing.TargetMustExtend;
025import java.lang.annotation.Documented;
026import java.lang.annotation.Retention;
027import java.lang.annotation.Target;
028import javax.annotation.processing.Processor;
029import javax.annotation.processing.RoundEnvironment;
030import javax.lang.model.element.AnnotationMirror;
031import javax.lang.model.element.AnnotationValue;
032import javax.lang.model.element.Element;
033import javax.lang.model.element.TypeElement;
034import lombok.NoArgsConstructor;
035import lombok.ToString;
036
037import static java.lang.annotation.ElementType.TYPE;
038import static java.lang.annotation.RetentionPolicy.RUNTIME;
039import static javax.lang.model.element.Modifier.ABSTRACT;
040import static javax.tools.Diagnostic.Kind.ERROR;
041import static org.apache.commons.lang3.StringUtils.isEmpty;
042
043/**
044 * {@link java.lang.annotation.Annotation} to mark
045 * {@link com.sun.source.util.Plugin}s with their name.
046 *
047 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
048 */
049@Documented
050@Retention(RUNTIME)
051@Target({ TYPE })
052@TargetMustExtend(AnnotatedJavacPlugin.class)
053public @interface JavacPluginName {
054    String value();
055
056    /**
057     * {@link Processor} implementation.
058     */
059    @ServiceProviderFor({ Processor.class })
060    @For({ JavacPluginName.class })
061    @NoArgsConstructor @ToString
062    public class ProcessorImpl extends AnnotatedProcessor {
063        @Override
064        protected void process(RoundEnvironment roundEnv, TypeElement annotation, Element element) {
065            super.process(roundEnv, annotation, element);
066
067            AnnotationMirror mirror = getAnnotationMirror(element, annotation);
068            AnnotationValue value = getAnnotationValue(mirror, "value");
069            String name = (String) value.getValue();
070
071            if (! name.isEmpty()) {
072                if (element.getModifiers().contains(ABSTRACT)) {
073                    print(ERROR, element, mirror, "%s is %s", element.getKind(), ABSTRACT);
074                }
075            } else {
076                print(ERROR, element, mirror, value, "value() must be a non-empty String");
077            }
078        }
079    }
080}