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:com.app.test.BaseTestCase.java

protected static void _initializeVelocityTemplate(Class clazz, Object classInstance) throws Exception {

    Field velocityEngine = clazz.getDeclaredField("_velocityEngine");

    velocityEngine.setAccessible(true);

    VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean();

    Map<String, Object> velocityPropertiesMap = new HashMap<>();

    velocityPropertiesMap.put("resource.loader", "class");
    velocityPropertiesMap.put("class.resource.loader.class",
            "org.apache.velocity.runtime.resource.loader." + "ClasspathResourceLoader");

    velocityEngineFactoryBean.setVelocityPropertiesMap(velocityPropertiesMap);

    velocityEngine.set(classInstance, velocityEngineFactoryBean.createVelocityEngine());
}

From source file:com.dangdang.ddframe.job.lite.spring.util.AopTargetUtils.java

private static Object getProxyTargetObject(final Object proxy, final String proxyType) {
    Field h;
    try {/*from   w  w w.j  ava2  s.co m*/
        h = proxy.getClass().getSuperclass().getDeclaredField(proxyType);
    } catch (final NoSuchFieldException ex) {
        return getProxyTargetObjectForCglibAndSpring4(proxy);
    }
    h.setAccessible(true);
    try {
        return getTargetObject(h.get(proxy));
    } catch (final IllegalAccessException ex) {
        throw new JobSystemException(ex);
    }
}

From source file:org.neo4j.ogm.auth.AuthenticationTest.java

private static void initialise(ServerControls controls) throws Exception {

    Field field = InProcessServerControls.class.getDeclaredField("server");
    field.setAccessible(true);
    neoServer = (AbstractNeoServer) field.get(controls);
}

From source file:com.micmiu.mvc.ferriswheel.utils.ReflectionUtils.java

/**
 * ?, ?DeclaredField, ?.// w  w w  . java  2s. com
 * <p/>
 * ?Object?, null.
 */
public static Field getAccessibleField(final Object obj, final String fieldName) {
    Validate.notNull(obj, "object can't be null");
    Validate.notBlank(fieldName, "fieldName can't be blank");
    for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
            .getSuperclass()) {
        try {
            Field field = superClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException e) {// NOSONAR
            // Field??,?
        }
    }
    return null;
}

From source file:com.netflix.subtitles.ttml.TtmlUtils.java

/**
 * Sets NULL value of style references list for div and body element.
 *
 * @param obj div or body element/*from w  w  w.  j  a v a  2s. c  o  m*/
 */
private static void setStyleListToNull(Object obj) {
    try {
        Field field = obj.getClass().getDeclaredField(STYLE_FIELD);
        field.setAccessible(true);
        field.set(obj, null);
    } catch (Exception e) {
        // ignore exceptions
    }
}

From source file:com.xhsoft.framework.common.utils.ReflectionUtils.java

/**
 * <p>Description:?, ?DeclaredField,    ?.?Object?, null.</p>
 * @param obj//from  w w w.  ja  va 2 s . c  o m
 * @param fieldName
 * @return Field
 */
public static Field getAccessibleField(final Object obj, final String fieldName) {
    Assert.notNull(obj, "object?");
    Assert.hasText(fieldName, "fieldName");

    for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
            .getSuperclass()) {
        try {
            Field field = superClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException e) {//NOSONAR
            /* Field??,?*/
        }
    }

    return null;
}

From source file:com.dangdang.ddframe.job.spring.util.AopTargetUtils.java

private static Object getProxyTargetObject(final Object proxy, final String proxyType) {
    Field h;
    try {//from  www . jav  a 2s  .co m
        h = proxy.getClass().getSuperclass().getDeclaredField(proxyType);
    } catch (final NoSuchFieldException ex) {
        return getProxyTargetObjectForCglibAndSpring4(proxy);
    }
    h.setAccessible(true);
    try {
        return getTargetObject(h.get(proxy));
    } catch (final IllegalAccessException ex) {
        throw new JobException(ex);
    }
}

