理想情况下,我想将一些生成的代码添加到ObjectFactory类中。如果那是不可能的,我想在另一个生成的方法中使用生成的ObjectFactory作为参数。
这种情况看起来像这样:
import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.outline.Outline;
import com.sun.tools.xjc.outline.ClassOutline;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
public class App extends Plugin {
...
private static void generateMyMethod(Outline model, ClassOutline classOutline) {
//somehow get 'objectFactoryInstance'
JMethod method = classOutline.implClass.method(JMod.PUBLIC | JMod.FINAL | JMod.STATIC, Void.class, "useObjectFactory")
.param(objectFactoryInstance, "input"));
}
}
,生成的代码应如下所示:
public final static void useObjectFactory(ObjectFactory input) {
//stuff happens here
}
问题是如何从我的XJC插件中访问生成的ObjectFactory?
除了ObjectFactory:
之外,其他生成的类都可以像这样访问@Override
public boolean run(Outline model, Options opt, ErrorHandler errorHandler) throws SAXException {
for (ClassOutline classOutline : model.getClasses()) {
//do something with the generated class
}
}
答案 0 :(得分:0)
我无法访问生成的ObjectFactory类,就像访问其他生成的类一样。所以现在我正在生成代码,假定将生成ObjectFactory。基于这个假设,我用我想要的方法创建另一个类。
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JVar;
import com.sun.tools.xjc.outline.ClassOutline;
//...
public void generateMethodThatUsesObjectFactory(ClassOutline classOutline) {
JMethod method = classOutline.implClass.method(JMod.PUBLIC |
JMod.FINAL, model.getModel().codeModel.VOID, "useObjectFactory");
JVar objectFactory = method.param(model.getModel().codeModel.ref(Object.class), "input");
JExpression objectFactoryCast = JExpr.cast(model.getModel().codeModel.ref("ObjectFactory"), objectFactory);
JBlock body = method.body();
//add the objectFactory and objectFactoryCast to the method body
//...
}
//...
生成的结果与此代码类似:
public final static void useObjectFactory(Object input) {
JAXBElement<SomeType> je = ((ObjectFactory) input).callMethodOfObjectFactory();
//...
}
我不认为这是一种理想的方法,但它有效。为了在某个时间点得到更好的答案,我现在不会接受我自己的答案。
答案 1 :(得分:0)
您无需遍历类,而是遍历包并通过{
field: 'operation',
title: 'Operations',
template: (e) => {
return '<button v-on:click="methodFunction">Click</button>';
}
}
访问工厂。
objectFactory()
顺便说一句,每个@Override
public boolean run(Outline model, Options opt, ErrorHandler errorHandler) throws SAXException {
for (PackageOutline packageOutline : model.getAllPackageContexts()) {
JDefinedClass factory = packageOutline.objectFactory();
//factory may be null for empty packages
}
}
是在xjc处理xml / json / etc时“即时”创建的。如果您在某些生成的ObjectFactory
类中进行了手动类型更改,则需要手动调整对象工厂。