samedi 4 février 2017

Need help in optmizing the following code

I am using google guava library to get all the classes recursively from the packages mentioned and iterating through all the public methods and getting their parameters. the "mapOfpackageMethodParameters" will be then passed to gson object which will give me json.

Is there any way i can optimize the two innermost for loops by using lambdas or java8 stream api. any other suggestions ??

String [] packagenames = {"com.example"};
    LinkedHashMap<String, LinkedHashMap<String, LinkedHashMap<String, String>>> mapOfpackageMethodParameters = new LinkedHashMap<>();
    for (String packagename : packagenames) {
        List<ClassInfo> clazzList = ClassPath.from(ClassLoader.getSystemClassLoader())
                .getTopLevelClassesRecursive(packagename).stream().filter(c -> c.getPackageName().endsWith("test"))
                .collect(Collectors.toList());
        for (ClassInfo class1 : clazzList) {
            List<Method> methodList = Arrays.asList(Class.forName(class1.getName()).getMethods());
            LinkedHashMap<String, LinkedHashMap<String, String>> methodMap = new LinkedHashMap<>();
            for (Method method : methodList) {
                List<Parameter> parameterList = Arrays.asList(method.getParameters());
                LinkedHashMap<String, String> parameterMap = parameterList.stream()
                        .collect(Collectors.toMap(param -> param.getName(), param -> param.getType().toString(),
                                (x, y) -> x, LinkedHashMap::new));
                methodMap.put(method.getName(), parameterMap);
            }
            mapOfpackageMethodParameters.put(class1.getName(), methodMap);
        }
    }





Aucun commentaire:

Enregistrer un commentaire