Example usage for java.lang.reflect Method setAccessible

List of usage examples for java.lang.reflect Method setAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Method setAccessible.

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:Main.java

public static int getResId(Context context, String resType, String resName) {
    int resId = 0;
    if (context != null && !TextUtils.isEmpty(resType) && !TextUtils.isEmpty(resName)) {
        if (rp != null) {
            try {
                Method pck = rp.getClass().getMethod("getResId", Context.class, String.class, String.class);
                pck.setAccessible(true);
                resId = ((Integer) pck.invoke(rp, context, resType, resName)).intValue();
            } catch (Throwable var5) {

            }//from w  w  w  . j a  va  2 s  .  c  o m
        }

        if (resId <= 0) {
            String pck1 = context.getPackageName();
            if (TextUtils.isEmpty(pck1)) {
                return resId;
            }

            if (resId <= 0) {
                resId = context.getResources().getIdentifier(resName, resType, pck1);
                if (resId <= 0) {
                    resId = context.getResources().getIdentifier(resName.toLowerCase(), resType, pck1);
                }
            }

            if (resId <= 0) {
                System.err.println("failed to parse " + resType + " resource \"" + resName + "\"");
            }
        }

        return resId;
    } else {
        return resId;
    }
}

From source file:com.hangum.tadpole.engine.initialize.JDBCDriverLoader.java

/**
 * Add jar loader// w w w.j a  v a  2s  . c  om
 * 
 * @param jarArray
 * @throws Exception
 */
private static void addJARLoader(Object[] jarArray) throws Exception {
    URLClassLoader systemClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    try {
        Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
        method.setAccessible(true);
        method.invoke(systemClassLoader, jarArray);
    } catch (Throwable t) {
        logger.error("jar loader", t);
    }
}

From source file:Main.java

/**
 * invoke the leftParameter and get the name property.
 * it will try the Array.length() and Map.get(),
 * then it will try get'Name' and is'Name',
 * last, it will to find the name by invoke field.
 *//*from   www. ja  va 2  s. c om*/
public static Object searchProperty(Object leftParameter, String name) throws Exception {
    Class<?> leftClass = leftParameter.getClass();
    Object result;
    if (leftParameter.getClass().isArray() && "length".equals(name)) {
        result = Array.getLength(leftParameter);
    } else if (leftParameter instanceof Map) {
        result = ((Map<Object, Object>) leftParameter).get(name);
    } else {
        try {
            String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
            Method method = leftClass.getMethod(getter, new Class<?>[0]);
            if (!method.isAccessible()) {
                method.setAccessible(true);
            }
            result = method.invoke(leftParameter, new Object[0]);
        } catch (NoSuchMethodException e2) {
            try {
                String getter = "is" + name.substring(0, 1).toUpperCase() + name.substring(1);
                Method method = leftClass.getMethod(getter, new Class<?>[0]);
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }
                result = method.invoke(leftParameter, new Object[0]);
            } catch (NoSuchMethodException e3) {
                Field field = leftClass.getField(name);
                result = field.get(leftParameter);
            }
        }
    }
    return result;
}

From source file:Main.java

/**
 * Inflates a preference hierarchy from the preference hierarchies of
 * {@link android.app.Activity Activities} that match the given {@link android.content.Intent}. An
 * {@link android.app.Activity} defines its preference hierarchy with meta-data using
 * the {@link android.preference.PreferenceManager#METADATA_KEY_PREFERENCES} key.
 * <p/>/* w w  w .j a v  a 2 s  . c o m*/
 * If a preference hierarchy is given, the new preference hierarchies will
 * be merged in.
 *
 * @param queryIntent     The intent to match activities.
 * @param rootPreferences Optional existing hierarchy to merge the new
 *                        hierarchies into.
 * @return The root hierarchy (if one was not provided, the new hierarchy's
 * root).
 */
