package com.eqot.xray.processor;

import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;

@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class ClassDef {
    final String packageName;
    final String className;
    final List<MethodDef> constructors = new ArrayList<>();
    final List<MethodDef> methods = new ArrayList<>();

    ClassDef(String target) {
        final String[] words = target.split("\\.");
        String tmpPackageName = "";
        String tmpClassName = "";
        for (int i = 0, l = words.length; i < l; i++) {
            if (i < l - 1) {
                if (i != 0) {
                    tmpPackageName += ".";
                }
                tmpPackageName += words[i];
            } else {
                tmpClassName = words[i];
            }
        }
        packageName = tmpPackageName;
        className = tmpClassName;

        Class clazz = null;
        try {
            clazz = Class.forName(target);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (clazz == null) {
            return;
        }

        for (Constructor constructor : clazz.getDeclaredConstructors()) {
            MethodDef methodDef = new MethodDef(constructor);
            constructors.add(methodDef);
        }

        for (Method method : clazz.getDeclaredMethods()) {
            MethodDef methodDef = new MethodDef(method);
            methods.add(methodDef);
        }
    }

    public class MethodDef {
        final String name;
        final boolean isStatic;
        final Class<?> returnType;
        final List<ParameterDef> parameters = new ArrayList<>();

        MethodDef(Method method) {
            name = method.getName();
            returnType = method.getReturnType();
            isStatic = (method.getModifiers() & Modifier.STATIC) != 0;

            addParameters(method.getParameters());
        }

        MethodDef(Constructor constructor) {
            name = constructor.getName();
            returnType = null;
            isStatic = false;

            addParameters(constructor.getParameters());
        }

        private void addParameters(Parameter[] parameters) {
            for (Parameter parameter : parameters) {
                ParameterDef parameterDef = new ParameterDef(parameter);
                this.parameters.add(parameterDef);
            }
        }
    }

    public class ParameterDef {
        final Class<?> type;
        final String name;

        ParameterDef(Parameter parameter) {
            type = parameter.getType();
            name = parameter.getName();
        }
    }
}

@@ -38,38 +38,41 @@
 ort 
-java.lang.reflect.Constructor;
+com.squareup.javapoet.ClassName;%0A
 %0Aimp
@@ -97,18 +97,19 @@
 ect.
-Executable
+Constructor
 ;%0Aim
@@ -448,44 +448,17 @@
 nal 
-String packageName;%0A    final String
+ClassName
  cla
@@ -623,498 +623,47 @@
     
-final String%5B%5D words = target.split(%22%5C%5C.%22);%0A        String tmpPackageName = %22%22;%0A        String tmpClassName = %22%22;%0A        for (int i = 0, l = words.length; i %3C l; i++) %7B%0A            if (i %3C l - 1) %7B%0A                if (i != 0) %7B%0A                    tmpPackageName += %22.%22;%0A                %7D%0A                tmpPackageName += words%5Bi%5D;%0A            %7D else %7B%0A                tmpClassName = words%5Bi%5D;%0A            %7D%0A        %7D%0A        packageName = tmpPackageName;%0A        className = tmpClassName
+className = ClassName.bestGuess(target)
 ;%0A%0A 

Simplify code