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

com.mycila.event.internal.Reflect Maven / Gradle / Ivy

/**
 * Copyright (C) 2010 Mycila ([email protected])
 *
 * 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.mycila.event.internal;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import static java.util.Arrays.asList;

/**
 * @author Mathieu Carbou ([email protected])
 */
public final class Reflect {

    public static Class getTargetClass(Class proxy) {
        if (proxy.getName().contains("$$")) {
            do {
                proxy = proxy.getSuperclass();
            } while (proxy.getName().contains("$$"));
            return proxy;
        }
        return proxy;
    }

    public static Class getTargetClass(Object instance) {
        return getTargetClass(instance.getClass());
    }

    public static Predicate withParameterTypes(final Class... classes) {
        return new Predicate() {
            @Override
            public boolean apply(Method m) {
                Class[] thisParams = m.getParameterTypes();
                if (thisParams.length != classes.length)
                    return false;
                int c = 0;
                for (Class thisParam : thisParams)
                    if (thisParam != classes[c++])
                        return false;
                return true;
            }
        };
    }

    public static  Predicate named(final String methodName) {
        return new Predicate() {
            @Override
            public boolean apply(T member) {
                return member.getName().equals(methodName);
            }
        };
    }

    public static  Predicate annotatedBy(final Class annotationType) {
        return new Predicate() {
            @Override
            public boolean apply(T element) {
                return element.isAnnotationPresent(annotationType);
            }
        };
    }

    public static Iterable findMethods(final Class clazz) {
        return transform(METHOD_CACHE.get(clazz), TO_METHOD);
    }

    public static Iterable findFields(Class type, Predicate predicate) {
        List fields = new LinkedList();
        while (type != null && type != Object.class) {
            for (Field field : type.getDeclaredFields())
                if (predicate.apply(field))
                    fields.add(field);
            type = type.getSuperclass();
        }
        return fields;
    }

    private Reflect() {
    }

    private static final Predicate METHOD_FILDER = new Predicate() {
        @Override
        public boolean apply(Method member) {
            return !(member.isSynthetic() || member.isBridge());
        }
    };

    private static final Function TO_METHOD = new Function() {
        @Override
        public Method apply(Signature from) {
            return from.method;
        }
    };

    private static final Function TO_SIGNATURE = new Function() {
        @Override
        public Signature apply(Method from) {
            return new Signature(from);
        }
    };

    private static final List OBJECT_METHODS = Lists.newLinkedList(transform(asList(Object.class.getDeclaredMethods()), TO_SIGNATURE));

    private static final WeakCache, Iterable> METHOD_CACHE = new WeakCache, Iterable>(new WeakCache.Provider, Iterable>() {
        @Override
        public Iterable get(Class clazz) {
            if (clazz == null)
                return Collections.emptyList();
            if (clazz == Object.class)
                return OBJECT_METHODS;
            final List thisMethods = Lists.newArrayList(transform(filter(asList(clazz.isInterface() ? clazz.getMethods() : clazz.getDeclaredMethods()), METHOD_FILDER), TO_SIGNATURE));
            return concat(thisMethods, Iterables.filter(METHOD_CACHE.get(clazz.getSuperclass()), new Predicate() {
                @Override
                public boolean apply(Signature input) {
                    int pos = thisMethods.indexOf(input);
                    if (pos == -1) return true;
                    Signature override = thisMethods.get(pos);
                    return !overrides(override.method, input.method);
                }
            }));
        }
    });

    /**
     * Returns true if a overrides b. Assumes signatures of a and b are the same and a's declaring
     * class is a subclass of b's declaring class.
     */
    private static boolean overrides(Method a, Method b) {
        // See JLS section 8.4.8.1
        int modifiers = b.getModifiers();
        if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)) {
            return true;
        }
        if (Modifier.isPrivate(modifiers)) {
            return false;
        }
        // b must be package-private
        return a.getDeclaringClass().getPackage().equals(b.getDeclaringClass().getPackage());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy