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:ch.algotrader.esper.aggregation.GenericTALibFunction.java

@Override
public Object getValue() {

    try {/*  ww  w.j av a2 s. c o  m*/
        // get the total number of parameters
        int numberOfArgs = 2 + this.inputParams.size() + this.optInputParams.size() + 2
                + this.outputParams.size();
        Object[] args = new Object[numberOfArgs];

        // get the size of the first input buffer
        int elements = this.inputParams.iterator().next().size();

        args[0] = elements - 1; // startIdx
        args[1] = elements - 1; // endIdx

        // inputParams
        int argCount = 2;
        for (CircularFifoBuffer<Number> buffer : this.inputParams) {

            // look at the first element of the buffer to determine the type
            Object firstElement = buffer.iterator().next();
            if (firstElement instanceof Double) {
                args[argCount] = ArrayUtils.toPrimitive(buffer.toArray(new Double[0]));
            } else if (firstElement instanceof Integer) {
                args[argCount] = ArrayUtils.toPrimitive(buffer.toArray(new Integer[0]));
            } else {
                throw new IllegalArgumentException("unsupported type " + firstElement.getClass());
            }
            argCount++;
        }

        // optInputParams
        for (Object object : this.optInputParams) {
            args[argCount] = object;
            argCount++;
        }

        // begin
        MInteger begin = new MInteger();
        args[argCount] = begin;
        argCount++;

        // length
        MInteger length = new MInteger();
        args[argCount] = length;
        argCount++;

        // OutputParams
        for (Map.Entry<String, Object> entry : this.outputParams.entrySet()) {
            args[argCount++] = entry.getValue();
        }

        // invoke the function
        RetCode retCode = (RetCode) this.function.invoke(this.core, args);

        if (retCode == RetCode.Success) {
            if (length.value == 0) {
                return null;
            }

            // if we only have one outPutParam return that value
            // otherwise return a Map
            if (this.outputParams.size() == 1) {
                Object value = this.outputParams.values().iterator().next();
                return getNumberFromNumberArray(value);
            } else {
                Object returnObject = this.outputClass.newInstance();
                for (Map.Entry<String, Object> entry : this.outputParams.entrySet()) {
                    Number value = getNumberFromNumberArray(entry.getValue());
                    String name = entry.getKey().toLowerCase().substring(3);

                    Field field = this.outputClass.getField(name);
                    field.set(returnObject, value);
                }
                return returnObject;
            }
        } else {
            throw new RuntimeException(retCode.toString());
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cfs.util.AESCriptografia.java

public void cifrarObjetoComKey(Object objeto, String key) {
    try {/*from  w  ww. ja v a 2 s.co m*/
        Field[] campos = objeto.getClass().getDeclaredFields();
        for (Field campo : campos) {
            campo.setAccessible(true);
            if (campo.getType().equals(String.class)) {
                if (campo.getName().equals("key")) {
                    campo.set(objeto, campo.get(objeto));
                } else {
                    campo.set(objeto, cifrar(String.valueOf(campo.get(objeto)), key));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.mysoft.b2b.event.util.MysoftBoneCPDataSource.java

/**
 * /*from w  w  w .  j  av a 2 s . co m*/
 *
 * @param config
 */
public MysoftBoneCPDataSource(BoneCPConfig config) {
    Field[] fields = BoneCPConfig.class.getDeclaredFields();
    for (Field field : fields) {
        try {
            field.setAccessible(true);
            field.set(this, field.get(config));
        } catch (Exception e) {
            // should never happen
        }
    }
}

From source file:be.fedict.trust.xkms2.ServiceConsumerInstanceResolver.java

private void injectServices(T endpoint, TrustService trustService) {
    LOG.debug("injecting services into JAX-WS endpoint...");
    Field[] fields = endpoint.getClass().getDeclaredFields();
    for (Field field : fields) {
        EJB ejbAnnotation = field.getAnnotation(EJB.class);
        if (null == ejbAnnotation) {
            continue;
        }/*from  w  w  w. j av  a  2 s .  c  om*/
        if (field.getType().equals(TrustService.class)) {
            field.setAccessible(true);
            try {
                field.set(endpoint, trustService);
            } catch (Exception e) {
                throw new RuntimeException("injection error: " + e.getMessage(), e);
            }
        }
    }
}

From source file:py.una.pol.karaku.test.test.configuration.KarakuWSClientConfigurationTest.java

@Before
public void initialize() {

    Field[] campos = wsConf.getClass().getDeclaredFields();

    for (Field f : campos) {
        if ("properties".equals(f.getName())) {
            f.setAccessible(true);//from   ww  w. j  a v  a2s.c  om
            try {
                f.set(wsConf, properties);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:lodsve.springfox.config.SpringFoxDocket.java

@PostConstruct
public void init() throws NoSuchFieldException, IllegalAccessException {
    apiInfo(apiInfo(properties));//from w w  w. j a  v  a  2 s . c  om
    forCodeGeneration(true);
    groupName(groupName);
    pathProvider(pathProvider);
    host(getHost());

    if (StringUtils.equals(DEFAULT_GROUP_NAME, groupName)) {
        return;
    }

    // apiSelector
    Field apiSelector = this.getClass().getSuperclass().getDeclaredField("apiSelector");
    apiSelector.setAccessible(true);
    Predicate<String> pathSelector = ApiSelector.DEFAULT.getPathSelector();
    pathSelector = Predicates.and(pathSelector, includePath());
    apiSelector.set(this, new ApiSelector(
            combine(ApiSelector.DEFAULT.getRequestHandlerSelector(), pathSelector), pathSelector));
}

From source file:com.cfs.util.AESCriptografia.java

public void decifrarObjetoComKey(Object objeto, String key) {
    try {/*  w  w w  .j ava 2 s  .  c om*/
        Field[] campos = objeto.getClass().getDeclaredFields();
        //            System.out.println("DEPURATION MASTER: START !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        for (Field campo : campos) {
            campo.setAccessible(true);
            if (campo.getType().equals(String.class)) {
                if (campo.getName().equals("key")) {
                    campo.set(objeto, campo.get(objeto));
                } else {
                    if (campo.get(objeto) == null) {
                        campo.set(objeto, "null");
                        System.out.println("[SET-NULL] " + campo.getName() + " = " + campo.get(objeto));
                    } else {
                        campo.set(objeto, decifrar(String.valueOf(campo.get(objeto)), key));
                        //                            System.out.println("[SET] " + campo.getName() + " = " + campo.get(objeto));
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolderTest.java

@Test
public void testIsSuppressed() throws Exception {
    Class<?> entry = Class.forName("com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder$Entry");
    Constructor<?> entryConstructor = entry.getDeclaredConstructor(String.class, int.class, int.class,
            int.class, int.class);
    entryConstructor.setAccessible(true);

    Object entryInstance = entryConstructor.newInstance("MockEntry", 100, 100, 350, 350);

    List<Object> entriesList = new ArrayList<>();
    entriesList.add(entryInstance);//  ww  w .j  a  v a  2  s  .c  o m

    ThreadLocal<?> threadLocal = mock(ThreadLocal.class);
    PowerMockito.doReturn(entriesList).when(threadLocal, "get");

    SuppressWarningsHolder holder = new SuppressWarningsHolder();
    Field entries = holder.getClass().getDeclaredField("ENTRIES");
    entries.setAccessible(true);
    entries.set(holder, threadLocal);

    assertFalse(SuppressWarningsHolder.isSuppressed("SourceName", 100, 10));
}

From source file:com.cfs.util.AESCriptografia.java

public void cifrarObjeto(Object objeto) {
    try {/*from  w  ww.j  av  a 2  s. com*/
        String chave = null;
        Method[] metodos = objeto.getClass().getMethods();
        for (Method metodo : metodos) {
            if (metodo.getName().startsWith("getKey")) {
                chave = String.valueOf(metodo.invoke(objeto));
            }
        }

        Field[] campos = objeto.getClass().getDeclaredFields();
        for (Field campo : campos) {
            campo.setAccessible(true);
            if (campo.getType().equals(String.class)) {
                if (campo.getName().equals("key")) {
                    campo.set(objeto, campo.get(objeto));
                } else {
                    campo.set(objeto, cifrar(String.valueOf(campo.get(objeto)), chave));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.cfs.util.AESCriptografia.java

public void decifrarObjeto(Object objeto) {
    try {/*  w w w  .  j  a v  a2  s .  c  om*/
        String chave = null;
        Method[] metodos = objeto.getClass().getMethods();
        for (Method metodo : metodos) {
            if (metodo.getName().startsWith("getKey")) {
                chave = String.valueOf(metodo.invoke(objeto));
            }
        }

        Field[] campos = objeto.getClass().getDeclaredFields();
        for (Field campo : campos) {
            campo.setAccessible(true);
            if (campo.getType().equals(String.class)) {
                if (campo.getName().equals("key")) {
                    campo.set(objeto, campo.get(objeto));
                } else {
                    campo.set(objeto, decifrar(String.valueOf(campo.get(objeto)), chave));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}