List of usage examples for org.springframework.util ReflectionUtils makeAccessible
@SuppressWarnings("deprecation") public static void makeAccessible(Field field)
From source file:ch.rasc.wampspring.method.InvocableWampHandlerMethod.java
/** * Invoke the handler method with the given argument values. *//* w ww . jav a2 s. c o m*/ protected Object doInvoke(Object... args) throws Exception { ReflectionUtils.makeAccessible(getBridgedMethod()); try { return getBridgedMethod().invoke(getBean(), args); } catch (IllegalArgumentException ex) { assertTargetBean(getBridgedMethod(), getBean(), args); throw new IllegalStateException(getInvocationErrorMessage(ex.getMessage(), args), ex); } catch (InvocationTargetException ex) { // Unwrap for HandlerExceptionResolvers ... Throwable targetException = ex.getTargetException(); if (targetException instanceof RuntimeException) { throw (RuntimeException) targetException; } else if (targetException instanceof Error) { throw (Error) targetException; } else if (targetException instanceof Exception) { throw (Exception) targetException; } else { String msg = getInvocationErrorMessage("Failed to invoke controller method", args); throw new IllegalStateException(msg, targetException); } } }
From source file:com.ciphertool.sentencebuilder.etl.importers.WordListImporterImplTest.java
@Test public void testImportWordList() { ThreadPoolTaskExecutor taskExecutorSpy = spy(new ThreadPoolTaskExecutor()); taskExecutorSpy.setCorePoolSize(4);/* ww w .j av a2s . co m*/ taskExecutorSpy.setMaxPoolSize(4); taskExecutorSpy.setQueueCapacity(100); taskExecutorSpy.setKeepAliveSeconds(1); taskExecutorSpy.setAllowCoreThreadTimeOut(true); taskExecutorSpy.initialize(); WordListImporterImpl wordListImporterImpl = new WordListImporterImpl(); wordListImporterImpl.setTaskExecutor(taskExecutorSpy); Field rowCountField = ReflectionUtils.findField(WordListImporterImpl.class, "rowCount"); ReflectionUtils.makeAccessible(rowCountField); AtomicInteger rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField, wordListImporterImpl); assertEquals(0, rowCountFromObject.intValue()); WordDao wordDaoMock = mock(WordDao.class); when(wordDaoMock.insertBatch(anyListOf(Word.class))).thenReturn(true); int persistenceBatchSizeToSet = 3; int concurrencyBatchSizeToSet = 3; wordListImporterImpl.setWordDao(wordDaoMock); wordListImporterImpl.setPersistenceBatchSize(persistenceBatchSizeToSet); wordListImporterImpl.setConcurrencyBatchSize(concurrencyBatchSizeToSet); Word word1 = new Word(new WordId("george", PartOfSpeechType.NOUN)); Word word2 = new Word(new WordId("elmer", PartOfSpeechType.NOUN)); Word word3 = new Word(new WordId("belden", PartOfSpeechType.NOUN)); List<Word> wordsToReturn = new ArrayList<Word>(); wordsToReturn.add(word1); wordsToReturn.add(word2); wordsToReturn.add(word3); PartOfSpeechFileParser fileParserMock = mock(PartOfSpeechFileParser.class); when(fileParserMock.parseFile()).thenReturn(wordsToReturn); wordListImporterImpl.setFileParser(fileParserMock); wordListImporterImpl.importWordList(); rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField, wordListImporterImpl); assertEquals(3, rowCountFromObject.intValue()); verify(wordDaoMock, times(1)).insertBatch(anyListOf(Word.class)); verify(taskExecutorSpy, times(1)).execute(any(Runnable.class)); }
From source file:com.ciphertool.zodiacengine.service.GeneticCipherSolutionServiceTest.java
@Test public void testBegin() throws IOException { GeneticCipherSolutionService geneticCipherSolutionService = new GeneticCipherSolutionService(); GeneticAlgorithm geneticAlgorithm = mock(GeneticAlgorithm.class); Population population = new Population(); SolutionChromosome solutionChromosome = new SolutionChromosome(); solutionChromosome.setEvaluationNeeded(false); population.addIndividual(solutionChromosome); when(geneticAlgorithm.getPopulation()).thenReturn(population); geneticCipherSolutionService.setGeneticAlgorithm(geneticAlgorithm); SolutionDao solutionDaoMock = mock(SolutionDao.class); geneticCipherSolutionService.setSolutionDao(solutionDaoMock); GenericCallback genericCallbackMock = mock(GenericCallback.class); Runtime runtimeMock = mock(Runtime.class); Field runtimeField = ReflectionUtils.findField(GeneticCipherSolutionService.class, "runtime"); ReflectionUtils.makeAccessible(runtimeField); ReflectionUtils.setField(runtimeField, geneticCipherSolutionService, runtimeMock); String[] commandsAfter = { "command1", "command2" }; geneticCipherSolutionService.setCommandsAfter(commandsAfter); String[] commandsBefore = { "command3", "command4" }; geneticCipherSolutionService.setCommandsBefore(commandsBefore); GeneticAlgorithmStrategy geneticAlgorithmStrategy = new GeneticAlgorithmStrategy(); // Before the algorithm begins, this will be false assertFalse(geneticCipherSolutionService.isRunning()); geneticCipherSolutionService.begin(geneticAlgorithmStrategy, genericCallbackMock, false); /*//from w w w. j a v a2s . com * After the algorithm ends, this will again be false. It is only true * actually during the algorithm. */ assertFalse(geneticCipherSolutionService.isRunning()); verify(solutionDaoMock, times(1)).insert(same(solutionChromosome)); verify(genericCallbackMock, times(1)).doCallback(); /* * These commands should still get called even though an exception was * caught */ verify(runtimeMock, times(1)).exec(eq("command1")); verify(runtimeMock, times(1)).exec(eq("command2")); verify(runtimeMock, times(1)).exec(eq("command3")); verify(runtimeMock, times(1)).exec(eq("command4")); verifyNoMoreInteractions(runtimeMock); verify(geneticAlgorithm, times(1)).evolveAutonomously(); verify(geneticAlgorithm, times(1)).setStrategy(same(geneticAlgorithmStrategy)); }
From source file:com.qcadoo.maven.plugins.jetty.JettyMojo.java
private void setField(final String name, final Object value) { Field field = ReflectionUtils.findField(getClass(), name); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, this, value); }
From source file:com.ciphertool.genetics.PopulationTest.java
@Test public void testSetTaskExecutor() { Population population = new Population(); TaskExecutor taskExecutor = mock(TaskExecutor.class); population.setTaskExecutor(taskExecutor); Field taskExecutorField = ReflectionUtils.findField(Population.class, "taskExecutor"); ReflectionUtils.makeAccessible(taskExecutorField); TaskExecutor taskExecutorFromObject = (TaskExecutor) ReflectionUtils.getField(taskExecutorField, population);/*from w w w . j ava2 s. co m*/ assertSame(taskExecutor, taskExecutorFromObject); }
From source file:com.ciphertool.genetics.algorithms.MultigenerationalGeneticAlgorithmTest.java
@Test public void testSetStrategy() { GeneticAlgorithmStrategy strategyToSet = new GeneticAlgorithmStrategy(); Population populationMock = mock(Population.class); CrossoverAlgorithm crossoverAlgorithmMock = mock(CrossoverAlgorithm.class); NonUniformMutationAlgorithm mutationAlgorithmMock = mock(NonUniformMutationAlgorithm.class); SelectionAlgorithm selectionAlgorithmMock = mock(SelectionAlgorithm.class); FitnessEvaluator fitnessEvaluatorMock = mock(FitnessEvaluator.class); FitnessEvaluator knownSolutionFitnessEvaluatorMock = mock(FitnessEvaluator.class); Object geneticStructure = new Object(); int lifeSpan = 10; boolean compareToKnownSolution = true; boolean mutateDuringCrossover = true; int maxMutationsPerIndividual = 5; strategyToSet.setGeneticStructure(geneticStructure); strategyToSet.setFitnessEvaluator(fitnessEvaluatorMock); strategyToSet.setLifespan(lifeSpan); strategyToSet.setKnownSolutionFitnessEvaluator(knownSolutionFitnessEvaluatorMock); strategyToSet.setCompareToKnownSolution(compareToKnownSolution); strategyToSet.setCrossoverAlgorithm(crossoverAlgorithmMock); strategyToSet.setMutationAlgorithm(mutationAlgorithmMock); strategyToSet.setMutateDuringCrossover(mutateDuringCrossover); strategyToSet.setMaxMutationsPerIndividual(maxMutationsPerIndividual); strategyToSet.setSelectionAlgorithm(selectionAlgorithmMock); MultigenerationalGeneticAlgorithm multigenerationalGeneticAlgorithm = new MultigenerationalGeneticAlgorithm(); multigenerationalGeneticAlgorithm.setPopulation(populationMock); multigenerationalGeneticAlgorithm.setStrategy(strategyToSet); Field strategyField = ReflectionUtils.findField(MultigenerationalGeneticAlgorithm.class, "strategy"); ReflectionUtils.makeAccessible(strategyField); GeneticAlgorithmStrategy strategyFromObject = (GeneticAlgorithmStrategy) ReflectionUtils .getField(strategyField, multigenerationalGeneticAlgorithm); assertSame(strategyToSet, strategyFromObject); verify(populationMock, times(1)).setGeneticStructure(same(geneticStructure)); verify(populationMock, times(1)).setFitnessEvaluator(same(fitnessEvaluatorMock)); verify(populationMock, times(1)).setLifespan(eq(lifeSpan)); verify(populationMock, times(1)).setKnownSolutionFitnessEvaluator(same(knownSolutionFitnessEvaluatorMock)); verify(populationMock, times(1)).setCompareToKnownSolution(eq(compareToKnownSolution)); verifyNoMoreInteractions(populationMock); verify(crossoverAlgorithmMock, times(1)).setMutationAlgorithm(same(mutationAlgorithmMock)); verify(crossoverAlgorithmMock, times(1)).setMutateDuringCrossover(eq(mutateDuringCrossover)); verifyNoMoreInteractions(crossoverAlgorithmMock); verify(mutationAlgorithmMock, times(1)).setMaxMutationsPerChromosome(eq(maxMutationsPerIndividual)); verifyNoMoreInteractions(mutationAlgorithmMock); Field crossoverAlgorithmField = ReflectionUtils.findField(MultigenerationalGeneticAlgorithm.class, "crossoverAlgorithm"); ReflectionUtils.makeAccessible(crossoverAlgorithmField); CrossoverAlgorithm crossoverAlgorithmFromObject = (CrossoverAlgorithm) ReflectionUtils .getField(crossoverAlgorithmField, multigenerationalGeneticAlgorithm); assertSame(crossoverAlgorithmMock, crossoverAlgorithmFromObject); Field mutationAlgorithmField = ReflectionUtils.findField(MultigenerationalGeneticAlgorithm.class, "mutationAlgorithm"); ReflectionUtils.makeAccessible(mutationAlgorithmField); MutationAlgorithm mutationAlgorithmFromObject = (MutationAlgorithm) ReflectionUtils .getField(mutationAlgorithmField, multigenerationalGeneticAlgorithm); assertSame(mutationAlgorithmMock, mutationAlgorithmFromObject); Field selectionAlgorithmField = ReflectionUtils.findField(MultigenerationalGeneticAlgorithm.class, "selectionAlgorithm"); ReflectionUtils.makeAccessible(selectionAlgorithmField); SelectionAlgorithm selectionAlgorithmFromObject = (SelectionAlgorithm) ReflectionUtils .getField(selectionAlgorithmField, multigenerationalGeneticAlgorithm); assertSame(selectionAlgorithmMock, selectionAlgorithmFromObject); }
From source file:com.github.marsbits.restfbmessenger.spring.boot.autoconfigure.MessengerAutoConfigurationTests.java
private Object getFieldValue(Object object, String name) { Field field = ReflectionUtils.findField(object.getClass(), name); ReflectionUtils.makeAccessible(field); return ReflectionUtils.getField(field, object); }
From source file:com.ciphertool.sentencebuilder.etl.importers.FrequencyListImporterImplTest.java
@Test public void testImportFrequencyList() { ThreadPoolTaskExecutor taskExecutorSpy = spy(new ThreadPoolTaskExecutor()); taskExecutorSpy.setCorePoolSize(4);/*ww w.j av a 2s. c om*/ taskExecutorSpy.setMaxPoolSize(4); taskExecutorSpy.setQueueCapacity(100); taskExecutorSpy.setKeepAliveSeconds(1); taskExecutorSpy.setAllowCoreThreadTimeOut(true); taskExecutorSpy.initialize(); FrequencyListImporterImpl frequencyListImporterImpl = new FrequencyListImporterImpl(); frequencyListImporterImpl.setTaskExecutor(taskExecutorSpy); Field rowUpdateCountField = ReflectionUtils.findField(FrequencyListImporterImpl.class, "rowUpdateCount"); ReflectionUtils.makeAccessible(rowUpdateCountField); AtomicInteger rowUpdateCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowUpdateCountField, frequencyListImporterImpl); assertEquals(0, rowUpdateCountFromObject.intValue()); Field rowInsertCountField = ReflectionUtils.findField(FrequencyListImporterImpl.class, "rowInsertCount"); ReflectionUtils.makeAccessible(rowInsertCountField); AtomicInteger rowInsertCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowInsertCountField, frequencyListImporterImpl); assertEquals(0, rowInsertCountFromObject.intValue()); WordDao wordDaoMock = mock(WordDao.class); when(wordDaoMock.insertBatch(anyListOf(Word.class))).thenReturn(true); int persistenceBatchSizeToSet = 2; int concurrencyBatchSizeToSet = 2; frequencyListImporterImpl.setWordDao(wordDaoMock); frequencyListImporterImpl.setPersistenceBatchSize(persistenceBatchSizeToSet); frequencyListImporterImpl.setConcurrencyBatchSize(concurrencyBatchSizeToSet); Word word1 = new Word(new WordId("george", PartOfSpeechType.NOUN), 100); Word word2 = new Word(new WordId("belden", PartOfSpeechType.NOUN), 200); Word word3 = new Word(new WordId("is", PartOfSpeechType.VERB_PARTICIPLE), 300); Word word4 = new Word(new WordId("awesome", PartOfSpeechType.ADJECTIVE), 400); List<Word> wordsToReturn = new ArrayList<Word>(); wordsToReturn.add(word1); wordsToReturn.add(word2); wordsToReturn.add(word3); wordsToReturn.add(word4); FrequencyFileParser fileParserMock = mock(FrequencyFileParser.class); when(fileParserMock.parseFile()).thenReturn(wordsToReturn); frequencyListImporterImpl.setFileParser(fileParserMock); Word wordFromDatabase1 = new Word(new WordId("george", PartOfSpeechType.NOUN)); Word wordFromDatabase2 = new Word(new WordId("belden", PartOfSpeechType.NOUN)); when(wordDaoMock.insertBatch(anyListOf(Word.class))).thenReturn(true); when(wordDaoMock.updateBatch(anyListOf(Word.class))).thenReturn(true); when(wordDaoMock.findByWordString(eq("george"))).thenReturn(Arrays.asList(wordFromDatabase1)); when(wordDaoMock.findByWordString(eq("belden"))).thenReturn(Arrays.asList(wordFromDatabase2)); when(wordDaoMock.findByWordString(eq("is"))).thenReturn(null); when(wordDaoMock.findByWordString(eq("awesome"))).thenReturn(null); frequencyListImporterImpl.importFrequencyList(); assertEquals(100, wordFromDatabase1.getFrequencyWeight()); assertEquals(200, wordFromDatabase2.getFrequencyWeight()); rowUpdateCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowUpdateCountField, frequencyListImporterImpl); rowInsertCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowInsertCountField, frequencyListImporterImpl); assertEquals(2, rowUpdateCountFromObject.intValue()); assertEquals(2, rowInsertCountFromObject.intValue()); verify(wordDaoMock, times(1)).insertBatch(anyListOf(Word.class)); verify(wordDaoMock, times(1)).updateBatch(anyListOf(Word.class)); verify(wordDaoMock, times(4)).findByWordString(anyString()); verify(taskExecutorSpy, times(2)).execute(any(Runnable.class)); }
From source file:com.alibaba.otter.shared.common.model.config.pipeline.PipelineParameter.java
/** * ?pipeline?//from ww w . ja va 2s.co m */ public void merge(PipelineParameter pipelineParameter) { try { Field[] fields = this.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; // Skip static and final fields. if (Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) { continue; } ReflectionUtils.makeAccessible(field); Object srcValue = field.get(pipelineParameter); if (srcValue != null) { // null field.set(this, srcValue); } } } catch (Exception e) { // ignore } }
From source file:com.ciphertool.genetics.PopulationTest.java
@Test public void testSetSelector() { Population population = new Population(); Selector selector = mock(Selector.class); population.setSelector(selector);//from w w w.j ava2 s .com Field selectorField = ReflectionUtils.findField(Population.class, "selector"); ReflectionUtils.makeAccessible(selectorField); Selector selectorFromObject = (Selector) ReflectionUtils.getField(selectorField, population); assertSame(selector, selectorFromObject); }