Android Open Source - roboject Reflection Util






From Project

Back to project page roboject.

License

The source code is released under:

Apache License

If you think the Android project roboject listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
//w  w w  . j ava 2s.c  o  m
This file is part of Roboject

Copyright (c) 2010-2011 akquinet A.G.

Contact:  http://www.akquinet.de/en

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 de.akquinet.android.roboject.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


/**
 * Utility class to do common reflection tasks.
 *
 * @author Philipp Kumar
 */
public class ReflectionUtil {

    public static boolean isObjectInstanceof(Object object, String classname) {
        if (object == null)
            return false;

        try {
            Class<?> aClass = Class.forName(classname);
            return aClass.isAssignableFrom(object.getClass());
        } catch (ClassNotFoundException e) {
            return false;
        }
    }

    /**
     * Checks the given class and all super classes for a certain class
     * annotation and returns the first one that matches. If no such annotation
     * is found, returns null.
     *
     * @param clazz           the class to check for the annotation
     * @param annotationClass the annotation type to look for
     * @return the annotation, if found, null otherwise
     */
    public static <T extends Annotation> T getAnnotation(
            Class<?> clazz, Class<T> annotationClass) {
        if (isTopMostClass(clazz)) {
            return null;
        }

        T annotation = clazz.getAnnotation(annotationClass);
        if (annotation != null) {
            return annotation;
        }
        return getAnnotation(clazz.getSuperclass(), annotationClass);
    }

    /**
     * Returns all declared fields of a class and all its super classes,
     * regardless of visibility modifier.
     */
    public static List<Field> getFields(Class<?> clazz) {
        List<Field> results = new ArrayList<Field>();
        getFields(results, clazz);
        return results;
    }

    public static List<Field> getAnnotatedFields(Class<?> clazz, Class<?> annotationType) {
        List<Field> allFields = getFields(clazz);

        List<Field> results = new ArrayList<Field>();
        for (Field field : allFields) {
            Annotation[] annotations = field.getAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation.annotationType().isAssignableFrom(annotationType)) {
                    results.add(field);
                }
            }
        }

        return results;
    }

    private static void getFields(List<Field> fields, Class<?> clazz) {
        if (isTopMostClass(clazz)) {
            return;
        }

        for (Field field : clazz.getDeclaredFields()) {
            fields.add(field);
        }
        getFields(fields, clazz.getSuperclass());
    }

    private static boolean isTopMostClass(Class<?> clazz) {
        return clazz == null || clazz.getName().startsWith("android.") || clazz == Object.class;
    }
}




Java Source Code List