From source file:com.agimatec.validation.jsr303.util.SecureActions.java

private static void setAccessibility(Field field) {
    if (!Modifier.isPublic(field.getModifiers())
            || (Modifier.isPublic(field.getModifiers()) && Modifier.isAbstract(field.getModifiers()))) {
        field.setAccessible(true);
    }/*from  w  w  w .  j  a  v  a 2 s  . c o  m*/
}

From source file:com.bynder.sdk.util.Utils.java

/**
 * Method called for each field in a query object. It extracts the different fields with
 * {@link ApiField} annotation and, if needed, converts it according to the conversion type
 * defined.//  w  w  w . j a  v  a2s . c om
 *
 * @param field Field information.
 * @param query Query object.
 * @param params Parameters name/value pairs to send to the API.
 *
 * @throws IllegalAccessException If the Field object is inaccessible.
 */
private static void convertField(final Field field, final Object query, final Map<String, String> params)
        throws IllegalAccessException {
    field.setAccessible(true);
    ApiField apiField = field.getAnnotation(ApiField.class);

    if (field.get(query) != null && apiField != null) {
        if (apiField.conversionType() == ConversionType.NONE) {
            params.put(apiField.name(), field.get(query).toString());
        } else {
            if (apiField.conversionType() == ConversionType.METAPROPERTY_FIELD) {
                MetapropertyField metapropertyField = (MetapropertyField) field.get(query);
                params.put(String.format("%s.%s", apiField.name(), metapropertyField.getMetapropertyId()),
                        StringUtils.join(metapropertyField.getOptionsIds(), Utils.STR_COMMA));
            } else if (apiField.conversionType() == ConversionType.LIST_FIELD) {
                List<?> listField = (List<?>) field.get(query);
                params.put(apiField.name(), StringUtils.join(listField, Utils.STR_COMMA));
            }
        }
    }
    field.setAccessible(false);
}

From source file:com.atlassian.connector.eclipse.internal.crucible.ui.operations.CrucibleFileInfoCompareEditorInput.java

private static void hackGalileo(Viewer contentViewer, TextMergeViewer textMergeViewer,
        final MergeSourceViewer fLeft, final MergeSourceViewer fRight) {
    // FIXME: hack for e3.5
    try {/*from  w w w.  j  av a 2  s .c  o m*/
        Method getCompareConfiguration = ContentMergeViewer.class.getDeclaredMethod("getCompareConfiguration");
        getCompareConfiguration.setAccessible(true);
        CompareConfiguration cc = (CompareConfiguration) getCompareConfiguration.invoke(textMergeViewer);

        Method getMergeContentProvider = ContentMergeViewer.class.getDeclaredMethod("getMergeContentProvider");
        getMergeContentProvider.setAccessible(true);
        IMergeViewerContentProvider cp = (IMergeViewerContentProvider) getMergeContentProvider
                .invoke(textMergeViewer);

        Method getSourceViewer = MergeSourceViewer.class.getDeclaredMethod("getSourceViewer");

        Method configureSourceViewer = TextMergeViewer.class.getDeclaredMethod("configureSourceViewer",
                SourceViewer.class, boolean.class);
        configureSourceViewer.setAccessible(true);
        configureSourceViewer.invoke(contentViewer, getSourceViewer.invoke(fLeft),
                cc.isLeftEditable() && cp.isLeftEditable(textMergeViewer.getInput()));
        configureSourceViewer.invoke(contentViewer, getSourceViewer.invoke(fRight),
                cc.isRightEditable() && cp.isRightEditable(textMergeViewer.getInput()));

        Field isConfiguredField = TextMergeViewer.class.getDeclaredField("isConfigured");
        isConfiguredField.setAccessible(true);
        isConfiguredField.set(contentViewer, true);
    } catch (Throwable t) {
        // ignore as it may not exist in other versions
    }
}