/** * Attempt to find a {@link Method} on the supplied class with the supplied name * and no parameters. Searches all superclasses up to {@code Object}. * <p>Returns {@code null} if no {@link Method} can be found. * @param clazz the class to introspect * @param name the name of the method * @return the Method object, or {@code null} if none found */ @Nullable public static Method findMethod(Class<?> clazz, String name) { return findMethod(clazz, name, new Class<?>[0]); }
private static boolean isClassLoaded(String className) { ClassLoader cl = ClassUtils.getDefaultClassLoader(); Method findLoadedClassMethod = ReflectionUtils.findMethod(cl.getClass(), "findLoadedClass", String.class); ReflectionUtils.makeAccessible(findLoadedClassMethod); Class<?> loadedClass = (Class<?>) ReflectionUtils.invokeMethod(findLoadedClassMethod, cl, className); return loadedClass != null; }
@Test // SPR-14408 public void decodeHeaderFieldParam() { Method decode = ReflectionUtils.findMethod(ContentDisposition.class, "decodeHeaderFieldParam", String.class); ReflectionUtils.makeAccessible(decode); String result = (String)ReflectionUtils.invokeMethod(decode, null, "test.txt"); assertEquals("test.txt", result); result = (String)ReflectionUtils.invokeMethod(decode, null, "UTF-8''%E4%B8%AD%E6%96%87.txt"); assertEquals("中文.txt", result); }
@Test void invalidExpressionEvaluationType() { String methodName = "nonBooleanOrStringExpression"; IllegalStateException exception = assertThrows(IllegalStateException.class, () -> condition.evaluateExecutionCondition(buildExtensionContext(methodName))); Method method = ReflectionUtils.findMethod(getClass(), methodName); assertThat(exception.getMessage(), is(equalTo("@DisabledIf(\"#{6 * 7}\") on " + method + " must evaluate to a String or a Boolean, not java.lang.Integer"))); }
@Test public void invokeListenerWithGenericEvent() { Method method = ReflectionUtils.findMethod( SampleEvents.class, "handleGenericString", GenericTestEvent.class); GenericTestEvent<String> event = new SmartGenericTestEvent<>(this, "test"); invokeListener(method, event); verify(this.sampleEvents, times(1)).handleGenericString(event); }
@Test public void invokeListenerWithAnnotationValue() { Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringAnnotationClasses"); PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test"); invokeListener(method, event); verify(this.sampleEvents, times(1)).handleStringAnnotationClasses(); }
@Test public void sameInstanceEquals() { Method m = ReflectionUtils.findMethod(getClass(), name.getMethodName()); AnnotatedElementKey instance = new AnnotatedElementKey(m, getClass()); assertKeyEquals(instance, instance); }
@Test public void testSPR2648() throws Exception { Method bridgeMethod = ReflectionUtils.findMethod(GenericSqlMapIntegerDao.class, "saveOrUpdate", Object.class); assertTrue(bridgeMethod != null && bridgeMethod.isBridge()); Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(bridgeMethod); assertFalse(bridgedMethod.isBridge()); assertEquals("saveOrUpdate", bridgedMethod.getName()); }
@Test public void listenerWithAnnotationValue() { Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringAnnotationValue"); supportsEventType(true, method, createGenericEventType(String.class)); }
@Test public void listenerWithAnnotationClasses() { Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringAnnotationClasses"); supportsEventType(true, method, createGenericEventType(String.class)); }
@Test public void defaultCacheNameNoDefaults() { Method method = ReflectionUtils.findMethod(Object.class, "toString"); assertEquals("java.lang.Object.toString()", source.determineCacheName(method, null, "")); }
protected MessagingMessageListenerAdapter getPayloadInstance(final Object payload, String methodName, Class... parameterTypes) { Method method = ReflectionUtils.findMethod(SampleBean.class, methodName, parameterTypes); MessagingMessageListenerAdapter adapter = new MessagingMessageListenerAdapter() { @Override protected Object extractMessage(javax.jms.Message message) { return payload; } }; adapter.setHandlerMethod(factory.createInvocableHandlerMethod(sample, method)); return adapter; }
@Ignore("[SPR-8644] findMethod() does not currently support var-args") @Test public void findMethodWithVarArgs() throws Exception { assertNotNull(ReflectionUtils.findMethod(B.class, "add", int.class, int.class, int.class)); }
@Test public void rawListener() { Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleRaw", ApplicationEvent.class); supportsEventType(true, method, getGenericApplicationEventType("applicationEvent")); }
@Test public void specifiedOrder() { Method method = ReflectionUtils.findMethod( SampleEvents.class, "handleRaw", ApplicationEvent.class); ApplicationListenerMethodAdapter adapter = createTestInstance(method); assertEquals(42, adapter.getOrder()); }
@Before @SuppressWarnings("resource") public void setup() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.refresh(); ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); this.resolver = new SessionAttributeMethodArgumentResolver(context.getBeanFactory(), adapterRegistry); this.session = mock(WebSession.class); this.exchange = MockServerWebExchange.builder(MockServerHttpRequest.get("/")).session(this.session).build(); this.handleMethod = ReflectionUtils.findMethod(getClass(), "handleWithSessionAttribute", (Class<?>[]) null); }
@Before @SuppressWarnings("resource") public void setup() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.refresh(); ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); this.resolver = new ExpressionValueMethodArgumentResolver(context.getBeanFactory(), adapterRegistry); Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null); this.paramSystemProperty = new MethodParameter(method, 0); this.paramNotSupported = new MethodParameter(method, 1); this.paramAlsoNotSupported = new MethodParameter(method, 2); }
@Test public void cacheResultReturnsProperType() throws Throwable { JCacheInterceptor interceptor = createInterceptor(createOperationSource( cacheManager, defaultCacheResolver, defaultExceptionCacheResolver, defaultKeyGenerator)); AnnotatedJCacheableService service = new AnnotatedJCacheableService(cacheManager.getCache("default")); Method method = ReflectionUtils.findMethod(AnnotatedJCacheableService.class, "cache", String.class); CacheOperationInvoker invoker = new DummyInvoker(0L); Object execute = interceptor.execute(invoker, service, method, new Object[] {"myId"}); assertNotNull("result cannot be null.", execute); assertEquals("Wrong result type", Long.class, execute.getClass()); assertEquals("Wrong result", 0L, execute); }
@Test public void phaseAndClassesSet() { Method m = ReflectionUtils.findMethod(SampleEvents.class, "phaseAndClassesSet"); assertPhase(m, TransactionPhase.AFTER_COMPLETION); supportsEventType(true, m, createGenericEventType(String.class)); supportsEventType(true, m, createGenericEventType(Integer.class)); supportsEventType(false, m, createGenericEventType(Double.class)); }
@Test public void varArgNull() { Method method = ReflectionUtils.findMethod(SampleMethods.class, "hello", Boolean.class, String[].class); MethodBasedEvaluationContext context = createEvaluationContext(method, null, null); assertNull(context.lookupVariable("a0")); assertNull(context.lookupVariable("p0")); assertNull(context.lookupVariable("flag")); assertNull(context.lookupVariable("a1")); assertNull(context.lookupVariable("p1")); assertNull(context.lookupVariable("vararg")); }