java - How to generate an anonymous class using sun-codemodel? -
i'm trying generate code below using codemodel, have no idea how create anonymous class syntax. can't find in codemodel documentation (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(); } } };
i can use code below instantiate variable "contentproducer" don't know how implement method writeto next constructor.
jclass contentproducer = events.ref(org.apache.http.entity.contentproducer.class); jvar contentproducer = tryblock.body().decl(contentproducer, "contentproducer").init(jexpr._new(contentproducer));
assuming can fill in writeto()
method body, need implement class anonymous inner class so:
jdefinedclass anonymouscontentproducer = codemodel.anonymousclass(contentproducer.class); jblock methodbody = anonymouscontentproducer.method(jmod.public, codemodel.void, "writeto").body(); body.decl(anonymouscontentproducer, "contentproducer", jexpr._new(anonymouscontentproducer));
which outputs:
contentproducer contentproducer = new contentproducer() { public void writeto() { } }
Comments
Post a Comment