List of usage examples for org.springframework.util ReflectionUtils makeAccessible
@SuppressWarnings("deprecation") public static void makeAccessible(Field field)
From source file:org.springframework.data.solr.server.support.SolrServerUtils.java
/** * Solr property names do not match the getters/setters used for them. Check on any write method, try to find the * according property and set the value for it. Will ignore all other, and nested properties * //from w ww . jav a2 s.c om * @param source * @param target */ private static void copyProperties(SolrServer source, SolrServer target) { BeanWrapperImpl wrapperImpl = new BeanWrapperImpl(source); for (PropertyDescriptor pd : wrapperImpl.getPropertyDescriptors()) { Method writer = pd.getWriteMethod(); if (writer != null) { try { Field property = ReflectionUtils.findField(source.getClass(), pd.getName()); if (property != null) { ReflectionUtils.makeAccessible(property); Object o = ReflectionUtils.getField(property, source); if (o != null) { writer.invoke(target, o); } } } catch (Exception e) { logger.warn("Could not copy property value for: " + pd.getName(), e); } } } }
From source file:org.springframework.faces.mvc.annotation.support.AnnotatedMethodInvoker.java
/** * Invoke the specified method, ensuring that the method is accessible and that all exceptions are re-thrown * correctly.// w w w . j a va 2 s . com */ private Object doInvokeMethod(Method method, Object target, Object[] args) throws Exception { ReflectionUtils.makeAccessible(method); try { return method.invoke(target, args); } catch (InvocationTargetException ex) { ReflectionUtils.rethrowException(ex.getTargetException()); } throw new IllegalStateException("Should never get here"); }
From source file:org.springframework.flex.core.MessageBrokerFactoryBean.java
private void setInitServletContext() { // This is undesirable but necessary at the moment for LCDS to be able to load its license configuration. // Hopefully we can get the BlazeDS/LCDS team to give us a better option in the future. Method initMethod = ReflectionUtils.findMethod(MessageBroker.class, "setServletContext", new Class[] { ServletContext.class }); if (initMethod == null) { initMethod = ReflectionUtils.findMethod(MessageBroker.class, "setInitServletContext", new Class[] { ServletContext.class }); }/*from w w w.jav a2 s .co m*/ ReflectionUtils.makeAccessible(initMethod); ReflectionUtils.invokeMethod(initMethod, this.messageBroker, new Object[] { this.servletContext }); }
From source file:org.springframework.integration.config.IdGeneratorConfigurer.java
private boolean setIdGenerator(ApplicationContext context) { try {//from w w w . ja va 2 s . c om IdGenerator idGeneratorBean = context.getBean(IdGenerator.class); if (logger.isDebugEnabled()) { logger.debug("using custom MessageHeaders.IdGenerator [" + idGeneratorBean.getClass() + "]"); } Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator"); ReflectionUtils.makeAccessible(idGeneratorField); IdGenerator currentIdGenerator = (IdGenerator) ReflectionUtils.getField(idGeneratorField, null); if (currentIdGenerator != null) { if (currentIdGenerator.equals(idGeneratorBean)) { // same instance is already set, nothing needs to be done return false; } else { if (IdGeneratorConfigurer.theIdGenerator.getClass() == idGeneratorBean.getClass()) { if (logger.isWarnEnabled()) { logger.warn("Another instance of " + idGeneratorBean.getClass() + " has already been established; ignoring"); } return true; } else { // different instance has been set, not legal throw new BeanDefinitionStoreException( "'MessageHeaders.idGenerator' has already been set and can not be set again"); } } } if (logger.isInfoEnabled()) { logger.info("Message IDs will be generated using custom IdGenerator [" + idGeneratorBean.getClass() + "]"); } ReflectionUtils.setField(idGeneratorField, null, idGeneratorBean); IdGeneratorConfigurer.theIdGenerator = idGeneratorBean; } catch (NoSuchBeanDefinitionException e) { // No custom IdGenerator. We will use the default. int idBeans = context.getBeansOfType(IdGenerator.class).size(); if (idBeans > 1 && logger.isWarnEnabled()) { logger.warn("Found too many 'IdGenerator' beans (" + idBeans + ") " + "Will use the existing UUID strategy."); } else if (logger.isDebugEnabled()) { logger.debug("Unable to locate MessageHeaders.IdGenerator. Will use the existing UUID strategy."); } return false; } catch (IllegalStateException e) { // thrown from ReflectionUtils if (logger.isWarnEnabled()) { logger.warn("Unexpected exception occurred while accessing idGenerator of MessageHeaders." + " Will use the existing UUID strategy.", e); } return false; } return true; }
From source file:org.springframework.integration.config.IdGeneratorConfigurer.java
private void unsetIdGenerator() { try {//w w w . java 2s . c om Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator"); ReflectionUtils.makeAccessible(idGeneratorField); idGeneratorField.set(null, null); IdGeneratorConfigurer.theIdGenerator = null; } catch (Exception e) { if (logger.isWarnEnabled()) { logger.warn("Unexpected exception occurred while accessing idGenerator of MessageHeaders.", e); } } }
From source file:org.springframework.integration.core.MessageIdGenerationTests.java
@Test @Ignore// w w w. j a v a 2 s . co m public void performanceTest() { int times = 1000000; StopWatch watch = new StopWatch(); watch.start(); for (int i = 0; i < times; i++) { new GenericMessage<Integer>(0); } watch.stop(); double defaultGeneratorElapsedTime = watch.getTotalTimeSeconds(); Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator"); ReflectionUtils.makeAccessible(idGeneratorField); ReflectionUtils.setField(idGeneratorField, null, (IdGenerator) () -> TimeBasedUUIDGenerator.generateId()); watch = new StopWatch(); watch.start(); for (int i = 0; i < times; i++) { new GenericMessage<Integer>(0); } watch.stop(); double timebasedGeneratorElapsedTime = watch.getTotalTimeSeconds(); logger.info("Generated " + times + " messages using default UUID generator " + "in " + defaultGeneratorElapsedTime + " seconds"); logger.info("Generated " + times + " messages using Timebased UUID generator " + "in " + timebasedGeneratorElapsedTime + " seconds"); logger.info("Time-based ID generator is " + defaultGeneratorElapsedTime / timebasedGeneratorElapsedTime + " times faster"); }
From source file:org.springframework.integration.core.MessageIdGenerationTests.java
private void assertDestroy() throws Exception { Field idGenField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator"); ReflectionUtils.makeAccessible(idGenField); assertNull("the idGenerator field has not been properly reset to null", idGenField.get(null)); }
From source file:org.springframework.test.context.TestContextManager.java
/** * Attempt to create a copy of the supplied {@code TestContext} using its * <em>copy constructor</em>. *///from w w w. j av a 2s .c o m private static TestContext copyTestContext(TestContext testContext) { Constructor<? extends TestContext> constructor = ClassUtils .getConstructorIfAvailable(testContext.getClass(), testContext.getClass()); if (constructor != null) { try { ReflectionUtils.makeAccessible(constructor); return constructor.newInstance(testContext); } catch (Exception ex) { if (logger.isInfoEnabled()) { logger.info(String.format("Failed to invoke copy constructor for [%s]; " + "concurrent test execution is therefore likely not supported.", testContext), ex); } } } // fallback to original instance return testContext; }
From source file:org.springframework.test.context.transaction.TransactionalTestExecutionListener.java
/** * Run all {@link BeforeTransaction @BeforeTransaction} methods for the * specified {@linkplain TestContext test context}. If one of the methods * fails, however, the caught exception will be rethrown in a wrapped * {@link RuntimeException}, and the remaining methods will <strong>not</strong> * be given a chance to execute.//from w w w. j a v a 2 s . co m * @param testContext the current test context */ protected void runBeforeTransactionMethods(TestContext testContext) throws Exception { try { List<Method> methods = getAnnotatedMethods(testContext.getTestClass(), BeforeTransaction.class); Collections.reverse(methods); for (Method method : methods) { if (logger.isDebugEnabled()) { logger.debug( "Executing @BeforeTransaction method [" + method + "] for test context " + testContext); } ReflectionUtils.makeAccessible(method); method.invoke(testContext.getTestInstance()); } } catch (InvocationTargetException ex) { if (logger.isErrorEnabled()) { logger.error("Exception encountered while executing @BeforeTransaction methods for test context " + testContext + ".", ex.getTargetException()); } ReflectionUtils.rethrowException(ex.getTargetException()); } }
From source file:org.springframework.test.context.transaction.TransactionalTestExecutionListener.java
/** * Run all {@link AfterTransaction @AfterTransaction} methods for the * specified {@linkplain TestContext test context}. If one of the methods * fails, the caught exception will be logged as an error, and the remaining * methods will be given a chance to execute. After all methods have * executed, the first caught exception, if any, will be rethrown. * @param testContext the current test context *///from w w w . j a v a 2 s . c o m protected void runAfterTransactionMethods(TestContext testContext) throws Exception { Throwable afterTransactionException = null; List<Method> methods = getAnnotatedMethods(testContext.getTestClass(), AfterTransaction.class); for (Method method : methods) { try { if (logger.isDebugEnabled()) { logger.debug( "Executing @AfterTransaction method [" + method + "] for test context " + testContext); } ReflectionUtils.makeAccessible(method); method.invoke(testContext.getTestInstance()); } catch (InvocationTargetException ex) { Throwable targetException = ex.getTargetException(); if (afterTransactionException == null) { afterTransactionException = targetException; } logger.error("Exception encountered while executing @AfterTransaction method [" + method + "] for test context " + testContext, targetException); } catch (Exception ex) { if (afterTransactionException == null) { afterTransactionException = ex; } logger.error("Exception encountered while executing @AfterTransaction method [" + method + "] for test context " + testContext, ex); } } if (afterTransactionException != null) { ReflectionUtils.rethrowException(afterTransactionException); } }