@Test public void testWithManyParams() { Method m = U.single(Cls.getMethodsNamed(Foo.class, "xy")); String[] names = Reflect.getMethodParameterNames(m); eq(names, U.array("a", "b", "c", "d", "e", "f")); Method m2 = U.single(Cls.getMethodsNamed(Foo.class, "xyz")); String[] names2 = Reflect.getMethodParameterNames(m2); eq(names2, U.array("a", "b", "c", "d", "e", "f", "g", "hh", "ii", "j")); }
public static String[] getMethodParameterNames(Method method) { Class<?>[] paramTypes = method.getParameterTypes(); String[] names = new String[paramTypes.length]; boolean defaultNames = true; Method getParameters = Cls.findMethod(method.getClass(), "getParameters"); if (getParameters != null) { Object[] parameters = Cls.invoke(getParameters, method); for (int i = 0; i < parameters.length; i++) { names[i] = Beany.getPropValue(parameters[i], "name"); U.notNull(names[i], "parameter name"); if (!names[i].equals("arg" + i)) { defaultNames = false; } } } if (defaultNames) { boolean useIndexMapping; CtMethod cm; try { ClassPool cp = new ClassPool(); cp.insertClassPath(new ClassClassPath(method.getDeclaringClass())); CtClass cc = cp.get(method.getDeclaringClass().getName()); useIndexMapping = cc.getClassFile().getMajorVersion() >= 52; CtClass[] params = new CtClass[paramTypes.length]; for (int i = 0; i < params.length; i++) { params[i] = cp.get(method.getParameterTypes()[i].getName()); } cm = cc.getDeclaredMethod(method.getName(), params); } catch (NotFoundException e) { throw U.rte("Cannot find the target method!", e); } MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); if (codeAttribute != null) { LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute .getAttribute(LocalVariableAttribute.tag); int offset = Modifier.isStatic(cm.getModifiers()) ? 0 : 1; for (int i = 0; i < names.length; i++) { names[i] = null; } for (int i = 0; i < attr.tableLength(); i++) { int index = i - offset; if (useIndexMapping) { index = attr.index(index); } String var = attr.variableName(i); if (index >= 0 && index < names.length && !"this".equals(var)) { names[index] = var; } } if (!validNames(names)) { for (int i = 0; i < names.length; i++) { names[i] = null; } for (int i = 0; i < attr.tableLength(); i++) { int index = i - offset; String var = attr.variableName(i); if (index >= 0 && index < names.length && !"this".equals(var)) { names[index] = var; } } } } U.must(validNames(names), "Couldn't retrieve the parameter names! Please report this problem. " + "You can explicitly specify the names using @Param(\"thename\"), " + "or configure the option '-parameters' on the Java 8 compiler."); } return names; }
Reflect extends Cls { public static String[] getMethodParameterNames(Method method) { Class<?>[] paramTypes = method.getParameterTypes(); String[] names = new String[paramTypes.length]; boolean defaultNames = true; Method getParameters = Cls.findMethod(method.getClass(), "getParameters"); if (getParameters != null) { Object[] parameters = Cls.invoke(getParameters, method); for (int i = 0; i < parameters.length; i++) { names[i] = Beany.getPropValue(parameters[i], "name"); U.notNull(names[i], "parameter name"); if (!names[i].equals("arg" + i)) { defaultNames = false; } } } if (defaultNames) { boolean useIndexMapping; CtMethod cm; try { ClassPool cp = new ClassPool(); cp.insertClassPath(new ClassClassPath(method.getDeclaringClass())); CtClass cc = cp.get(method.getDeclaringClass().getName()); useIndexMapping = cc.getClassFile().getMajorVersion() >= 52; CtClass[] params = new CtClass[paramTypes.length]; for (int i = 0; i < params.length; i++) { params[i] = cp.get(method.getParameterTypes()[i].getName()); } cm = cc.getDeclaredMethod(method.getName(), params); } catch (NotFoundException e) { throw U.rte("Cannot find the target method!", e); } MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); if (codeAttribute != null) { LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute .getAttribute(LocalVariableAttribute.tag); int offset = Modifier.isStatic(cm.getModifiers()) ? 0 : 1; for (int i = 0; i < names.length; i++) { names[i] = null; } for (int i = 0; i < attr.tableLength(); i++) { int index = i - offset; if (useIndexMapping) { index = attr.index(index); } String var = attr.variableName(i); if (index >= 0 && index < names.length && !"this".equals(var)) { names[index] = var; } } if (!validNames(names)) { for (int i = 0; i < names.length; i++) { names[i] = null; } for (int i = 0; i < attr.tableLength(); i++) { int index = i - offset; String var = attr.variableName(i); if (index >= 0 && index < names.length && !"this".equals(var)) { names[index] = var; } } } } U.must(validNames(names), "Couldn't retrieve the parameter names! Please report this problem. " + "You can explicitly specify the names using @Param(\"thename\"), " + "or configure the option '-parameters' on the Java 8 compiler."); } return names; } }
Reflect extends Cls { public static String[] getMethodParameterNames(Method method) { Class<?>[] paramTypes = method.getParameterTypes(); String[] names = new String[paramTypes.length]; boolean defaultNames = true; Method getParameters = Cls.findMethod(method.getClass(), "getParameters"); if (getParameters != null) { Object[] parameters = Cls.invoke(getParameters, method); for (int i = 0; i < parameters.length; i++) { names[i] = Beany.getPropValue(parameters[i], "name"); U.notNull(names[i], "parameter name"); if (!names[i].equals("arg" + i)) { defaultNames = false; } } } if (defaultNames) { boolean useIndexMapping; CtMethod cm; try { ClassPool cp = new ClassPool(); cp.insertClassPath(new ClassClassPath(method.getDeclaringClass())); CtClass cc = cp.get(method.getDeclaringClass().getName()); useIndexMapping = cc.getClassFile().getMajorVersion() >= 52; CtClass[] params = new CtClass[paramTypes.length]; for (int i = 0; i < params.length; i++) { params[i] = cp.get(method.getParameterTypes()[i].getName()); } cm = cc.getDeclaredMethod(method.getName(), params); } catch (NotFoundException e) { throw U.rte("Cannot find the target method!", e); } MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); if (codeAttribute != null) { LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute .getAttribute(LocalVariableAttribute.tag); int offset = Modifier.isStatic(cm.getModifiers()) ? 0 : 1; for (int i = 0; i < names.length; i++) { names[i] = null; } for (int i = 0; i < attr.tableLength(); i++) { int index = i - offset; if (useIndexMapping) { index = attr.index(index); } String var = attr.variableName(i); if (index >= 0 && index < names.length && !"this".equals(var)) { names[index] = var; } } if (!validNames(names)) { for (int i = 0; i < names.length; i++) { names[i] = null; } for (int i = 0; i < attr.tableLength(); i++) { int index = i - offset; String var = attr.variableName(i); if (index >= 0 && index < names.length && !"this".equals(var)) { names[index] = var; } } } } U.must(validNames(names), "Couldn't retrieve the parameter names! Please report this problem. " + "You can explicitly specify the names using @Param(\"thename\"), " + "or configure the option '-parameters' on the Java 8 compiler."); } return names; }  }
Reflect extends Cls { public static String[] getMethodParameterNames(Method method) { Class<?>[] paramTypes = method.getParameterTypes(); String[] names = new String[paramTypes.length]; boolean defaultNames = true; Method getParameters = Cls.findMethod(method.getClass(), "getParameters"); if (getParameters != null) { Object[] parameters = Cls.invoke(getParameters, method); for (int i = 0; i < parameters.length; i++) { names[i] = Beany.getPropValue(parameters[i], "name"); U.notNull(names[i], "parameter name"); if (!names[i].equals("arg" + i)) { defaultNames = false; } } } if (defaultNames) { boolean useIndexMapping; CtMethod cm; try { ClassPool cp = new ClassPool(); cp.insertClassPath(new ClassClassPath(method.getDeclaringClass())); CtClass cc = cp.get(method.getDeclaringClass().getName()); useIndexMapping = cc.getClassFile().getMajorVersion() >= 52; CtClass[] params = new CtClass[paramTypes.length]; for (int i = 0; i < params.length; i++) { params[i] = cp.get(method.getParameterTypes()[i].getName()); } cm = cc.getDeclaredMethod(method.getName(), params); } catch (NotFoundException e) { throw U.rte("Cannot find the target method!", e); } MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); if (codeAttribute != null) { LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute .getAttribute(LocalVariableAttribute.tag); int offset = Modifier.isStatic(cm.getModifiers()) ? 0 : 1; for (int i = 0; i < names.length; i++) { names[i] = null; } for (int i = 0; i < attr.tableLength(); i++) { int index = i - offset; if (useIndexMapping) { index = attr.index(index); } String var = attr.variableName(i); if (index >= 0 && index < names.length && !"this".equals(var)) { names[index] = var; } } if (!validNames(names)) { for (int i = 0; i < names.length; i++) { names[i] = null; } for (int i = 0; i < attr.tableLength(); i++) { int index = i - offset; String var = attr.variableName(i); if (index >= 0 && index < names.length && !"this".equals(var)) { names[index] = var; } } } } U.must(validNames(names), "Couldn't retrieve the parameter names! Please report this problem. " + "You can explicitly specify the names using @Param(\"thename\"), " + "or configure the option '-parameters' on the Java 8 compiler."); } return names; }  static void setFieldValue(Object instance, String fieldName, Object value); static void setFieldValue(Field field, Object instance, Object value); static List<Field> getFields(Class<?> clazz); static List<Field> getFieldsAnnotated(Class<?> clazz, Class<? extends Annotation> annotation); static Map<String, Class<?>> classMap(Iterable<Class<?>> classes); static Class<?>[] typesOf(Object[] args); static List<Method> getDeclaredMethods(Class<?> clazz); static String[] getMethodParameterNames(Method method); static String[] getLambdaParameterNames(Serializable lambda); static Method getLambdaMethod(Serializable lambda); static Method getLambdaMethod(Serializable lambda, String functionalMethodName); static Object invokeStatic(String className, String methodName, Object... args); static boolean annotatedMethod(Object instance, String methodName, Class<Annotation> annotation); static Constructor<T> getConstructor(Class<T> clazz, Class<?>... paramTypes); static Field getField(Class<?> clazz, String name); static List<Method> getMethodsAnnotated(Class<?> clazz, Class<? extends Annotation> annotation); }
Reflect extends Cls { public static String[] getMethodParameterNames(Method method) { Class<?>[] paramTypes = method.getParameterTypes(); String[] names = new String[paramTypes.length]; boolean defaultNames = true; Method getParameters = Cls.findMethod(method.getClass(), "getParameters"); if (getParameters != null) { Object[] parameters = Cls.invoke(getParameters, method); for (int i = 0; i < parameters.length; i++) { names[i] = Beany.getPropValue(parameters[i], "name"); U.notNull(names[i], "parameter name"); if (!names[i].equals("arg" + i)) { defaultNames = false; } } } if (defaultNames) { boolean useIndexMapping; CtMethod cm; try { ClassPool cp = new ClassPool(); cp.insertClassPath(new ClassClassPath(method.getDeclaringClass())); CtClass cc = cp.get(method.getDeclaringClass().getName()); useIndexMapping = cc.getClassFile().getMajorVersion() >= 52; CtClass[] params = new CtClass[paramTypes.length]; for (int i = 0; i < params.length; i++) { params[i] = cp.get(method.getParameterTypes()[i].getName()); } cm = cc.getDeclaredMethod(method.getName(), params); } catch (NotFoundException e) { throw U.rte("Cannot find the target method!", e); } MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); if (codeAttribute != null) { LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute .getAttribute(LocalVariableAttribute.tag); int offset = Modifier.isStatic(cm.getModifiers()) ? 0 : 1; for (int i = 0; i < names.length; i++) { names[i] = null; } for (int i = 0; i < attr.tableLength(); i++) { int index = i - offset; if (useIndexMapping) { index = attr.index(index); } String var = attr.variableName(i); if (index >= 0 && index < names.length && !"this".equals(var)) { names[index] = var; } } if (!validNames(names)) { for (int i = 0; i < names.length; i++) { names[i] = null; } for (int i = 0; i < attr.tableLength(); i++) { int index = i - offset; String var = attr.variableName(i); if (index >= 0 && index < names.length && !"this".equals(var)) { names[index] = var; } } } } U.must(validNames(names), "Couldn't retrieve the parameter names! Please report this problem. " + "You can explicitly specify the names using @Param(\"thename\"), " + "or configure the option '-parameters' on the Java 8 compiler."); } return names; }  static void setFieldValue(Object instance, String fieldName, Object value); static void setFieldValue(Field field, Object instance, Object value); static List<Field> getFields(Class<?> clazz); static List<Field> getFieldsAnnotated(Class<?> clazz, Class<? extends Annotation> annotation); static Map<String, Class<?>> classMap(Iterable<Class<?>> classes); static Class<?>[] typesOf(Object[] args); static List<Method> getDeclaredMethods(Class<?> clazz); static String[] getMethodParameterNames(Method method); static String[] getLambdaParameterNames(Serializable lambda); static Method getLambdaMethod(Serializable lambda); static Method getLambdaMethod(Serializable lambda, String functionalMethodName); static Object invokeStatic(String className, String methodName, Object... args); static boolean annotatedMethod(Object instance, String methodName, Class<Annotation> annotation); static Constructor<T> getConstructor(Class<T> clazz, Class<?>... paramTypes); static Field getField(Class<?> clazz, String name); static List<Method> getMethodsAnnotated(Class<?> clazz, Class<? extends Annotation> annotation);  }