001package ball.annotation.processing;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: ConstructorPropertiesProcessor.java 5895 2020-05-08 15:55:11Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/annotation/processing/ConstructorPropertiesProcessor.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.beans.ConstructorProperties;
025import java.util.List;
026import java.util.Objects;
027import java.util.Set;
028import java.util.stream.Stream;
029import javax.annotation.processing.Processor;
030import javax.annotation.processing.RoundEnvironment;
031import javax.lang.model.element.AnnotationMirror;
032import javax.lang.model.element.AnnotationValue;
033import javax.lang.model.element.Element;
034import javax.lang.model.element.ExecutableElement;
035import javax.lang.model.element.TypeElement;
036import javax.lang.model.element.VariableElement;
037import lombok.NoArgsConstructor;
038import lombok.ToString;
039import org.apache.commons.lang3.StringUtils;
040
041import static java.util.stream.Collectors.toList;
042import static javax.tools.Diagnostic.Kind.ERROR;
043import static javax.tools.Diagnostic.Kind.WARNING;
044
045/**
046 * {@link Processor} implementation to verify {@link ConstructorProperties}
047 * annotation are actual bean properties of the
048 * {@link java.lang.reflect.Constructor}'s {@link Class}.
049 *
050 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
051 * @version $Revision: 5895 $
052 */
053@ServiceProviderFor({ Processor.class })
054@For({ ConstructorProperties.class })
055@NoArgsConstructor @ToString
056public class ConstructorPropertiesProcessor extends AnnotatedProcessor {
057    @Override
058    public void process(RoundEnvironment roundEnv,
059                        TypeElement annotation, Element element) {
060        super.process(roundEnv, annotation, element);
061
062        AnnotationMirror mirror = getAnnotationMirror(element, annotation);
063        AnnotationValue value = getAnnotationValue(mirror, "value");
064        List<String> names =
065            Stream.of(value)
066            .filter(Objects::nonNull)
067            .map(t -> (List<?>) t.getValue())
068            .flatMap(List::stream)
069            .map(t -> (AnnotationValue) t)
070            .map(t -> (String) t.getValue())
071            .collect(toList());
072        List<? extends VariableElement> parameters =
073            ((ExecutableElement) element).getParameters();
074
075        if (names.size() != parameters.size()) {
076            print(WARNING, element, mirror, value,
077                  "value() does not match %s parameters", element.getKind());
078        }
079
080        TypeElement type = (TypeElement) element.getEnclosingElement();
081        Set<String> properties = getPropertyNames(type);
082
083        names.stream()
084            .filter(StringUtils::isNotEmpty)
085            .filter(t -> (! properties.contains(t)))
086            .forEach(t -> print(WARNING, element, mirror,
087                                "bean property '%s' not defined", t));
088    }
089}