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:org.tylproject.vaadin.addon.beanfactory.DefaultBeanFactory.java

/**
 * @throws java.lang.UnsupportedOperationException
 *          if no field is annotated using
 *          {@link org.springframework.data.annotation.Id}
 */// w ww  . j a v  a 2  s . c  o  m
protected void setIdValue(T target, ObjectId value) throws ReflectiveOperationException {

    for (Field f : beanClass.getDeclaredFields()) {
        if (f.isAnnotationPresent(org.springframework.data.annotation.Id.class)) {
            f.setAccessible(true);
            f.set(target, value);
            return;
        }
    }
    for (Method m : beanClass.getDeclaredMethods()) {
        if (m.getName().equals("setId")) {
            m.invoke(target, value);
            return;
        }
    }
    throw new UnsupportedOperationException("no id field was found");
}

From source file:at.ac.tuwien.infosys.jcloudscale.test.integration.base.TestFieldHandling.java

@Test
public void testPublicFieldSetViaReflection() throws Exception {

    assertEquals(0, cs.countCloudObjects());
    FieldCloudObject ob = new FieldCloudObject();
    assertEquals(1, cs.countCloudObjects());

    // we need to go to the superclass as this is in fact a dynamic proxy generated
    // by CGLib/*from   w  ww  .j a v a  2  s . co m*/
    Field field = ob.getClass().getSuperclass().getDeclaredField("publicField");
    field.set(ob, "public_changed");

    assertEquals(ob.publicField, "public_changed");
    assertEquals(ob.getPublicField(), "public_changed");
    assertEquals(ob.getPublicField(), ob.publicField);

}

From source file:net.eledge.android.toolkit.db.internal.FieldType.java

public void convertToField(Object instance, Field field, Cursor cursor, int columnIndex)
        throws IllegalArgumentException, IllegalAccessException {
    field.set(instance, cursor.getString(columnIndex));
}

From source file:de.thkwalter.et.ortskurve.OrtskurveControllerTest.java

/**
 * Diese Methode initialisiert die Tests.
 * /*w w  w .  ja  va 2s.  c  om*/
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
    // Das Objekt der zu testenden Klasse wird erzeugt.
    this.ortskurveController = new OrtskurveController();

    // Das im Test verwendete Datenmodell der Ortskurve wird erzeugt und im Objekt der zu testenden Klasse gespeichert.
    this.testOrtskurveModell = new OrtskurveModell();
    Field ortskurveModellFeld = OrtskurveController.class.getDeclaredField("ortskurveModell");
    ortskurveModellFeld.setAccessible(true);
    ortskurveModellFeld.set(this.ortskurveController, this.testOrtskurveModell);
}

From source file:de.thkwalter.et.ortskurve.OrtskurveControllerTest.java

/**
 * Test fr die Methode {@link OrtskurveController#getOrtskurveModell()}.
 * // w  ww .  java2  s .c o  m
 * @throws SecurityException 
 * @throws NoSuchFieldException 
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 */
@Test
public void testGetOrtskurveModell()
        throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    // Das Datenmodell der Ortskurvenberechnung wird im Controller gespeichert.
    Field ortskurveModellFeld = OrtskurveController.class.getDeclaredField("ortskurveModell");
    ortskurveModellFeld.setAccessible(true);
    ortskurveModellFeld.set(this.ortskurveController, this.testOrtskurveModell);

    // Die zu testende Methode wird aufgerufen.
    OrtskurveModell ortskurveModell = this.ortskurveController.getOrtskurveModell();

    // Es wird berprft, ob das Datenmodell der Ortskurvenberechnung korrekt zurckgegeben worden ist.
    assertEquals(this.testOrtskurveModell, ortskurveModell);
}

From source file:io.apiman.test.integration.runner.handlers.AbstractAnnotationHandler.java

