下面列出了怎么用 com.sun.codemodel.internal.JMethod 的API类实例代码及写法,或者点击链接到github查看源代码。
private void writeMember(JDefinedClass cls, TypeMirror paramType,
String paramName) {
if (cls == null)
return;
String accessorName =BindingHelper.mangleNameToPropertyName(paramName);
String getterPrefix = paramType.toString().equals("boolean")? "is" : "get";
JType propType = getType(paramType);
JMethod m = cls.method(JMod.PUBLIC, propType, getterPrefix+ accessorName);
JDocComment methodDoc = m.javadoc();
JCommentPart ret = methodDoc.addReturn();
ret.add("returns "+propType.name());
JBlock body = m.body();
body._return( JExpr._this().ref(paramName) );
m = cls.method(JMod.PUBLIC, cm.VOID, "set"+accessorName);
JVar param = m.param(propType, paramName);
methodDoc = m.javadoc();
JCommentPart part = methodDoc.addParam(paramName);
part.add("the value for the "+ paramName+" property");
body = m.body();
body.assign( JExpr._this().ref(paramName), param );
}
private void writeDefaultGetPort(Port port, JType retType, JDefinedClass cls) {
String portGetter = port.getPortGetter();
JMethod m = cls.method(JMod.PUBLIC, retType, portGetter);
JDocComment methodDoc = m.javadoc();
if (port.getJavaDoc() != null) {
methodDoc.add(port.getJavaDoc());
}
JCommentPart ret = methodDoc.addReturn();
ret.add("returns " + retType.name());
JBlock body = m.body();
StringBuilder statement = new StringBuilder("return ");
statement.append("super.getPort(new QName(\"").append(port.getName().getNamespaceURI()).append("\", \"").append(port.getName().getLocalPart()).append("\"), ");
statement.append(retType.name());
statement.append(".class);");
body.directStatement(statement.toString());
writeWebEndpoint(port, m);
}
public boolean run(
Outline outline,
Options opt,
ErrorHandler errorHandler ) {
for( ClassOutline ci : outline.getClasses() ) {
JDefinedClass impl = ci.implClass;
if (ci.getSuperClass() == null) {
JVar $loc = impl.field(JMod.PROTECTED, Locator.class, fieldName);
$loc.annotate(XmlLocation.class);
$loc.annotate(XmlTransient.class);
impl._implements(Locatable.class);
impl.method(JMod.PUBLIC, Locator.class, "sourceLocation").body()._return($loc);
JMethod setter = impl.method(JMod.PUBLIC, Void.TYPE, "setSourceLocation");
JVar $newLoc = setter.param(Locator.class, "newLocator");
setter.body().assign($loc, $newLoc);
}
}
return true;
}
private void writeMember(JDefinedClass cls, TypeMirror paramType,
String paramName) {
if (cls == null)
return;
String accessorName =BindingHelper.mangleNameToPropertyName(paramName);
String getterPrefix = paramType.toString().equals("boolean")? "is" : "get";
JType propType = getType(paramType);
JMethod m = cls.method(JMod.PUBLIC, propType, getterPrefix+ accessorName);
JDocComment methodDoc = m.javadoc();
JCommentPart ret = methodDoc.addReturn();
ret.add("returns "+propType.name());
JBlock body = m.body();
body._return( JExpr._this().ref(paramName) );
m = cls.method(JMod.PUBLIC, cm.VOID, "set"+accessorName);
JVar param = m.param(propType, paramName);
methodDoc = m.javadoc();
JCommentPart part = methodDoc.addParam(paramName);
part.add("the value for the "+ paramName+" property");
body = m.body();
body.assign( JExpr._this().ref(paramName), param );
}
public boolean run(
Outline outline,
Options opt,
ErrorHandler errorHandler ) {
for( ClassOutline ci : outline.getClasses() ) {
JDefinedClass impl = ci.implClass;
if (ci.getSuperClass() == null) {
JVar $loc = impl.field(JMod.PROTECTED, Locator.class, fieldName);
$loc.annotate(XmlLocation.class);
$loc.annotate(XmlTransient.class);
impl._implements(Locatable.class);
impl.method(JMod.PUBLIC, Locator.class, "sourceLocation").body()._return($loc);
JMethod setter = impl.method(JMod.PUBLIC, Void.TYPE, "setSourceLocation");
JVar $newLoc = setter.param(Locator.class, "newLocator");
setter.body().assign($loc, $newLoc);
}
}
return true;
}
private void writeGetPort(Port port, JType retType, JDefinedClass cls) {
JMethod m = cls.method(JMod.PUBLIC, retType, port.getPortGetter());
JDocComment methodDoc = m.javadoc();
if (port.getJavaDoc() != null) {
methodDoc.add(port.getJavaDoc());
}
JCommentPart ret = methodDoc.addReturn();
JCommentPart paramDoc = methodDoc.addParam("features");
paramDoc.append("A list of ");
paramDoc.append("{@link " + WebServiceFeature.class.getName() + "}");
paramDoc.append("to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.");
ret.add("returns " + retType.name());
m.varParam(WebServiceFeature.class, "features");
JBlock body = m.body();
StringBuilder statement = new StringBuilder("return ");
statement.append("super.getPort(new QName(\"").append(port.getName().getNamespaceURI()).append("\", \"").append(port.getName().getLocalPart()).append("\"), ");
statement.append(retType.name());
statement.append(".class, features);");
body.directStatement(statement.toString());
writeWebEndpoint(port, m);
}
private void writeMember(JDefinedClass cls, TypeMirror paramType,
String paramName) {
if (cls == null)
return;
String accessorName =BindingHelper.mangleNameToPropertyName(paramName);
String getterPrefix = paramType.toString().equals("boolean")? "is" : "get";
JType propType = getType(paramType);
JMethod m = cls.method(JMod.PUBLIC, propType, getterPrefix+ accessorName);
JDocComment methodDoc = m.javadoc();
JCommentPart ret = methodDoc.addReturn();
ret.add("returns "+propType.name());
JBlock body = m.body();
body._return( JExpr._this().ref(paramName) );
m = cls.method(JMod.PUBLIC, cm.VOID, "set"+accessorName);
JVar param = m.param(propType, paramName);
methodDoc = m.javadoc();
JCommentPart part = methodDoc.addParam(paramName);
part.add("the value for the "+ paramName+" property");
body = m.body();
body.assign( JExpr._this().ref(paramName), param );
}
public boolean run(
Outline outline,
Options opt,
ErrorHandler errorHandler ) {
for( ClassOutline ci : outline.getClasses() ) {
JDefinedClass impl = ci.implClass;
if (ci.getSuperClass() == null) {
JVar $loc = impl.field(JMod.PROTECTED, Locator.class, fieldName);
$loc.annotate(XmlLocation.class);
$loc.annotate(XmlTransient.class);
impl._implements(Locatable.class);
impl.method(JMod.PUBLIC, Locator.class, "sourceLocation").body()._return($loc);
JMethod setter = impl.method(JMod.PUBLIC, Void.TYPE, "setSourceLocation");
JVar $newLoc = setter.param(Locator.class, "newLocator");
setter.body().assign($loc, $newLoc);
}
}
return true;
}
public boolean run(
Outline outline,
Options opt,
ErrorHandler errorHandler ) {
for( ClassOutline ci : outline.getClasses() ) {
JDefinedClass impl = ci.implClass;
if (ci.getSuperClass() == null) {
JVar $loc = impl.field(JMod.PROTECTED, Locator.class, fieldName);
$loc.annotate(XmlLocation.class);
$loc.annotate(XmlTransient.class);
impl._implements(Locatable.class);
impl.method(JMod.PUBLIC, Locator.class, "sourceLocation").body()._return($loc);
JMethod setter = impl.method(JMod.PUBLIC, Void.TYPE, "setSourceLocation");
JVar $newLoc = setter.param(Locator.class, "newLocator");
setter.body().assign($loc, $newLoc);
}
}
return true;
}
private void writeDefaultGetPort(Port port, JType retType, JDefinedClass cls) {
String portGetter = port.getPortGetter();
JMethod m = cls.method(JMod.PUBLIC, retType, portGetter);
JDocComment methodDoc = m.javadoc();
if (port.getJavaDoc() != null) {
methodDoc.add(port.getJavaDoc());
}
JCommentPart ret = methodDoc.addReturn();
ret.add("returns " + retType.name());
JBlock body = m.body();
StringBuilder statement = new StringBuilder("return ");
statement.append("super.getPort(new QName(\"").append(port.getName().getNamespaceURI()).append("\", \"").append(port.getName().getLocalPart()).append("\"), ");
statement.append(retType.name());
statement.append(".class);");
body.directStatement(statement.toString());
writeWebEndpoint(port, m);
}
public boolean run(
Outline outline,
Options opt,
ErrorHandler errorHandler ) {
for( ClassOutline ci : outline.getClasses() ) {
JDefinedClass impl = ci.implClass;
if (ci.getSuperClass() == null) {
JVar $loc = impl.field(JMod.PROTECTED, Locator.class, fieldName);
$loc.annotate(XmlLocation.class);
$loc.annotate(XmlTransient.class);
impl._implements(Locatable.class);
impl.method(JMod.PUBLIC, Locator.class, "sourceLocation").body()._return($loc);
JMethod setter = impl.method(JMod.PUBLIC, Void.TYPE, "setSourceLocation");
JVar $newLoc = setter.param(Locator.class, "newLocator");
setter.body().assign($loc, $newLoc);
}
}
return true;
}
public boolean run(
Outline outline,
Options opt,
ErrorHandler errorHandler ) {
for( ClassOutline ci : outline.getClasses() ) {
JDefinedClass impl = ci.implClass;
if (ci.getSuperClass() == null) {
JVar $loc = impl.field(JMod.PROTECTED, Locator.class, fieldName);
$loc.annotate(XmlLocation.class);
$loc.annotate(XmlTransient.class);
impl._implements(Locatable.class);
impl.method(JMod.PUBLIC, Locator.class, "sourceLocation").body()._return($loc);
JMethod setter = impl.method(JMod.PUBLIC, Void.TYPE, "setSourceLocation");
JVar $newLoc = setter.param(Locator.class, "newLocator");
setter.body().assign($loc, $newLoc);
}
}
return true;
}
ElementOutlineImpl(BeanGenerator parent, CElementInfo ei) {
super(ei,
parent.getClassFactory().createClass(
parent.getContainer( ei.parent, Aspect.EXPOSED ), ei.shortName(), ei.getLocator() ));
this.parent = parent;
parent.elements.put(ei,this);
JCodeModel cm = parent.getCodeModel();
implClass._extends(
cm.ref(JAXBElement.class).narrow(
target.getContentInMemoryType().toType(parent,Aspect.EXPOSED).boxify()));
if(ei.hasClass()) {
JType implType = ei.getContentInMemoryType().toType(parent,Aspect.IMPLEMENTATION);
JExpression declaredType = JExpr.cast(cm.ref(Class.class),implType.boxify().dotclass()); // why do we have to cast?
JClass scope=null;
if(ei.getScope()!=null)
scope = parent.getClazz(ei.getScope()).implRef;
JExpression scopeClass = scope==null?JExpr._null():scope.dotclass();
JFieldVar valField = implClass.field(JMod.PROTECTED|JMod.FINAL|JMod.STATIC,QName.class,"NAME",createQName(cm,ei.getElementName()));
// take this opportunity to generate a constructor in the element class
JMethod cons = implClass.constructor(JMod.PUBLIC);
cons.body().invoke("super")
.arg(valField)
.arg(declaredType)
.arg(scopeClass)
.arg(cons.param(implType,"value"));
// generate no-arg constructor in the element class (bug #391; section 5.6.2 in JAXB spec 2.1)
JMethod noArgCons = implClass.constructor(JMod.PUBLIC);
noArgCons.body().invoke("super")
.arg(valField)
.arg(declaredType)
.arg(scopeClass)
.arg(JExpr._null());
}
}
ElementOutlineImpl(BeanGenerator parent, CElementInfo ei) {
super(ei,
parent.getClassFactory().createClass(
parent.getContainer( ei.parent, Aspect.EXPOSED ), ei.shortName(), ei.getLocator() ));
this.parent = parent;
parent.elements.put(ei,this);
JCodeModel cm = parent.getCodeModel();
implClass._extends(
cm.ref(JAXBElement.class).narrow(
target.getContentInMemoryType().toType(parent,Aspect.EXPOSED).boxify()));
if(ei.hasClass()) {
JType implType = ei.getContentInMemoryType().toType(parent,Aspect.IMPLEMENTATION);
JExpression declaredType = JExpr.cast(cm.ref(Class.class),implType.boxify().dotclass()); // why do we have to cast?
JClass scope=null;
if(ei.getScope()!=null)
scope = parent.getClazz(ei.getScope()).implRef;
JExpression scopeClass = scope==null?JExpr._null():scope.dotclass();
JFieldVar valField = implClass.field(JMod.PROTECTED|JMod.FINAL|JMod.STATIC,QName.class,"NAME",createQName(cm,ei.getElementName()));
// take this opportunity to generate a constructor in the element class
JMethod cons = implClass.constructor(JMod.PUBLIC);
cons.body().invoke("super")
.arg(valField)
.arg(declaredType)
.arg(scopeClass)
.arg(cons.param(implType,"value"));
// generate no-arg constructor in the element class (bug #391; section 5.6.2 in JAXB spec 2.1)
JMethod noArgCons = implClass.constructor(JMod.PUBLIC);
noArgCons.body().invoke("super")
.arg(valField)
.arg(declaredType)
.arg(scopeClass)
.arg(JExpr._null());
}
}
/**
* Generates an attribute wildcard property on a class.
*/
private void generateAttributeWildcard(ClassOutlineImpl cc) {
String FIELD_NAME = "otherAttributes";
String METHOD_SEED = model.getNameConverter().toClassName(FIELD_NAME);
JClass mapType = codeModel.ref(Map.class).narrow(QName.class, String.class);
JClass mapImpl = codeModel.ref(HashMap.class).narrow(QName.class, String.class);
// [RESULT]
// Map<QName,String> m = new HashMap<QName,String>();
JFieldVar $ref = cc.implClass.field(JMod.PRIVATE,
mapType, FIELD_NAME, JExpr._new(mapImpl));
$ref.annotate2(XmlAnyAttributeWriter.class);
MethodWriter writer = cc.createMethodWriter();
JMethod $get = writer.declareMethod(mapType, "get" + METHOD_SEED);
$get.javadoc().append(
"Gets a map that contains attributes that aren't bound to any typed property on this class.\n\n"
+ "<p>\n"
+ "the map is keyed by the name of the attribute and \n"
+ "the value is the string value of the attribute.\n"
+ "\n"
+ "the map returned by this method is live, and you can add new attribute\n"
+ "by updating the map directly. Because of this design, there's no setter.\n");
$get.javadoc().addReturn().append("always non-null");
$get.body()._return($ref);
}
public ObjectFactoryGeneratorImpl( BeanGenerator outline, Model model, JPackage targetPackage ) {
this.outline = outline;
this.model = model;
this.codeModel = this.model.codeModel;
this.classRef = codeModel.ref(Class.class);
// create the ObjectFactory class skeleton
objectFactory = this.outline.getClassFactory().createClass(
targetPackage, "ObjectFactory", null );
objectFactory.annotate2(XmlRegistryWriter.class);
// generate the default constructor
//
// m1 result:
// public ObjectFactory() {}
JMethod m1 = objectFactory.constructor(JMod.PUBLIC);
m1.javadoc().append("Create a new ObjectFactory that can be used to " +
"create new instances of schema derived classes " +
"for package: " + targetPackage.name());
// add some class javadoc
objectFactory.javadoc().append(
"This object contains factory methods for each \n" +
"Java content interface and Java element interface \n" +
"generated in the " + targetPackage.name() + " package. \n" +
"<p>An ObjectFactory allows you to programatically \n" +
"construct new instances of the Java representation \n" +
"for XML content. The Java representation of XML \n" +
"content can consist of schema derived interfaces \n" +
"and classes representing the binding of schema \n" +
"type definitions, element declarations and model \n" +
"groups. Factory methods for each of these are \n" +
"provided in this class." );
}
/**
* Generates an attribute wildcard property on a class.
*/
private void generateAttributeWildcard(ClassOutlineImpl cc) {
String FIELD_NAME = "otherAttributes";
String METHOD_SEED = model.getNameConverter().toClassName(FIELD_NAME);
JClass mapType = codeModel.ref(Map.class).narrow(QName.class, String.class);
JClass mapImpl = codeModel.ref(HashMap.class).narrow(QName.class, String.class);
// [RESULT]
// Map<QName,String> m = new HashMap<QName,String>();
JFieldVar $ref = cc.implClass.field(JMod.PRIVATE,
mapType, FIELD_NAME, JExpr._new(mapImpl));
$ref.annotate2(XmlAnyAttributeWriter.class);
MethodWriter writer = cc.createMethodWriter();
JMethod $get = writer.declareMethod(mapType, "get" + METHOD_SEED);
$get.javadoc().append(
"Gets a map that contains attributes that aren't bound to any typed property on this class.\n\n"
+ "<p>\n"
+ "the map is keyed by the name of the attribute and \n"
+ "the value is the string value of the attribute.\n"
+ "\n"
+ "the map returned by this method is live, and you can add new attribute\n"
+ "by updating the map directly. Because of this design, there's no setter.\n");
$get.javadoc().addReturn().append("always non-null");
$get.body()._return($ref);
}
/**
* Adds "@Generated" to the classes, methods, and fields.
*/
private void augument(ClassOutline co) {
annotate(co.implClass);
for (JMethod m : co.implClass.methods())
annotate(m);
for (JFieldVar f : co.implClass.fields().values())
annotate(f);
}
/**
* Adds "@Generated" to the classes, methods, and fields.
*/
private void augument(ClassOutline co) {
annotate(co.implClass);
for (JMethod m : co.implClass.methods())
annotate(m);
for (JFieldVar f : co.implClass.fields().values())
annotate(f);
}
/**
* Generates an attribute wildcard property on a class.
*/
private void generateAttributeWildcard(ClassOutlineImpl cc) {
String FIELD_NAME = "otherAttributes";
String METHOD_SEED = model.getNameConverter().toClassName(FIELD_NAME);
JClass mapType = codeModel.ref(Map.class).narrow(QName.class, String.class);
JClass mapImpl = codeModel.ref(HashMap.class).narrow(QName.class, String.class);
// [RESULT]
// Map<QName,String> m = new HashMap<QName,String>();
JFieldVar $ref = cc.implClass.field(JMod.PRIVATE,
mapType, FIELD_NAME, JExpr._new(mapImpl));
$ref.annotate2(XmlAnyAttributeWriter.class);
MethodWriter writer = cc.createMethodWriter();
JMethod $get = writer.declareMethod(mapType, "get" + METHOD_SEED);
$get.javadoc().append(
"Gets a map that contains attributes that aren't bound to any typed property on this class.\n\n"
+ "<p>\n"
+ "the map is keyed by the name of the attribute and \n"
+ "the value is the string value of the attribute.\n"
+ "\n"
+ "the map returned by this method is live, and you can add new attribute\n"
+ "by updating the map directly. Because of this design, there's no setter.\n");
$get.javadoc().addReturn().append("always non-null");
$get.body()._return($ref);
}
ElementOutlineImpl(BeanGenerator parent, CElementInfo ei) {
super(ei,
parent.getClassFactory().createClass(
parent.getContainer( ei.parent, Aspect.EXPOSED ), ei.shortName(), ei.getLocator() ));
this.parent = parent;
parent.elements.put(ei,this);
JCodeModel cm = parent.getCodeModel();
implClass._extends(
cm.ref(JAXBElement.class).narrow(
target.getContentInMemoryType().toType(parent,Aspect.EXPOSED).boxify()));
if(ei.hasClass()) {
JType implType = ei.getContentInMemoryType().toType(parent,Aspect.IMPLEMENTATION);
JExpression declaredType = JExpr.cast(cm.ref(Class.class),implType.boxify().dotclass()); // why do we have to cast?
JClass scope=null;
if(ei.getScope()!=null)
scope = parent.getClazz(ei.getScope()).implRef;
JExpression scopeClass = scope==null?JExpr._null():scope.dotclass();
JFieldVar valField = implClass.field(JMod.PROTECTED|JMod.FINAL|JMod.STATIC,QName.class,"NAME",createQName(cm,ei.getElementName()));
// take this opportunity to generate a constructor in the element class
JMethod cons = implClass.constructor(JMod.PUBLIC);
cons.body().invoke("super")
.arg(valField)
.arg(declaredType)
.arg(scopeClass)
.arg(cons.param(implType,"value"));
// generate no-arg constructor in the element class (bug #391; section 5.6.2 in JAXB spec 2.1)
JMethod noArgCons = implClass.constructor(JMod.PUBLIC);
noArgCons.body().invoke("super")
.arg(valField)
.arg(declaredType)
.arg(scopeClass)
.arg(JExpr._null());
}
}
/**
* Adds "@Generated" to the classes, methods, and fields.
*/
private void augument(ClassOutline co) {
annotate(co.implClass);
for (JMethod m : co.implClass.methods())
annotate(m);
for (JFieldVar f : co.implClass.fields().values())
annotate(f);
}
/**
* Adds "@Generated" to the classes, methods, and fields.
*/
private void augument(ClassOutline co) {
annotate(co.implClass);
for (JMethod m : co.implClass.methods())
annotate(m);
for (JFieldVar f : co.implClass.fields().values())
annotate(f);
}
/**
* Generates an attribute wildcard property on a class.
*/
private void generateAttributeWildcard(ClassOutlineImpl cc) {
String FIELD_NAME = "otherAttributes";
String METHOD_SEED = model.getNameConverter().toClassName(FIELD_NAME);
JClass mapType = codeModel.ref(Map.class).narrow(QName.class, String.class);
JClass mapImpl = codeModel.ref(HashMap.class).narrow(QName.class, String.class);
// [RESULT]
// Map<QName,String> m = new HashMap<QName,String>();
JFieldVar $ref = cc.implClass.field(JMod.PRIVATE,
mapType, FIELD_NAME, JExpr._new(mapImpl));
$ref.annotate2(XmlAnyAttributeWriter.class);
MethodWriter writer = cc.createMethodWriter();
JMethod $get = writer.declareMethod(mapType, "get" + METHOD_SEED);
$get.javadoc().append(
"Gets a map that contains attributes that aren't bound to any typed property on this class.\n\n"
+ "<p>\n"
+ "the map is keyed by the name of the attribute and \n"
+ "the value is the string value of the attribute.\n"
+ "\n"
+ "the map returned by this method is live, and you can add new attribute\n"
+ "by updating the map directly. Because of this design, there's no setter.\n");
$get.javadoc().addReturn().append("always non-null");
$get.body()._return($ref);
}
/**
* Generates an attribute wildcard property on a class.
*/
private void generateAttributeWildcard(ClassOutlineImpl cc) {
String FIELD_NAME = "otherAttributes";
String METHOD_SEED = model.getNameConverter().toClassName(FIELD_NAME);
JClass mapType = codeModel.ref(Map.class).narrow(QName.class, String.class);
JClass mapImpl = codeModel.ref(HashMap.class).narrow(QName.class, String.class);
// [RESULT]
// Map<QName,String> m = new HashMap<QName,String>();
JFieldVar $ref = cc.implClass.field(JMod.PRIVATE,
mapType, FIELD_NAME, JExpr._new(mapImpl));
$ref.annotate2(XmlAnyAttributeWriter.class);
MethodWriter writer = cc.createMethodWriter();
JMethod $get = writer.declareMethod(mapType, "get" + METHOD_SEED);
$get.javadoc().append(
"Gets a map that contains attributes that aren't bound to any typed property on this class.\n\n"
+ "<p>\n"
+ "the map is keyed by the name of the attribute and \n"
+ "the value is the string value of the attribute.\n"
+ "\n"
+ "the map returned by this method is live, and you can add new attribute\n"
+ "by updating the map directly. Because of this design, there's no setter.\n");
$get.javadoc().addReturn().append("always non-null");
$get.body()._return($ref);
}
ElementOutlineImpl(BeanGenerator parent, CElementInfo ei) {
super(ei,
parent.getClassFactory().createClass(
parent.getContainer( ei.parent, Aspect.EXPOSED ), ei.shortName(), ei.getLocator() ));
this.parent = parent;
parent.elements.put(ei,this);
JCodeModel cm = parent.getCodeModel();
implClass._extends(
cm.ref(JAXBElement.class).narrow(
target.getContentInMemoryType().toType(parent,Aspect.EXPOSED).boxify()));
if(ei.hasClass()) {
JType implType = ei.getContentInMemoryType().toType(parent,Aspect.IMPLEMENTATION);
JExpression declaredType = JExpr.cast(cm.ref(Class.class),implType.boxify().dotclass()); // why do we have to cast?
JClass scope=null;
if(ei.getScope()!=null)
scope = parent.getClazz(ei.getScope()).implRef;
JExpression scopeClass = scope==null?JExpr._null():scope.dotclass();
JFieldVar valField = implClass.field(JMod.PROTECTED|JMod.FINAL|JMod.STATIC,QName.class,"NAME",createQName(cm,ei.getElementName()));
// take this opportunity to generate a constructor in the element class
JMethod cons = implClass.constructor(JMod.PUBLIC);
cons.body().invoke("super")
.arg(valField)
.arg(declaredType)
.arg(scopeClass)
.arg(cons.param(implType,"value"));
// generate no-arg constructor in the element class (bug #391; section 5.6.2 in JAXB spec 2.1)
JMethod noArgCons = implClass.constructor(JMod.PUBLIC);
noArgCons.body().invoke("super")
.arg(valField)
.arg(declaredType)
.arg(scopeClass)
.arg(JExpr._null());
}
}
/**
* Adds "@Generated" to the classes, methods, and fields.
*/
private void augument(ClassOutline co) {
annotate(co.implClass);
for (JMethod m : co.implClass.methods())
annotate(m);
for (JFieldVar f : co.implClass.fields().values())
annotate(f);
}
public ObjectFactoryGeneratorImpl( BeanGenerator outline, Model model, JPackage targetPackage ) {
this.outline = outline;
this.model = model;
this.codeModel = this.model.codeModel;
this.classRef = codeModel.ref(Class.class);
// create the ObjectFactory class skeleton
objectFactory = this.outline.getClassFactory().createClass(
targetPackage, "ObjectFactory", null );
objectFactory.annotate2(XmlRegistryWriter.class);
// generate the default constructor
//
// m1 result:
// public ObjectFactory() {}
JMethod m1 = objectFactory.constructor(JMod.PUBLIC);
m1.javadoc().append("Create a new ObjectFactory that can be used to " +
"create new instances of schema derived classes " +
"for package: " + targetPackage.name());
// add some class javadoc
objectFactory.javadoc().append(
"This object contains factory methods for each \n" +
"Java content interface and Java element interface \n" +
"generated in the " + targetPackage.name() + " package. \n" +
"<p>An ObjectFactory allows you to programatically \n" +
"construct new instances of the Java representation \n" +
"for XML content. The Java representation of XML \n" +
"content can consist of schema derived interfaces \n" +
"and classes representing the binding of schema \n" +
"type definitions, element declarations and model \n" +
"groups. Factory methods for each of these are \n" +
"provided in this class." );
}
private void write(Fault fault) throws JClassAlreadyExistsException {
String className = Names.customExceptionClassName(fault);
JDefinedClass cls = cm._class(className, ClassType.CLASS);
JDocComment comment = cls.javadoc();
if(fault.getJavaDoc() != null){
comment.add(fault.getJavaDoc());
comment.add("\n\n");
}
for (String doc : getJAXWSClassComment()) {
comment.add(doc);
}
cls._extends(java.lang.Exception.class);
//@WebFault
JAnnotationUse faultAnn = cls.annotate(WebFault.class);
faultAnn.param("name", fault.getBlock().getName().getLocalPart());
faultAnn.param("targetNamespace", fault.getBlock().getName().getNamespaceURI());
JType faultBean = fault.getBlock().getType().getJavaType().getType().getType();
//faultInfo filed
JFieldVar fi = cls.field(JMod.PRIVATE, faultBean, "faultInfo");
//add jaxb annotations
fault.getBlock().getType().getJavaType().getType().annotate(fi);
fi.javadoc().add("Java type that goes as soapenv:Fault detail element.");
JFieldRef fr = JExpr.ref(JExpr._this(), fi);
//Constructor
JMethod constrc1 = cls.constructor(JMod.PUBLIC);
JVar var1 = constrc1.param(String.class, "message");
JVar var2 = constrc1.param(faultBean, "faultInfo");
constrc1.javadoc().addParam(var1);
constrc1.javadoc().addParam(var2);
JBlock cb1 = constrc1.body();
cb1.invoke("super").arg(var1);
cb1.assign(fr, var2);
//constructor with Throwable
JMethod constrc2 = cls.constructor(JMod.PUBLIC);
var1 = constrc2.param(String.class, "message");
var2 = constrc2.param(faultBean, "faultInfo");
JVar var3 = constrc2.param(Throwable.class, "cause");
constrc2.javadoc().addParam(var1);
constrc2.javadoc().addParam(var2);
constrc2.javadoc().addParam(var3);
JBlock cb2 = constrc2.body();
cb2.invoke("super").arg(var1).arg(var3);
cb2.assign(fr, var2);
//getFaultInfo() method
JMethod fim = cls.method(JMod.PUBLIC, faultBean, "getFaultInfo");
fim.javadoc().addReturn().add("returns fault bean: "+faultBean.fullName());
JBlock fib = fim.body();
fib._return(fi);
fault.setExceptionClass(cls);
}
public void writeMethodAnnotations(TWSDLOperation wsdlOperation, JMethod jMethod) {
for (TJavaGeneratorExtension e : extensions) {
e.writeMethodAnnotations(wsdlOperation, jMethod);
}
}