List of usage examples for java.lang Class getMethods
@CallerSensitive public Method[] getMethods() throws SecurityException
From source file:com.springframework.core.annotation.AnnotationUtils.java
static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) { Boolean flag = annotatedInterfaceCache.get(iface); if (flag != null) { return flag.booleanValue(); }/* ww w. j av a2 s.com*/ Boolean found = Boolean.FALSE; for (Method ifcMethod : iface.getMethods()) { try { if (ifcMethod.getAnnotations().length > 0) { found = Boolean.TRUE; break; } } catch (Exception ex) { // Assuming nested Class values not resolvable within annotation attributes... logIntrospectionFailure(ifcMethod, ex); } } annotatedInterfaceCache.put(iface, found); return found.booleanValue(); }
From source file:org.fishwife.jrugged.spring.AnnotatedMethodScanner.java
Set<Method> extractAnnotatedMethods(Set<BeanDefinition> filteredComponents, Class<? extends Annotation> annoClass) { Set<Method> annotatedMethods = new HashSet<Method>(); for (BeanDefinition bd : filteredComponents) { try {/*from www . java 2 s . c om*/ String className = bd.getBeanClassName(); Class<?> beanClass = classLoader.loadClass(className); for (Method m : beanClass.getMethods()) { if (m.getAnnotation(annoClass) != null) { annotatedMethods.add(m); } } } catch (ClassNotFoundException cnfe) { // no-op } } return annotatedMethods; }
From source file:com.weibo.api.motan.rpc.AbstractProvider.java
private void initMethodMap(Class<T> clz) { Method[] methods = clz.getMethods(); List<String> dupList = new ArrayList<String>(); for (Method method : methods) { String methodDesc = ReflectUtil.getMethodDesc(method); methodMap.put(methodDesc, method); if (methodMap.get(method.getName()) == null) { methodMap.put(method.getName(), method); } else {//from w w w . j a v a 2 s.co m dupList.add(method.getName()); } } if (!dupList.isEmpty()) { for (String removedName : dupList) { methodMap.remove(removedName); } } }
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 w w w . j a v a 2 s . c o m 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:Main.java
public static void setProperties(Object object, Map<String, ? extends Object> properties, boolean includeSuperClasses) { if (object == null || properties == null) { return;/*from ww w . ja v a2 s. co m*/ } Class<?> objectClass = object.getClass(); for (Map.Entry<String, ? extends Object> entry : properties.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if (key != null && key.length() > 0) { String setterName = "set" + Character.toUpperCase(key.charAt(0)) + key.substring(1); Method setter = null; // Try to use the exact setter if (value != null) { try { if (includeSuperClasses) { setter = objectClass.getMethod(setterName, value.getClass()); } else { setter = objectClass.getDeclaredMethod(setterName, value.getClass()); } } catch (Exception ex) { } } // Find a more generic setter if (setter == null) { Method[] methods = includeSuperClasses ? objectClass.getMethods() : objectClass.getDeclaredMethods(); for (Method method : methods) { if (method.getName().equals(setterName)) { Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == 1 && isAssignableFrom(parameterTypes[0], value)) { setter = method; break; } } } } // Invoke if (setter != null) { try { setter.invoke(object, value); } catch (Exception e) { } } } } }
From source file:com.delphix.session.module.rmi.impl.RmiMethodOrdering.java
public RmiMethodOrdering(Class<?> clazz) { Map<String, Method> methodMap = Maps.newHashMap(); for (Method m : clazz.getMethods()) { List<String> paramNames = Lists.newArrayList(); for (Class<?> paramType : m.getParameterTypes()) { paramNames.add(paramType.getCanonicalName()); }//from w w w . j a v a 2 s .c o m String str = String.format("%s(%s)", m.getName(), StringUtils.join(paramNames, ", ")); methodMap.put(str, m); } List<String> sortedNames = new ArrayList<String>(methodMap.keySet()); Collections.sort(sortedNames); for (String name : sortedNames) { Method m = methodMap.get(name); placement.put(m, methods.size()); methods.add(m); } }
From source file:org.jdbcluster.JDBClusterUtil.java
/** * Tries to determine the best fitting method for a set of parameter types. * Uses getMethod() first, if there is no direct match every parameter is * checked if there is a method with a superclass or superinterface match. * etc. For this best fitting method a scoring is used. It counts for every * parameter the "distance" from the class to the requested superclass or * superinterface. These "distances" are cummulated to one value. The method * with the lowest scoring value is returned. * //from w ww.j a va2 s . c om * @see #getMethod(Class, String, Class[]) * @param clazz * Class of Object * @param methodName * name of the method to find * @param parameterTypes * parameter types of method * @return Method to find */ static public Method getMethodBestParameterFit(Class clazz, String methodName, Class... parameterTypes) { /* * first try the normal way (exact match) */ try { return JDBClusterUtil.getMethod(clazz, methodName, parameterTypes); } catch (ConfigurationException e) { if (!(e.getCause() instanceof NoSuchMethodException)) throw e; } /* * get all methods and select only equal name and parameter length in * mList */ Method[] mAll = clazz.getMethods(); List<Method> mList = new ArrayList<Method>(); for (Method m : mAll) { if (m.getParameterTypes().length == parameterTypes.length && m.getName().equals(methodName)) mList.add(m); } if (mList.size() > 1) logger.warn("possible ambiguity. Found more than one method with name [" + methodName + "]. " + "trying best fit method"); /* * check if parameter superclasses do fit for all above selected methods */ HashMap<Method, Integer> methodScore = new HashMap<Method, Integer>(); for (Method m : mList) { Class<?>[] mParameterTypes = m.getParameterTypes(); for (int i = 0; i < parameterTypes.length; i++) { int count = countSuper(mParameterTypes[i], parameterTypes[i]); Integer oldInt = methodScore.get(m); if (oldInt == null) oldInt = 0; methodScore.put(m, oldInt + count); if (count < 0) { methodScore.put(m, -1); break; // if there is no match this method is not fitting } } } /* * evaluate scoring */ Method mResult = null; int resultScore = Integer.MAX_VALUE; for (Method m : mList) { int score = methodScore.get(m); if (score != -1) { if (score < resultScore) { resultScore = score; mResult = m; } } } if (mResult == null) throw new ConfigurationException( "cant get MethodBestParameterFit for method [" + methodName + "] with the specified name"); return mResult; }
From source file:jp.gr.java_conf.ka_ka_xyz.processor.AnnotationProcessor.java
private Method[] getMethods(Class clazz) { return (clazz == null) ? new Method[0] : clazz.getMethods(); }
From source file:code.google.nfs.rpc.server.RPCServerHandler.java
public void registerProcessor(String instanceName, Object instance) { processors.put(instanceName, instance); Class<?> instanceClass = instance.getClass(); Method[] methods = instanceClass.getMethods(); for (Method method : methods) { Class<?>[] argTypes = method.getParameterTypes(); StringBuilder methodKeyBuilder = new StringBuilder(); methodKeyBuilder.append(instanceName).append("#"); methodKeyBuilder.append(method.getName()).append("$"); for (Class<?> argClass : argTypes) { methodKeyBuilder.append(argClass.getName()).append("_"); }//from ww w . j a v a 2 s .com cacheMethods.put(methodKeyBuilder.toString(), method); } }
From source file:my.school.spring.beans.PostProxyInvokerContextListener.java
@Override public void onApplicationEvent(ContextRefreshedEvent event) { ApplicationContext context = event.getApplicationContext(); for (String beanName : context.getBeanDefinitionNames()) { final String beanClassName = beanFactory.getBeanDefinition(beanName).getBeanClassName(); if (beanClassName == null) { continue; }/*w w w . ja v a2 s.com*/ try { Class<?> originalClass = Class.forName(beanClassName); for (Method originalMethod : originalClass.getMethods()) { if (originalMethod.isAnnotationPresent(PostProxy.class)) { try { Object bean = context.getBean(beanName); Method currentMethod = bean.getClass().getMethod(originalMethod.getName(), originalMethod.getParameterTypes()); currentMethod.invoke(bean); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) { LOG.error(ex.getLocalizedMessage()); } } } } catch (NullPointerException | ClassNotFoundException ex) { LOG.warn("Failed to find bean's class: {} (bean name: {})", beanClassName, beanName); } } }