Example usage for org.springframework.util ReflectionUtils getField

List of usage examples for org.springframework.util ReflectionUtils getField

Introduction

In this page you can find the example usage for org.springframework.util ReflectionUtils getField.

Prototype

@Nullable
public static Object getField(Field field, @Nullable Object target) 

Source Link

Document

Get the field represented by the supplied Field field object on the specified Object target object .

Usage

From source file:py.una.pol.karaku.dao.entity.interceptors.InterceptorHandler.java

/**
 * Intercepta un atributo de un bean especifico.
 * // w w  w.  j av a 2s .  c  o m
 * <p>
 * Reglas:
 * <ol>
 * <li>Si el item es un atributo normal, invocar a su respectivo
 * interceptor.
 * </p>
 * <li>Si es una coleccin, y tiene tiene la anotacin {@link OneToMany}, y
 * su {@link CascadeType} es {@link CascadeType#ALL}, entonces se propaga la
 * intercepcin a los miembros de la coleccin. </p>
 * 
 * @param op
 *            Operacin actual.
 * @param field
 *            campo sobre el cual se esta ejecutando.
 * @param bean
 *            objeto que esta siendo interceptado.
 */
private void intercept(@Nonnull Operation op, @Nonnull Field field, @Nonnull Object bean) {

    if (field.getAnnotation(OneToMany.class) != null) {
        OneToMany otm = field.getAnnotation(OneToMany.class);
        CascadeType[] cascade = otm.cascade();
        if (cascade != null && ListHelper.contains(cascade, CascadeType.ALL)) {
            field.setAccessible(true);
            Collection<?> c = (Collection<?>) ReflectionUtils.getField(field, bean);
            if (Hibernate.isInitialized(c) && ListHelper.hasElements(c)) {
                for (Object o : c) {
                    this.intercept(op, o);
                }
            }
        }
        return;
    }
    field.setAccessible(true);
    Class<?> type = field.getType();

    Set<Interceptor> typeInterceptors = addAll(byType.get(void.class), byType.get(type));

    Annotation[] annons = field.getAnnotations();

    Set<Interceptor> annonInterceptors = new HashSet<Interceptor>();
    if (byAnnotation.get(void.class) != null) {
        annonInterceptors.addAll(byAnnotation.get(void.class));
    }

    for (Annotation an : annons) {
        if (byAnnotation.get(an.annotationType()) != null) {
            annonInterceptors.addAll(byAnnotation.get(an.annotationType()));
        }
    }

    typeInterceptors.retainAll(annonInterceptors);

    for (Interceptor bi : typeInterceptors) {
        if (this.isAssignable(field) && bi.interceptable(op, field, bean)) {
            bi.intercept(op, field, bean);
        }
    }
}

From source file:com.github.lothar.security.acl.jpa.query.AclJpaQuery.java

@SuppressWarnings("unchecked")
private static <T> T getField(Class<?> type, Object object, String fieldName) {
    Field field = ReflectionUtils.findField(type, fieldName);
    field.setAccessible(true);//from  w  w  w .  j  a v  a  2  s .com
    Object property = ReflectionUtils.getField(field, object);
    return (T) property;
}

From source file:com.ciphertool.sentencebuilder.etl.importers.FrequencyListImporterImplTest.java

@Test
public void testSetTaskExecutor() {
    TaskExecutor taskExecutorToSet = mock(TaskExecutor.class);
    FrequencyListImporterImpl frequencyListImporterImpl = new FrequencyListImporterImpl();
    frequencyListImporterImpl.setTaskExecutor(taskExecutorToSet);

    Field taskExecutorField = ReflectionUtils.findField(FrequencyListImporterImpl.class, "taskExecutor");
    ReflectionUtils.makeAccessible(taskExecutorField);
    TaskExecutor taskExecutorFromObject = (TaskExecutor) ReflectionUtils.getField(taskExecutorField,
            frequencyListImporterImpl);/*from www.  j av a 2  s  . c o  m*/

    assertEquals(taskExecutorToSet, taskExecutorFromObject);
}

From source file:com.ciphertool.genetics.algorithms.crossover.LiberalCrossoverAlgorithmTest.java

@Test
public void testSetMutateDuringCrossover() {
    boolean mutateDuringCrossoverToSet = true;

    LiberalCrossoverAlgorithm liberalCrossoverAlgorithm = new LiberalCrossoverAlgorithm();
    liberalCrossoverAlgorithm.setMutateDuringCrossover(mutateDuringCrossoverToSet);

    Field mutateDuringCrossoverField = ReflectionUtils.findField(LiberalCrossoverAlgorithm.class,
            "mutateDuringCrossover");
    ReflectionUtils.makeAccessible(mutateDuringCrossoverField);
    boolean mutateDuringCrossoverFromObject = (boolean) ReflectionUtils.getField(mutateDuringCrossoverField,
            liberalCrossoverAlgorithm);/*from w  w w .  j a  v  a  2 s.  c  o m*/

    assertEquals(mutateDuringCrossoverToSet, mutateDuringCrossoverFromObject);
}

