All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tngtech.archunit.junit.ReflectionUtils Maven / Gradle / Ivy

Go to download

A Java architecture test library, to specify and assert architecture rules in plain Java - Module 'archunit-junit'

There is a newer version: 0.8.3
Show newest version
/*
 * Copyright 2017 TNG Technology Consulting GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tngtech.archunit.junit;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import static com.google.common.collect.Collections2.filter;

class ReflectionUtils {
    private ReflectionUtils() {
    }

    static Set> getAllSuperTypes(Class type) {
        if (type == null) {
            return Collections.emptySet();
        }

        ImmutableSet.Builder> result = ImmutableSet.>builder()
                .add(type)
                .addAll(getAllSuperTypes(type.getSuperclass()));
        for (Class c : type.getInterfaces()) {
            result.addAll(getAllSuperTypes(c));
        }
        return result.build();
    }

    static Collection getAllFields(Class owner, Predicate predicate) {
        return filter(getAll(owner, new Collector() {
            @Override
            protected Collection extractFrom(Class type) {
                return ImmutableList.copyOf(type.getDeclaredFields());
            }
        }), toGuava(predicate));
    }

    static Collection getAllMethods(Class owner, Predicate predicate) {
        return filter(getAll(owner, new Collector() {
            @Override
            protected Collection extractFrom(Class type) {
                return ImmutableList.copyOf(type.getDeclaredMethods());
            }
        }), toGuava(predicate));
    }

    private static  List getAll(Class type, Collector collector) {
        for (Class t : getAllSuperTypes(type)) {
            collector.collectFrom(t);
        }
        return collector.collected;
    }

    private abstract static class Collector {
        private final List collected = new ArrayList<>();

        void collectFrom(Class type) {
            collected.addAll(extractFrom(type));
        }

        protected abstract Collection extractFrom(Class type);
    }

    interface Predicate {
        boolean apply(T input);
    }

    private static  com.google.common.base.Predicate toGuava(final Predicate predicate) {
        return new com.google.common.base.Predicate() {
            @Override
            public boolean apply(T input) {
                return predicate.apply(input);
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy