Example usage for java.lang.reflect Field isAccessible

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

Introduction

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

Prototype

@Deprecated(since = "9")
public boolean isAccessible() 

Source Link

Document

Get the value of the accessible flag for this reflected object.

Usage

From source file:com.technophobia.webdriver.substeps.runner.DefaultWebDriverFactory.java

/**
 * By default the HtmlUnit driver is set to en-us. This can cause problems
 * with formatters.//from  ww  w  .  ja  va2  s  .  co  m
 */
private void setDriverLocale(final WebDriver driver) {

    try {
        final Field field = driver.getClass().getDeclaredField("webClient");
        if (field != null) {
            final boolean original = field.isAccessible();
            field.setAccessible(true);

            final WebClient webClient = (WebClient) field.get(driver);
            if (webClient != null) {
                webClient.addRequestHeader("Accept-Language", "en-gb");
            }
            field.setAccessible(original);
        } else {
            Assert.fail("Failed to get webclient field to set accept language");
        }
    } catch (final IllegalAccessException ex) {

        LOG.warn(ex.getMessage());

    } catch (final SecurityException e) {

        LOG.warn(e.getMessage());
    } catch (final NoSuchFieldException e) {

        LOG.warn(e.getMessage());
    }
}

From source file:org.springframework.batch.core.jsr.launch.support.BatchPropertyBeanPostProcessor.java

private void injectBatchProperties(final Object artifact, final Properties artifactProperties) {
    ReflectionUtils.doWithFields(artifact.getClass(), new ReflectionUtils.FieldCallback() {
        @Override/* w w w. j  av  a  2  s .  c  o  m*/
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            if (isValidFieldModifier(field) && isAnnotated(field)) {
                boolean isAccessible = field.isAccessible();
                field.setAccessible(true);

                String batchProperty = getBatchPropertyFieldValue(field, artifactProperties);

                if (StringUtils.hasText(batchProperty)) {
                    field.set(artifact, batchProperty);
                }

                field.setAccessible(isAccessible);
            }
        }
    });
}

From source file:org.jnosql.artemis.reflection.Reflections.java

/**
 * Make the given field accessible, explicitly setting it accessible
 * if necessary. The setAccessible(true) method is only
 * called when actually necessary, to avoid unnecessary
 * conflicts with a JVM SecurityManager (if active).
 *
 * @param field field the field to make accessible
 *//*  w ww .  j a va 2  s . c  o m*/
public void makeAccessible(Field field) {
    if ((!Modifier.isPublic(field.getModifiers())
            || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) && !field.isAccessible()) {
        field.setAccessible(true);
    }
}

From source file:org.wso2.carbon.javaee.tomee.ASTomEEServerListener.java

private void installServerInfo() {
    //        if (SystemInstance.get().getOptions().get("tomee.keep-server-info", false)) {
    //            return;
    //        }//from   ww  w .  ja va 2  s. c  o m

    // force static init
    final String value = ServerInfo.getServerInfo();

    Field field = null;
    boolean acc = true;
    try {
        field = ServerInfo.class.getDeclaredField("serverInfo");
        acc = field.isAccessible();
        final int slash = value.indexOf('/');
        field.setAccessible(true);
        final String version = OpenEjbVersion.get().getVersion();
        final String tomeeVersion = (Integer.parseInt("" + version.charAt(0)) - 3)
                + version.substring(1, version.length());
        field.set(null,
                value.substring(0, slash) + " (TomEE)" + value.substring(slash) + " (" + tomeeVersion + ")");
    } catch (Exception e) {
        // no-op
    } finally {
        if (field != null) {
            field.setAccessible(acc);
        }
    }
}

From source file:org.sonatype.nexus.yum.internal.support.YumNexusTestSupport.java

private void lookupField(Field field, String hint) throws Exception {
    Object value = lookup(field.getType(), hint);
    if (!field.isAccessible()) {
        field.setAccessible(true);// ww  w.ja  v  a2 s.  co m
        field.set(this, value);
        field.setAccessible(false);
    }
}

From source file:com.mewmew.fairy.v1.cli.AnnotatedCLI.java

