Android Open Source - AndroidPlugin Reflection Utils






From Project

Back to project page AndroidPlugin.

License

The source code is released under:

MIT License

If you think the Android project AndroidPlugin 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

/*
 * Copyright (C) 2015 HouKx <hkx.aidream@gmail.com>
 *//  w w  w .j av a 2 s  . c  om
 * 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 androidx.pluginmgr;

import java.lang.reflect.Field;

/**
 * ???????
 * 
 * @author HouKangxi
 * 
 */
public class ReflectionUtils {

    public static <T> T getFieldValue(Object obj, String fieldName)
            throws IllegalAccessException, IllegalArgumentException,
            NoSuchFieldException {
        return getFieldValue(obj, fieldName, true);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getFieldValue(Object obj, String fieldName,
            boolean resolveParent) throws IllegalAccessException,
            IllegalArgumentException, NoSuchFieldException {
        Object[] rs = getField(obj, fieldName, resolveParent);
        if (rs == null) {
            throw new NoSuchFieldException("field:" + fieldName);
        }
        Field field = (Field) rs[0];
        Object targetObj = rs[1];
        return (T) field.get(targetObj);
    }

    public static void setFieldValue(Object obj, String fieldName, Object val)
            throws IllegalAccessException, IllegalArgumentException,
            NoSuchFieldException {
        setFieldValue(obj, fieldName, val, true);
    }

    public static void setFieldValue(Object obj, String fieldName, Object val,
            boolean resolveParent) throws IllegalAccessException,
            IllegalArgumentException, NoSuchFieldException {
        Object[] rs = getField(obj, fieldName, resolveParent);
        if (rs == null) {
            throw new NoSuchFieldException("field:" + fieldName);
        }
        Field field = (Field) rs[0];
        Object targetObj = rs[1];
        field.set(targetObj, val);
    }

    private static Object[] getField(Object obj, String elFieldName,
            boolean resolveParent) throws IllegalAccessException,
            IllegalArgumentException, NoSuchFieldException {
        if (obj == null) {
            return null;
        }
        String[] fieldNames = elFieldName.split("[.]");
        Object targetObj = obj;
        Class<?> targetClass = targetObj.getClass();
        Object val = null;
        int i = 0;
        Field field = null;
        Object[] rs = new Object[2];
        for (String fName : fieldNames) {
            i++;
            field = getField_(targetClass, fName, resolveParent);
            field.setAccessible(true);
            rs[0] = field;
            rs[1] = targetObj;
            val = field.get(targetObj);
            if (val == null) {
                if (i < fieldNames.length) {
                    throw new IllegalAccessException(
                            "can not getFieldValue as field '" + fName
                                    + "' value is null in '"
                                    + targetClass.getName() + "'");
                }
                break;
            }
            targetObj = val;
            targetClass = targetObj.getClass();
        }
        return rs;
    }

    public static Field getField_(Class<?> targetClass, String fieldName,
            boolean resolveParent) throws IllegalAccessException,
            IllegalArgumentException, NoSuchFieldException {
        NoSuchFieldException noSuchFieldExceptionOccor = null;
        Field rsField = null;
        try {
            Field field = targetClass.getDeclaredField(fieldName);
            rsField = field;
            if (!resolveParent) {
                field.setAccessible(true);
                return field;
            }
        } catch (NoSuchFieldException e) {
            noSuchFieldExceptionOccor = e;
        }
        if (noSuchFieldExceptionOccor != null) {
            if (resolveParent) {
                while (true) {
                    targetClass = targetClass.getSuperclass();
                    if (targetClass == null) {
                        break;
                    }
                    try {
                        Field field = targetClass.getDeclaredField(fieldName);
                        field.setAccessible(true);
                        return rsField = field;
                    } catch (NoSuchFieldException e) {
                        if (targetClass.getSuperclass() == null) {
                            throw e;
                        }
                    }
                }
            } else {
                throw noSuchFieldExceptionOccor;
            }
        }
        return rsField;
    }
}




Java Source Code List

android.widget.ViewStub.java
androidx.plmgrdemo.MainActivity.java
androidx.plmgrdemo.PlugListViewAdapter.java
androidx.pluginmgr.ActivityClassGenerator.java
androidx.pluginmgr.ActivityOverider.java
androidx.pluginmgr.FileUtil.java
androidx.pluginmgr.FrameworkClassLoader.java
androidx.pluginmgr.LayoutInflaterWrapper.java
androidx.pluginmgr.PlugInfo.java
androidx.pluginmgr.PluginActivityLifeCycleCallback.java
androidx.pluginmgr.PluginClassLoader.java
androidx.pluginmgr.PluginContextWrapper.java
androidx.pluginmgr.PluginManager.java
androidx.pluginmgr.PluginManifestUtil.java
androidx.pluginmgr.ReflectionUtils.java
androidx.pluginmgr.XmlManifestReader.java
com.limemobile.app.demo.pluginclienta.ClientABindService.java
com.limemobile.app.demo.pluginclienta.ClientAStartedService.java
com.limemobile.app.demo.pluginclienta.MainActivity.java
com.limemobile.app.demo.pluginclienta.TestButton.java
com.limemobile.app.demo.pluginclienta.TestFragmentActivity.java
com.limemobile.app.demo.pluginclienta.TestFragment.java
com.limemobile.app.demo.pluginclientb.ClientBStartedService.java
com.limemobile.app.demo.pluginclientb.MainActivity.java
com.limemobile.app.demo.pluginhost.HostBindService.java
com.limemobile.app.demo.pluginhost.HostStartedService.java
com.limemobile.app.demo.pluginhost.MainActivity.java
com.limemobile.app.demo.pluginhost.MyApplication.java
com.limemobile.app.demo.pluginhost.TestHostClass.java
com.limemobile.app.plugin.IPluginActivity.java
com.limemobile.app.plugin.IPluginContentProvider.java
com.limemobile.app.plugin.IPluginService.java
com.limemobile.app.plugin.PluginClientActivity.java
com.limemobile.app.plugin.PluginClientFragmentActivity.java
com.limemobile.app.plugin.PluginClientService.java
com.limemobile.app.plugin.PluginHostApplication.java
com.limemobile.app.plugin.PluginHostContentProvider.java
com.limemobile.app.plugin.PluginHostDelegateActivity.java
com.limemobile.app.plugin.PluginHostDelegateContentProvider.java
com.limemobile.app.plugin.PluginHostDelegateFragmentActivity.java
com.limemobile.app.plugin.PluginHostDelegateService.java
com.limemobile.app.plugin.internal.IPluginActivityDelegate.java
com.limemobile.app.plugin.internal.IPluginContentProviderDelegate.java
com.limemobile.app.plugin.internal.IPluginServiceDelegate.java
com.limemobile.app.plugin.internal.PluginClientDexClassLoader.java
com.limemobile.app.plugin.internal.PluginClientInfo.java
com.limemobile.app.plugin.internal.PluginClientManager.java
com.limemobile.app.plugin.internal.PluginDelegateActivityImpl.java
com.limemobile.app.plugin.internal.PluginDelegateContentProviderImpl.java
com.limemobile.app.plugin.internal.PluginDelegateServiceImpl.java
com.limemobile.app.plugin.internal.ReflectFieldAccessor.java