本文整理汇总了Java中javax.tools.JavaFileManager类的典型用法代码示例。如果您正苦于以下问题:Java JavaFileManager类的具体用法?Java JavaFileManager怎么用?Java JavaFileManager使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JavaFileManager类属于javax.tools包,在下文中一共展示了JavaFileManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createUri
import javax.tools.JavaFileManager; //导入依赖的package包/类
/**
* Creates a URI for a class file using a location, a class name, and a kind.
*
* @param location
* the location of the file relative to {@code BASE_LOCATION}, not null
* @param className
* the name of the class, not null
* @param kind
* the kind of the class, not null
*
* @return the URI, not null
*
* @throws IllegalArgumentException
* if {@code location} is null
* @throws IllegalArgumentException
* if {@code className} is null
* @throws IllegalArgumentException
* if {@code kind} is null
*/
private static URI createUri(
final JavaFileManager.Location location,
final String className,
final JavaFileObject.Kind kind) {
checkNotNull(location, "Argument \'location\' cannot be null.");
checkNotNull(className, "Argument \'className\' cannot be null.");
checkNotNull(kind, "Argument \'kind\' cannot be null.");
final StringBuilder uri = new StringBuilder();
uri.append(BASE_LOCATION);
uri.append(location.getName());
uri.append("/");
uri.append(className.replace(".", "/"));
uri.append(kind.extension);
return URI.create(uri.toString());
}
示例2: deriveClassOutputPath
import javax.tools.JavaFileManager; //导入依赖的package包/类
private String deriveClassOutputPath()
{
try
{
String ping = "__dummy__";
//JavaFileObject classFile = _jpe.getFiler().createClassFile( ping );
JavaFileObject classFile = _javacTask.getContext().get( JavaFileManager.class ).getJavaFileForOutput( StandardLocation.CLASS_OUTPUT, ping, JavaFileObject.Kind.CLASS, null );
if( !isPhysicalFile( classFile ) )
{
return "";
}
File dummyFile = new File( classFile.toUri() );
String path = dummyFile.getAbsolutePath();
path = path.substring( 0, path.length() - (File.separatorChar + ping + ".class").length() );
return path;
}
catch( IOException e )
{
throw new RuntimeException( e );
}
}
示例3: UsagesVisitor
import javax.tools.JavaFileManager; //导入依赖的package包/类
protected UsagesVisitor (
JavacTaskImpl jt,
CompilationUnitTree cu,
JavaFileManager manager,
javax.tools.JavaFileObject sibling,
Set<? super Pair<String,String>> topLevels) throws MalformedURLException, IllegalArgumentException {
this(
jt,
cu,
inferBinaryName(manager, sibling),
sibling.toUri().toURL(),
false,
false,
null,
null,
topLevels);
}
示例4: run
import javax.tools.JavaFileManager; //导入依赖的package包/类
private void run() {
File classToCheck = new File(System.getProperty("test.classes"),
getClass().getSimpleName() + ".class");
DiagnosticCollector<JavaFileObject> dc =
new DiagnosticCollector<JavaFileObject>();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
JavaFileManager fm = JavapFileManager.create(dc, pw);
JavapTask t = new JavapTask(pw, fm, dc, null,
Arrays.asList(classToCheck.getPath()));
if (t.run() != 0)
throw new Error("javap failed unexpectedly");
List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
for (Diagnostic<? extends JavaFileObject> d: diags) {
if (d.getKind() == Diagnostic.Kind.ERROR)
throw new AssertionError(d.getMessage(Locale.ENGLISH));
}
String lineSep = System.getProperty("line.separator");
String out = sw.toString().replace(lineSep, "\n");
if (!out.equals(expOutput)) {
throw new AssertionError("The output is not equal to the one expected");
}
}
示例5: ToolEnvironment
import javax.tools.JavaFileManager; //导入依赖的package包/类
/**
* Constructor
*
* @param context Context for this javadoc instance.
*/
protected ToolEnvironment(Context context) {
context.put(ToolEnvKey, this);
this.context = context;
messager = Messager.instance0(context);
syms = Symtab.instance(context);
finder = JavadocClassFinder.instance(context);
enter = JavadocEnter.instance(context);
names = Names.instance(context);
externalizableSym = syms.enterClass(syms.java_base, names.fromString("java.io.Externalizable"));
chk = Check.instance(context);
types = com.sun.tools.javac.code.Types.instance(context);
fileManager = context.get(JavaFileManager.class);
if (fileManager instanceof JavacFileManager) {
((JavacFileManager)fileManager).setSymbolFileEnabled(false);
}
docTrees = JavacTrees.instance(context);
source = Source.instance(context);
elements = JavacElements.instance(context);
typeutils = JavacTypes.instance(context);
elementToTreePath = new HashMap<>();
}
示例6: getFileForInput
import javax.tools.JavaFileManager; //导入依赖的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: inferModuleName
import javax.tools.JavaFileManager; //导入依赖的package包/类
@Override
@CheckForNull
public String inferModuleName(@NonNull final Location location) throws IOException {
checkSingleOwnerThread();
try {
for (JavaFileManager jfm : cfg.getFileManagers(location, null)) {
final String modName = jfm.inferModuleName(location);
if (modName != null) {
return modName;
}
}
return null;
} finally {
clearOwnerThread();
}
}
示例8: getJavaFileForOutput
import javax.tools.JavaFileManager; //导入依赖的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: findSource
import javax.tools.JavaFileManager; //导入依赖的package包/类
private Pair<JavacTask, CompilationUnitTree> findSource(String moduleName,
String binaryName) throws IOException {
JavaFileObject jfo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH,
binaryName,
JavaFileObject.Kind.SOURCE);
if (jfo == null)
return null;
List<JavaFileObject> jfos = Arrays.asList(jfo);
JavaFileManager patchFM = moduleName != null
? new PatchModuleFileManager(baseFileManager, jfo, moduleName)
: baseFileManager;
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, patchFM, d -> {}, null, null, jfos);
Iterable<? extends CompilationUnitTree> cuts = task.parse();
task.enter();
return Pair.of(task, cuts.iterator().next());
}
示例10: analyzeModule
import javax.tools.JavaFileManager; //导入依赖的package包/类
void analyzeModule(StandardJavaFileManager fm, String moduleName)
throws
IOException,
ConstantPoolException,
InvalidDescriptor {
JavaFileManager.Location location =
fm.getLocationForModule(StandardLocation.SYSTEM_MODULES, moduleName);
if (location == null)
throw new AssertionError("can't find module " + moduleName);
for (JavaFileObject file : fm.list(location, "", EnumSet.of(CLASS), true)) {
String className = fm.inferBinaryName(location, file);
int index = className.lastIndexOf('.');
String pckName = index == -1 ? "" : className.substring(0, index);
if (shouldAnalyzePackage(pckName)) {
analyzeClassFile(ClassFile.read(file.openInputStream()));
}
}
}
示例11: runTest
import javax.tools.JavaFileManager; //导入依赖的package包/类
void runTest(String aJava, String bJava) throws Exception {
try (JavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null)) {
ToolBox tb = new ToolBox();
ToolBox.MemoryFileManager memoryFM1 = new ToolBox.MemoryFileManager(fm);
new JavacTask(tb).fileManager(memoryFM1)
.sources(aJava, bJava)
.run();
ToolBox.MemoryFileManager memoryFM2 = new ToolBox.MemoryFileManager(fm);
new JavacTask(tb).fileManager(memoryFM2)
.sources(bJava, aJava)
.run();
Assert.check(Arrays.equals(memoryFM1.getFileBytes(StandardLocation.CLASS_OUTPUT, "B"),
memoryFM2.getFileBytes(StandardLocation.CLASS_OUTPUT, "B")));
}
}
示例12: hijackJavacFileManager
import javax.tools.JavaFileManager; //导入依赖的package包/类
private void hijackJavacFileManager()
{
if( !(_fileManager instanceof ManifoldJavaFileManager) && _manFileManager == null )
{
_ctx = _javacTask.getContext();
_fileManager = _ctx.get( JavaFileManager.class );
_javaInputFiles = new HashSet<>();
_gosuInputFiles = fetchGosuInputFiles();
_treeMaker = TreeMaker.instance( _ctx );
_javacElements = JavacElements.instance( _ctx );
_typeProcessor = new TypeProcessor( _javacTask );
_issueReporter = new IssueReporter( Log.instance( getContext() ) );
_seenModules = new LinkedHashSet<>();
injectManFileManager();
}
}
示例13: JavahTask
import javax.tools.JavaFileManager; //导入依赖的package包/类
JavahTask(Writer out,
JavaFileManager fileManager,
DiagnosticListener<? super JavaFileObject> diagnosticListener,
Iterable<String> options,
Iterable<String> classes) {
this();
this.log = getPrintWriterForWriter(out);
this.fileManager = fileManager;
this.diagnosticListener = diagnosticListener;
try {
handleOptions(options, false);
} catch (BadArgs e) {
throw new IllegalArgumentException(e.getMessage());
}
this.classes = new ArrayList<String>();
if (classes != null) {
for (String classname: classes) {
classname.getClass(); // null-check
this.classes.add(classname);
}
}
}
示例14: DocEnv
import javax.tools.JavaFileManager; //导入依赖的package包/类
/**
* Constructor
*
* @param context Context for this javadoc instance.
*/
protected DocEnv(Context context) {
context.put(docEnvKey, this);
this.context = context;
messager = Messager.instance0(context);
syms = Symtab.instance(context);
finder = JavadocClassFinder.instance(context);
enter = JavadocEnter.instance(context);
names = Names.instance(context);
externalizableSym = syms.enterClass(syms.java_base, names.fromString("java.io.Externalizable"));
chk = Check.instance(context);
types = Types.instance(context);
fileManager = context.get(JavaFileManager.class);
if (fileManager instanceof JavacFileManager) {
((JavacFileManager)fileManager).setSymbolFileEnabled(false);
}
// Default. Should normally be reset with setLocale.
this.doclocale = new DocLocale(this, "", breakiterator);
source = Source.instance(context);
}
示例15: testGetPackageDeclaration
import javax.tools.JavaFileManager; //导入依赖的package包/类
public void testGetPackageDeclaration() throws Exception {
ClasspathInfo ci = ClasspathInfo.create( bootPath, classPath, null);
JavaFileManager fm = ClasspathInfoAccessor.getINSTANCE().createFileManager(ci, null);
JarFile jf = new JarFile( rtJar );
for( Enumeration entries = jf.entries(); entries.hasMoreElements(); ) {
JarEntry je = (JarEntry)entries.nextElement();
String jeName = je.getName();
if ( je.isDirectory() ) {
String packageName = jeName.replace( "/", "." );
if ( !fm.list( StandardLocation.PLATFORM_CLASS_PATH,packageName, EnumSet.of( JavaFileObject.Kind.CLASS ), false).iterator().hasNext() ) {
// empty package
continue;
}
PackageElement pd = JavacParser.createJavacTask(ci, (DiagnosticListener) null, (String) null, null, null, null, null, null, Collections.emptyList()).getElements().getPackageElement( packageName );
assertNotNull( "Declaration for " + packageName + " should not be null.", pd );
}
}
}