/** * Returns all <i>used</i> JPackages. * * A JPackage is considered as "used" if a ClassItem or * a InterfaceItem resides in that package. * * This value is dynamically calculated every time because * one can freely remove ClassItem/InterfaceItem. * * @return * Given the same input, the order of packages in the array * is always the same regardless of the environment. */ public final JPackage[] getUsedPackages(Aspect aspect) { Set<JPackage> s = new TreeSet<JPackage>(); for (CClassInfo bean : model.beans().values()) { JClassContainer cont = getContainer(bean.parent(), aspect); if (cont.isPackage()) { s.add((JPackage) cont); } } for (CElementInfo e : model.getElementMappings(null).values()) { // at the first glance you might think we should be iterating all elements, // not just global ones, but if you think about it, local ones live inside // another class, so those packages are already enumerated when we were // walking over CClassInfos. s.add(e._package()); } return s.toArray(new JPackage[s.size()]); }
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(); }
private void dump() throws IOException { // collect packages used in the class. Set<JPackage> packages = new TreeSet<JPackage>(new Comparator<JPackage>() { public int compare(JPackage lhs, JPackage rhs) { return lhs.name().compareTo(rhs.name()); } }); for( ClassOutline ci : classes ) packages.add(ci._package()._package()); for( JPackage pkg : packages ) dump( pkg ); out.flush(); }
public Writer openSource(JPackage pkg, String fileName) throws IOException { Writer w = super.openSource(pkg,fileName); PrintWriter out = new PrintWriter(w); // write prolog if this is a java source file if( prolog != null ) { out.println( "//" ); String s = prolog; int idx; while( (idx=s.indexOf('\n'))!=-1 ) { out.println("// "+ s.substring(0,idx) ); s = s.substring(idx+1); } out.println("//"); out.println(); } out.flush(); // we can't close the stream for that would close the undelying stream. return w; }
/** * Report progress to {@link XJCListener}. * @param pkg The package of file being written. Value of {@code null} means that file has no package. * @param fileName The file name being written. Value can't be {@code null}. */ private void report(final JPackage pkg, final String fileName) { if (fileName == null) { throw new IllegalArgumentException("File name is null"); } final String pkgName; final String fileNameOut; if (pkg != null && (pkgName = pkg.name().replace('.', File.separatorChar)).length() > 0 ) { final StringBuilder sb = new StringBuilder(fileName.length() + pkgName.length() + 1); sb.append(pkgName); sb.append(File.separatorChar); sb.append(fileName); fileNameOut = sb.toString(); } else { fileNameOut = fileName; } if(progress.isCanceled()) throw new AbortException(); progress.generatedFile(fileNameOut, current++, totalFileCount); }
@Override public Writer openSource(JPackage pkg, String fileName) throws IOException { Writer w = super.openSource(pkg,fileName); PrintWriter out = new PrintWriter(w); // write prolog if this is a java source file if( prolog != null ) { out.println( "//" ); String s = prolog; int idx; while( (idx=s.indexOf('\n'))!=-1 ) { out.println("// "+ s.substring(0,idx) ); s = s.substring(idx+1); } out.println("//"); out.println(); } out.flush(); // we can't close the stream for that would close the undelying stream. return w; }
@Override public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { final String name = pkg != null && pkg.name().length() > 0 ? pkg.name() + '.' + fileName : fileName; out.println( "-----------------------------------" + name + "-----------------------------------"); return new FilterOutputStream(out) { @Override public void close() { // don't let this stream close } }; }
protected File getFile(JPackage pkg, String fileName ) throws IOException { File f = super.getFile(pkg, fileName); options.addGeneratedFile(f); // we can't really tell the file type, for we don't know // what this file is used for. Fortunately, // FILE_TYPE doesn't seem to be used, so it doesn't really // matter what we set. return f; }
public Writer openSource(JPackage pkg, String fileName) throws IOException { String tmp = fileName.substring(0, fileName.length()-5); if (pkg.name() != null && ! "".equals(pkg.name())) { w = filer.createSourceFile(pkg.name() + "." + tmp).openWriter(); } else { w = filer.createSourceFile(tmp).openWriter(); } return w; }
public PackageOutlineImpl getPackageContext(JPackage p) { PackageOutlineImpl r = packageContexts.get(p); if (r == null) { r = new PackageOutlineImpl(this, model, p); packageContexts.put(p, r); } return r; }
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; }
protected PackageOutlineImpl( BeanGenerator outline, Model model, JPackage _pkg ) { this._model = model; this._package = _pkg; switch(model.strategy) { case BEAN_ONLY: objectFactoryGenerator = new PublicObjectFactoryGenerator(outline,model,_pkg); break; case INTF_AND_IMPL: objectFactoryGenerator = new DualObjectFactoryGenerator(outline,model,_pkg); break; default: throw new IllegalStateException(); } }
DualObjectFactoryGenerator(BeanGenerator outline, Model model, JPackage targetPackage) { this.publicOFG = new PublicObjectFactoryGenerator(outline,model,targetPackage); this.privateOFG = new PrivateObjectFactoryGenerator(outline,model,targetPackage); // put the marker so that we can detect missing jaxb.properties publicOFG.getObjectFactory().field(JMod.PRIVATE|JMod.STATIC|JMod.FINAL, Void.class, "_useJAXBProperties", 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()); }
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 dump( JPackage pkg ) throws IOException { println("package "+pkg.name()+" {"); indent++; dumpChildren(pkg); indent--; println("}"); }
public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { StandardLocation loc; if(fileName.endsWith(".java")) { // Annotation Processing doesn't do the proper Unicode escaping on Java source files, // so we can't rely on Filer.createSourceFile. loc = SOURCE_PATH; } else { // put non-Java files directly to the output folder loc = CLASS_PATH; } return filer.createResource(loc, pkg.name(), fileName).openOutputStream(); }
public Writer openSource(JPackage pkg, String fileName) throws IOException { String name; if(pkg.isUnnamed()) name = fileName; else name = pkg.name()+'.'+fileName; name = name.substring(0,name.length()-5); // strip ".java" return filer.createSourceFile(name).openWriter(); }
/** * Gets the specified package name (options/@package). */ public JPackage getTargetPackage() { if(model.options.defaultPackage!=null) // "-p" takes precedence over everything else return codeModel._package(model.options.defaultPackage); String p; if( defaultPackage!=null ) p = defaultPackage; else p = getOption("package", ""); return codeModel._package(p); }
private void report(JPackage pkg, String fileName) { String name = pkg.name().replace('.', File.separatorChar); if(name.length()!=0) name += File.separatorChar; name += fileName; if(progress.isCanceled()) throw new AbortException(); progress.generatedFile(name,current++,totalFileCount); }
public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { String name = fileName; if(!pkg.isUnnamed()) name = toDirName(pkg)+name; zip.putNextEntry(new ZipEntry(name)); return filter; }
public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { String pkgName = pkg.name(); if(pkgName.length()!=0) pkgName += '.'; out.println( "-----------------------------------" + pkgName+fileName + "-----------------------------------"); return new FilterOutputStream(out) { public void close() { // don't let this stream close } }; }
private void report(JPackage pkg, String fileName) { if(pkg.isUnnamed()) progress.println(fileName); else progress.println( pkg.name().replace('.',File.separatorChar) +File.separatorChar+fileName); }
public JStaticJavaFile(JPackage _pkg, String _className, URL _source, LineFilter _filter ) { super(_className+".java"); if(_source==null) throw new NullPointerException(); this.pkg = _pkg; this.clazz = new JStaticClass(); this.className = _className; this.source = _source; this.filter = _filter; }
public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { return new FilterOutputStream(out) { public void close() { // don't let this stream close } }; }
private void report(JPackage pkg, String fileName) { if(pkg == null || pkg.isUnnamed()) progress.println(fileName); else progress.println( pkg.name().replace('.',File.separatorChar) +File.separatorChar+fileName); }
public JStaticJavaFile(JPackage _pkg, String _className, Class<?> loadingClass, LineFilter _filter) { super(_className + ".java"); if (loadingClass == null) throw new NullPointerException(); this.pkg = _pkg; this.clazz = new JStaticClass(); this.className = _className; this.source = new ResourceLoader(_className, loadingClass); this.filter = _filter; }
public JClassContainer onPackage(JPackage pkg) { return model.strategy.getPackage(pkg, EXPOSED); }