Example usage for java.lang.reflect Field setAccessible

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

Introduction

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

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:SigningProcess.java

public static HashMap returnCertificates() {
    HashMap map = new HashMap();
    try {//  ww w .j  a va2  s.c o m
        providerMSCAPI = new SunMSCAPI();
        Security.addProvider(providerMSCAPI);
        ks = KeyStore.getInstance("Windows-MY");
        ks.load(null, null);
        Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
        spiField.setAccessible(true);
        KeyStoreSpi spi = (KeyStoreSpi) spiField.get(ks);
        Field entriesField = spi.getClass().getSuperclass().getDeclaredField("entries");
        entriesField.setAccessible(true);
        Collection entries = (Collection) entriesField.get(spi);
        for (Object entry : entries) {
            alias = (String) invokeGetter(entry, "getAlias");
            //                System.out.println("alias :" + alias);
            privateKey = (Key) invokeGetter(entry, "getPrivateKey");
            certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain");
            //                System.out.println(alias + ": " + privateKey + "CERTIFICATES -----------"+Arrays.toString(certificateChain));
        }
        map.put("privateKey", privateKey);
        map.put("certificateChain", certificateChain);

    } catch (KeyStoreException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    } catch (IOException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    } catch (NoSuchAlgorithmException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    } catch (CertificateException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    } catch (NoSuchFieldException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    } catch (SecurityException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    } catch (IllegalArgumentException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    } catch (IllegalAccessException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    } catch (NoSuchMethodException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    } catch (InvocationTargetException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    }
    return map;
}

From source file:com.frostwire.android.gui.UniversalScanner.java

private static Uri nativeScanFile(Context context, String path) {
    try {/*from ww w . ja v  a 2s  . co m*/
        File f = new File(path);

        Class<?> clazz = Class.forName("android.media.MediaScanner");

        Constructor<?> mediaScannerC = clazz.getDeclaredConstructor(Context.class);
        Object scanner = mediaScannerC.newInstance(context);

        try {
            Method setLocaleM = clazz.getDeclaredMethod("setLocale", String.class);
            setLocaleM.invoke(scanner, Locale.US.toString());
        } catch (Throwable e) {
            e.printStackTrace();
        }

        Field mClientF = clazz.getDeclaredField("mClient");
        mClientF.setAccessible(true);
        Object mClient = mClientF.get(scanner);

        Method scanSingleFileM = clazz.getDeclaredMethod("scanSingleFile", String.class, String.class,
                String.class);
        Uri fileUri = (Uri) scanSingleFileM.invoke(scanner, f.getAbsolutePath(), "external", "data/raw");
        int n = context.getContentResolver().delete(fileUri, null, null);
        if (n > 0) {
            LOG.debug("Deleted from Files provider: " + fileUri);
        }

        Field mNoMediaF = mClient.getClass().getDeclaredField("mNoMedia");
        mNoMediaF.setAccessible(true);
        mNoMediaF.setBoolean(mClient, false);

        // This is only for HTC (tested only on HTC One M8)
        try {
            Field mFileCacheF = clazz.getDeclaredField("mFileCache");
            mFileCacheF.setAccessible(true);
            mFileCacheF.set(scanner, new HashMap<String, Object>());
        } catch (Throwable e) {
            // no an HTC, I need some time to refactor this hack
        }

        try {
            Field mFileCacheF = clazz.getDeclaredField("mNoMediaPaths");
            mFileCacheF.setAccessible(true);
            mFileCacheF.set(scanner, new HashMap<String, String>());
        } catch (Throwable e) {
            e.printStackTrace();
        }

        Method doScanFileM = mClient.getClass().getDeclaredMethod("doScanFile", String.class, String.class,
                long.class, long.class, boolean.class, boolean.class, boolean.class);
        Uri mediaUri = (Uri) doScanFileM.invoke(mClient, f.getAbsolutePath(), null, f.lastModified(),
                f.length(), false, true, false);

        Method releaseM = clazz.getDeclaredMethod("release");
        releaseM.invoke(scanner);

        return mediaUri;

    } catch (Throwable e) {
        e.printStackTrace();
        return null;
    }
}

From source file:net.pms.util.ProcessUtil.java

public static Integer getProcessID(Process p) {
    Integer pid = null;//  www. j  a v a2 s.co m

    if (p != null && p.getClass().getName().equals("java.lang.UNIXProcess")) {
        try {
            Field f = p.getClass().getDeclaredField("pid");
            f.setAccessible(true);
            pid = f.getInt(p);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException
                | IllegalAccessException e) {
            LOGGER.debug("Can't determine the Unix process ID: " + e.getMessage());
        }
    }

    return pid;
}

From source file:com.ms.commons.test.common.ReflectUtil.java

