Example usage for java.lang.reflect Field set

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Sets the field represented by this Field object on the specified object argument to the specified new value.

Usage

From source file:Main.java

private static void setField(final Object bean, final Field field, final Object value) throws Exception {
    if (!field.isAccessible()) {
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            public Void run() {
                field.setAccessible(true);
                return null;
            }// w  ww  . j  av  a2s.  c o  m
        });
    }
    field.set(bean, value);
}

From source file:com.searchbox.core.ref.ReflectionUtils.java

public static void copyAllFields(Object from, Object to) {
    for (Field fromField : findAllFields(from.getClass())) {
        try {/* w w  w  . j  a v a 2 s  .  c  om*/
            Field toField = findUnderlying(to.getClass(), fromField.getName());
            if (toField != null) {
                toField.setAccessible(true);
                fromField.setAccessible(true);
                toField.set(to, fromField.get(from));
            }
        } catch (Exception e) {
            LOGGER.warn("Could not copye Fields.", e);
        }
    }
}

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  ww  w.j  av a2  s .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:natalia.dymnikova.cluster.scheduler.impl.Codec.java

private static Object doSanitize(final Object f) {
    final Class<?> aClass = f.getClass();
    if (!isStatic(aClass.getModifiers())) {
        final Class<?> enclosingClass = aClass.getEnclosingClass();
        if (enclosingClass != null) {
            for (final Field field : aClass.getDeclaredFields()) {
                if (enclosingClass.equals(field.getType())) {
                    field.setAccessible(true);
                    try {
                        field.set(f, null);
                    } catch (final IllegalAccessException e) {
                        throw unchecked(e);
                    }/*from ww  w  . j a  va2s  .c  o m*/
                }
            }
        }
    }
    return f;
}

From source file:com.gargoylesoftware.htmlunit.httpclient.HtmlUnitSSLConnectionSocketFactory.java

private static void setEmptyHostname(final HttpHost host) {
    try {/*from w  w w. j av a2s . co  m*/
        final Field field = HttpHost.class.getDeclaredField("hostname");
        field.setAccessible(true);
        field.set(host, "");
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

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 ww  w .j av  a 2  s .c  om
        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
    }
}

From source file:io.github.pellse.decorator.util.reflection.ReflectionUtils.java

private static <T> T privateSetField(T obj, Field field, Object value, boolean override)
        throws IllegalArgumentException, IllegalAccessException {
    field.setAccessible(true);/* w w w . ja v a 2  s. c o m*/
    if (field.get(obj) == null || override)
        field.set(obj, value);

    return obj;
}

From source file:Main.java

public static NetworkInfo createNetworkInfo(final int type, final boolean connected) throws Exception {
    Constructor<NetworkInfo> ctor = NetworkInfo.class.getDeclaredConstructor(int.class);
    ctor.setAccessible(true);//from  w  ww  .j  av a  2 s . c  o  m
    NetworkInfo networkInfo = ctor.newInstance(0);
    Field typeField = NetworkInfo.class.getDeclaredField("mNetworkType");
    Field connectedField = NetworkInfo.class.getDeclaredField("mState");
    Field detailedStateField = NetworkInfo.class.getDeclaredField("mDetailedState");
    typeField.setAccessible(true);
    connectedField.setAccessible(true);
    detailedStateField.setAccessible(true);
    typeField.setInt(networkInfo, type);
    connectedField.set(networkInfo,
            connected == true ? NetworkInfo.State.CONNECTED : NetworkInfo.State.DISCONNECTED);
    detailedStateField.set(networkInfo,
            connected == true ? NetworkInfo.DetailedState.CONNECTED : NetworkInfo.DetailedState.DISCONNECTED);
    return networkInfo;
}

From source file:com.feilong.commons.core.lang.reflect.FieldUtil.java

/**
 * .//from w ww.j a v  a2s.c o m
 * 
 * @param owner
 *            the owner
 * @param fieldName
 *            
 * @param value
 *            
 * @throws ReflectException
 *             the reflect exception
 * @see java.lang.Object#getClass()
 * @see java.lang.Class#getField(String)
 * @see java.lang.reflect.Field#set(Object, Object)
 */
public static void setProperty(Object owner, String fieldName, Object value) throws ReflectException {
    try {
        Class<?> ownerClass = owner.getClass();
        Field field = ownerClass.getField(fieldName);
        field.set(ownerClass, value);
    } catch (Exception e) {
        log.error(e.getClass().getName(), e);
        throw new ReflectException(e);
    }
}

From source file:Main.java

public static void setStaticFieldValue(@SuppressWarnings("rawtypes") final Class clazz, final String name,
        final Object value) throws Exception {
    Field field = clazz.getDeclaredField(name);
    field.setAccessible(true);// w  ww.jav a 2  s. co  m
    // There seems to be no field "modifiers" in Android/DalvikVM?
    // Field modifiersField = Field.class.getDeclaredField("modifiers");
    // modifiersField.setAccessible(true);
    // modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(null, value);
}