Example usage for java.lang Class getDeclaredField

List of usage examples for java.lang Class getDeclaredField

Introduction

In this page you can find the example usage for java.lang Class getDeclaredField.

Prototype

@CallerSensitive
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException 

Source Link

Document

Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.

Usage

From source file:io.cloudslang.lang.cli.services.ConsolePrinterImplTest.java

@Test
public void testInitialize() throws Exception {
    ConsolePrinterImpl consolePrinter = new ConsolePrinterImpl();

    consolePrinter.initialize();//from  w  ww.j  a  v  a 2 s. com

    Class<? extends ConsolePrinterImpl> consolePrinterClass = consolePrinter.getClass();
    Field consolePrinterClassExecutorDeclaredField = consolePrinterClass
            .getDeclaredField(SINGLE_THREAD_EXECUTOR);

    consolePrinterClassExecutorDeclaredField.setAccessible(true);
    Object singleThreadExecutor = consolePrinterClassExecutorDeclaredField.get(consolePrinter);

    assertTrue(singleThreadExecutor instanceof ThreadPoolExecutor);
    assertEquals(1, ((ThreadPoolExecutor) singleThreadExecutor).getMaximumPoolSize());
}

From source file:org.kuali.coeus.s2sgen.impl.generate.support.stylesheet.StylesheetValidationTest.java

private Resource getXsltStylesheet(Class<? extends S2SFormGenerator> generatorClass) {
    try {/*from   w  w  w  .j  a  v a  2s .co  m*/
        final Field stylesheet = generatorClass.getDeclaredField("stylesheet");
        stylesheet.setAccessible(true);
        final Value value = stylesheet.getAnnotation(Value.class);
        final ResourceLoader resourceLoader = new DefaultResourceLoader(this.getClass().getClassLoader());
        return resourceLoader.getResource(value.value());
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(generatorClass.getName() + " cannot find stylesheet", e);
    }
}

From source file:fr.smartcontext.yatte.context.cli.properties.PropertiesContextInitializer.java

private void setFeature(Properties prop, String varName, Class<?> class_, Object instance, String feature)
        throws NoSuchFieldException, IllegalAccessException {
    String value = prop.getProperty(varName + DOT + feature);
    Field field = class_.getDeclaredField(feature);
    field.setAccessible(true);/*from  w w w . ja v  a  2s.  c  o  m*/
    field.set(instance, value);
}

From source file:com.redhat.rcm.maven.plugin.buildmetadata.data.MetaDataProviderBuilder.java

private Field findField(final Class<?> type, final String propertyName) throws NoSuchFieldException {
    try {//  w ww .  ja  v a  2 s. co m
        return type.getDeclaredField(propertyName);
    } catch (final NoSuchFieldException e) {
        if (type.getSuperclass().equals(Object.class)) {
            throw e;
        }
    }
    return findField(type.getSuperclass(), propertyName);
}

From source file:com.linkedin.pinot.integration.tests.ChaosMonkeyIntegrationTest.java

private int getProcessPid(Process process) {
    Class<? extends Process> clazz = process.getClass();
    try {/*from ww  w .java2  s  .c  o m*/
        Field field = clazz.getDeclaredField("pid");
        field.setAccessible(true);
        return field.getInt(process);
    } catch (NoSuchFieldException e) {
        return -1;
    } catch (IllegalAccessException e) {
        return -1;
    }
}

From source file:org.craftercms.commons.jackson.mvc.CrafterJackson2MessageConverter.java

private Field findField(final Class<?> object, final String fieldName) {
    if (object != null) {
        try {/*from w w w.  ja v  a2 s.com*/
            return object.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            return findField(object.getSuperclass(), fieldName);
        }
    }

    log.debug("Field {} does not exist", fieldName);

    return null;
}

From source file:edu.syr.pcpratts.rootbeer.runtime.Serializer.java

public void writeStaticField(Class cls, String name, Object value) {
    while (true) {
        try {/*from  w w w .  j av a  2s .c  o  m*/
            Field f = cls.getDeclaredField(name);
            f.setAccessible(true);
            f.set(null, value);
            return;
        } catch (Exception ex) {
            cls = cls.getSuperclass();
        }
    }
}

