List of usage examples for java.lang.reflect Method getName
@Override
public String getName()
From source file:com.igormaznitsa.upom.UPomModel.java
private static Method findMethod(final Class klazz, final String methodName, final boolean onlyPublic) { Method result = null;/* ww w . jav a2s. co m*/ for (final Method m : klazz.getMethods()) { if (onlyPublic && !Modifier.isPublic(m.getModifiers())) { continue; } if (m.getName().equalsIgnoreCase(methodName)) { result = m; break; } } return result; }
From source file:com.liferay.cli.support.util.ReflectionUtils.java
/** * Determine whether the given method is an "equals" method. * //from w w w. j av a 2 s . c o m * @see java.lang.Object#equals */ public static boolean isEqualsMethod(final Method method) { if (method == null || !method.getName().equals("equals")) { return false; } final Class<?>[] parameterTypes = method.getParameterTypes(); return parameterTypes.length == 1 && parameterTypes[0] == Object.class; }
From source file:io.stallion.reflection.PropertyUtils.java
public static Boolean isWriteable(Object target, String propertyName) { if (propertyName == null || "".equals(propertyName)) throw new PropertyException("encountered invalid null or empty property name"); String setterName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().equals(setterName) && method.getParameterTypes().length == 1) { return true; }//from w w w . j a v a 2 s. co m } return false; }
From source file:io.stallion.reflection.PropertyUtils.java
private static Method getSetter(Object target, String propertyName) { if (propertyName == null || "".equals(propertyName)) throw new PropertyException("encountered invalid null or empty property name"); String setterName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().equals(setterName) && method.getParameterTypes().length == 1) { return method; }/*from ww w. j a v a 2s . c o m*/ } return null; }
From source file:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java
/** * Returns a {@link Method} with a certain name and parameter types declared in the given class or any subclass * (except {@link Object})./* ww w. ja v a2 s . com*/ * <p/> * If no parameter types are specified i.e., {@code paramTypes} is {@code null} the parameter types are ignored * for signature comparison.<br/> * If a parameter type is not known i.e., it is {@code null}, all declared methods are checked whether their * parameter types conform to the known parameter types i.e., if every known type is assignable to the parameter * type of the method and if exactly one was found, it is returned.<br/> * Otherwise a {@link NoSuchMethodException} is thrown indicating that no or several methods were found. * * @param type the class * @param methodName the name of the method to find * @param clazzes the full-qualified class names of the parameters * @return the accessible method resolved * @throws ClassNotFoundException if a class cannot be located by the specified class loader */ public static Method findMethod(final Class<?> type, String methodName, Class<?>... clazzes) throws ClassNotFoundException { Method method = null; // If all parameter types are known, find the method that exactly matches the signature if (clazzes != null && !ArrayUtils.contains(clazzes, null)) { for (Class<?> clazz = type; clazz != Object.class; clazz = clazz.getSuperclass()) { try { method = type.getDeclaredMethod(methodName, clazzes); break; } catch (NoSuchMethodException e) { // Ignore } } } // If no method was found, find all possible candidates if (method == null) { List<Method> candidates = new ArrayList<>(); for (Class<?> clazz = type; clazz != null; clazz = clazz.getSuperclass()) { for (Method declaredMethod : clazz.getDeclaredMethods()) { if (declaredMethod.getName().equals(methodName) && (clazzes == null || ClassUtils.isAssignable(clazzes, declaredMethod.getParameterTypes()))) { // Check if there is already a overridden method with the same signature for (Method candidate : candidates) { if (candidate.getName().equals(declaredMethod.getName())) { /** * If there is at least one parameters in the method of the super type, which is a * sub type of the corresponding parameter of the sub type, remove the method declared * in the sub type. */ if (!Arrays.equals(declaredMethod.getParameterTypes(), candidate.getParameterTypes()) && ClassUtils.isAssignable(declaredMethod.getParameterTypes(), candidate.getParameterTypes())) { candidates.remove(candidate); } else { declaredMethod = null; } break; } } // If the method has a different signature matching the given types, add it to the candidates if (declaredMethod != null) { candidates.add(declaredMethod); } } } } if (candidates.size() != 1) { throw new JCloudScaleException( String.format("Cannot find distinct method '%s.%s()' with parameter types %s", type, methodName, Arrays.toString(clazzes))); } method = candidates.get(0); } //do we really need this dependency? //ReflectionUtils.makeAccessible(method); if (method != null && !method.isAccessible()) method.setAccessible(true); return method; }
From source file:lucee.transformer.bytecode.reflection.ASMProxyFactory.java
private static ASMMethod getMethod(ExtendableClassLoader pcl, Resource classRoot, Type type, Class clazz, Method method) throws IOException, InstantiationException, IllegalAccessException, IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException { String className = createMethodName(clazz, method.getName(), method.getParameterTypes()); // check if already in memory cache ASMMethod asmm = methods.get(className); if (asmm != null) return asmm; // try to load existing ASM Class Class<?> asmClass;/*from ww w. ja v a2 s .c om*/ try { asmClass = pcl.loadClass(className); } catch (ClassNotFoundException cnfe) { byte[] barr = _createMethod(type, clazz, method, classRoot, className); asmClass = pcl.loadClass(className, barr); } asmm = newInstance(asmClass, clazz, method.getParameterTypes()); methods.put(className, asmm); return asmm; }
From source file:io.stallion.reflection.PropertyUtils.java
private static Class getPropertyType(Object target, String propertyName) { String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); String getterIsName = "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if ((method.getName().equals(getterName) || method.getName().equals(getterIsName)) && !method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0) { return method.getReturnType(); }//from w w w .j a va 2s.co m } throw new PropertyException( "no property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }
From source file:com.espertech.esper.util.MethodResolver.java
/** * Attempts to find the static or instance method described by the parameters, * or a method of the same name that will accept the same type of * parameters./*from w w w . ja v a 2s . co m*/ * @param declaringClass - the class to search for the method * @param methodName - the name of the method * @param paramTypes - the parameter types for the method * @param allowInstance - true to allow instance methods as well, false to allow only static method * @return - the Method object for this method * @throws EngineNoSuchMethodException if the method could not be found */ public static Method resolveMethod(Class declaringClass, String methodName, Class[] paramTypes, boolean allowInstance, boolean[] allowEventBeanType, boolean[] allowEventBeanCollType) throws EngineNoSuchMethodException { // Get all the methods for this class Method[] methods = declaringClass.getMethods(); Method bestMatch = null; int bestConversionCount = -1; // Examine each method, checking if the signature is compatible Method conversionFailedMethod = null; for (Method method : methods) { // Check the modifiers: we only want public and static, if required if (!isPublicAndStatic(method, allowInstance)) { continue; } // Check the name if (!method.getName().equals(methodName)) { continue; } // Check the parameter list int conversionCount = compareParameterTypesAllowContext(method.getParameterTypes(), paramTypes, allowEventBeanType, allowEventBeanCollType, method.getGenericParameterTypes()); // Parameters don't match if (conversionCount == -1) { conversionFailedMethod = method; continue; } // Parameters match exactly if (conversionCount == 0) { bestMatch = method; break; } // No previous match if (bestMatch == null) { bestMatch = method; bestConversionCount = conversionCount; } else { // Current match is better if (conversionCount < bestConversionCount) { bestMatch = method; bestConversionCount = conversionCount; } } } if (bestMatch != null) { logWarnBoxedToPrimitiveType(declaringClass, methodName, bestMatch, paramTypes); return bestMatch; } StringBuilder parameters = new StringBuilder(); if (paramTypes != null && paramTypes.length != 0) { String appendString = ""; for (Object param : paramTypes) { parameters.append(appendString); if (param == null) { parameters.append("(null)"); } else { parameters.append(param.toString()); } appendString = ", "; } } throw new EngineNoSuchMethodException( "Unknown method " + declaringClass.getSimpleName() + '.' + methodName + '(' + parameters + ')', conversionFailedMethod); }
From source file:org.paxml.util.ReflectUtils.java
/** * Call a method on an object./*w w w . ja v a 2s .com*/ * * @param obj * the object * @param method * the method name * @param args * the arguments * @return the return value */ public static Object callMethod(Object obj, String method, Object[] args) { Class clazz = obj.getClass(); for (Method m : clazz.getMethods()) { if (!m.getName().equals(method)) { continue; } Class[] argTypes = m.getParameterTypes(); if (argTypes.length == args.length) { Object[] actualArgs = new Object[args.length]; for (int i = args.length - 1; i >= 0; i--) { actualArgs[i] = ReflectUtils.coerceType(args[i], argTypes[i]); } try { return m.invoke(obj, actualArgs); } catch (Exception e) { throw new PaxmlRuntimeException(e); } } } throw new PaxmlRuntimeException("No method named '" + method + "' has " + args.length + " parameters from class: " + clazz.getName()); }
From source file:org.paxml.util.ReflectUtils.java
/** * Call a static method. If not found, exception will be thrown. * /*w w w. j a v a 2 s. com*/ * @param className * the class name * @param method * the method name * @param args * the args name * @return the method return value. * */ public static Object callStaticMethod(String className, String method, Object[] args) { if (args == null) { args = new Object[0]; } Class clazz = ReflectUtils.loadClassStrict(className, null); for (Method m : clazz.getMethods()) { if (!m.getName().equals(method)) { continue; } Class[] argTypes = m.getParameterTypes(); if (argTypes.length == args.length) { Object[] actualArgs = new Object[args.length]; for (int i = args.length - 1; i >= 0; i--) { actualArgs[i] = ReflectUtils.coerceType(args[i], argTypes[i]); } try { return m.invoke(null, actualArgs); } catch (Exception e) { throw new PaxmlRuntimeException(e); } } } throw new PaxmlRuntimeException( "No method named '" + method + "' has " + args.length + " parameters from class: " + className); }