Java Code Examples for javax.tools.JavaFileManager#Location

The following examples show how to use javax.tools.JavaFileManager#Location . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormattingFiler.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public FileObject createResource(
        JavaFileManager.Location location,
        CharSequence pkg,
        CharSequence relativeName,
        Element... originatingElements)
        throws IOException {
    return delegate.createResource(location, pkg, relativeName, originatingElements);
}
 
Example 2
Source File: MRJARModuleFileManagerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testList() throws IOException {
    //Source level 9, broken jar
    ModuleFileManager fm = new ModuleFileManager(
            CachingArchiveProvider.getDefault(),
            bCp,
            (u)->Collections.singleton(u),
            SourceLevelUtils.JDK1_9,
            StandardLocation.MODULE_PATH,
            false);
    JavaFileManager.Location l = StreamSupport.stream(fm.listLocationsForModules(StandardLocation.MODULE_PATH).spliterator(), true)
            .flatMap((s)->s.stream())
            .findFirst()
            .orElse(null);
    assertNotNull(l);
    Iterable<JavaFileObject> res = fm.list(l, "org.me", EnumSet.of(JavaFileObject.Kind.CLASS), false);    //NOI18N
    assertEquals(Arrays.asList("A Base","B Base"), toContent(res));  //NOI18N
    assertEquals(Arrays.asList("org.me.A","org.me.B"), toInferedName(fm, res));  //NOI18N
    //Source level 9, multi release jar
    fm = new ModuleFileManager(
            CachingArchiveProvider.getDefault(),
            mvCp,
            (u)->Collections.singleton(u),
            SourceLevelUtils.JDK1_9,
            StandardLocation.MODULE_PATH,
            false);
    l = StreamSupport.stream(fm.listLocationsForModules(StandardLocation.MODULE_PATH).spliterator(), true)
            .flatMap((s)->s.stream())
            .findFirst()
            .orElse(null);
    assertNotNull(l);
    res = fm.list(l, "org.me", EnumSet.of(JavaFileObject.Kind.CLASS), false);    //NOI18N
    assertEquals(Arrays.asList("A 9","B Base"), toContent(res));  //NOI18N
    assertEquals(Arrays.asList("org.me.A","org.me.B"), toInferedName(fm, res));  //NOI18N
}
 
Example 3
Source File: AbstractJsonTypeManifold.java    From manifold with Apache License 2.0 5 votes vote down vote up
@Override
protected String contribute( JavaFileManager.Location location, String topLevelFqn, boolean genStubs, String existing,
                             T model, DiagnosticListener<JavaFileObject> errorHandler )
{
  StringBuilder sb = new StringBuilder();
  String pkg = ManClassUtil.getPackage( topLevelFqn );
  sb.append( "package " ).append( pkg ).append( ";\n\n" )
    .append( "import " ).append( Json.class.getName() ).append( ";\n" )
    .append( "import " ).append( ManIdentifierUtil.class.getName() ).append( ";\n" )
    .append( "import " ).append( Bindings.class.getName() ).append( ";\n" )
    .append( "import " ).append( Loader.class.getName() ).append( ";\n" )
    .append( "import " ).append( Endpoint.class.getName() ).append( ";\n" )
    .append( "import " ).append( Requester.class.getName() ).append( ";\n" )
    .append( "import " ).append( List.class.getName() ).append( ";\n" )
    .append( "import " ).append( ArrayList.class.getName() ).append( ";\n" )
    .append( "import " ).append( Map.class.getName() ).append( ";\n" )
    .append( "import " ).append( DataBindings.class.getName() ).append( ";\n" )
    .append( "import " ).append( IJsonBindingsBacked.class.getName() ).append( ";\n" )
    .append( "import " ).append( IJsonList.class.getName() ).append( ";\n" )
    .append( "import " ).append( Structural.class.getName() ).append( ";\n" )
    .append( "import " ).append( FragmentValue.class.getName() ).append( ";\n" )
    .append( "import " ).append( IProxyFactory.class.getName() ).append( ";\n" )
    .append( "import " ).append( RuntimeMethods.class.getName() ).append( ";\n\n" );
  model.report( errorHandler );
  IJsonParentType type = model.getType();
  type.prepareToRender( location, getModule(), errorHandler );
  type.render( this, sb, 0, true );
  return sb.toString();
}
 