From source file:io.cloudslang.lang.logging.LoggingServiceImplTest.java

@Test
public void testInitialize() throws Exception {
    LoggingServiceImpl localLoggingService = new LoggingServiceImpl();

    // Tested call
    localLoggingService.initialize();//from  w ww.jav  a2 s  .c om

    Class<? extends LoggingServiceImpl> loggingServiceClass = localLoggingService.getClass();
    Field loggingServiceClassDeclaredField = loggingServiceClass.getDeclaredField(SINGLE_THREAD_EXECUTOR);

    loggingServiceClassDeclaredField.setAccessible(true);
    Object singleThreadExecutor = loggingServiceClassDeclaredField.get(localLoggingService);

    assertTrue(singleThreadExecutor instanceof ThreadPoolExecutor);
    assertEquals(1, ((ThreadPoolExecutor) singleThreadExecutor).getMaximumPoolSize());
}

From source file:com.dubsar_dictionary.Dubsar.FAQActivity.java

/**
 * Set Proxy for Android 3.2 and below./*from ww w  .j av  a  2  s  . c  om*/
 */
@SuppressWarnings("all")
private static boolean setProxyUpToHC(WebView webview, String host, int port) {
    Log.d(LOG_TAG, "Setting proxy with <= 3.2 API.");

    HttpHost proxyServer = new HttpHost(host, port);
    // Getting network
    Class networkClass = null;
    Object network = null;
    try {
        networkClass = Class.forName("android.webkit.Network");
        if (networkClass == null) {
            Log.e(LOG_TAG, "failed to get class for android.webkit.Network");
            return false;
        }
        Method getInstanceMethod = networkClass.getMethod("getInstance", Context.class);
        if (getInstanceMethod == null) {
            Log.e(LOG_TAG, "failed to get getInstance method");
        }
        network = getInstanceMethod.invoke(networkClass, new Object[] { webview.getContext() });
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error getting network: " + ex);
        return false;
    }
    if (network == null) {
        Log.e(LOG_TAG, "error getting network: network is null");
        return false;
    }
    Object requestQueue = null;
    try {
        Field requestQueueField = networkClass.getDeclaredField("mRequestQueue");
        requestQueue = getFieldValueSafely(requestQueueField, network);
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error getting field value");
        return false;
    }
    if (requestQueue == null) {
        Log.e(LOG_TAG, "Request queue is null");
        return false;
    }
    Field proxyHostField = null;
    try {
        Class requestQueueClass = Class.forName("android.net.http.RequestQueue");
        proxyHostField = requestQueueClass.getDeclaredField("mProxyHost");
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error getting proxy host field");
        return false;
    }

    boolean temp = proxyHostField.isAccessible();
    try {
        proxyHostField.setAccessible(true);
        proxyHostField.set(requestQueue, proxyServer);
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error setting proxy host");
    } finally {
        proxyHostField.setAccessible(temp);
    }

    Log.d(LOG_TAG, "Setting proxy with <= 3.2 API successful!");
    return true;
}

From source file:io.cloudslang.lang.logging.LoggingServiceImplTest.java

@Test
public void testDestroy() throws Exception {
    LoggingServiceImpl localLoggingService = new LoggingServiceImpl();
    localLoggingService.initialize();//  w ww.  j  a va  2s. co m

    Class<? extends LoggingServiceImpl> loggingServiceClass = localLoggingService.getClass();
    Field loggingServiceClassDeclaredField = loggingServiceClass.getDeclaredField(SINGLE_THREAD_EXECUTOR);

    loggingServiceClassDeclaredField.setAccessible(true);
    Object singleThreadExecutor = loggingServiceClassDeclaredField.get(localLoggingService);
    assertNotNull(singleThreadExecutor);

    // Tested call
    localLoggingService.destroy();

    singleThreadExecutor = loggingServiceClassDeclaredField.get(localLoggingService);
    assertNull(singleThreadExecutor);
}