本文整理汇总了Java中javax.tools.FileObject类的典型用法代码示例。如果您正苦于以下问题:Java FileObject类的具体用法?Java FileObject怎么用?Java FileObject使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileObject类属于javax.tools包,在下文中一共展示了FileObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: write
import javax.tools.FileObject; //导入依赖的package包/类
/** Emit a class file for a given class.
* @param c The class from which a class file is generated.
*/
public FileObject write(ClassSymbol c)
throws IOException
{
String className = c.flatName().toString();
FileObject outFile
= fileManager.getFileForOutput(StandardLocation.NATIVE_HEADER_OUTPUT,
"", className.replaceAll("[.$]", "_") + ".h", null);
Writer out = outFile.openWriter();
try {
write(out, c);
if (verbose)
log.printVerbose("wrote.file", outFile);
out.close();
out = null;
} finally {
if (out != null) {
// if we are propogating an exception, delete the file
out.close();
outFile.delete();
outFile = null;
}
}
return outFile; // may be null if write failed
}
示例2: readHTMLDocumentation
import javax.tools.FileObject; //导入依赖的package包/类
/**
* Utility for subclasses which read HTML documentation files.
*/
String readHTMLDocumentation(InputStream input, FileObject filename) throws IOException {
byte[] filecontents = new byte[input.available()];
try {
DataInputStream dataIn = new DataInputStream(input);
dataIn.readFully(filecontents);
} finally {
input.close();
}
String encoding = env.getEncoding();
String rawDoc = (encoding!=null)
? new String(filecontents, encoding)
: new String(filecontents);
Pattern bodyPat = Pattern.compile("(?is).*<body\\b[^>]*>(.*)</body\\b.*");
Matcher m = bodyPat.matcher(rawDoc);
if (m.matches()) {
return m.group(1);
} else {
String key = rawDoc.matches("(?is).*<body\\b.*")
? "javadoc.End_body_missing_from_html_file"
: "javadoc.Body_missing_from_html_file";
env.error(SourcePositionImpl.make(filename, Position.NOPOS, null), key);
return "";
}
}
示例3: getJavaFileForOutput
import javax.tools.FileObject; //导入依赖的package包/类
@Override
public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
final Pair<Location,URL> p = baseLocation(location);
if (!hasLocation(p.first())) {
throw new IllegalArgumentException(String.valueOf(p.first()));
}
final File root = new File (outputRoot);
assert p.second() == null || p.second().equals(BaseUtilities.toURI(root).toURL()) :
String.format("Expected: %s, Current %s", p.second(), root);
final String nameStr = FileObjects.convertPackage2Folder(className, File.separatorChar) + '.' + FileObjects.SIG;
final File file = new File (root, nameStr);
if (FileObjects.isValidFileName(className)) {
return tx.createFileObject(location, file, root, null, null);
} else {
LOG.log(
Level.WARNING,
"Invalid class name: {0} sibling: {1}", //NOI18N
new Object[]{
className,
sibling
});
return FileObjects.nullWriteFileObject(FileObjects.fileFileObject(file, root, null, null));
}
}
示例4: findResources
import javax.tools.FileObject; //导入依赖的package包/类
@Override
protected Enumeration<URL> findResources(final String name) throws IOException {
try {
return readAction(new Callable<Enumeration<URL>>(){
@Override
public Enumeration<URL> call() throws Exception {
@SuppressWarnings("UseOfObsoleteCollectionType")
final Vector<URL> v = new Vector<URL>();
for (final Pair<URL,Archive> p : archives) {
final Archive archive = p.second();
final FileObject file = archive.getFile(name);
if (file != null) {
v.add(file.toUri().toURL());
usedRoots
.map((c) -> RES_PROCESSORS.equals(name) ? null : c)
.ifPresent((c) -> c.accept(p.first()));
}
}
return v.elements();
}
});
} catch (Exception ex) {
throw new IOException(ex);
}
}
示例5: readJavaFileObject
import javax.tools.FileObject; //导入依赖的package包/类
private int readJavaFileObject(final FileObject jfo) throws IOException {
assert LOCK.getReadLockCount() > 0;
if (buffer == null) {
buffer = new byte[INI_SIZE];
}
int len = 0;
final InputStream in = jfo.openInputStream();
try {
while (true) {
if (buffer.length == len) {
byte[] nb = new byte[2*buffer.length];
System.arraycopy(buffer, 0, nb, 0, len);
buffer = nb;
}
int l = in.read(buffer,len,buffer.length-len);
if (l<=0) {
break;
}
len+=l;
}
} finally {
in.close();
}
return len;
}
示例6: getFileForInput
import javax.tools.FileObject; //导入依赖的package包/类
@Override
@CheckForNull
public FileObject getFileForInput(
@NonNull final Location l,
@NonNull final String packageName,
@NonNull final String relativeName) throws IOException {
checkSingleOwnerThread();
try {
JavaFileManager[] fms = cfg.getFileManagers(l, null);
for (JavaFileManager fm : fms) {
FileObject result = fm.getFileForInput(l, packageName, relativeName);
if (result != null) {
return result;
}
}
return null;
} finally {
clearOwnerThread();
}
}
示例7: checkContains
import javax.tools.FileObject; //导入依赖的package包/类
void checkContains(StandardJavaFileManager fm, Location l, FileObject fo, boolean expect) throws IOException {
boolean found = fm.contains(l, fo);
if (found) {
if (expect) {
out.println("file found, as expected: " + l + " " + fo.getName());
} else {
error("file not found: " + l + " " + fo.getName());
}
} else {
if (expect) {
error("file found unexpectedly: " + l + " " + fo.getName());
} else {
out.println("file not found, as expected: " + l + " " + fo.getName());
}
}
}
示例8: getJavaFileForOutput
import javax.tools.FileObject; //导入依赖的package包/类
@Override
@CheckForNull
public JavaFileObject getJavaFileForOutput(
@NonNull final Location l,
@NonNull final String className,
@NonNull final JavaFileObject.Kind kind,
@NonNull final FileObject sibling)
throws IOException, UnsupportedOperationException, IllegalArgumentException {
checkSingleOwnerThread();
try {
final JavaFileManager[] fms = cfg.getFileManagers (l, null);
if (fms.length == 0) {
throw new UnsupportedOperationException("No JavaFileManager for location: " + l); //NOI18N
} else {
return mark (
fms[0].getJavaFileForOutput (l, className, kind, sibling),
l);
}
} finally {
clearOwnerThread();
}
}
示例9: writeFileObjects
import javax.tools.FileObject; //导入依赖的package包/类
private void writeFileObjects(JarOutputStream jos) throws IOException {
for (FileObject fo : fileObjects) {
String p = guessPath(fo);
JarEntry e = new JarEntry(p);
jos.putNextEntry(e);
try {
byte[] buf = new byte[1024];
try (BufferedInputStream in = new BufferedInputStream(fo.openInputStream())) {
int n;
while ((n = in.read(buf)) > 0)
jos.write(buf, 0, n);
} catch (IOException ex) {
error("Exception while adding " + fo.getName() + " to jar file", ex);
}
} finally {
jos.closeEntry();
}
}
}
示例10: writeServices
import javax.tools.FileObject; //导入依赖的package包/类
private void writeServices() {
for (Map.Entry<Filer,Map<String,SortedSet<ServiceLoaderLine>>> outputFiles : outputFilesByProcessor.entrySet()) {
Filer filer = outputFiles.getKey();
for (Map.Entry<String,SortedSet<ServiceLoaderLine>> entry : outputFiles.getValue().entrySet()) {
try {
FileObject out = filer.createResource(StandardLocation.CLASS_OUTPUT, "", entry.getKey(),
originatingElementsByProcessor.get(filer).get(entry.getKey()).toArray(new Element[0]));
OutputStream os = out.openOutputStream();
try {
PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
for (ServiceLoaderLine line : entry.getValue()) {
line.write(w);
}
w.flush();
w.close();
} finally {
os.close();
}
} catch (IOException x) {
processingEnv.getMessager().printMessage(Kind.ERROR, "Failed to write to " + entry.getKey() + ": " + x.toString());
}
}
}
}
示例11: generateConverterDoc
import javax.tools.FileObject; //导入依赖的package包/类
private static void generateConverterDoc(ModelContext ctx, ConverterModel converter) throws TransformerFactoryConfigurationError, IOException {
String bundleName = converter.getBundleName();
int dot = bundleName.lastIndexOf('.');
String packageName = bundleName.substring(0, dot);
String fileName = bundleName.substring(dot + 1) + ".xml";
FileObject fo;
try {
fo = ctx.getFiler().getResource(StandardLocation.SOURCE_PATH, packageName, fileName);
} catch (IOException ioe) {
fo = ctx.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, packageName, fileName);
ctx.note("generating " + fo.getName());
PrintWriter out = new PrintWriter(fo.openOutputStream());
XMLUtils.writeDOMToFile(converter.generateDoc(), null, out);
out.close();
}
}
示例12: createResource
import javax.tools.FileObject; //导入依赖的package包/类
public FileObject createResource(Location location,
CharSequence pkg,
CharSequence relativeName,
Element... originatingElements) throws IOException {
locationCheck(location);
String strPkg = pkg.toString();
if (strPkg.length() > 0)
checkName(strPkg);
FileObject fileObject =
fileManager.getFileForOutput(location, strPkg,
relativeName.toString(), null);
checkFileReopening(fileObject, true);
if (fileObject instanceof JavaFileObject)
return new FilerOutputJavaFileObject(null, (JavaFileObject)fileObject);
else
return new FilerOutputFileObject(null, fileObject);
}
示例13: getFileObjectForOutput
import javax.tools.FileObject; //导入依赖的package包/类
private FileObject getFileObjectForOutput(DocPath path) throws IOException {
// break the path into a package-part and the rest, by finding
// the position of the last '/' before an invalid character for a
// package name, such as the "." before an extension or the "-"
// in filenames like package-summary.html, doc-files or src-html.
String p = path.getPath();
int lastSep = -1;
for (int i = 0; i < p.length(); i++) {
char ch = p.charAt(i);
if (ch == '/') {
lastSep = i;
} else if (i == lastSep + 1 && !Character.isJavaIdentifierStart(ch)
|| !Character.isJavaIdentifierPart(ch)) {
break;
}
}
String pkg = (lastSep == -1) ? "" : p.substring(0, lastSep);
String rest = p.substring(lastSep + 1);
return fileManager.getFileForOutput(location, pkg, rest, null);
}
示例14: getJavaFileForOutput
import javax.tools.FileObject; //导入依赖的package包/类
@Override
public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException
{
if (kind != Kind.CLASS)
{
return _wrapped.getJavaFileForOutput(location, className, kind, sibling);
}
if (className.contains("/"))
{
className = className.replace('/', '.');
}
ScriptingOutputFileObject fileObject;
if (sibling != null)
{
fileObject = new ScriptingOutputFileObject(Paths.get(sibling.getName()), className, className.substring(className.lastIndexOf('.') + 1));
}
else
{
fileObject = new ScriptingOutputFileObject(null, className, className.substring(className.lastIndexOf('.') + 1));
}
_classOutputs.add(fileObject);
return fileObject;
}
示例15: listenForNewClassFile
import javax.tools.FileObject; //导入依赖的package包/类
private void listenForNewClassFile(OutputMemoryJavaFileObject jfo, JavaFileManager.Location location,
String className, JavaFileObject.Kind kind, FileObject sibling) {
//debug("listenForNewClassFile %s loc=%s kind=%s\n", className, location, kind);
if (location == CLASS_OUTPUT) {
state.debug(DBG_GEN, "Compiler generating class %s\n", className);
OuterWrap w = ((sibling instanceof SourceMemoryJavaFileObject)
&& (((SourceMemoryJavaFileObject) sibling).getOrigin() instanceof OuterWrap))
? (OuterWrap) ((SourceMemoryJavaFileObject) sibling).getOrigin()
: null;
classObjs.compute(w, (k, v) -> (v == null)? new ArrayList<>() : v)
.add(jfo);
}
}