public static PreferenceScreen inflateFromIntent(@NonNull PreferenceManager manager, Intent queryIntent,
        PreferenceScreen rootPreferences) {
    try {
        Method m = PreferenceManager.class.getDeclaredMethod("inflateFromIntent", Intent.class,
                PreferenceScreen.class);
        m.setAccessible(true);
        return (PreferenceScreen) m.invoke(manager, queryIntent, rootPreferences);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Allow calling the hidden method {@code makeOptionalFitsSystem()} through reflection on
 * {@code view}./*from w ww .j a  v  a2 s .  c  o  m*/
 */
public static void makeOptionalFitsSystemWindows(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        try {
            // We need to use getMethod() for makeOptionalFitsSystemWindows since both View
            // and ViewGroup implement the method
            Method method = view.getClass().getMethod("makeOptionalFitsSystemWindows");
            if (!method.isAccessible()) {
                method.setAccessible(true);
            }
            method.invoke(view);
        } catch (NoSuchMethodException e) {
            Log.d(TAG, "Could not find method makeOptionalFitsSystemWindows. Oh well...");
        } catch (InvocationTargetException e) {
            Log.d(TAG, "Could not invoke makeOptionalFitsSystemWindows", e);
        } catch (IllegalAccessException e) {
            Log.d(TAG, "Could not invoke makeOptionalFitsSystemWindows", e);
        }
    }
}

From source file:com.networknt.audit.ServerInfoDisabledTest.java

static void addURL(URL url) throws Exception {
    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class clazz = URLClassLoader.class;
    // Use reflection
    Method method = clazz.getDeclaredMethod("addURL", new Class[] { URL.class });
    method.setAccessible(true);
    method.invoke(classLoader, new Object[] { url });
}

From source file:Main.java

public static Object invokeMethod(Object paramObject, String paramString, Class<?>[] paramArrayOfClass,
        Object[] paramArrayOfObject) {
    Class localClass = paramObject.getClass();

    try {// w  w  w.j  a  va2 s.  c o  m
        Method localException = localClass.getDeclaredMethod(paramString, paramArrayOfClass);
        localException.setAccessible(true);
        return localException.invoke(paramObject, paramArrayOfObject);
    } catch (Exception var9) {
        Method localMethod = null;

        try {
            if (localClass != null && localClass.getSuperclass() != null) {
                localMethod = localClass.getSuperclass().getDeclaredMethod(paramString, paramArrayOfClass);
                if (localMethod != null) {
                    localMethod.setAccessible(true);
                    return localMethod.invoke(paramObject, paramArrayOfObject);
                }
            }
        } catch (Exception var8) {
            ;
        }

        return null;
    }
}

From source file:com.jkoolcloud.tnt4j.streams.utils.ZorkaAttach.java

/**
 * Loads required classpath entries to running JVM.
 * //from  w w w. j  av a2s  . c  o m
 * @param classPathEntriesURL
 *            classpath entries URLs to attach to JVM
 * @throws Exception
 *             if exception occurs while extending system class loader's classpath
 */
private static void extendClasspath(URL... classPathEntriesURL) throws Exception {
    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);
    for (URL classPathEntryURL : classPathEntriesURL) {
        try {
            method.invoke(classLoader, classPathEntryURL);
        } catch (Exception e) {
            LOGGER.log(OpLevel.ERROR, StreamsResources.getString(ZorkaConstants.RESOURCE_BUNDLE_NAME,
                    "ZorkaAttach.could.not.load"), classPathEntryURL, e);
        }
    }
}

From source file:org.mongojack.internal.util.JacksonAccessor.java

private static Method findMethod(Class clazz, String name, Class[] argTypes) {
    try {/*from w  w  w  . jav  a 2s .c o m*/
        Method method = clazz.getDeclaredMethod(name, argTypes);
        method.setAccessible(true);
        return method;
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Object searchProperty(Object leftParameter, String name) throws Exception {
    Class<?> leftClass = leftParameter.getClass();
    Object result;//www .  j av a  2s  .  c om
    if (leftParameter.getClass().isArray() && "length".equals(name)) {
        result = Array.getLength(leftParameter);
    } else if (leftParameter instanceof Map) {
        result = ((Map<Object, Object>) leftParameter).get(name);
    } else {
        try {
            String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
            Method method = leftClass.getMethod(getter, new Class<?>[0]);
            if (!method.isAccessible()) {
                method.setAccessible(true);
            }
            result = method.invoke(leftParameter, new Object[0]);
        } catch (NoSuchMethodException e2) {
            try {
                String getter = "is" + name.substring(0, 1).toUpperCase() + name.substring(1);
                Method method = leftClass.getMethod(getter, new Class<?>[0]);
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }
                result = method.invoke(leftParameter, new Object[0]);
            } catch (NoSuchMethodException e3) {
                Field field = leftClass.getField(name);
                result = field.get(leftParameter);
            }
        }
    }
    return result;
}