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