001package ball.annotation.processing;
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 java.lang.annotation.Documented;
023import java.lang.annotation.Retention;
024import java.lang.annotation.Target;
025import java.util.Collections;
026import java.util.EnumSet;
027import java.util.stream.Stream;
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.ElementKind;
034import javax.lang.model.element.TypeElement;
035import lombok.NoArgsConstructor;
036import lombok.ToString;
037
038import static java.lang.annotation.ElementType.TYPE;
039import static java.lang.annotation.RetentionPolicy.RUNTIME;
040import static java.util.stream.Collectors.toCollection;
041import static javax.tools.Diagnostic.Kind.ERROR;
042import static javax.tools.Diagnostic.Kind.WARNING;
043
044/**
045 * {@link AnnotatedNoAnnotationProcessor}
046 * {@link java.lang.annotation.Annotation} to specify super-{@link Class}
047 * criteria.
048 *
049 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
050 */
051@Documented
052@Retention(RUNTIME)
053@Target({ TYPE })
054@TargetMustExtend(AnnotatedNoAnnotationProcessor.class)
055public @interface ForSubclassesOf {
056    Class<?> value();
057
058    /**
059     * {@link ElementKind} subset if {@link ForElementKinds} is specified.
060     */
061    public static final EnumSet<ElementKind> ELEMENT_KINDS =
062        Stream.of(ElementKind.values())
063        .filter(t -> (t.isClass() || t.isInterface()))
064        .collect(toCollection(() -> EnumSet.noneOf(ElementKind.class)));
065
066    /**
067     * {@link Processor} implementation.
068     */
069    @ServiceProviderFor({ Processor.class })
070    @For({ ForSubclassesOf.class })
071    @NoArgsConstructor @ToString
072    public static class ProcessorImpl extends AnnotatedProcessor {
073        @Override
074        public void process(RoundEnvironment roundEnv, TypeElement annotation, Element element) {
075            super.process(roundEnv, annotation, element);
076
077            ForElementKinds kinds = element.getAnnotation(ForElementKinds.class);
078
079            if (kinds != null) {
080                EnumSet<ElementKind> set = EnumSet.noneOf(ElementKind.class);
081
082                Collections.addAll(set, kinds.value());
083
084                if (! set.removeAll(ELEMENT_KINDS)) {
085                    print(ERROR, element,
086                          "%s annotated with @%s and @%s but does not specify one of %s",
087                          element.getKind(), annotation.getSimpleName(),
088                          ForElementKinds.class.getSimpleName(), ELEMENT_KINDS);
089                }
090
091                if (! set.isEmpty()) {
092                    print(WARNING, element,
093                          "%s annotated with @%s and @%s; %s will be ignored",
094                          element.getKind(), annotation.getSimpleName(), ForElementKinds.class.getSimpleName(), set);
095                }
096            }
097        }
098    }
099}