Example usage for org.springframework.util ReflectionUtils makeAccessible

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

Introduction

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

Prototype

@SuppressWarnings("deprecation") 
public static void makeAccessible(Field field) 

Source Link

Document

Make the given field accessible, explicitly setting it accessible if necessary.

Usage

From source file:de.systemoutprintln.util.logging.spring.LogPostProcessor.java

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
    ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {

        @Override/*from  ww  w  .j ava 2s .  c  o  m*/
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(field);

            if (field.getAnnotation(Log.class) != null) {
                Logger logger = LoggerFactory.getLogger(bean.getClass());
                field.set(bean, logger);
            }
        }
    });

    return bean;
}

From source file:com.anteam.demo.codec.common.CoderUtil.java

public static Object encode(Object source, EncryptAlgorithm encryptAlgorithm, byte[] key)
        throws EncoderException {
    if (source == null || encryptAlgorithm == null) {
        return null;
    }//  w  ww  . j ava  2s  .c  o  m
    Object result = source;
    if (source instanceof byte[]) {
        return CoderUtil.encode((byte[]) source, encryptAlgorithm, key);
    } else if (source instanceof String) {
        return CoderUtil.encode((String) source, encryptAlgorithm, key);
    }
    Field[] fields = source.getClass().getDeclaredFields();
    for (Field field : fields) {
        Encrypted encrypted = field.getAnnotation(Encrypted.class);
        if (encrypted != null) {
            ReflectionUtils.makeAccessible(field);
            if (!Modifier.isStatic(field.getModifiers())) {
                try {
                    field.set(source, CoderUtil.encode(field.get(source)));
                } catch (IllegalAccessException e) {
                    LOG.error("?:" + source.getClass().getName() + ":" + field.getName(), e);
                }
            }
        }
    }
    return result;
}

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

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test//  w  w  w .ja  va2s .  c  om
public void testSetMutationAlgorithm() {
    MutationAlgorithm mutationAlgorithmToSet = mock(MutationAlgorithm.class);

    ConservativeSinglePointCrossoverAlgorithm conservativeSinglePointCrossoverAlgorithm = new ConservativeSinglePointCrossoverAlgorithm();
    conservativeSinglePointCrossoverAlgorithm.setMutationAlgorithm(mutationAlgorithmToSet);

    Field mutationAlgorithmField = ReflectionUtils.findField(ConservativeSinglePointCrossoverAlgorithm.class,
            "mutationAlgorithm");
    ReflectionUtils.makeAccessible(mutationAlgorithmField);
    MutationAlgorithm mutationAlgorithmFromObject = (MutationAlgorithm) ReflectionUtils
            .getField(mutationAlgorithmField, conservativeSinglePointCrossoverAlgorithm);

    assertSame(mutationAlgorithmToSet, mutationAlgorithmFromObject);
}

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

@BeforeClass
public static void setUp() throws IllegalArgumentException, IllegalAccessException {
    Field maxGenesPerGroupField = ReflectionUtils.findField(GroupMutationAlgorithm.class,
            "MAX_GENES_PER_GROUP");
    ReflectionUtils.makeAccessible(maxGenesPerGroupField);
    ReflectionUtils.setField(maxGenesPerGroupField, null, 2);

    groupMutationAlgorithm = new GroupMutationAlgorithm();

    geneDaoMock = mock(VariableLengthGeneDao.class);
    groupMutationAlgorithm.setGeneDao(geneDaoMock);

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(GroupMutationAlgorithm.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, groupMutationAlgorithm, logMock);
}

From source file:ch.ralscha.extdirectspring.util.ExtDirectSpringUtil.java

