/** * Finds a method by reflection. This iterates all methods of the class, comparing names and parameter types. * Unlike the method Class.getMethod(String, Class ...), this method is able to match primitive and wrapper types. * If no appropriate method is found, a ConfigurationError is raised. * @param type the class that holds the method * @param methodName the name of the method * @param paramTypes the parameter types of the method * @return a method with matching names and parameters */ public static Method getMethod(Class<?> type, String methodName, Class<?> ... paramTypes) { Method method = findMethod(type, methodName, paramTypes); if (method == null) throw new ConfigurationError("method not found in class " + type.getName() + ": " + methodName + '(' + ArrayFormat.format(paramTypes) + ')'); return method; }
private Converter tryToCreateFactoryConverter(Class sourceType, Class targetType) { { // find instance method <targetType>Value() in source type String methodName = StringUtil.uncapitalize(targetType.getSimpleName()) + "Value"; Method typeValueMethod = BeanUtil.findMethod(sourceType, methodName, new Class[0]); if (typeValueMethod != null && (typeValueMethod.getModifiers() & Modifier.STATIC) == 0) return new SourceClassMethodInvoker(sourceType, targetType, typeValueMethod); } { // find static getInstance() method in target type Method getInstanceMethod = BeanUtil.findMethod(targetType, "getInstance", sourceType); if (getInstanceMethod != null && (getInstanceMethod.getModifiers() & Modifier.STATIC) == Modifier.STATIC) return new StaticTargetClassMethodInvoker(sourceType, targetType, getInstanceMethod); } { // find static valueOf() method in target type Method valueOfMethod = BeanUtil.findMethod(targetType, "valueOf", sourceType); if (valueOfMethod != null && (valueOfMethod.getModifiers() & Modifier.STATIC) == Modifier.STATIC) return new StaticTargetClassMethodInvoker(sourceType, targetType, valueOfMethod); } { // find target type constructor which takes source type argument Constructor constructor = BeanUtil.findConstructor(targetType, sourceType); if (constructor != null) return new ConstructorInvoker(sourceType, constructor); } return findPoorConfiguredMatch(sourceType, targetType); }
private void checkParallelizable(Converter<?, ?> converter) { if (converter.isParallelizable()) { Assert.isTrue(converter instanceof Cloneable, "Parallelizable converters must implement " + Cloneable.class); Assert.isTrue(BeanUtil.findMethod(converterClass, "clone", (Class[]) null) != null, "Parallelizable converters must have a public clone() method"); } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { String methodName = method.getName(); Method localMethod = BeanUtil.findMethod(this.getClass(), methodName, method.getParameterTypes()); if (localMethod != null) return BeanUtil.invoke(this, localMethod, args); else { if ("setNull".equals(methodName) && args != null && args.length >= 2) params[(Integer) args[0] - 1] = null; else if (methodName.startsWith("set") && args != null && args.length >= 2 && args[0] instanceof Integer) params[(Integer) args[0] - 1] = args[1]; Object result = BeanUtil.invoke(realStatement, method, args); if (result instanceof ResultSet) result = DBUtil.createLoggingResultSet((ResultSet) result, (PreparedStatement) proxy); return result; } } catch (ConfigurationError e) { if (e.getCause() instanceof InvocationTargetException && e.getCause().getCause() instanceof SQLException) throw e.getCause().getCause(); else throw e; } }
else { Method genericSetMethod = BeanUtil.findMethod(target.getClass(), "set", String.class, Object.class); if (genericSetMethod != null) { BeanUtil.invoke(target, genericSetMethod, true, new Object[] { featureName, value });
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { String methodName = method.getName(); Method localMethod = BeanUtil.findMethod(this.getClass(), methodName, method.getParameterTypes()); Object result; boolean profile = methodName.startsWith("execute") && "true".equals(System.getProperty("profile")); long startTime = 0; if (profile) startTime = System.nanoTime(); if (localMethod != null) result = BeanUtil.invoke(this, localMethod, args); else result = BeanUtil.invoke(realStatement, method, args); if (result instanceof ResultSet) result = DBUtil.createLoggingResultSet((ResultSet) result, (Statement) proxy); if (profile) { long duration = (System.nanoTime() - startTime) / 1000000; Profiler.defaultInstance().addSample(CollectionUtil.toList("SQL", sql), duration); } return result; } catch (ConfigurationError e) { if (e.getCause() instanceof InvocationTargetException && e.getCause().getCause() instanceof SQLException) throw e.getCause().getCause(); else throw e; } }
else { Method genericGetMethod = BeanUtil.findMethod(target.getClass(), "get", String.class); if (genericGetMethod != null) return BeanUtil.invoke(target, genericGetMethod, new Object[] { featureName });