Example 4
Source File: Model.java    From manifold with Apache License 2.0 5 votes vote down vote up
String makeSource( String extensionClassFqn, JavaFileManager.Location location, IModule module, DiagnosticListener<JavaFileObject> errorHandler )
{
  SrcClass srcClass = new SrcClass( extensionClassFqn, SrcClass.Kind.Class, location, module, errorHandler )
    .addAnnotation( new SrcAnnotationExpression( Extension.class ) )
    .modifiers( Modifier.PUBLIC );
  int i = 0;
  for( IFile file : _favsFiles )
  {
    srcClass.addField(
      new SrcField( FIELD_FILE_URL + i++, String.class )
        .initializer( new SrcRawExpression( String.class, file.getPath().getFileSystemPathString() ) )
        .modifiers( Modifier.STATIC | Modifier.FINAL ) );
  }
  for( Map.Entry<Token, Token> entry : FavsParser.instance().parseFavsForType( _favsFiles, _extensionFqn, errorHandler ).entrySet() )
  {
    SrcMethod method = new SrcMethod()
      .modifiers( Modifier.PUBLIC | Modifier.STATIC )
      .name( "favorite" + entry.getKey() )
      .addParam(
        new SrcParameter( "thiz", deriveExtendedClassFrom( extensionClassFqn ) )
          .addAnnotation( new SrcAnnotationExpression( This.class ) ) )
      .returns( String.class )
      .body( new SrcStatementBlock()
        .addStatement(
          new SrcReturnStatement( String.class, entry.getValue()._value.toString() ) ) );
    method.addAnnotation( makeSourcePositionAnnotation( entry.getKey() ) );
    srcClass.addMethod( method );
  }
  return srcClass.render().toString();
}
 
Example 5
Source File: ProxyFileManager.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@NonNull
private <T extends javax.tools.FileObject> T mark(
        @NonNull final T result,
        @NonNull JavaFileManager.Location l) throws MalformedURLException {
    if (ModuleLocation.isInstance(l)) {
        l = ModuleLocation.cast(l).getBaseLocation();
    }
    boolean valid = true;
    ProcessorGenerated.Type type = null;
    if (l == StandardLocation.CLASS_OUTPUT) {
        type = ProcessorGenerated.Type.RESOURCE;
    } else if (l == StandardLocation.SOURCE_OUTPUT) {
        type = ProcessorGenerated.Type.SOURCE;
    }
    if (cfg.getSiblings().getProvider().hasSibling() &&
        cfg.getSiblings().getProvider().isInSourceRoot()) {
        if (type == ProcessorGenerated.Type.SOURCE) {
            cfg.getProcessorGeneratedFiles().register(
                cfg.getSiblings().getProvider().getSibling(),
                result,
                type);
        } else if (type == ProcessorGenerated.Type.RESOURCE) {
            try {
                result.openInputStream().close();
            } catch (IOException ioe) {
                //Marking only created files
                cfg.getProcessorGeneratedFiles().register(
                    cfg.getSiblings().getProvider().getSibling(),
                    result,
                    type);
            }
        }
        if (!FileObjects.isValidFileName(result)) {
            LOG.log(
                Level.WARNING,
                "Cannot write Annotation Processor generated file: {0} ({1})",   //NOI18N
                new Object[] {
                    result.getName(),
                    result.toUri()
                });
            valid = false;
        }
    }
    return valid && (cfg.getProcessorGeneratedFiles().canWrite() || !cfg.getSiblings().getProvider().hasSibling()) ?
            result :
            (T) FileObjects.nullWriteFileObject((InferableJavaFileObject)result);    //safe - NullFileObject subclass of both JFO and FO.
}
 
Example 6
Source File: JdkCompiler.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
@Override
public ClassLoader getClassLoader(JavaFileManager.Location location) {
    return classLoader;
}
 
Example 7
Source File: JdkCompiler.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Override
public ClassLoader getClassLoader(JavaFileManager.Location location) {
    return classLoader;
}
 
Example 8
Source File: TestFiler.java    From javapoet with Apache License 2.0 4 votes vote down vote up
@Override public FileObject getResource(JavaFileManager.Location location, CharSequence pkg,
    CharSequence relativeName) throws IOException {
  throw new UnsupportedOperationException("Not implemented.");
}
 
Example 9
Source File: FakeStandardJavaFileManager.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public void setLocation(JavaFileManager.Location location, Iterable<? extends File> path) {
  throw new UnsupportedOperationException();
}
 