From source file:com.ciphertool.genetics.PopulationTest.java

@Test
public void testSetFitnessComparator() {
    Population population = new Population();

    AscendingFitnessComparator ascendingFitnessComparator = new AscendingFitnessComparator();
    population.setFitnessComparator(ascendingFitnessComparator);

    Field fitnessComparatorField = ReflectionUtils.findField(Population.class, "fitnessComparator");
    ReflectionUtils.makeAccessible(fitnessComparatorField);
    AscendingFitnessComparator fitnessComparatorFromObject = (AscendingFitnessComparator) ReflectionUtils
            .getField(fitnessComparatorField, population);

    assertSame(ascendingFitnessComparator, fitnessComparatorFromObject);
}

From source file:com.ciphertool.genetics.algorithms.crossover.LiberalUnevaluatedCrossoverAlgorithmTest.java

@Test
public void testSetCoin() {
    Coin coinToSet = mock(Coin.class);
    LiberalUnevaluatedCrossoverAlgorithm liberalUnevaluatedCrossoverAlgorithm = new LiberalUnevaluatedCrossoverAlgorithm();
    liberalUnevaluatedCrossoverAlgorithm.setCoin(coinToSet);

    Field coinField = ReflectionUtils.findField(LiberalUnevaluatedCrossoverAlgorithm.class, "coin");
    ReflectionUtils.makeAccessible(coinField);
    Coin coinFromObject = (Coin) ReflectionUtils.getField(coinField, liberalUnevaluatedCrossoverAlgorithm);

    assertSame(coinToSet, coinFromObject);
}

From source file:com.ciphertool.genetics.algorithms.mutation.LiberalMutationAlgorithmTest.java

@Test
public void testSetMaxMutationsPerChromosome() {
    Integer maxMutationsPerChromosomeToSet = 3;

    LiberalMutationAlgorithm liberalMutationAlgorithm = new LiberalMutationAlgorithm();
    liberalMutationAlgorithm.setMaxMutationsPerChromosome(maxMutationsPerChromosomeToSet);

    Field maxMutationsPerChromosomeField = ReflectionUtils.findField(LiberalMutationAlgorithm.class,
            "maxMutationsPerChromosome");
    ReflectionUtils.makeAccessible(maxMutationsPerChromosomeField);
    Integer maxMutationsPerChromosomeFromObject = (Integer) ReflectionUtils
            .getField(maxMutationsPerChromosomeField, liberalMutationAlgorithm);

    assertSame(maxMutationsPerChromosomeToSet, maxMutationsPerChromosomeFromObject);
}

From source file:com.groupon.jenkins.dynamic.build.DbBackedBuild.java

public String getState() {
    String stateName = null;//from   w ww  .  j  a v  a2  s .com
    try {
        final Field field = Run.class.getDeclaredField("state");
        field.setAccessible(true);
        stateName = ReflectionUtils.getField(field, this).toString();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }

    return stateName;
}

From source file:com.alibaba.otter.shared.common.utils.zookeeper.ZooKeeperx.java

public void configMutliCluster(ZooKeeper zk) {
    if (_servers.size() == 1) {
        return;//from   w w  w.j  a  v a 2s.c  o  m
    }
    String cluster1 = _servers.get(0);
    try {
        if (_servers.size() > 1) {
            // accessible
            ReflectionUtils.makeAccessible(clientCnxnField);
            ReflectionUtils.makeAccessible(hostProviderField);
            ReflectionUtils.makeAccessible(serverAddressesField);

            // 
            for (int i = 1; i < _servers.size(); i++) {
                String cluster = _servers.get(i);
                // ?zk??
                ClientCnxn cnxn = (ClientCnxn) ReflectionUtils.getField(clientCnxnField, zk);
                HostProvider hostProvider = (HostProvider) ReflectionUtils.getField(hostProviderField, cnxn);
                List<InetSocketAddress> serverAddrs = (List<InetSocketAddress>) ReflectionUtils
                        .getField(serverAddressesField, hostProvider);
                // 
                serverAddrs.addAll(new ConnectStringParser(cluster).getServerAddresses());
            }
        }
    } catch (Exception e) {
        try {
            if (zk != null) {
                zk.close();
            }
        } catch (InterruptedException ie) {
            // ignore interrupt
        }
        throw new ZkException("zookeeper_create_error, serveraddrs=" + cluster1, e);
    }

}