/**
 * Invokes a method on a Spring managed bean.
 *
 * @param context a Spring application context
 * @param beanName the name of the bean//from  ww w  . jav a 2 s. c  om
 * @param methodInfo the methodInfo object
 * @param params the parameters
 * @return the result of the method invocation
 * @throws IllegalArgumentException if there is no bean in the context
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static Object invoke(ApplicationContext context, String beanName, MethodInfo methodInfo,
        final Object[] params)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Object bean = context.getBean(beanName);

    Method handlerMethod = methodInfo.getMethod();
    ReflectionUtils.makeAccessible(handlerMethod);
    Object result = handlerMethod.invoke(bean, params);

    if (result != null && result.getClass().equals(javaUtilOptionalClass)) {
        return OptionalUnwrapper.unwrap(result);
    }

    return result;
}

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

@Test
public void testSetFitnessEvaluator() {
    FitnessEvaluator fitnessEvaluatorToSet = mock(FitnessEvaluator.class);
    ConservativeCrossoverAlgorithm conservativeCrossoverAlgorithm = new ConservativeCrossoverAlgorithm();
    conservativeCrossoverAlgorithm.setFitnessEvaluator(fitnessEvaluatorToSet);

    Field fitnessEvaluatorField = ReflectionUtils.findField(ConservativeCrossoverAlgorithm.class,
            "fitnessEvaluator");
    ReflectionUtils.makeAccessible(fitnessEvaluatorField);
    FitnessEvaluator fitnessEvaluatorFromObject = (FitnessEvaluator) ReflectionUtils
            .getField(fitnessEvaluatorField, conservativeCrossoverAlgorithm);

    assertSame(fitnessEvaluatorToSet, fitnessEvaluatorFromObject);
}

From source file:net.ggtools.maven.ddlgenerator.DDLGenerator.java

public void createSchema() {
    log.info("Exporting DDL file to " + ddlFile);
    createDirectoriesIfNeeded();// w  w w  .j  a  va2s. co m
    puManager.preparePersistenceUnitInfos();
    final PersistenceUnitInfo puInfo = puManager.obtainPersistenceUnitInfo(persistenceUnitName);
    final Ejb3Configuration ejb3Config = new Ejb3Configuration();
    ejb3Config.configure(puInfo, configProperties);
    final Field field = ReflectionUtils.findField(Ejb3Configuration.class, "cfg");
    ReflectionUtils.makeAccessible(field);
    final ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(configProperties)
            .buildServiceRegistry();
    final Configuration configuration = (Configuration) ReflectionUtils.getField(field, ejb3Config);
    final SchemaExport export = new SchemaExport(registry, configuration);
    export.setDelimiter(";"); // TODO introduce parameter
    export.setOutputFile(ddlFile.getAbsolutePath());
    export.execute(true, false, false, true);
}

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

@Test
public void testSetWordDao() {
    WordDao wordDaoToSet = mock(WordDao.class);
    WordListImporterImpl wordListImporterImpl = new WordListImporterImpl();
    wordListImporterImpl.setWordDao(wordDaoToSet);

    Field wordDaoField = ReflectionUtils.findField(WordListImporterImpl.class, "wordDao");
    ReflectionUtils.makeAccessible(wordDaoField);
    WordDao wordDaoFromObject = (WordDao) ReflectionUtils.getField(wordDaoField, wordListImporterImpl);

    assertSame(wordDaoToSet, wordDaoFromObject);
}

From source file:com.example.config.StartupApplicationListener.java

private Set<Class<?>> sources(ApplicationReadyEvent event) {
    Method method = ReflectionUtils.findMethod(SpringApplication.class, "getAllSources");
    if (method == null) {
        method = ReflectionUtils.findMethod(SpringApplication.class, "getSources");
    }//from w  w w  . java  2 s . co m
    ReflectionUtils.makeAccessible(method);
    @SuppressWarnings("unchecked")
    Set<Object> objects = (Set<Object>) ReflectionUtils.invokeMethod(method, event.getSpringApplication());
    Set<Class<?>> result = new LinkedHashSet<>();
    for (Object object : objects) {
        if (object instanceof String) {
            object = ClassUtils.resolveClassName((String) object, null);
        }
        result.add((Class<?>) object);
    }
    return result;
}

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

@Test
public void testSetFitnessEvaluator() {
    FitnessEvaluator fitnessEvaluatorToSet = mock(FitnessEvaluator.class);
    LowestCommonGroupCrossoverAlgorithm lowestCommonGroupCrossoverAlgorithm = new LowestCommonGroupCrossoverAlgorithm();
    lowestCommonGroupCrossoverAlgorithm.setFitnessEvaluator(fitnessEvaluatorToSet);

    Field fitnessEvaluatorField = ReflectionUtils.findField(LowestCommonGroupCrossoverAlgorithm.class,
            "fitnessEvaluator");
    ReflectionUtils.makeAccessible(fitnessEvaluatorField);
    FitnessEvaluator fitnessEvaluatorFromObject = (FitnessEvaluator) ReflectionUtils
            .getField(fitnessEvaluatorField, lowestCommonGroupCrossoverAlgorithm);

    assertSame(fitnessEvaluatorToSet, fitnessEvaluatorFromObject);
}