List of usage examples for java.lang.reflect Method getParameterTypes
@Override
public Class<?>[] getParameterTypes()
From source file:com.bfd.harpc.main.ConfigHelper.java
/** * ??/*ww w .j a v a2 s. c o m*/ * <p> * * @param configObject * ? * @param configPrefix * ?? * @param configuration * {@link PropertiesConfiguration} * @throws RpcException */ public static void initConfig(Object configObject, String configPrefix, PropertiesConfiguration configuration) throws RpcException { Method[] methods = configObject.getClass().getMethods(); for (Method method : methods) { if (method.getName().length() > 3 && method.getName().startsWith("set") && method.getParameterTypes().length == 1) { String attribute = method.getName().substring(3); char ch = attribute.charAt(0); attribute = Character.toLowerCase(ch) + attribute.substring(1); String value = configuration.getProperty(configPrefix + attribute, ""); try { if (StringUtils.isNotEmpty(value)) { Type type = method.getParameterTypes()[0]; if (type == boolean.class) { method.invoke(configObject, Boolean.valueOf(value)); } else if (type == int.class) { method.invoke(configObject, Integer.valueOf(value)); } else if (type == long.class) { method.invoke(configObject, Long.valueOf(value)); } else { method.invoke(configObject, value); } } } catch (Exception e) { LOGGER.error("Init config error", e); throw new RpcException(RpcException.CONFIG_EXCEPTION, e); } } } }
From source file:Main.java
public static Method findMethodByNameAndReturnType(Class<?> targetObject, String methodName, String returnType, Class<?>... params) { for (Method method : targetObject.getDeclaredMethods()) { if (method.getReturnType().getName().equals(returnType) && method.getName().equals(methodName)) { Class[] parameterTypes = method.getParameterTypes(); if (params.length != parameterTypes.length) { continue; }/*from www.j av a 2 s . c om*/ for (int i = 0; i < params.length; i++) { if (params[i] != parameterTypes[i]) { break; } } method.setAccessible(true); return method; } } throw new NoSuchMethodError(); }
From source file:io.lavagna.common.QueryType.java
private static SqlParameterSource extractParameters(Method m, Object[] args) { Class<?>[] parameterTypes = m.getParameterTypes(); Annotation[][] parameterAnnotations = m.getParameterAnnotations(); if (parameterAnnotations == null || parameterAnnotations.length == 0) { return new EmptySqlParameterSource(); }/* ww w . ja v a 2 s. c o m*/ MapSqlParameterSource ps = new MapSqlParameterSource(); for (int i = 0; i < args.length; i++) { String name = parameterName(parameterAnnotations[i]); if (name != null) { ps.addValue(name, args[i], StatementCreatorUtils.javaTypeToSqlParameterType(parameterTypes[i])); } } return ps; }
From source file:com.adaptris.util.SimpleBeanUtil.java
/** * Invoke the setter method on the object. * <p>/* ww w . j a v a 2s . c o m*/ * Uses the first match, so overloaded methods may cause unexpected behaviour. Assumes that the {@code String} value can be * converted into its corresponding primitive value (long/double/boolean/string/float/int). If it can't a runtime exception is * probably going to be thrown. * </p> * * @param obj the object. * @param methodName the method name (e.g. {@code setClientID}, case insensitive match). * @param value the value; which will be converted into the appropriate primitive. * @return true if the setter was successfully called; false otherwise. */ public static boolean callSetter(Object obj, String methodName, String value) { boolean result = false; try { Method m = getSetterMethod(obj.getClass(), methodName); Object param = OBJECTIFIERS.get(m.getParameterTypes()[0]).objectify(value); m.invoke(obj, param); result = true; } catch (NullPointerException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { } return result; }
From source file:Main.java
static Map<String, Method> getSetPropertyMethods(Class<?> beanClass) { HashMap<String, Method> props; if (setPropsCache.containsKey(beanClass)) { props = setPropsCache.get(beanClass); } else {//from w ww .j a v a 2s.co m props = new HashMap<String, Method>(); setPropsCache.put(beanClass, props); Method[] ms = beanClass.getMethods(); for (int i = 0; i < ms.length; i++) { Method m = ms[i]; String name = m.getName(); if (name.startsWith("set") && name.length() > 3 && Character.isUpperCase(name.charAt(3)) && m.getParameterTypes().length == 1) { name = name.substring(3); props.put(name, m); } } } return props; }
From source file:Main.java
/** * search the method and return the defined method. * it will {@link Class#getMethod(String, Class[])}, if exception occurs, * it will search for all methods, and find the most fit method. */// w w w . j a va2 s . c o m public static Method searchMethod(Class<?> currentClass, String name, Class<?>[] parameterTypes, boolean boxed) throws NoSuchMethodException { if (currentClass == null) { throw new NoSuchMethodException("class == null"); } try { return currentClass.getMethod(name, parameterTypes); } catch (NoSuchMethodException e) { Method likeMethod = null; for (Method method : currentClass.getMethods()) { if (method.getName().equals(name) && parameterTypes.length == method.getParameterTypes().length && Modifier.isPublic(method.getModifiers())) { if (parameterTypes.length > 0) { Class<?>[] types = method.getParameterTypes(); boolean eq = true; boolean like = true; for (int i = 0; i < parameterTypes.length; i++) { Class<?> type = types[i]; Class<?> parameterType = parameterTypes[i]; if (type != null && parameterType != null && !type.equals(parameterType)) { eq = false; if (boxed) { type = getBoxedClass(type); parameterType = getBoxedClass(parameterType); } if (!type.isAssignableFrom(parameterType)) { eq = false; like = false; break; } } } if (!eq) { if (like && (likeMethod == null || likeMethod.getParameterTypes()[0] .isAssignableFrom(method.getParameterTypes()[0]))) { likeMethod = method; } continue; } } return method; } } if (likeMethod != null) { return likeMethod; } throw e; } }
From source file:Utils.java
public static boolean isJavaBeanCompliantSetter(Method method) { if (method == null) return false; if (method.getReturnType() != Void.TYPE) return false; if (!method.getName().startsWith("set")) return false; if (method.getParameterTypes().length != 1) return false; return true;// w ww . j a v a2s.co m }
From source file:MethodHashing.java
public static long methodHash(Method method) throws Exception { Class[] parameterTypes = method.getParameterTypes(); String methodDesc = method.getName() + "("; for (int j = 0; j < parameterTypes.length; j++) { methodDesc += getTypeString(parameterTypes[j]); }/*from www . ja va 2 s.c om*/ methodDesc += ")" + getTypeString(method.getReturnType()); long hash = 0; ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(512); MessageDigest messagedigest = MessageDigest.getInstance("SHA"); DataOutputStream dataoutputstream = new DataOutputStream( new DigestOutputStream(bytearrayoutputstream, messagedigest)); dataoutputstream.writeUTF(methodDesc); dataoutputstream.flush(); byte abyte0[] = messagedigest.digest(); for (int j = 0; j < Math.min(8, abyte0.length); j++) hash += (long) (abyte0[j] & 0xff) << j * 8; return hash; }
From source file:com.espertech.esper.metrics.jmx.CommonJMXUtil.java
private static MBeanParameterInfo[] extractParameterInfo(Method m) { Class<?>[] types = m.getParameterTypes(); Annotation[][] annotations = m.getParameterAnnotations(); MBeanParameterInfo[] params = new MBeanParameterInfo[types.length]; for (int i = 0; i < params.length; i++) { boolean hasAnnotation = false; for (int j = 0; j < annotations[i].length; j++) { if (annotations[i][j] instanceof JmxParam) { JmxParam param = (JmxParam) annotations[i][j]; params[i] = new MBeanParameterInfo(param.name(), types[i].getName(), param.description()); hasAnnotation = true;/*ww w. j a v a2 s. co m*/ break; } } if (!hasAnnotation) { params[i] = new MBeanParameterInfo("", types[i].getName(), ""); } } return params; }
From source file:Main.java
/** * Conveniance method used to invoke a JavaBeans-compatible setter method. As far as possible, this method will take * care of parameter conversions necessary for reflection access. * * @param setter Method to invoke.//from w ww. j av a2 s . c om * @param target Target object on which 'setter' should be invoked. * @param value Value to pass to the setter. * @throws IllegalAccessException normal reflection exception * @throws InvocationTargetException normal reflection exception */ public static void invokeSetter(final Method setter, final Object target, Object value) throws IllegalAccessException, InvocationTargetException { if (value instanceof Number) value = convertNumber(setter.getParameterTypes()[0], (Number) value); setter.invoke(target, value); }