/** * 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(); }
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")); }
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; }
/** Gets the xjc:superInterface customization if it's turned on. */ public JClass getSuperInterface() { Element sc = DOMUtil.getElement(dom,XJC_NS,"superInterface"); if (sc == null) return null; String name = DOMUtil.getAttribute(sc,"name"); if (name == null) return null; JDefinedClass c; try { c = codeModel._class(name, ClassType.INTERFACE); c.hide(); } catch (JClassAlreadyExistsException e) { c = e.getExistingClass(); } return c; }
/** * Returns the set of all classes/interfaces that a given type * implements/extends, including itself. * * For example, if you pass java.io.FilterInputStream, then the returned * set will contain java.lang.Object, java.lang.InputStream, and * java.lang.FilterInputStream. */ private static void getAssignableTypes( JClass t, Set<JClass> s ) { if(!s.add(t)) return; // add its raw type s.add(t.erasure()); // if this type is added for the first time, // recursively process the super class. JClass _super = t._extends(); if(_super!=null) getAssignableTypes(_super,s); // recursively process all implemented interfaces Iterator<JClass> itr = t._implements(); while(itr.hasNext()) getAssignableTypes(itr.next(),s); }
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 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 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; }
/** * @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(); }
/** * @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 ContentListField(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(); }
/** * 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 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; }
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()); } }
public PrivateObjectFactoryGenerator(BeanGenerator outline, Model model, JPackage targetPackage) { super(outline, model, targetPackage.subPackage("impl")); JPackage implPkg = targetPackage.subPackage("impl"); // put JAXBContextFactory into the impl package JClass factory = outline.generateStaticClass(JAXBContextFactory.class,implPkg); // and then put jaxb.properties to point to it JPropertyFile jaxbProperties = new JPropertyFile("jaxb.properties"); targetPackage.addResourceFile(jaxbProperties); jaxbProperties.add( JAXBContext.JAXB_CONTEXT_FACTORY, factory.fullName()); }
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 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; }
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(); } }
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; }
/** * @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 List<JClass> getAllObjectFactories() { List<JClass> r = new ArrayList<JClass>(); for (PackageOutline pkg : outline.getAllPackageContexts()) { r.add(pkg.objectFactory()); } return r; }
/** * Gets the type of this property, if any. * <p> * {@code <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()); } }
@Override public void writeMethodAnnotations(TWSDLOperation two, JMethod jMethod) { JAnnotationUse actionAnn = null; if (!(two instanceof Operation)) return; Operation o = ((Operation)two); // explicit input action if (o.getInput().getAction() != null && !o.getInput().getAction().equals("")) { // explicitly specified actionAnn = jMethod.annotate(Action.class); actionAnn.param("input", o.getInput().getAction()); } // explicit output action if (o.getOutput() != null && o.getOutput().getAction() != null && !o.getOutput().getAction().equals("")) { // explicitly specified if (actionAnn == null) actionAnn = jMethod.annotate(Action.class); actionAnn.param("output", o.getOutput().getAction()); } // explicit fault action if (o.getFaults() != null && o.getFaults().size() > 0) { Map<String, JClass> map = o.getFaults(); JAnnotationArrayMember jam = null; for (Fault f : o.faults()) { if (f.getAction() == null) continue; if (f.getAction().equals("")) continue; if (actionAnn == null) { actionAnn = jMethod.annotate(Action.class); } if (jam == null) { jam = actionAnn.paramArray("fault"); } final JAnnotationUse faAnn = jam.annotate(FaultAction.class); faAnn.param("className", map.get(f.getName())); faAnn.param("value", f.getAction()); } } }
public JClass getExceptionClass(){ return exceptionClass; }