de.akquinet.android.roboject.RobojectActivity.java
de.akquinet.android.roboject.RobojectException.java
de.akquinet.android.roboject.RobojectFragmentActivity.java
de.akquinet.android.roboject.RobojectFragment.java
de.akquinet.android.roboject.RobojectSupportFragment.java
de.akquinet.android.roboject.Roboject.java
de.akquinet.android.roboject.ServiceRegistry.java
de.akquinet.android.roboject.ServicesConnector.java
de.akquinet.android.roboject.annotations.InjectExtra.java
de.akquinet.android.roboject.annotations.InjectLayout.java
de.akquinet.android.roboject.annotations.InjectObject.java
de.akquinet.android.roboject.annotations.InjectResource.java
de.akquinet.android.roboject.annotations.InjectService.java
de.akquinet.android.roboject.annotations.InjectView.java
de.akquinet.android.roboject.injectors.ClassInjector.java
de.akquinet.android.roboject.injectors.ExtraInjector.java
de.akquinet.android.roboject.injectors.FieldInjector.java
de.akquinet.android.roboject.injectors.Injector.java
de.akquinet.android.roboject.injectors.LayoutInjector.java
de.akquinet.android.roboject.injectors.ResourceInjector.java
de.akquinet.android.roboject.injectors.ServiceInjector.java
de.akquinet.android.roboject.injectors.ViewInjector.java
de.akquinet.android.roboject.tutorial.activities.AdderActivity.java
de.akquinet.android.roboject.tutorial.activities.RobojectAdderActivity.java
de.akquinet.android.roboject.tutorial.services.TestService.java
de.akquinet.android.roboject.util.AndroidUtil.java
de.akquinet.android.roboject.util.IntentRegistry.java
de.akquinet.android.roboject.util.ReflectionUtil.java
de.akquinet.android.robojecttest.InjectLayoutTest.java
de.akquinet.android.robojecttest.InjectLocalServiceFragmentTest.java
de.akquinet.android.robojecttest.InjectLocalServiceSupportFragmentTest.java
de.akquinet.android.robojecttest.InjectLocalServiceViaClassTest.java
de.akquinet.android.robojecttest.InjectLocalServiceViaObjectTest.java
de.akquinet.android.robojecttest.InjectRemoteServiceFragmentTest.java
de.akquinet.android.robojecttest.InjectRemoteServiceSupportFragmentTest.java
de.akquinet.android.robojecttest.InjectRemoteServiceTest.java
de.akquinet.android.robojecttest.InjectResourceCustomTest.java
de.akquinet.android.robojecttest.InjectResourceFragmentTest.java
de.akquinet.android.robojecttest.InjectResourceSupportFragmentTest.java
de.akquinet.android.robojecttest.InjectResourceTest.java
de.akquinet.android.robojecttest.InjectServiceFragmentTest.java
de.akquinet.android.robojecttest.InjectServiceSupportFragmentTest.java
de.akquinet.android.robojecttest.InjectServiceTest.java
de.akquinet.android.robojecttest.InjectViewCustomTest.java
de.akquinet.android.robojecttest.InjectViewFragmentActivityTest.java
de.akquinet.android.robojecttest.InjectViewFragmentTest.java
de.akquinet.android.robojecttest.InjectViewSupportFragmentTest.java
de.akquinet.android.robojecttest.InjectViewTest.java
de.akquinet.android.robojecttest.activities.DummyFragmentActivity.java
de.akquinet.android.robojecttest.activities.DummySupportFragmentActivity.java
de.akquinet.android.robojecttest.activities.InjectLayoutTestActivity.java
de.akquinet.android.robojecttest.activities.InjectLocalServiceViaClassTestActivity.java
de.akquinet.android.robojecttest.activities.InjectLocalServiceViaObjectTestActivity.java
de.akquinet.android.robojecttest.activities.InjectRemoteServiceTestActivity.java
de.akquinet.android.robojecttest.activities.InjectResourceTestActivity.java
de.akquinet.android.robojecttest.activities.InjectServiceTestActivity.java
de.akquinet.android.robojecttest.activities.InjectViewTestActivity.java
de.akquinet.android.robojecttest.activities.InjectViewTestFragmentActivity.java
de.akquinet.android.robojecttest.fragments.InjectNonAndroidServiceTestFragment.java
de.akquinet.android.robojecttest.fragments.InjectNonAndroidServiceTestSupportFragment.java
de.akquinet.android.robojecttest.fragments.InjectRemoteServiceTestFragment.java
de.akquinet.android.robojecttest.fragments.InjectRemoteServiceTestSupportFragment.java
de.akquinet.android.robojecttest.fragments.InjectResourceTestFragment.java
de.akquinet.android.robojecttest.fragments.InjectResourceTestSupportFragment.java
de.akquinet.android.robojecttest.fragments.InjectServiceTestFragment.java
de.akquinet.android.robojecttest.fragments.InjectServiceTestSupportFragment.java
de.akquinet.android.robojecttest.fragments.InjectViewTestFragment.java
de.akquinet.android.robojecttest.fragments.InjectViewTestSupportFragment.java
de.akquinet.android.robojecttest.services.AdderFragmentImplementation.java
de.akquinet.android.robojecttest.services.AdderImplementation.java
de.akquinet.android.robojecttest.services.AdderInterface.java
de.akquinet.android.robojecttest.services.AdderSupportFragmentImplementation.java
de.akquinet.android.robojecttest.services.RemoteTestService.java
de.akquinet.android.robojecttest.services.TestService.java