List of usage examples for java.lang.reflect Method getParameterTypes
@Override
public Class<?>[] getParameterTypes()
From source file:com.googlecode.loosejar.ClassLoaderAnalyzer.java
private Method findMethod(Class<?> clazz, String name, Class<?>[] paramTypes) { Class<?> type = clazz;//w ww . ja v a 2 s . com while (!Object.class.equals(type) && type != null) { Method[] methods = type.getDeclaredMethods(); for (Method method : methods) { if (name.equals(method.getName()) && Arrays.equals(paramTypes, method.getParameterTypes())) { return method; } } type = type.getSuperclass(); } return null; }
From source file:com.googlecode.jdbcproc.daofactory.impl.block.service.OutputParametersGetterBlockServiceImpl.java
private int findEntityParameterIndex(Method daoMethod) { int entityParameterIndex = -1; for (int i = 0; i < daoMethod.getParameterCount(); i++) { if (!BlockFactoryUtils.isSimpleOrListType(daoMethod.getParameterTypes()[i])) { entityParameterIndex = i;/* w ww .j a va2 s . co m*/ break; } } Assert.isTrue(entityParameterIndex >= 0, "Did not find an entity parameter, although this is an entity (probably with lists)"); return entityParameterIndex; }
From source file:pl.com.bottega.ecommerce.system.saga.impl.SimpleSagaEngine.java
private Method findHandlerMethodForEvent(Class<?> type, Object event) { for (Method method : type.getMethods()) { if (method.getAnnotation(SagaAction.class) != null || method.getAnnotation(LoadSaga.class) != null) { if (method.getParameterTypes().length == 1 && method.getParameterTypes()[0].isAssignableFrom(event.getClass())) { return method; }//w ww . j a v a 2 s . c o m } } throw new RuntimeException("no method handling " + event.getClass()); }
From source file:net.camelpe.extension.camel.typeconverter.CdiTypeConverterBuilder.java
private void ensureFallbackConverterMethodIsValid(final Class<?> type, final Method method) throws IllegalArgumentException { final Class<?>[] parameterTypes = method.getParameterTypes(); final boolean hasCorrectParameters = (parameterTypes != null) && ((parameterTypes.length == 3) || (((parameterTypes.length == 4) && Exchange.class.isAssignableFrom(parameterTypes[1])) && (TypeConverterRegistry.class .isAssignableFrom(parameterTypes[parameterTypes.length - 1])))); if (!hasCorrectParameters) { throw new IllegalArgumentException( "Illegal fallback converter method [" + method + "] on type [" + type.getName() + "]: a fallback converter method must have exactly three parameters, or it must have " + "exactly four parameters. In both cases, the second parameter must be of type [" + Exchange.class.getName() + "] and the last parameter must be of type [" + TypeConverterRegistry.class.getName() + "]."); }//from w w w .ja va 2 s . co m final int modifiers = method.getModifiers(); if (isAbstract(modifiers) || !isPublic(modifiers)) { throw new IllegalArgumentException( "Illegal fallback converter method [" + method + "] on type [" + type.getName() + "]: a fallback converter method must not be abstract, and it must be public."); } final Class<?> toType = method.getReturnType(); if (toType.equals(Void.class)) { throw new IllegalArgumentException("Illegal fallback converter method [" + method + "] on type [" + type.getName() + "]: a fallback converter method must not return void."); } }
From source file:com.crossbusiness.resiliency.aspect.spring.AnnotationFallbackAspect.java
public Object rerouteToFallback(ProceedingJoinPoint pjp, Fallback fallbackConfig) throws Throwable { String[] fallbacks = fallbackConfig.value(); Class<? extends Throwable>[] fallbackableExceptions = fallbackConfig.exceptions(); List<Object> fallbackBeans = new ArrayList<Object>(fallbacks.length); for (String fallback : fallbacks) { try {//from www. jav a 2s .c o m fallbackBeans.add(context.getBean(fallback)); } catch (BeansException be) { log.error("configuration error: cannot find bean with name: '{}'", fallback, be); //configuration errors should be fixed immediately. throw be; } } MethodSignature targetMethodSig = (MethodSignature) pjp.getSignature(); Method targetMethod = targetMethodSig.getMethod(); Class[] paramTypes = (Class[]) targetMethod.getParameterTypes(); Object[] args = pjp.getArgs(); log.debug("fallbacks: {} method: '{}'", fallbacks, targetMethod); try { return pjp.proceed(); } catch (Throwable t) { // if the exception is not what we're looking for, rethrow it if (!isFallbackableException(t, fallbackableExceptions)) throw t; log.debug("got exception while trying the targetBean method: '{}'. will try fallbackBean...", targetMethod); Iterator<Object> iter = fallbackBeans.iterator(); while (iter.hasNext()) { Object fallbackBean = iter.next(); Method fallbackMethod; try { fallbackMethod = fallbackBean.getClass().getMethod(targetMethod.getName(), paramTypes); } catch (NoSuchMethodException | SecurityException nsme) { log.error( "configuration error: No matchig method found in fallbackBean: '{}' that matches to targetBean method: '{}'", new Object[] { fallbackBean.getClass().getName(), targetMethod, nsme }); //configuration errors should be fixed immediately. throw nsme; } try { log.debug("trying fallbackBean method: '{}'...", fallbackMethod); return fallbackMethod.invoke(fallbackBean, args); } catch (IllegalArgumentException | IllegalAccessException iae) { log.error( "configuration error: arguments missmatch: fallbackBean method: '{}' arguments missmatch to targetBean method: '{}' arguments", new Object[] { fallbackMethod, targetMethod, iae }); //configuration errors should be fixed immediately. throw iae; } catch (InvocationTargetException ite) { log.debug( "got exception while trying the fallbackBean method: '{}'. will try next fallbackBean...", fallbackMethod); //fallbackBean method thrown an exception. try next bean or throw exception if this is the last bean if (!iter.hasNext()) { //TODO : do we still need to check isFallbackableException? throw ite.getCause(); } } } //code should never reach this line. throw t; } }
From source file:com.wavemaker.tools.javaservice.JavaServiceDefinition2Test.java
public void testGetNonOverloadedMethods() throws Exception { List<Method> methods = ClassUtils.getPublicMethods(JavaServiceDefinitionClass2.class); Collection<Method> retMethods = JavaServiceDefinition.filterOverloadedMethods(methods); assertEquals(5, retMethods.size());/*from w w w. j av a 2 s . co m*/ methods = ClassUtils.getPublicMethods(JavaServiceDefinitionClass_Overloading.class); retMethods = JavaServiceDefinition.filterOverloadedMethods(methods); assertEquals(1, retMethods.size()); for (Method m : retMethods) { assertEquals(0, m.getParameterTypes().length); } }
From source file:com.tmind.framework.pub.utils.MethodUtils.java
public static Method findMethod(Class start, String methodName, int argCount, Class args[]) { // For overriden methods we need to find the most derived version. // So we start with the given class and walk up the superclass chain. for (Class cl = start; cl != null; cl = cl.getSuperclass()) { Method methods[] = getPublicDeclaredMethods(cl); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method == null) { continue; }/*w w w. j ava 2s.com*/ // skip static methods. int mods = method.getModifiers(); if (Modifier.isStatic(mods)) { continue; } // make sure method signature matches. Class params[] = method.getParameterTypes(); if (method.getName().equals(methodName) && params.length == argCount) { boolean different = false; if (argCount > 0) { for (int j = 0; j < argCount; j++) { if (params[j] != args[j]) { different = true; continue; } } if (different) { continue; } } return method; } } } // Now check any inherited interfaces. This is necessary both when // the argument class is itself an interface, and when the argument // class is an abstract class. Class ifcs[] = start.getInterfaces(); for (int i = 0; i < ifcs.length; i++) { Method m = findMethod(ifcs[i], methodName, argCount); if (m != null) { return m; } } return null; }
From source file:nats.client.spring.AnnotationConfigBeanPostProcessor.java
@Override public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException { final Class<?> clazz = bean.getClass(); for (final Method method : clazz.getMethods()) { final Subscribe annotation = AnnotationUtils.findAnnotation(method, Subscribe.class); if (annotation != null) { final Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length != 1 || !parameterTypes[0].equals(Message.class)) { throw new BeanInitializationException(String.format( "Method '%s' on bean with name '%s' must have a single parameter of type %s when using the @%s annotation.", method.toGenericString(), beanName, Message.class.getName(), Subscribe.class.getName())); }//from www . ja v a 2 s . com nats.subscribe(annotation.value()).addMessageHandler(new MessageHandler() { @Override public void onMessage(Message message) { try { method.invoke(bean, message); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { final Throwable targetException = e.getTargetException(); if (targetException instanceof RuntimeException) { throw (RuntimeException) targetException; } throw new RuntimeException(targetException); } } }); } } return bean; }
From source file:pl.bristleback.server.bristle.conf.resolver.action.ActionResolver.java
public ActionInformation prepareActionInformation(Class<?> clazz, Method action) { ActionInformation actionInformation = prepareAction(clazz, action); List<ActionParameterInformation> parameters = new ArrayList<ActionParameterInformation>(); for (int i = 0; i < action.getParameterTypes().length; i++) { Type parameterType = action.getGenericParameterTypes()[i]; ActionParameterInformation parameterInformation = parametersResolver .prepareActionParameter(parameterType, action.getParameterAnnotations()[i]); parameters.add(parameterInformation); }/*www . j ava2 s. com*/ actionInformation.setParameters(parameters); ActionResponseInformation responseInformation = responseResolver.resolveResponse(action); actionInformation.setResponseInformation(responseInformation); return actionInformation; }
From source file:pl.bristleback.server.bristle.conf.resolver.action.ActionResolver.java
private boolean isDefaultRemoteAction(Class clazz, Method action) { if (!DefaultAction.class.isAssignableFrom(clazz)) { // this action class does not have default action return false; }//from ww w . j a v a 2s . co m Method defaultMethod = DefaultAction.class.getMethods()[0]; if (!defaultMethod.getName().equals(action.getName())) { return false; } Class[] defaultParameterTypes = defaultMethod.getParameterTypes(); if (defaultParameterTypes.length != action.getParameterTypes().length) { return false; } int parametersLength = defaultParameterTypes.length; for (int i = 0; i < parametersLength - 1; i++) { Class defaultParameterType = defaultParameterTypes[i]; if (!defaultParameterType.isAssignableFrom(action.getParameterTypes()[i])) { return false; } } Type requiredLastParameterType = action.getGenericParameterTypes()[parametersLength - 1]; Type actualLastParameterType = null; for (int i = 0; i < clazz.getInterfaces().length; i++) { Class interfaceOfClass = clazz.getInterfaces()[i]; if (DefaultAction.class.equals(interfaceOfClass)) { Type genericInterface = clazz.getGenericInterfaces()[i]; if (genericInterface instanceof ParameterizedType) { actualLastParameterType = ((ParameterizedType) (genericInterface)).getActualTypeArguments()[1]; } else { actualLastParameterType = Object.class; } } } return requiredLastParameterType.equals(actualLastParameterType); }