@Test public void testExtractStaticMethod() throws Exception { final Method testedMethod = getClass().getMethod("_mockMethod2", double.class, float.class, Object.class); final Collection<Method> methods = Collections.singleton(testedMethod); final Map<Method, List<String>> methodMap = MethodParamNamesExtractor.extract(getClass(), methods); final List<String> paramNames = methodMap.get(testedMethod); assertEquals("method param count", 3, paramNames.size()); final List<String> expectedNames = Arrays.asList("d1", "f1", "o1"); int index = 0; for (final String paramName : paramNames) { assertNotNull(paramName, paramName); assertEquals(paramName, expectedNames.get(index++)); } }
public static Map<Method, List<String>> extract(final Class<?> type, final Collection<Method> methods) throws IOException { final InputStream classBytes = MethodParamNamesExtractor.class.getClassLoader().getResourceAsStream(toResourceName(type)); final ClassReader cr = new ClassReader(classBytes); final Map<String, Method> methodsSignature = new HashMap<>(); for (final Method method: methods) { methodsSignature.put(method.getName(), method); } if (methodsSignature.size() != new HashSet<>(methods).size()) { throw new IllegalArgumentException("method names must be unique in service: " + type.getName()); } final Map<Method, SortedMap<Integer, String>> paramsMap = new HashMap<>(); cr.accept(new ClassVisitor(Opcodes.ASM4) { public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { final Method method = methodsSignature.get(name); if(method != null) { final SortedMap<Integer, String> names = new TreeMap<>(); paramsMap.put(method, names); return new MethodVisitor(Opcodes.ASM4) { @Override public void visitLocalVariable(final String name, final String description, final String signature, final Label start, final Label end, final int index) { if (Modifier.isStatic(method.getModifiers()) || index > 0) { names.put(index, name); } } }; } else { return super.visitMethod(access, name, desc, signature, exceptions); } } }, 0); final Map<Method, List<String>> result = new HashMap<>(); for (final Method method : paramsMap.keySet()) { final SortedMap<Integer, String> params = paramsMap.get(method); final List<String> names = new ArrayList<>(); final int length = method.getParameterTypes().length; int index = 0; for (final String name: params.values()) { if (index < length) { names.add(name); } index++; } result.put(method, names); } return result; }
MethodParamNamesExtractor { public static Map<Method, List<String>> extract(final Class<?> type, final Collection<Method> methods) throws IOException { final InputStream classBytes = MethodParamNamesExtractor.class.getClassLoader().getResourceAsStream(toResourceName(type)); final ClassReader cr = new ClassReader(classBytes); final Map<String, Method> methodsSignature = new HashMap<>(); for (final Method method: methods) { methodsSignature.put(method.getName(), method); } if (methodsSignature.size() != new HashSet<>(methods).size()) { throw new IllegalArgumentException("method names must be unique in service: " + type.getName()); } final Map<Method, SortedMap<Integer, String>> paramsMap = new HashMap<>(); cr.accept(new ClassVisitor(Opcodes.ASM4) { public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { final Method method = methodsSignature.get(name); if(method != null) { final SortedMap<Integer, String> names = new TreeMap<>(); paramsMap.put(method, names); return new MethodVisitor(Opcodes.ASM4) { @Override public void visitLocalVariable(final String name, final String description, final String signature, final Label start, final Label end, final int index) { if (Modifier.isStatic(method.getModifiers()) || index > 0) { names.put(index, name); } } }; } else { return super.visitMethod(access, name, desc, signature, exceptions); } } }, 0); final Map<Method, List<String>> result = new HashMap<>(); for (final Method method : paramsMap.keySet()) { final SortedMap<Integer, String> params = paramsMap.get(method); final List<String> names = new ArrayList<>(); final int length = method.getParameterTypes().length; int index = 0; for (final String name: params.values()) { if (index < length) { names.add(name); } index++; } result.put(method, names); } return result; } }
MethodParamNamesExtractor { public static Map<Method, List<String>> extract(final Class<?> type, final Collection<Method> methods) throws IOException { final InputStream classBytes = MethodParamNamesExtractor.class.getClassLoader().getResourceAsStream(toResourceName(type)); final ClassReader cr = new ClassReader(classBytes); final Map<String, Method> methodsSignature = new HashMap<>(); for (final Method method: methods) { methodsSignature.put(method.getName(), method); } if (methodsSignature.size() != new HashSet<>(methods).size()) { throw new IllegalArgumentException("method names must be unique in service: " + type.getName()); } final Map<Method, SortedMap<Integer, String>> paramsMap = new HashMap<>(); cr.accept(new ClassVisitor(Opcodes.ASM4) { public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { final Method method = methodsSignature.get(name); if(method != null) { final SortedMap<Integer, String> names = new TreeMap<>(); paramsMap.put(method, names); return new MethodVisitor(Opcodes.ASM4) { @Override public void visitLocalVariable(final String name, final String description, final String signature, final Label start, final Label end, final int index) { if (Modifier.isStatic(method.getModifiers()) || index > 0) { names.put(index, name); } } }; } else { return super.visitMethod(access, name, desc, signature, exceptions); } } }, 0); final Map<Method, List<String>> result = new HashMap<>(); for (final Method method : paramsMap.keySet()) { final SortedMap<Integer, String> params = paramsMap.get(method); final List<String> names = new ArrayList<>(); final int length = method.getParameterTypes().length; int index = 0; for (final String name: params.values()) { if (index < length) { names.add(name); } index++; } result.put(method, names); } return result; }  }
MethodParamNamesExtractor { public static Map<Method, List<String>> extract(final Class<?> type, final Collection<Method> methods) throws IOException { final InputStream classBytes = MethodParamNamesExtractor.class.getClassLoader().getResourceAsStream(toResourceName(type)); final ClassReader cr = new ClassReader(classBytes); final Map<String, Method> methodsSignature = new HashMap<>(); for (final Method method: methods) { methodsSignature.put(method.getName(), method); } if (methodsSignature.size() != new HashSet<>(methods).size()) { throw new IllegalArgumentException("method names must be unique in service: " + type.getName()); } final Map<Method, SortedMap<Integer, String>> paramsMap = new HashMap<>(); cr.accept(new ClassVisitor(Opcodes.ASM4) { public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { final Method method = methodsSignature.get(name); if(method != null) { final SortedMap<Integer, String> names = new TreeMap<>(); paramsMap.put(method, names); return new MethodVisitor(Opcodes.ASM4) { @Override public void visitLocalVariable(final String name, final String description, final String signature, final Label start, final Label end, final int index) { if (Modifier.isStatic(method.getModifiers()) || index > 0) { names.put(index, name); } } }; } else { return super.visitMethod(access, name, desc, signature, exceptions); } } }, 0); final Map<Method, List<String>> result = new HashMap<>(); for (final Method method : paramsMap.keySet()) { final SortedMap<Integer, String> params = paramsMap.get(method); final List<String> names = new ArrayList<>(); final int length = method.getParameterTypes().length; int index = 0; for (final String name: params.values()) { if (index < length) { names.add(name); } index++; } result.put(method, names); } return result; }  static Map<Method, List<String>> extract(final Class<?> type, final Collection<Method> methods); }
MethodParamNamesExtractor { public static Map<Method, List<String>> extract(final Class<?> type, final Collection<Method> methods) throws IOException { final InputStream classBytes = MethodParamNamesExtractor.class.getClassLoader().getResourceAsStream(toResourceName(type)); final ClassReader cr = new ClassReader(classBytes); final Map<String, Method> methodsSignature = new HashMap<>(); for (final Method method: methods) { methodsSignature.put(method.getName(), method); } if (methodsSignature.size() != new HashSet<>(methods).size()) { throw new IllegalArgumentException("method names must be unique in service: " + type.getName()); } final Map<Method, SortedMap<Integer, String>> paramsMap = new HashMap<>(); cr.accept(new ClassVisitor(Opcodes.ASM4) { public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { final Method method = methodsSignature.get(name); if(method != null) { final SortedMap<Integer, String> names = new TreeMap<>(); paramsMap.put(method, names); return new MethodVisitor(Opcodes.ASM4) { @Override public void visitLocalVariable(final String name, final String description, final String signature, final Label start, final Label end, final int index) { if (Modifier.isStatic(method.getModifiers()) || index > 0) { names.put(index, name); } } }; } else { return super.visitMethod(access, name, desc, signature, exceptions); } } }, 0); final Map<Method, List<String>> result = new HashMap<>(); for (final Method method : paramsMap.keySet()) { final SortedMap<Integer, String> params = paramsMap.get(method); final List<String> names = new ArrayList<>(); final int length = method.getParameterTypes().length; int index = 0; for (final String name: params.values()) { if (index < length) { names.add(name); } index++; } result.put(method, names); } return result; }  static Map<Method, List<String>> extract(final Class<?> type, final Collection<Method> methods);  }