下面列出了javax.xml.bind.annotation.XmlMixed#com.sun.codemodel.internal.JClass 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) {
JCodeModel cm = outline().getCodeModel();
JClass elementType = ei.toType(outline(),EXPOSED).boxify();
// [RESULT]
// $t = new ArrayList();
// for( Type e : $var ) {
// $var.add(new JAXBElement(e));
// }
// [core.fromRawValue]
JClass col = cm.ref(ArrayList.class).narrow(elementType);
JVar $t = block.decl(col,uniqueName+"_col",JExpr._new(col));
JForEach loop = block.forEach(itemType(), uniqueName+"_i", $t);
loop.body().invoke($var,"add").arg(createJAXBElement(loop.var()));
acc.fromRawValue(block, uniqueName, $t);
}
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) {
JCodeModel cm = outline().getCodeModel();
JClass elementType = ei.toType(outline(),EXPOSED).boxify();
// [RESULT]
// $t = new ArrayList();
// for( Type e : $var ) {
// $var.add(new JAXBElement(e));
// }
// [core.fromRawValue]
JClass col = cm.ref(ArrayList.class).narrow(elementType);
JVar $t = block.decl(col,uniqueName+"_col",JExpr._new(col));
JForEach loop = block.forEach(itemType(), uniqueName+"_i", $t);
loop.body().invoke($var,"add").arg(createJAXBElement(loop.var()));
acc.fromRawValue(block, uniqueName, $t);
}
/** Gets the xjc:superClass customization if it's turned on. */
public JClass getSuperClass() {
Element sc = DOMUtil.getElement(dom,XJC_NS,"superClass");
if (sc == null) return null;
JDefinedClass c;
try {
String v = DOMUtil.getAttribute(sc,"name");
if(v==null) return null;
c = codeModel._class(v);
c.hide();
} catch (JClassAlreadyExistsException e) {
c = e.getExistingClass();
}
return c;
}
/**
* Generates the minimum {@link JDefinedClass} skeleton
* without filling in its body.
*/
private ClassOutlineImpl generateClassDef(CClassInfo bean) {
ImplStructureStrategy.Result r = model.strategy.createClasses(this, bean);
JClass implRef;
if (bean.getUserSpecifiedImplClass() != null) {
// create a place holder for a user-specified class.
JDefinedClass usr;
try {
usr = codeModel._class(bean.getUserSpecifiedImplClass());
// but hide that file so that it won't be generated.
usr.hide();
} catch (JClassAlreadyExistsException e) {
// it's OK for this to collide.
usr = e.getExistingClass();
}
usr._extends(r.implementation);
implRef = usr;
} else {
implRef = r.implementation;
}
return new ClassOutlineImpl(this, bean, r.exposed, r.implementation, implRef);
}
public JClass generateStaticClass(Class src, JPackage out) {
String shortName = getShortName(src.getName());
// some people didn't like our jars to contain files with .java extension,
// so when we build jars, we'' use ".java_". But when we run from the workspace,
// we want the original source code to be used, so we check both here.
// see bug 6211503.
URL res = src.getResource(shortName + ".java");
if (res == null) {
res = src.getResource(shortName + ".java_");
}
if (res == null) {
throw new InternalError("Unable to load source code of " + src.getName() + " as a resource");
}
JStaticJavaFile sjf = new JStaticJavaFile(out, shortName, res, null);
out.addResourceFile(sjf);
return sjf.getJClass();
}
/** Gets the xjc:superClass customization if it's turned on. */
public JClass getSuperClass() {
Element sc = DOMUtil.getElement(dom,XJC_NS,"superClass");
if (sc == null) return null;
JDefinedClass c;
try {
String v = DOMUtil.getAttribute(sc,"name");
if(v==null) return null;
c = codeModel._class(v);
c.hide();
} catch (JClassAlreadyExistsException e) {
c = e.getExistingClass();
}
return c;
}
public void toRawValue(JBlock block, JVar $var) {
JCodeModel cm = outline().getCodeModel();
JClass elementType = ei.toType(outline(),EXPOSED).boxify();
// [RESULT]
// $var = new ArrayList();
// for( JAXBElement e : [core.toRawValue] ) {
// if(e==null)
// $var.add(null);
// else
// $var.add(e.getValue());
// }
block.assign($var,JExpr._new(cm.ref(ArrayList.class).narrow(itemType().boxify())));
JVar $col = block.decl(core.getRawType(), "col" + hashCode());
acc.toRawValue(block,$col);
JForEach loop = block.forEach(elementType, "v" + hashCode()/*unique string handling*/, $col);
JConditional cond = loop.body()._if(loop.var().eq(JExpr._null()));
cond._then().invoke($var,"add").arg(JExpr._null());
cond._else().invoke($var,"add").arg(loop.var().invoke("getValue"));
}
static NClass getRef( final Class<? extends XmlAdapter> adapter, boolean copy ) {
if(copy) {
// TODO: this is a hack. the code generation should be defered until
// the backend. (right now constant generation happens in the front-end)
return new EagerNClass(adapter) {
@Override
public JClass toType(Outline o, Aspect aspect) {
return o.addRuntime(adapter);
}
public String fullName() {
// TODO: implement this method later
throw new UnsupportedOperationException();
}
};
} else {
return NavigatorImpl.theInstance.ref(adapter);
}
}
public JClass generateStaticClass(Class src, JPackage out) {
String shortName = getShortName(src.getName());
// some people didn't like our jars to contain files with .java extension,
// so when we build jars, we'' use ".java_". But when we run from the workspace,
// we want the original source code to be used, so we check both here.
// see bug 6211503.
URL res = src.getResource(shortName + ".java");
if (res == null) {
res = src.getResource(shortName + ".java_");
}
if (res == null) {
throw new InternalError("Unable to load source code of " + src.getName() + " as a resource");
}
JStaticJavaFile sjf = new JStaticJavaFile(out, shortName, res, null);
out.addResourceFile(sjf);
return sjf.getJClass();
}
public JavaType getResponseBeanJavaType(){
JCodeModel cm = _responseBean.getJavaType().getType().getType().owner();
if(_asyncOpType.equals(AsyncOperationType.CALLBACK)){
JClass future = cm.ref(java.util.concurrent.Future.class).narrow(cm.ref(Object.class).wildcard());
return new JavaSimpleType(new JAXBTypeAndAnnotation(future));
}else if(_asyncOpType.equals(AsyncOperationType.POLLING)){
JClass polling = cm.ref(javax.xml.ws.Response.class).narrow(_responseBean.getJavaType().getType().getType().boxify());
return new JavaSimpleType(new JAXBTypeAndAnnotation(polling));
}
return null;
}
public List<JClass> getAllObjectFactories() {
List<JClass> r = new ArrayList<JClass>();
for (PackageOutline pkg : outline.getAllPackageContexts()) {
r.add(pkg.objectFactory());
}
return r;
}
private void dump( ClassOutline ci ) throws IOException {
JDefinedClass cls = ci.implClass;
StringBuilder buf = new StringBuilder();
buf.append("interface ");
buf.append(cls.name());
boolean first=true;
Iterator itr = cls._implements();
while(itr.hasNext()) {
if(first) {
buf.append(" extends ");
first=false;
} else {
buf.append(", ");
}
buf.append( printName((JClass)itr.next()) );
}
buf.append(" {");
println(buf.toString());
indent++;
// dump the field
for( FieldOutline fo : ci.getDeclaredFields() ) {
String type = printName(fo.getRawType());
println(type+' '+fo.getPropertyInfo().getName(true)+';');
}
dumpChildren(cls);
indent--;
println("}");
}
private void dump( ClassOutline ci ) throws IOException {
JDefinedClass cls = ci.implClass;
StringBuilder buf = new StringBuilder();
buf.append("interface ");
buf.append(cls.name());
boolean first=true;
Iterator itr = cls._implements();
while(itr.hasNext()) {
if(first) {
buf.append(" extends ");
first=false;
} else {
buf.append(", ");
}
buf.append( printName((JClass)itr.next()) );
}
buf.append(" {");
println(buf.toString());
indent++;
// dump the field
for( FieldOutline fo : ci.getDeclaredFields() ) {
String type = printName(fo.getRawType());
println(type+' '+fo.getPropertyInfo().getName(true)+';');
}
dumpChildren(cls);
indent--;
println("}");
}
public JClass toType(Outline o, Aspect aspect) {
JClass r = rawType.toType(o,aspect);
for( NType arg : args )
r = r.narrow(arg.toType(o,aspect).boxify());
return r;
}
/**
* 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);
}
private void dump( ClassOutline ci ) throws IOException {
JDefinedClass cls = ci.implClass;
StringBuilder buf = new StringBuilder();
buf.append("interface ");
buf.append(cls.name());
boolean first=true;
Iterator itr = cls._implements();
while(itr.hasNext()) {
if(first) {
buf.append(" extends ");
first=false;
} else {
buf.append(", ");
}
buf.append( printName((JClass)itr.next()) );
}
buf.append(" {");
println(buf.toString());
indent++;
// dump the field
for( FieldOutline fo : ci.getDeclaredFields() ) {
String type = printName(fo.getRawType());
println(type+' '+fo.getPropertyInfo().getName(true)+';');
}
dumpChildren(cls);
indent--;
println("}");
}
/**
* @param coreList
* A concrete class that implements the List interface.
* An instance of this class will be used to store data
* for this field.
*/
protected UntypedListField(ClassOutlineImpl context, CPropertyInfo prop, JClass coreList) {
// the JAXB runtime picks ArrayList if the signature is List,
// so don't do eager allocation if it's ArrayList.
// otherwise we need to do eager allocation so that the collection type specified by the user
// will be used.
super(context, prop, !coreList.fullName().equals("java.util.ArrayList"));
this.coreList = coreList.narrow(exposedType.boxify());
generate();
}
public final JClass addRuntime(Class clazz) {
JClass g = generatedRuntime.get(clazz);
if (g == null) {
// put code into a separate package to avoid name conflicts.
JPackage implPkg = getUsedPackages(Aspect.IMPLEMENTATION)[0].subPackage("runtime");
g = generateStaticClass(clazz, implPkg);
generatedRuntime.put(clazz, g);
}
return g;
}
private static JClass pickOne(Set<JClass> s) {
// we may have more than one candidates at this point.
// any user-defined generated types should have
// precedence over system-defined existing types.
//
// so try to return such a type if any.
for (JClass c : s)
if (c instanceof JDefinedClass)
return c;
// we can do more if we like. for example,
// we can avoid types in the RI runtime.
// but for now, just return the first one.
return s.iterator().next();
}
public JExpression createConstant(Outline outline, XmlString literal) {
// correctly identifying which constant it maps to is hard, so
// here I'm cheating
JClass type = toType(outline,Aspect.EXPOSED);
for (CEnumConstant mem : members) {
if(mem.getLexicalValue().equals(literal.value))
return type.staticRef(mem.getName());
}
return null;
}
/**
* @param coreList
* A concrete class that implements the List interface.
* An instance of this class will be used to store data
* for this field.
*/
protected UntypedListField(ClassOutlineImpl context, CPropertyInfo prop, JClass coreList) {
// the JAXB runtime picks ArrayList if the signature is List,
// so don't do eager allocation if it's ArrayList.
// otherwise we need to do eager allocation so that the collection type specified by the user
// will be used.
super(context, prop, !coreList.fullName().equals("java.util.ArrayList"));
this.coreList = coreList.narrow(exposedType.boxify());
generate();
}
/**
* @param coreList
* A concrete class that implements the List interface.
* An instance of this class will be used to store data
* for this field.
*/
protected DummyListField(ClassOutlineImpl context, CPropertyInfo prop, JClass coreList) {
// the JAXB runtime picks ArrayList if the signature is List,
// so don't do eager allocation if it's ArrayList.
// otherwise we need to do eager allocation so that the collection type specified by the user
// will be used.
super(context, prop, !coreList.fullName().equals("java.util.ArrayList"));
this.coreList = coreList.narrow(exposedType.boxify());
generate();
}
public JavaType getResponseBeanJavaType(){
JCodeModel cm = _responseBean.getJavaType().getType().getType().owner();
if(_asyncOpType.equals(AsyncOperationType.CALLBACK)){
JClass future = cm.ref(java.util.concurrent.Future.class).narrow(cm.ref(Object.class).wildcard());
return new JavaSimpleType(new JAXBTypeAndAnnotation(future));
}else if(_asyncOpType.equals(AsyncOperationType.POLLING)){
JClass polling = cm.ref(javax.xml.ws.Response.class).narrow(_responseBean.getJavaType().getType().getType().boxify());
return new JavaSimpleType(new JAXBTypeAndAnnotation(polling));
}
return null;
}
/**
* Gets the type of this property, if any.
* <p>
* <element-ref> particle doesn't have the type.
*
* @return
* null if none is specified.
*/
public final JClass getType() {
try {
String type = DOMUtil.getAttribute(element,"supertype");
if(type==null) return null;
// TODO: does this attribute defaults to the current package?
int idx = type.lastIndexOf('.');
if(idx<0) return parent.parent.codeModel.ref(type);
else return parent.parent.getTargetPackage().ref(type);
} catch( ClassNotFoundException e ) {
// TODO: better error handling
throw new NoClassDefFoundError(e.getMessage());
}
}
private void dump( ClassOutline ci ) throws IOException {
JDefinedClass cls = ci.implClass;
StringBuilder buf = new StringBuilder();
buf.append("interface ");
buf.append(cls.name());
boolean first=true;
Iterator itr = cls._implements();
while(itr.hasNext()) {
if(first) {
buf.append(" extends ");
first=false;
} else {
buf.append(", ");
}
buf.append( printName((JClass)itr.next()) );
}
buf.append(" {");
println(buf.toString());
indent++;
// dump the field
for( FieldOutline fo : ci.getDeclaredFields() ) {
String type = printName(fo.getRawType());
println(type+' '+fo.getPropertyInfo().getName(true)+';');
}
dumpChildren(cls);
indent--;
println("}");
}
public final JClass toType(Outline o, Aspect aspect) {
switch(aspect) {
case IMPLEMENTATION:
return o.getClazz(this).implRef;
case EXPOSED:
return o.getClazz(this).ref;
default:
throw new IllegalStateException();
}
}
/**
* @param coreList
* A concrete class that implements the List interface.
* An instance of this class will be used to store data
* for this field.
*/
protected NoExtendedContentField(ClassOutlineImpl context, CPropertyInfo prop, JClass coreList) {
// the JAXB runtime picks ArrayList if the signature is List,
// so don't do eager allocation if it's ArrayList.
// otherwise we need to do eager allocation so that the collection type specified by the user
// will be used.
super(context, prop, false);
this.coreList = coreList;
generate();
}
public JExpression createConstant(Outline outline, XmlString literal) {
// correctly identifying which constant it maps to is hard, so
// here I'm cheating
JClass type = toType(outline,Aspect.EXPOSED);
for (CEnumConstant mem : members) {
if(mem.getLexicalValue().equals(literal.value))
return type.staticRef(mem.getName());
}
return null;
}
public JavaType getCallBackType(){
if(_asyncOpType.equals(AsyncOperationType.CALLBACK)){
JCodeModel cm = _responseBean.getJavaType().getType().getType().owner();
JClass cb = cm.ref(javax.xml.ws.AsyncHandler.class).narrow(_responseBean.getJavaType().getType().getType().boxify());
return new JavaSimpleType(new JAXBTypeAndAnnotation(cb));
}
return null;
}