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:com.codenjoy.dojo.tetris.model.GameSetupRule.java

public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
    GivenFiguresInQueue givenAnnotation = method.getAnnotation(GivenFiguresInQueue.class);
    if (givenAnnotation != null) {
        FigureProperties[] figures = givenAnnotation.value();

        FigureQueue figureQueue = mock(FigureQueue.class);
        initQueueWithFigures(figures, figureQueue);
        Field gameField = findField(gameClass, target);

        Glass glass = (Glass) getFieldValue(Glass.class, target);
        when(glass.accept(Matchers.<Figure>anyObject(), anyInt(), anyInt())).thenReturn(true);

        try {/*from  w  w w  .  j  ava 2  s .  co m*/
            gameField.set(target, gameClass.getConstructor(FigureQueue.class, Glass.class, PrinterFactory.class)
                    .newInstance(figureQueue, glass, new PrinterFactoryImpl()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return base;
}

From source file:it.infn.ct.futuregateway.apiserver.v1.TaskServiceTest.java

/**
 * Create a TaskService for test./*from   w w  w  .  jav  a 2  s . co  m*/
 * Add the mock and perform the basic customisation.
 *
 * @return A TaskService with provided mocks
 */
private TaskService getTaskService() {
    TaskService ts = null;
    try {
        Mockito.when(this.em.getTransaction()).thenReturn(this.et);
        Mockito.when(this.emf.createEntityManager()).thenReturn(this.em);
        Mockito.when(this.context.getAttribute(Constants.MONITORQUEUE)).thenReturn(this.mq);
        Mockito.when(this.context.getAttribute(Constants.SESSIONFACTORY)).thenReturn(this.emf);
        Mockito.when(this.context.getAttribute(Constants.CACHEDIR)).thenReturn(TestData.PATHTOTEMPSTORAGE);
        Mockito.when(this.request.getServletContext()).thenReturn(this.context);
        ts = new TaskService();
        Field req = BaseService.class.getDeclaredField("request");
        req.setAccessible(true);
        req.set(ts, this.request);
    } catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException ex) {
        Assert.fail("Impossible to generate the TaskService");
    }
    Assert.assertNotNull(ts.getRequest());
    return ts;
}

From source file:com.pispower.video.sdk.net.SimpleSSLSocketFactory.java

/**
 * Pre-ICS Android had a bug resolving HTTPS addresses. This workaround
 * fixes that bug.//from   www .java2 s.  com
 * 
 * @param socket
 *            The socket to alter
 * @param host
 *            Hostname to connect to
 * @see <a
 *      href="https://code.google.com/p/android/issues/detail?id=13117#c14">https://code.google.com/p/android/issues/detail?id=13117#c14</a>
 */
@SuppressWarnings("deprecation")
private void injectHostname(Socket socket, String host) {
    try {
        if (Integer.valueOf(Build.VERSION.SDK) >= 4) {
            Field field = InetAddress.class.getDeclaredField("hostName");
            field.setAccessible(true);
            field.set(socket.getInetAddress(), host);
        }
    } catch (Exception ignored) {
    }
}

From source file:org.slc.sli.ingestion.tool.ValidationControllerTest.java

@Test
public void testProcessZip() throws IOException, NoSuchFieldException, IllegalAccessException {
    ZipFileHandler handler = Mockito.mock(ZipFileHandler.class);

    ErrorReport er = Mockito.mock(ErrorReport.class);
    Field errorReport = validationController.getClass().getDeclaredField("errorReport");
    errorReport.setAccessible(true);//from  ww w.jav a2  s  .com
    errorReport.set(null, er);

    Resource zipFileResource = new ClassPathResource(zipFileName);
    File zipFile = zipFileResource.getFile();

    ValidationController vc = Mockito.spy(validationController);
    Mockito.doNothing().when(vc).processControlFile((File) Mockito.any());
    Mockito.doReturn(zipFile).when(handler).handle(zipFile, er);

    vc.setZipFileHandler(handler);
    vc.processZip(zipFile);
    Mockito.verify(handler, Mockito.atLeastOnce()).handle(zipFile, er);
}

From source file:org.slc.sli.ingestion.tool.ValidationControllerTest.java

@Test
public void testProcessInvalidZip() throws IOException, NoSuchFieldException, IllegalAccessException {

    ZipFileHandler handler = Mockito.mock(ZipFileHandler.class);
    Field zipField = validationController.getClass().getDeclaredField("zipFileHandler");
    zipField.setAccessible(true);/*from ww w.  j a v  a 2  s  .  co  m*/
    zipField.set(validationController, handler);

    Resource zipFileResource = new ClassPathResource(zipFileName);
    File zipFile = zipFileResource.getFile();

    ValidationController vc = Mockito.spy(validationController);

    ErrorReport er = Mockito.mock(ErrorReport.class);
    Field errorReport = validationController.getClass().getDeclaredField("errorReport");
    errorReport.setAccessible(true);
    errorReport.set(null, er);

    Mockito.when(er.hasErrors()).thenReturn(true);
    Mockito.doReturn(zipFile).when(handler).handle(zipFile, er);
    vc.processZip(zipFile);
    Mockito.verify(vc, Mockito.never()).processControlFile(zipFile);
}

From source file:candr.yoclip.option.OptionField.java

protected void setStringOption(final T bean, final String value) {

    set(bean, new Value<T>() {

        public void set(final T bean, final Field field) throws IllegalAccessException {

            field.set(bean, value);
        }/*from w  w  w .  j a va 2  s  .  co m*/
    });
}

From source file:com.ngandroid.lib.NgAndroid.java

public <T> T buildScope(Class<T> clss) {
    T instance;/*www . j  av  a 2s  . com*/
    try {
        instance = clss.newInstance();
        Field[] fields = clss.getDeclaredFields();
        for (Field f : fields) {
            f.setAccessible(true);
            Class type = f.getType();
            if (type.isInterface() && !f.isAnnotationPresent(Ignore.class)) {
                f.set(instance, buildModel(type));
            }
        }
    } catch (InstantiationException | IllegalAccessException e) {
        // TODO
        throw new RuntimeException("Error instantiating scope.", e);
    }
    return instance;
}

From source file:candr.yoclip.option.OptionField.java

protected void setBooleanOption(final T bean, final String value) {

    final Boolean booleanValue = Boolean.valueOf(value);
    set(bean, new Value<T>() {

        public void set(final T bean, final Field field) throws IllegalAccessException {

            field.set(bean, booleanValue);
        }/*from  ww w  . j  a  v a2  s  . c om*/
    });
}

From source file:candr.yoclip.option.OptionField.java

protected void setByteOption(final T bean, final String value) {

    final Byte byteValue = StringUtils.isEmpty(value) ? 0 : Byte.valueOf(value);
    set(bean, new Value<T>() {

        public void set(final T bean, final Field field) throws IllegalAccessException {

            field.set(bean, byteValue);
        }//from ww w . j ava  2  s .  c  o  m
    });
}

From source file:candr.yoclip.option.OptionField.java

protected void setShortOption(final T bean, final String value) {

    final Short shortValue = StringUtils.isEmpty(value) ? 0 : Short.valueOf(value);
    set(bean, new Value<T>() {

        public void set(final T bean, final Field field) throws IllegalAccessException {

            field.set(bean, shortValue);
        }/*from  ww  w.  j  a  v a2 s  .c  o  m*/
    });
}