2016-10-27 10 views
1

私は、利便性の理由から、新しい生成クラスにマージしたい複数のクラスに静的メソッドを持っています。注釈処理とjavapoetで静的メソッドをマージ

私はアノテーション処理とjavapoetを使用しています。

問題:アノテーション処理から、私はExecutableElementsのリストとして静的メソッドを取得します。

JavaPoetについては、それらのMethodSpecを作成する必要があります。私は努力しています:

public MethodSpec apply(@Nullable ExecutableElement input) { 
    TypeMirror returnType = input.getReturnType(); 

    return MethodSpec.methodBuilder(THE_METHOD_NAME) 
    .addModifiers(Modifier.PUBLIC, Modifier.STATIC) 
    .returns(THE_RETURN_TYPE) 
    .addParameter(EVERY_PARAMETER_WITH_TYPE_AND_NAME) 
    .addStatement("$T.$S($S)", THE_ENCLOSING_CLASS, THE_METHOD_NAME, THE_PARAMETERS) 
    .build(); 
} 

私の質問:どのように私はCAPSの欠けている言葉の値を取得するには? ExecutableElementsがリフレクションAPIのように動作しないようです。

答えて

0

これはあなたが始めるのに使用できる概略的なコードであってもよい:

THE_METHOD_NAME - >input.getSimpleName()

THE_RETURN_TYPE - >input.getReturnType()

THE_ENCLOSING_CLASS - >input.getEnclosingElement().getSimpleName()

EVERY_PARAMETER_WITH_TYPE_AND_NAME - >

for (VariableElement ve: executableElement.getParameters()){ 
    System.out.println("Param name: " + ve.getSimpleName()); 
    // note here that the type can be generic, so you'll need to parse 
    // the generic part and use to build a javapoet 
    // ParameterizedTypeName 
    System.out.println("Param type: " + ve.asType()); 
} 
+0

ありがとうございます。しかし、上記のコードで見られるように、getReturnTypeは "TypeMirror"型です...これをjavapoetに渡すと、TypeMirrorが返されます。 –

+0

'com.squareup.javapoet.ClassName'を使用してビルドできます've.asType()'の出力の文字列表現から型を作ります。私は[javapoet](https://github.com/square/javapoet)から取られたサンプルコードに基づいてこれを書いていましたが、 –

関連する問題