/**
 * If possible assign value to given field
 *
 * @param field filed to be assigned to/*from  w ww  .j a  v a  2s. c o  m*/
 * @param value assigned value
 * @throws IllegalAccessException
 */
public <ValueType> void setField(Object target, Field field, Class<ValueType> valueType, ValueType value)
        throws IllegalAccessException {
    if (field.getType().isAssignableFrom(value.getClass())) {
        field.setAccessible(true);
        field.set(target, value);
    }
}

From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java

/**
 * Wirte a bean field./*from   w w w.  j a  v  a2s . co m*/
 *
 * @param bean the bean
 * @param field the field
 * @param value the value
 * @throws RuntimeException if the bean field can not be accessed
 */
public static synchronized void writeField(Object bean, Field field, Object value) {
    AccessibleScope asc = new AccessibleScope(field);
    try {
        field.set(bean, value);
    } catch (Exception ex) {
        throw new RuntimeException("Failure accessing bean field: " + bean.getClass().getName() + "." + field
                + "; " + ex.getMessage(), ex);
    } finally {
        asc.restore();
    }
}

From source file:edu.cornell.library.scholars.webapp.controller.api.distribute.file.FileDistributorTest.java

private void setVitroHomeDirectory(File home) {
    try {//  w  w w .  ja  v a 2s.  c  o  m
        Field instanceField = ApplicationUtils.class.getDeclaredField("instance");
        instanceField.setAccessible(true);
        instanceField.set(null, new ApplicationStub(new VitroHomeDirectory(null, home.toPath(), null)));
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.cloud.api.ApiServletTest.java

@After
public void cleanupEnvironmentHacks() throws Exception {
    Field smsField = ApiDBUtils.class.getDeclaredField("s_ms");
    smsField.setAccessible(true);/*from w  w  w. java 2s  .  co m*/
    smsField.set(null, null);
}

From source file:jp.terasoluna.fw.beans.jxpath.JXPATH152PatchActivatorTest.java

/**
 * testActivate01() <br>//from   w w w  . ja  va  2 s. co m
 * <br>
 * () <br>
 * A <br>
 * <br>
 * () ?<br>
 * () JXPathIntrospector.byClasst:HashMap<br>
 * () JXPathIntrospector.byInterface:HashMap<br>
 * <br>
 * () JXPathIntrospector.byClass?HashMapForJXPathIntrospector?????<br>
 * () JXPathIntrospector.byInterface?HashMapForJXPathIntrospector?????<br>
 * () :INFO<br>
 * JXPATH-152 Patch activation succeeded.<br>
 * <br>
 * ??????? JXPathIntrospector?byClass?byInterface?HashMapForJXPathIntrospector???????? <br>
 * @throws Exception ?????
 */
@Test
public void testActivate01() throws Exception {
    // ??
    Field field = JXPathIntrospector.class.getDeclaredField("byClass");
    field.setAccessible(true);
    field.set(JXPathIntrospector.class, new HashMap<Object, Object>());
    field = JXPathIntrospector.class.getDeclaredField("byInterface");
    field.setAccessible(true);
    field.set(JXPathIntrospector.class, new HashMap<Object, Object>());

    // 
    Method method = JXPATH152PatchActivator.class.getDeclaredMethod("activate");
    method.setAccessible(true);
    method.invoke(JXPATH152PatchActivator.class);

    // 
    assertThat(logger.getLoggingEvents(), is(asList(info("JXPATH-152 Patch activation succeeded."))));
    field = JXPathIntrospector.class.getDeclaredField("byClass");
    field.setAccessible(true);
    assertTrue(
            ((Map<?, ?>) field.get(JXPathIntrospector.class)).getClass() == HashMapForJXPathIntrospector.class);
    field = JXPathIntrospector.class.getDeclaredField("byInterface");
    field.setAccessible(true);
    assertTrue(
            ((Map<?, ?>) field.get(JXPathIntrospector.class)).getClass() == HashMapForJXPathIntrospector.class);
}