001package ball.util.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: AnnotatedAntTaskConfigurationChecker.html 5431 2020-02-12 19:03:17Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/hcf-dev/blog/2019-07-08-fivethirtyeight-best-scrabble-string/src/main/resources/javadoc/src-html/ball/util/ant/taskdefs/AnnotatedAntTaskConfigurationChecker.html $
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 java.lang.annotation.Annotation;
024import java.lang.reflect.AnnotatedElement;
025import java.lang.reflect.Field;
026import java.lang.reflect.Method;
027import java.util.ArrayList;
028import java.util.Collections;
029import java.util.HashSet;
030import org.apache.commons.lang3.ClassUtils;
031import org.apache.commons.lang3.reflect.FieldUtils;
032import org.apache.commons.lang3.reflect.MethodUtils;
033import org.apache.tools.ant.Task;
034import org.apache.tools.ant.TaskConfigurationChecker;
035
036import static ball.beans.PropertyMethodEnum.getPropertyName;
037
038/**
039 * {@link TaskConfigurationChecker} implmentation to check
040 * {@link AnnotatedAntTask} annotations.
041 *
042 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
043 * @version $Revision: 5431 $
044 */
045public class AnnotatedAntTaskConfigurationChecker
046             extends TaskConfigurationChecker {
047    private final Task task;
048
049    /**
050     * Sole constructor.
051     *
052     * @param   task            The Ant {@link Task}.
053     */
054    public AnnotatedAntTaskConfigurationChecker(Task task) {
055        super(task);
056
057        this.task = task;
058
059        HashSet<Class<?>> set = new HashSet<>();
060
061        set.add(task.getClass());
062        set.addAll(ClassUtils.getAllSuperclasses(task.getClass()));
063        set.addAll(ClassUtils.getAllInterfaces(task.getClass()));
064
065        for (Class<?> type : set) {
066            ArrayList<AnnotatedElement> list = new ArrayList<>();
067
068            Collections.addAll(list, type.getDeclaredFields());
069            Collections.addAll(list, type.getDeclaredMethods());
070
071            for (AnnotatedElement element : list) {
072                for (Annotation annotation : element.getAnnotations()) {
073                    AntTaskAttributeConstraint constraint =
074                        annotation.annotationType()
075                        .getAnnotation(AntTaskAttributeConstraint.class);
076
077                    if (constraint != null) {
078                        assertConfig(constraint, element);
079                    }
080                }
081            }
082        }
083    }
084
085    private void assertConfig(AntTaskAttributeConstraint constraint,
086                              AnnotatedElement element) {
087        try {
088            String name = null;
089            Object value = null;
090
091            if (element instanceof Field) {
092                name = ((Field) element).getName();
093                value = FieldUtils.readField((Field) element, task, true);
094            } else if (element instanceof Method) {
095                name = getPropertyName((Method) element);
096                value =
097                    MethodUtils.invokeMethod(task, true,
098                                             ((Method) element).getName(),
099                                             new Object[] {  });
100            } else {
101                throw new IllegalStateException(String.valueOf(element));
102            }
103
104            constraint.value().newInstance()
105                .check(task, this, name, value);
106        } catch (Exception exception) {
107            fail(exception.toString());
108        }
109    }
110}