package org.restler.spring.mvc;

import org.objectweb.asm.ClassReader;
import org.restler.client.RestlerException;
import org.restler.spring.mvc.asm.ClassUtils;
import org.restler.spring.mvc.asm.ParameterNameDiscoveringVisitor;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ParameterNameDiscoverer {

    public String[] getParameterNames(Method method) {
        String[] result = getParameterNamesDefault(method);

        if(result == null) {
            result = getParameterNamesFromLocalVariableTable(method);
        }

        return result;
    }

    private String[] getParameterNamesDefault(Method method) {
        Parameter[] parameters = method.getParameters();
        String[] parameterNames = new String[parameters.length];

        for(int i = 0; i < parameters.length; ++i) {
            Parameter parameter = parameters[i];
            if(!parameter.isNamePresent()) {
                return null;
            }

            parameterNames[i] = parameter.getName();
        }

        if(parameterNames.length == 0) {
            return getParameterNamesFromLocalVariableTable(method);
        }

        return parameterNames;
    }

    private String[] getParameterNamesFromLocalVariableTable(Method method) {
        Class<?> declaringClass = method.getDeclaringClass();

        Map<Member, String[]> map = inspectClass(declaringClass);

        if (map != null) {
            return map.get(method);
        }
        return null;
    }

    private Map<Member, String[]> inspectClass(Class<?> clazz) {
        InputStream inputStream = clazz.getResourceAsStream(ClassUtils.getClassFileName(clazz));
        if (inputStream == null) {
            return null;
        }
        try {
            ClassReader classReader = new ClassReader(inputStream);
            Map<Member, String[]> map = new ConcurrentHashMap<>(32);
            classReader.accept(new ParameterNameDiscoveringVisitor(clazz, map), 0);
            return map;
        }
        catch (IOException e) {
            throw new RestlerException("Can't read class from stream.", e);
        }
        catch(IllegalArgumentException e) {
            throw new RestlerException("Illegal argument.", e);
        }
        finally {
            try {
                inputStream.close();
            }
            catch (IOException ex) {
                // ignore
            }
        }
    }
}

@@ -931,17 +931,16 @@
 ength%5D;%0A
-%0A
         
@@ -942,16 +942,17 @@
      for
+ 
 (int i =
@@ -982,11 +982,11 @@
 th; 
+i
 ++
-i
 ) %7B%0A
@@ -1012,20 +1012,16 @@
 er param
-eter
  = param
@@ -1048,19 +1048,16 @@
   if
+ 
 (!param
-eter
 .isN
@@ -1107,33 +1107,32 @@
 ;%0A            %7D%0A
-%0A
             para
@@ -1152,20 +1152,16 @@
  = param
-eter
 .getName
@@ -1178,129 +1178,8 @@
   %7D%0A
-%0A        if(parameterNames.length == 0) %7B%0A            return getParameterNamesFromLocalVariableTable(method);%0A        %7D%0A%0A
     

Remove useless way.