Sun CodeModel通用方法 [英] Sun CodeModel generic method

查看:101
本文介绍了Sun CodeModel通用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道使用CodeModel生成以下通用方法声明:

  public< T> T getValue(Class< T> clazz){...} 

用法:

  ValueType value = getValue(ValueType.class); 

似乎不会被现有的暗示处理。



我知道我可以如下处理代码,但它需要强制转换:

  public Object getValue(Class class ){...} 

用法:

  ValueType value =(ValueType)getValue(ValueType.class); 

显然这是因为剧组的混乱。

Object 返回类型创建方法,Generify方法,然后覆盖返回类型。

>

  final JDefinedClass exampleClass = codeModel._class(com.example.ExampleClass); 
final JMethod method = exampleClass.method(JMod.PUBLIC,Object.class,getValue);
final JTypeVar t = method.generify(T);
method.type(t);
method.param(codeModel.ref(Class.class).narrow(t),type);
method.body()._ return(JExpr._null());


Does anyone know to to generate the following generic method declaration using CodeModel:

public <T> T getValue(Class<T> clazz){...}

usage:

ValueType value = getValue(ValueType.class);

Seems not to be handled by the existing implmentation.

I know I could handle the code as follows, but it requires a cast:

public Object getValue(Class class){...}

usage:

ValueType value = (ValueType)getValue(ValueType.class);

Obviously this is a bit messy because of the cast.

解决方案

Create the method with an Object return type, generify the method, then overwrite the return type.

final JDefinedClass exampleClass = codeModel._class( "com.example.ExampleClass" );
final JMethod method = exampleClass.method( JMod.PUBLIC, Object.class, "getValue" );
final JTypeVar t = method.generify( "T" );
method.type( t );
method.param( codeModel.ref( Class.class ).narrow( t ), "type" );
method.body()._return(JExpr._null());

这篇关于Sun CodeModel通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