我正在尝试使用 codemodel 生成下面的代码,但我不知道如何创建匿名类语法。我在代码模型文档中找不到它(https://codemodel.java.net/nonav/apidocs/index.html?com/sun/codemodel)
ContentProducer cp = new ContentProducer() {
public void writeTo(OutputStream outstream) {
try {
Writer writer = new OutputStreamWriter(outstream, "UTF-8");
writer.write("<product><descricao>Update de descricao</descricao></product>");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
};
我可以使用下面的代码来实例化变量“ContentProducer”,但我不知道如何在构造函数旁边实现方法 writeTo。
JClass ContentProducer = events.ref(org.apache.http.entity.ContentProducer.class);
JVar contentProducer = tryBlock.body().decl(ContentProducer, "contentProducer").init(JExpr._new(ContentProducer));