java反射

反射 java反射

  • 获取类
class c = Class.forName("com.example.TestClass");
  • 获取类权限
    ++public++ ++private++ ++static++
System.out.print(Modifier.toString(c.getModifiers()));
  • 获取类名
System.out.print(c.getSimpleName());
  • 获取所有属性
Field[] fields = c.getDeclaredFields();
  • 属性权限
System.out.print(Modifier.toString(f.getModifiers()) + " ");
  • 属性返回类型
System.out.print(f.getType().getSimpleName() + " ");
  • 属性名
System.out.print(f.getName() + " ");
  • 获取所有方法
Method[] methods = c.getMethods();
System.out.print(Modifier.toString(method.getModifiers()));   
System.out.print(" " + method.getReturnType().getSimpleName());
                System.out.print(" " + method.getName());
                Class<?>[] parameters = method.getParameterTypes();
  • 指定方法
c = Class.forName("com.example.TestClass");
o = c.newInstance();
Method method = c.getDeclaredMethod("method2", String.class);
            method.setAccessible(true);
            System.out.print(" " + (String) method.invoke(o, "123"));
  • 指定属性
Field f = c.getField("field1");
            f.setAccessible(true);
            System.out.println("" + f.get(o));
  • 构造方法
System.out.println("无参构造方法:");
            o = c.newInstance();
            System.out.println("有参构造方法:");
            Constructor<?> oo = c.getConstructor(String.class);

例子:通过反射使用SystemProperties的api

package com.sprd.ext;

import android.text.TextUtils;

import java.lang.reflect.Method;
import android.os.SystemProperties;
/**
 * Created by SPRD on 6/13/17.
 */

public class SystemPropertiesUtils {

    private static final String TAG = "SystemPropertiesUtils";

    private static Class<?> mClassType = null;
    private static Method mGetMethod = null;
    private static Method mGetIntMethod = null;
    private static Method mGetBooleanMethod = null;


    private static Class<?> getSystemPropertiesClass() throws ClassNotFoundException {
        if (mClassType == null) {
            mClassType = Class.forName("android.os.SystemProperties");
        }
        return mClassType;
    }

    private static Method getMethod() throws Exception {
        if (mGetMethod == null) {
            Class clazz = getSystemPropertiesClass();
            mGetMethod = clazz.getDeclaredMethod("get", String.class);
        }
        return mGetMethod;
    }

    private static Method getIntMethod() throws Exception {
        if (mGetIntMethod == null) {
            Class clazz = getSystemPropertiesClass();
            mGetIntMethod = clazz.getDeclaredMethod("getInt", String.class, int.class);
        }
        return mGetIntMethod;
    }

    private static Method getBooleanMethod() throws Exception {
        if (mGetBooleanMethod == null) {
            Class clazz = getSystemPropertiesClass();
            mGetBooleanMethod = clazz.getDeclaredMethod("getBoolean", String.class, boolean.class);
        }
        return mGetBooleanMethod;
    }

    public static String get(String key, String def) {
        try {
            String value = (String) getMethod().invoke(null, key);
            if (!TextUtils.isEmpty(value)) {
                return value;
            }
        } catch (Exception e) {
            LogUtils.d(TAG, "Unable to read system properties");
        }

        return def;
    }

    public static int getInt(String key, int def) {
        int value = def;
        try {
            value = (int) getIntMethod().invoke(null, key, def);
        } catch (Exception e) {
            LogUtils.d(TAG, "Unable to read system properties");
        }
        return value;
    }
  
     public static boolean getBoolean(String key, boolean def) {
        boolean value = def;        
        try {
            value = (Boolean) getBooleanMethod().invoke(null, key, def);
        } catch (Exception e) {
            LogUtils.d(TAG, "Unable to read system properties");
        }
        return value;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值