Java 代码生成:为类创建接口

Java 代码生成:为类创建接口,java,reflection,Java,Reflection,我正在编写一个util来使用ApacheVelocity为类生成接口。目前,它使用以下DTO: public class ClassDescriptor { private String name; private List<MethodDescriptor> methods; // getters/setters } public class MethodDescriptor { private String name; private String retu

我正在编写一个util来使用ApacheVelocity为类生成接口。目前,它使用以下DTO:

public class ClassDescriptor {
  private String name;
  private List<MethodDescriptor> methods;
  // getters/setters
}

public class MethodDescriptor {
  private String name;
  private String returnType;
  private List<ParamDescriptor> parameters;
  // getters/setters
}

public class ParamDescriptor {
  public String name;
  public String type;
  public List<String> generics;
  // getters/setters
}
公共类描述符{
私有字符串名称;
私有列表方法;
//能手/二传手
}
公共类方法描述符{
私有字符串名称;
私有字符串返回类型;
私有列表参数;
//能手/二传手
}
公共类参数描述符{
公共字符串名称;
公共字符串类型;
公共列表泛型;
//能手/二传手
}
以下是目前使用的代码:

final Class<?> clazz;
final ClassDescriptor classDescriptor = new ClassDescriptor();
final List<MethodDescriptor> methodDescriptors = new ArrayList<MethodDescriptor>();
for (Method method : clazz.getDeclaredMethods()) {
  final MethodDescriptor methodDescriptor = new MethodDescriptor();
  final Paranamer paranamer = new AdaptiveParanamer();
  final String[] parameterNames = paranamer.lookupParameterNames(method, false);
  final List<ParamDescriptor> paramDescriptors = new ArrayList<ParamDescriptor>();

  for (int i = 0; i < method.getParameterTypes().length; i++) {
    final ParamDescriptor paramDescriptor = new ParamDescriptor();
    paramDescriptor.setName(parameterNames[i]);
    paramDescriptors.add(paramDescriptor);
    paramDescriptor.setType(method.getGenericParameterTypes()[i].toString().replace("class ", ""));
  }
  methodDescriptor.setParameters(paramDescriptors);
  methodDescriptor.setName(method.getName());

  methodDescriptor.setReturnType(method.getGenericReturnType().toString());
  methodDescriptors.add(methodDescriptor);
}
classDescriptor.setMethods(methodDescriptors);
classDescriptor.setName(simpleName);
final Class clazz;
final ClassDescriptor ClassDescriptor=新的ClassDescriptor();
最终列表methodDescriptors=new ArrayList();
for(方法:clazz.getDeclaredMethods()){
final MethodDescriptor MethodDescriptor=新的MethodDescriptor();
最终Paranamer Paranamer=新的自适应Paranamer();
最终字符串[]parameterNames=paranamer.lookupParameterNames(方法,false);
最终列表参数描述符=新的ArrayList();
对于(int i=0;i
这个""",?????应该包含代码来获取参数的泛型列表,这就是问题所在,我仍然找不到一种方法来做到这一点。我正在使用以下测试类:

public class TestDto {
  public void test(Map<Double, Integer> test) {
  }
}
公共类TestDto{
公共空隙试验(Map试验){
}
}
我怎样才能得到这个信息?我已经尝试了
参数化类型
,但没有成功

更新:上面的代码现在正在运行。

Class klazz=TestDto.Class;
    Class<TestDto> klazz = TestDto.class;
    try {
        Method method = klazz.getDeclaredMethod("test", Map.class);
        Type type = method.getGenericParameterTypes()[0];
        System.out.println("Type: " + type);
    } catch (NoSuchMethodException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SecurityException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }

    Type: java.util.Map<java.lang.Double, java.lang.Integer>
试一试{ 方法方法=klazz.getDeclaredMethod(“测试”,Map.class); Type Type=method.getGenericParameterTypes()[0]; System.out.println(“类型:“+Type”); }catch(NoSuchMethodException-ex){ Logger.getLogger(App.class.getName()).log(Level.SEVERE,null,ex); }catch(SecurityException-ex){ Logger.getLogger(App.class.getName()).log(Level.SEVERE,null,ex); } 类型:java.util.Map

由于类型擦除,这仍然是大量的信息。没有听到任何关于运行时泛型类型使用的消息。

非常好!我的主要问题是让泛型类将它们添加到导入列表中。我忘记了,我们可以使用java.util.bla这样的完整路径。简单类也有一个小问题,因为它返回“class java.util.String”。当前我使用的是
paramDescriptor.setType(method.getGenericParameterTypes()[I].toString().replace(“class”),
。不是很高雅,但很管用。我必须检查这是否适用于退货类型,然后我会给出答案。