0

コード モデルを使用してコードにクラスをインポートしようとしています。これは私のコードです。

JCodeModel model = new JCodeModel();
JClass mapper = model.directClass("com.another.Mapper");
JDefinedClass dc = model._class("com.example.Something");
JMethod method = dc.method(JMod.PUBLIC | JMod.STATIC, Void.TYPE,
        "testMethod");
JBlock executerBlock = method.body();
    executerBlock.directStatement("Mapper.get()");
File file = new File("./src");
file.mkdirs();
model.build(file);

今、結果として次のクラスを取得しています。

package com.example;
public class Something {
    public static void testMethod() {
    Mapper.get()
    }
}

しかし、実際に必要なのは、

package com.example;
import com.another.Mapper;
public class Something {
    public static void testMethod() {
    Mapper.get()
    }
}

使わないと輸入が来ません。このインポートを行うにはどうすればよいですか。

4

1 に答える 1

0

Mapperをインポートとして追加するには、次の代わりに / を呼び出す必要がありinvoke()ます。staticInvoke()directStatement()

JClass mapper = model.directClass("com.another.Mapper");
JDefinedClass dc = model._class("com.example.Something");
JMethod method = dc.method(JMod.PUBLIC | JMod.STATIC, Void.TYPE, "testMethod");
JBlock executerBlock = method.body();
executerBlock.staticInvoke(mapper, "get");
于 2015-06-14T14:40:30.797 に答える