Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import java.lang.reflect.Method;

public class Main {
    public static String getSystemProperties(Context context, String str) throws IllegalArgumentException {
        try {
            Class loadClass = context.getClassLoader().loadClass("android.os.SystemProperties");
            return (String) loadClass.getMethod("get", String.class).invoke(loadClass, str);
        } catch (IllegalArgumentException e) {
            throw e;
        } catch (Exception e2) {
            return "";
        }
    }

    private static Object invoke(Class<?> cls, Object receiver, String methodname, Class<?>[] clsArr,
            Object[] objArr) throws Exception {
        Method method = null;
        if (objArr == null || objArr.length == 0) {
            method = cls.getMethod(methodname, new Class[0]);
            method.setAccessible(true);
            return method.invoke(receiver, new Object[0]);
        }
        method = cls.getMethod(methodname, clsArr);
        method.setAccessible(true);
        return method.invoke(receiver, objArr);
    }

    private static Object invoke(Class<?> cls, Object receiver, String methodname, Object... args)
            throws Exception {
        Method method = null;
        if (args == null || args.length == 0) {
            method = cls.getMethod(methodname, new Class[0]);
            method.setAccessible(true);
            return method.invoke(receiver, new Object[0]);
        }
        method = cls.getMethod(methodname, getParameterTypes(args));
        method.setAccessible(true);
        return method.invoke(receiver, args);
    }

    public static Object invoke(String className, String methodName, Class<?>[] clsArr, Object[] objArr)
            throws Exception {
        Class cls = Class.forName(className);
        return invoke(cls, cls, methodName, clsArr, objArr);
    }

    public static Object invoke(String classname, String methodname, Object[] objArr) throws Exception {
        Class cls = Class.forName(classname);
        return invoke(cls, (Object) cls, methodname, objArr);
    }

    private static Class<?>[] getParameterTypes(Object[] objArr) {
        Class<?>[] clsArr = new Class[objArr.length];
        for (int i = 0; i < clsArr.length; i++) {
            clsArr[i] = objArr[i].getClass();
        }
        return clsArr;
    }
}