Example 10
Source File: ExtensionProducerSampleTypeManifold.java    From manifold with Apache License 2.0 4 votes vote down vote up
@Override
protected String contribute( JavaFileManager.Location location, String topLevelFqn, boolean genStubs, String existing, Model model,
                             DiagnosticListener<JavaFileObject> errorHandler )
{
  return model.makeSource( topLevelFqn, location, getModule(), errorHandler );
}
 
Example 11
Source File: JdkCompiler.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Override
public ClassLoader getClassLoader(JavaFileManager.Location location) {
    return classLoader;
}
 
Example 12
Source File: MemoryFileManager.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns a {@linkplain FileObject file object} for input
 * representing the specified <a href="JavaFileManager.html#relative_name">relative
 * name</a> in the specified package in the given location.
 *
 * <p>If the returned object represents a {@linkplain
 * JavaFileObject.Kind#SOURCE source} or {@linkplain
 * JavaFileObject.Kind#CLASS class} file, it must be an instance
 * of {@link JavaFileObject}.
 *
 * <p>Informally, the file object returned by this method is
 * located in the concatenation of the location, package name, and
 * relative name.  For example, to locate the properties file
 * "resources/compiler.properties" in the package
 * "com.sun.tools.javac" in the {@linkplain
 * StandardLocation#SOURCE_PATH SOURCE_PATH} location, this method
 * might be called like so:
 *
 * <pre>getFileForInput(SOURCE_PATH, "com.sun.tools.javac", "resources/compiler.properties");</pre>
 *
 * <p>If the call was executed on Windows, with SOURCE_PATH set to
 * <code>"C:\Documents&nbsp;and&nbsp;Settings\UncleBob\src\share\classes"</code>,
 * a valid result would be a file object representing the file
 * <code>"C:\Documents&nbsp;and&nbsp;Settings\UncleBob\src\share\classes\com\sun\tools\javac\resources\compiler.properties"</code>.
 *
 * @param location a location
 * @param packageName a package name
 * @param relativeName a relative name
 * @return a file object, might return {@code null} if the file
 * does not exist
 * @throws IllegalArgumentException if the location is not known
 * to this file manager and the file manager does not support
 * unknown locations, or if {@code relativeName} is not valid
 * @throws IOException if an I/O error occurred, or if {@link
 * #close} has been called and this file manager cannot be
 * reopened
 * @throws IllegalStateException if {@link #close} has been called
 * and this file manager cannot be reopened
 */
@Override
public FileObject getFileForInput(JavaFileManager.Location location,
        String packageName,
        String relativeName)
        throws IOException {
    proc.debug(DBG_FMGR, "getFileForInput location=%s packageName=%s\n", location, packageName);
    return stdFileManager.getFileForInput(location, packageName, relativeName);
}
 
Example 13
Source File: JarTask.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Adds a set of file objects to be written into the jar file, by copying them
 * from a Location in a JavaFileManager.
 * The file objects to be written are specified by a series of paths;
 * each path can be in one of the following forms:
 * <ul>
 * <li>The name of a class. For example, java.lang.Object.
 * In this case, the corresponding .class file will be written to the jar file.
 * <li>the name of a package followed by {@code .*}. For example, {@code java.lang.*}.
 * In this case, all the class files in the specified package will be written to
 * the jar file.
 * <li>the name of a package followed by {@code .**}. For example, {@code java.lang.**}.
 * In this case, all the class files in the specified package, and any subpackages
 * will be written to the jar file.
 * </ul>
 *
 * @param fm the file manager in which to find the file objects
 * @param l  the location in which to find the file objects
 * @param paths the paths specifying the file objects to be copied
 * @return this task object
 * @throws IOException if errors occur while determining the set of file objects
 */
public JarTask files(JavaFileManager fm, JavaFileManager.Location l, String... paths)
        throws IOException {
    for (String p : paths) {
        if (p.endsWith(".**"))
            addPackage(fm, l, p.substring(0, p.length() - 3), true);
        else if (p.endsWith(".*"))
            addPackage(fm, l, p.substring(0, p.length() - 2), false);
        else
            addFile(fm, l, p);
    }
    return this;
}
 
Example 14
Source File: Filer.java    From jdk1.8-source-analysis with Apache License 2.0 2 votes vote down vote up
/**
  * Creates a new auxiliary resource file for writing and returns a
  * file object for it.  The file may be located along with the
  * newly created source files, newly created binary files, or
  * other supported location.  The locations {@link
  * StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link
  * StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be
  * supported.  The resource may be named relative to some package
  * (as are source and class files), and from there by a relative
  * pathname.  In a loose sense, the full pathname of the new file
  * will be the concatenation of {@code location}, {@code pkg}, and
  * {@code relativeName}.
  *
  * <p>Files created via this method are not registered for
  * annotation processing, even if the full pathname of the file
  * would correspond to the full pathname of a new source file
  * or new class file.
  *
  * @param location location of the new file
  * @param pkg package relative to which the file should be named,
  *          or the empty string if none
  * @param relativeName final pathname components of the file
  * @param originatingElements type or package elements causally
  * associated with the creation of this file, may be elided or
  * {@code null}
  * @return a {@code FileObject} to write the new resource
  * @throws IOException if the file cannot be created
  * @throws FilerException if the same pathname has already been
  * created
  * @throws IllegalArgumentException for an unsupported location
  * @throws IllegalArgumentException if {@code relativeName} is not relative
  */
FileObject createResource(JavaFileManager.Location location,
                          CharSequence pkg,
                          CharSequence relativeName,
                          Element... originatingElements) throws IOException;
 
Example 15
Source File: Filer.java    From Java8CN with Apache License 2.0 2 votes vote down vote up
/**
  * Creates a new auxiliary resource file for writing and returns a
  * file object for it.  The file may be located along with the
  * newly created source files, newly created binary files, or
  * other supported location.  The locations {@link
  * StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link
  * StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be
  * supported.  The resource may be named relative to some package
  * (as are source and class files), and from there by a relative
  * pathname.  In a loose sense, the full pathname of the new file
  * will be the concatenation of {@code location}, {@code pkg}, and
  * {@code relativeName}.
  *
  * <p>Files created via this method are not registered for
  * annotation processing, even if the full pathname of the file
  * would correspond to the full pathname of a new source file
  * or new class file.
  *
  * @param location location of the new file
  * @param pkg package relative to which the file should be named,
  *          or the empty string if none
  * @param relativeName final pathname components of the file
  * @param originatingElements type or package elements causally
  * associated with the creation of this file, may be elided or
  * {@code null}
  * @return a {@code FileObject} to write the new resource
  * @throws IOException if the file cannot be created
  * @throws FilerException if the same pathname has already been
  * created
  * @throws IllegalArgumentException for an unsupported location
  * @throws IllegalArgumentException if {@code relativeName} is not relative
  */
FileObject createResource(JavaFileManager.Location location,
                          CharSequence pkg,
                          CharSequence relativeName,
                          Element... originatingElements) throws IOException;
 
Example 16
Source File: Filer.java    From java-n-IDE-for-Android with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an object for reading an existing resource.  The
 * locations {@link StandardLocation#CLASS_OUTPUT CLASS_OUTPUT}
 * and {@link StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must
 * be supported.
 *
 * @param location location of the file
 * @param pkg package relative to which the file should be searched,
 *          or the empty string if none
 * @param relativeName final pathname components of the file
 * @return an object to read the file
 * @throws FilerException if the same pathname has already been
 * opened for writing
 * @throws IOException if the file cannot be opened
 * @throws IllegalArgumentException for an unsupported location
 * @throws IllegalArgumentException if {@code relativeName} is not relative
 */
FileObject getResource(JavaFileManager.Location location,
                       CharSequence pkg,
                       CharSequence relativeName) throws IOException;
 
Example 17
Source File: Filer.java    From openjdk-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
  * Creates a new auxiliary resource file for writing and returns a
  * file object for it.  The file may be located along with the
  * newly created source files, newly created binary files, or
  * other supported location.  The locations {@link
  * StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link
  * StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be
  * supported.  The resource may be named relative to some package
  * (as are source and class files), and from there by a relative
  * pathname.  In a loose sense, the full pathname of the new file
  * will be the concatenation of {@code location}, {@code pkg}, and
  * {@code relativeName}.
  *
  * <p>Files created via this method are not registered for
  * annotation processing, even if the full pathname of the file
  * would correspond to the full pathname of a new source file
  * or new class file.
  *
  * @param location location of the new file
  * @param pkg package relative to which the file should be named,
  *          or the empty string if none
  * @param relativeName final pathname components of the file
  * @param originatingElements type or package elements causally
  * associated with the creation of this file, may be elided or
  * {@code null}
  * @return a {@code FileObject} to write the new resource
  * @throws IOException if the file cannot be created
  * @throws FilerException if the same pathname has already been
  * created
  * @throws IllegalArgumentException for an unsupported location
  * @throws IllegalArgumentException if {@code relativeName} is not relative
  */
FileObject createResource(JavaFileManager.Location location,
                          CharSequence pkg,
                          CharSequence relativeName,
                          Element... originatingElements) throws IOException;
 
Example 18
Source File: Filer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 2 votes vote down vote up
/**
  * Creates a new auxiliary resource file for writing and returns a
  * file object for it.  The file may be located along with the
  * newly created source files, newly created binary files, or
  * other supported location.  The locations {@link
  * StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link
  * StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be
  * supported.  The resource may be named relative to some package
  * (as are source and class files), and from there by a relative
  * pathname.  In a loose sense, the full pathname of the new file
  * will be the concatenation of {@code location}, {@code pkg}, and
  * {@code relativeName}.
  *
  * <p>Files created via this method are not registered for
  * annotation processing, even if the full pathname of the file
  * would correspond to the full pathname of a new source file
  * or new class file.
  *
  * @param location location of the new file
  * @param pkg package relative to which the file should be named,
  *          or the empty string if none
  * @param relativeName final pathname components of the file
  * @param originatingElements type or package elements causally
  * associated with the creation of this file, may be elided or
  * {@code null}
  * @return a {@code FileObject} to write the new resource
  * @throws IOException if the file cannot be created
  * @throws FilerException if the same pathname has already been
  * created
  * @throws IllegalArgumentException for an unsupported location
  * @throws IllegalArgumentException if {@code relativeName} is not relative
  */
FileObject createResource(JavaFileManager.Location location,
                          CharSequence pkg,
                          CharSequence relativeName,
                          Element... originatingElements) throws IOException;
 
Example 19
Source File: Filer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Returns an object for reading an existing resource.  The
 * locations {@link StandardLocation#CLASS_OUTPUT CLASS_OUTPUT}
 * and {@link StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must
 * be supported.
 *
 * <p>If {@code moduleAndPkg} contains a "{@code /}" character, the
 * prefix before the "{@code /}" character is the module name and
 * the suffix after the "{@code /}" character is the package
 * name. The package suffix may be empty; however, if a module
 * name is present, it must be nonempty. If {@code moduleAndPkg}
 * does not contain a "{@code /}" character, the entire argument
 * is interpreted as a package name.
 *
 * <p>If the given location is neither a {@linkplain
 * JavaFileManager.Location#isModuleOrientedLocation()
 * module oriented location}, nor an {@linkplain
 * JavaFileManager.Location#isOutputLocation()
 * output location containing multiple modules}, and the explicit
 * module prefix is given, {@link FilerException} is thrown.
 *
 * <p>If the given location is either a module oriented location,
 * or an output location containing multiple modules, and no explicit
 * modules prefix is given, a suitable module is
 * inferred. If a suitable module cannot be inferred {@link
 * FilerException} is thrown. An implementation may use information
 * about the configuration of the annotation processing tool
 * as part of the inference.
 *
 * @implNote In the reference implementation, if the annotation
 * processing tool is processing a single module <i>M</i>,
 * then <i>M</i> is used as the module for files read without
 * an explicit module prefix. If the tool is processing multiple
 * modules, and {@link
 * Elements#getPackageElement(java.lang.CharSequence)
 * Elements.getPackageElement(package-of(name))}
 * returns a package, the module that owns the returned package is used
 * as the source module. A separate option may be used to provide the target
 * module if it cannot be determined using the above rules.
 *
 * @param location location of the file
 * @param moduleAndPkg module and/or package relative to which the file
 *          should be searched for, or the empty string if none
 * @param relativeName final pathname components of the file
 * @return an object to read the file
 * @throws FilerException if the same pathname has already been
 * opened for writing, if the source module cannot be determined,
 * or if the target module is not writable, or if an explicit target
 * module is specified and the location does not support it.
 * @throws IOException if the file cannot be opened
 * @throws IllegalArgumentException for an unsupported location
 * @throws IllegalArgumentException if {@code moduleAndPkg} is ill-formed
 * @throws IllegalArgumentException if {@code relativeName} is not relative
 */
FileObject getResource(JavaFileManager.Location location,
                       CharSequence moduleAndPkg,
                       CharSequence relativeName) throws IOException;
 
Example 20
Source File: IJsonParentType.java    From manifold with Apache License 2.0 votes vote down vote up
void prepareToRender( JavaFileManager.Location location, IModule module, DiagnosticListener<JavaFileObject> errorHandler );