public static void setObject(Object bean, String field, Object value) {
    Class<?> clazz = bean.getClass();
    try {//from  w  w  w. j a va  2 s. c om
        Field f = getDeclaredField(clazz, field);
        f.setAccessible(true);
        try {
            f.set(bean, value);
        } catch (IllegalArgumentException e) {
            throw new UnknowException(e);
        } catch (IllegalAccessException e) {
            throw new UnknowException(e);
        }
    } catch (SecurityException e) {
        throw new UnknowException(e);
    } catch (NoSuchFieldException e) {
        throw new JavaFieldNotFoundException(clazz, field);
    }
}

From source file:com.ms.commons.test.common.ReflectUtil.java

public static Object getObject(Object bean, String field) {
    Class<?> clazz = bean.getClass();
    try {/*from  w w  w  .  ja  v  a  2  s  .c  o  m*/
        Field f = getDeclaredField(clazz, field);
        f.setAccessible(true);
        try {
            return f.get(bean);
        } catch (IllegalArgumentException e) {
            throw new UnknowException(e);
        } catch (IllegalAccessException e) {
            throw new UnknowException(e);
        }
    } catch (SecurityException e) {
        throw new UnknowException(e);
    } catch (NoSuchFieldException e) {
        throw new JavaFieldNotFoundException(clazz, field);
    }
}

From source file:com.conversantmedia.mapreduce.tool.DistributedResourceManager.java

public static void setFieldValue(Field field, Object bean, Object value) throws IllegalAccessException {
    // Set it on the field
    field.setAccessible(true);

    // Perform type conversion if possible..
    SimpleTypeConverter converter = new SimpleTypeConverter();
    try {//  w ww . j  a va2s. c  o m
        value = converter.convertIfNecessary(value, field.getType());
    } catch (ConversionNotSupportedException e) {
        // If conversion isn't supported, ignore and try and set anyway.
    }
    field.set(bean, value);
}

From source file:com.psiphon3.psiphonlibrary.WebViewProxySettings.java

@TargetApi(Build.VERSION_CODES.KITKAT) // for android.util.ArrayMap methods
@SuppressWarnings("rawtypes")
private static boolean setWebkitProxyLollipop(Context appContext, String host, int port) {
    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", port + "");
    System.setProperty("https.proxyHost", host);
    System.setProperty("https.proxyPort", port + "");
    try {//from  w  w w .  j  a  va2s  .  c  o m
        Class applictionClass = Class.forName("android.app.Application");
        Field mLoadedApkField = applictionClass.getDeclaredField("mLoadedApk");
        mLoadedApkField.setAccessible(true);
        Object mloadedApk = mLoadedApkField.get(appContext);
        Class loadedApkClass = Class.forName("android.app.LoadedApk");
        Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers");
        mReceiversField.setAccessible(true);
        ArrayMap receivers = (ArrayMap) mReceiversField.get(mloadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object receiver : ((ArrayMap) receiverMap).keySet()) {
                Class clazz = receiver.getClass();
                if (clazz.getName().contains("ProxyChangeListener")) {
                    Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
                    onReceiveMethod.invoke(receiver, appContext, intent);
                }
            }
        }
        return true;
    } catch (ClassNotFoundException e) {
        MyLog.d("Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    } catch (NoSuchFieldException e) {
        MyLog.d("Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    } catch (IllegalAccessException e) {
        MyLog.d("Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    } catch (NoSuchMethodException e) {
        MyLog.d("Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    } catch (InvocationTargetException e) {
        MyLog.d("Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    }
    return false;
}

From source file:org.cybercat.automation.annotations.AnnotationBuilder.java

private static void processIntegrationService(Object targetObject, Class<? extends Object> clazz)
        throws AutomationFrameworkException {
    if (clazz == null)
        return;// w  ww  .j ava2  s. co  m
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        if (field.getAnnotation(CCProperty.class) != null) {
            processPropertyField(targetObject, field);
        }
    }
    processIntegrationService(targetObject, clazz.getSuperclass());
}

From source file:me.totalfreedom.totalfreedommod.util.FUtil.java

@SuppressWarnings("unchecked")
public static <T> T getField(Object from, String name) {
    Class<?> checkClass = from.getClass();
    do {//from ww  w  .  j  ava  2s.  c  o m
        try {
            Field field = checkClass.getDeclaredField(name);
            field.setAccessible(true);
            return (T) field.get(from);

        } catch (NoSuchFieldException | IllegalAccessException ex) {
        }
    } while (checkClass.getSuperclass() != Object.class && ((checkClass = checkClass.getSuperclass()) != null));

    return null;
}

From source file:org.cybercat.automation.annotations.AnnotationBuilder.java

private static <T extends Object> void processCCFeatureForObject(T entity, Class<? extends Object> clazz)
        throws AutomationFrameworkException {
    if (clazz == null)
        return;/*from  w  w w . java  2  s .  com*/
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        if (field.getAnnotation(CCFeature.class) != null) {
            processCCFeatureField(entity, field);
        } else if (field.getAnnotation(CCIntegrationService.class) != null) {
            createIntegrationService(field, entity);
        } else if (field.getAnnotation(CCProperty.class) != null) {
            processPropertyField(entity, field);
        }
    }
    processCCFeatureForObject(entity, (Class<? extends Object>) clazz.getSuperclass());
}