private void magicallySetField(Object obj, Field field, Object value) {
    if (value == null) {
        return;//  w  w w .jav  a  2 s  .com
    }
    boolean wasInaccessible = !field.isAccessible();
    if (wasInaccessible) {
        field.setAccessible(true);
    }
    try {
        field.set(obj, value);
    } catch (IllegalAccessException e) {
        System.err.println("diving into unsafe territory !");
        long offset = unsafe.objectFieldOffset(field);
        if (value instanceof Integer) {
            unsafe.putInt(obj, offset, (Integer) value);
        } else if (value instanceof Long) {
            unsafe.putLong(obj, offset, (Long) value);
        } else if (value instanceof Boolean) {
            unsafe.putBoolean(obj, offset, (Boolean) value);
        } else if (value instanceof Float) {
            unsafe.putFloat(obj, offset, (Float) value);
        } else if (value instanceof Double) {
            unsafe.putDouble(obj, offset, (Double) value);
        } else if (value instanceof Byte) {
            unsafe.putByte(obj, offset, (Byte) value);
        } else if (value instanceof Character) {
            unsafe.putChar(obj, offset, (Character) value);
        } else if (value instanceof Short) {
            unsafe.putShort(obj, offset, (Short) value);
        } else {
            unsafe.putObject(obj, offset, value);
        }
    }
    field.setAccessible(!wasInaccessible);
}

From source file:com.dbs.sdwt.jpa.JpaUtil.java

public Object getValueFromField(Field field, Object object) {
    boolean accessible = field.isAccessible();
    try {/*  w ww . j av a  2 s .  c om*/
        return getField(field, object);
    } finally {
        field.setAccessible(accessible);
    }
}

From source file:org.pentaho.hadoop.shim.HadoopConfigurationLocatorTest.java

@Test
public void registerNativeLibraryPaths()
        throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    HadoopConfigurationLocator locator = new HadoopConfigurationLocator();
    locator.registerNativeLibraryPaths("test,ing");

    Field f = ClassLoader.class.getDeclaredField("usr_paths");
    boolean accessible = f.isAccessible();
    f.setAccessible(true);/*from w ww  .j  a  va  2s  .c  om*/
    try {
        String[] usrPaths = (String[]) f.get(null);
        assertTrue(Arrays.asList(usrPaths).contains("test"));
        assertTrue(Arrays.asList(usrPaths).contains("ing"));
    } finally {
        f.setAccessible(accessible);
    }
}

From source file:org.springframework.batch.core.jsr.configuration.support.BatchPropertyBeanPostProcessor.java

private void injectBatchProperties(final Object bean, final Properties artifactProperties) {
    ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
        @Override/*from  w  ww  . jav  a 2  s. c om*/
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            if (isValidFieldModifier(field) && isAnnotated(field)) {
                boolean isAccessible = field.isAccessible();
                field.setAccessible(true);

                String batchProperty = getBatchPropertyFieldValue(field, artifactProperties);

                if (batchProperty != null) {
                    field.set(bean, batchProperty);
                }

                field.setAccessible(isAccessible);
            }
        }
    });
}

From source file:org.pentaho.hadoop.shim.HadoopConfigurationLocatorTest.java

@Test
public void registerNativeLibraryPaths_no_duplicates()
        throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    HadoopConfigurationLocator locator = new HadoopConfigurationLocator();
    locator.registerNativeLibraryPaths("test,ing");

    Field f = ClassLoader.class.getDeclaredField("usr_paths");
    boolean accessible = f.isAccessible();
    f.setAccessible(true);//from w ww  .jav  a  2  s. co  m
    try {
        String[] usrPaths = (String[]) f.get(null);
        int pathCount = usrPaths.length;
        locator.registerNativeLibraryPaths("ing");
        usrPaths = (String[]) f.get(null);
        assertTrue(Arrays.asList(usrPaths).contains("test"));
        assertTrue(Arrays.asList(usrPaths).contains("ing"));
        assertEquals(pathCount, usrPaths.length);
    } finally {
        f.setAccessible(accessible);
    }
}