List of usage examples for java.lang.reflect Method getParameterTypes
@Override
public Class<?>[] getParameterTypes()
From source file:com.acuityph.commons.util.ClassUtils.java
/** * Checks if is property getter.//from w w w. j a va 2 s. co m * * @param method * the method * @return true, if is property getter */ public static boolean isPropertyGetter(final Method method) { Assert.notNull(method, "method is null!"); final int mod = method.getModifiers(); final boolean expectsNoParameters = method.getParameterTypes().length == 0; final boolean returnsSomething = !void.class.equals(method.getReturnType()); return !isPrivate(mod) && !isStatic(mod) && expectsNoParameters && returnsSomething && isPropertyGetterName(method.getName()); }
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 w ww . j a v a 2 s . c o 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:it.govpay.model.BasicModel.java
public static String diff(Object a, Object b) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { if (a == null && b != null) return "[NULL] [NOT NULL]"; if (a != null && b == null) return "[NOT NULL] [NULL]"; if (a == null && b == null) return null; Method[] methods = a.getClass().getDeclaredMethods(); for (Method method : methods) { if ((method.getName().startsWith("get") || method.getName().startsWith("is")) && method.getParameterTypes().length == 0) { if (method.getReturnType().isAssignableFrom(List.class)) { String diff = diff((List<?>) method.invoke(a), (List<?>) method.invoke(b)); if (diff != null) return diff; } else { if (!equals(method.invoke(a), method.invoke(b))) { return method.getName() + "[" + method.invoke(a) + "] [" + method.invoke(b) + "]"; }/*w ww .j a v a2s .c o m*/ } } } return null; }
From source file:Main.java
public static Method getGetter(Object bean, String property) { Map<String, Method> cache = GETTER_CACHE.get(bean.getClass()); if (cache == null) { cache = new ConcurrentHashMap<String, Method>(); for (Method method : bean.getClass().getMethods()) { if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers()) && !void.class.equals(method.getReturnType()) && method.getParameterTypes().length == 0) { String name = method.getName(); if (name.length() > 3 && name.startsWith("get")) { cache.put(name.substring(3, 4).toLowerCase() + name.substring(4), method); } else if (name.length() > 2 && name.startsWith("is")) { cache.put(name.substring(2, 3).toLowerCase() + name.substring(3), method); }/*w w w. j av a 2 s .c o m*/ } } Map<String, Method> old = GETTER_CACHE.putIfAbsent(bean.getClass(), cache); if (old != null) { cache = old; } } return cache.get(property); }
From source file:com.zhangyue.zeus.util.BeanUtils.java
/** * map??BeanBean??mapkey??mapkey?OMIT_REG? * /*w w w .ja v a 2s. c o m*/ * @param <E> * @param cla * @param map * @return */ @SuppressWarnings({ "rawtypes" }) public static <E> E toBean(Class<E> cla, Map<String, Object> map) { // E obj = null; try { obj = cla.newInstance(); if (obj == null) { throw new Exception(); } } catch (Exception e) { LOG.error(",:" + cla); return null; } // ?mapkey Map<String, Object> newmap = new HashMap<String, Object>(); for (Map.Entry<String, Object> en : map.entrySet()) { newmap.put("set" + en.getKey().trim().replaceAll(OMIT_REG, "").toLowerCase(), en.getValue()); } // Method[] ms = cla.getMethods(); for (Method method : ms) { String mname = method.getName().toLowerCase(); if (mname.startsWith("set")) { Class[] clas = method.getParameterTypes(); Object v = newmap.get(mname); if (v != null && clas.length == 1) { try { method.invoke(obj, v); } catch (Exception e) { LOG.error("," + cla + "." + method.getName() + ".?" + clas[0] + ";:" + v.getClass()); } } } } return obj; }
From source file:com.github.tddts.jet.util.SpringUtil.java
public static Method getMethod(JoinPoint joinPoint) throws NoSuchMethodException { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); if (method.getDeclaringClass().isInterface()) { method = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(), method.getParameterTypes()); }//w w w .java 2s. co m return method; }
From source file:net.daboross.bukkitdev.skywars.util.CrossVersion.java
/** * Supports Bukkit earlier than... 1.8?/*from w ww .j av a 2 s.c o m*/ */ public static Collection<? extends Player> getOnlinePlayers(Server s) { try { return s.getOnlinePlayers(); } catch (NoSuchMethodError ignored) { Class<? extends Server> theClass = s.getClass(); try { for (Method method : theClass.getMethods()) { if ("getOnlinePlayers".equals(method.getName()) && method.getParameterTypes().length == 0 && method.getReturnType().isArray()) { return Arrays.asList((Player[]) method.invoke(s)); } } } catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { SkyStatic.getLogger().log(Level.WARNING, "Couldn't use fallback .getOnlinePlayers method of Server! Acting as if there are no online players!!", ex); } SkyStatic.getLogger().log(Level.WARNING, "Couldn't find old fallback .getOnlinePlayers method of Server! Acting as if there are no online players!!"); } return Collections.emptyList(); }
From source file:Main.java
public static Method getMethod(Object o, String methodName, Class<?> returnType, Class<?>... parameters) { for (Method method : o.getClass().getDeclaredMethods()) { if (!method.getName().equals(methodName) || method.getReturnType() != returnType) continue; Class<?>[] pars = method.getParameterTypes(); if (parameters.length != pars.length) continue; boolean found = true; for (int i = 0; i < parameters.length; i++) { if (pars[i] != parameters[i]) { found = false;// ww w. ja v a2 s . c o m break; } } if (found) { return method; } } return null; }
From source file:com.adaptris.util.SimpleBeanUtil.java
private static Method getSetterMethod(Class c, String methodName) { Method result = null;/* w ww . j a v a 2 s . c o m*/ Method[] methods = c.getMethods(); for (Method m : methods) { String name = m.getName(); if (name.equalsIgnoreCase(methodName)) { Class[] params = m.getParameterTypes(); if (params.length == 1 && PRIMITIVES.contains(params[0])) { result = m; break; } } } return result; }
From source file:jenkins.plugins.git.MethodUtils.java
/** * <p>Retrieves a method whether or not it's accessible. If no such method * can be found, return {@code null}.</p> * * @param cls The class that will be subjected to the method search * @param methodName The method that we wish to call * @param parameterTypes Argument class types * @return The method// w w w. j a v a2 s. co m */ static Method getMethodImpl(final Class<?> cls, final String methodName, final Class<?>... parameterTypes) { Validate.notNull(cls, "Null class not allowed."); Validate.notEmpty(methodName, "Null or blank methodName not allowed."); // fast path, check if directly declared on the class itself for (final Method method : cls.getDeclaredMethods()) { if (methodName.equals(method.getName()) && Arrays.equals(parameterTypes, method.getParameterTypes())) { return method; } } if (!cls.isInterface()) { // ok, now check if directly implemented on a superclass // Java 8: note that super-interface implementations trump default methods for (Class<?> klass = cls.getSuperclass(); klass != null; klass = klass.getSuperclass()) { for (final Method method : klass.getDeclaredMethods()) { if (methodName.equals(method.getName()) && Arrays.equals(parameterTypes, method.getParameterTypes())) { return method; } } } } // ok, now we are looking for an interface method... the most specific one // in the event that we have two unrelated interfaces both declaring a method of the same name // we will give up and say we could not find the method (the logic here is that we are primarily // checking for overrides, in the event of a Java 8 default method, that default only // applies if there is no conflict from an unrelated interface... thus if there are // default methods and they are unrelated then they don't exist... if there are multiple unrelated // abstract methods... well they won't count as a non-abstract implementation Method res = null; for (final Class<?> klass : (List<Class<?>>) ClassUtils.getAllInterfaces(cls)) { for (final Method method : klass.getDeclaredMethods()) { if (methodName.equals(method.getName()) && Arrays.equals(parameterTypes, method.getParameterTypes())) { if (res == null) { res = method; } else { Class<?> c = res.getDeclaringClass(); if (c == klass) { // match, ignore } else if (c.isAssignableFrom(klass)) { // this is a more specific match res = method; } else if (!klass.isAssignableFrom(c)) { // multiple overlapping interfaces declare this method and there is no common ancestor return null; } } } } } return res; }