下面列出了怎么用 com.sun.codemodel.internal.JPackage 的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
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;
}
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();
}
/**
* 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()]);
}
/**
* 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 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;
}
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 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;
}
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;
}
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 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();
}
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 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);
}
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;
}
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();
}
}
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;
}
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 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();
}
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();
}
}
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 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;
}
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 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 dump( JPackage pkg ) throws IOException {
println("package "+pkg.name()+" {");
indent++;
dumpChildren(pkg);
indent--;
println("}");
}
public OutputStream openBinary(JPackage pkg, String fileName)
throws IOException {
return new FilterOutputStream(out) {
public void close() {
// don't let this stream close
}
};
}
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 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) {
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);
}