langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/nio/PathFileObject.java
changeset 26266 2d24bda701dc
parent 25874 83c19f00452c
child 27579 d1a63c99cdd5
equal deleted inserted replaced
26265:46aacfffd3b5 26266:2d24bda701dc
    42 import javax.lang.model.element.Modifier;
    42 import javax.lang.model.element.Modifier;
    43 import javax.lang.model.element.NestingKind;
    43 import javax.lang.model.element.NestingKind;
    44 import javax.tools.JavaFileObject;
    44 import javax.tools.JavaFileObject;
    45 
    45 
    46 import com.sun.tools.javac.util.BaseFileManager;
    46 import com.sun.tools.javac.util.BaseFileManager;
       
    47 import com.sun.tools.javac.util.DefinedBy;
       
    48 import com.sun.tools.javac.util.DefinedBy.Api;
    47 
    49 
    48 
    50 
    49 /**
    51 /**
    50  *  Implementation of JavaFileObject using java.nio.file API.
    52  *  Implementation of JavaFileObject using java.nio.file API.
    51  *
    53  *
   148      */
   150      */
   149     Path getPath() {
   151     Path getPath() {
   150         return path;
   152         return path;
   151     }
   153     }
   152 
   154 
   153     @Override
   155     @Override @DefinedBy(Api.COMPILER)
   154     public Kind getKind() {
   156     public Kind getKind() {
   155         return BaseFileManager.getKind(path.getFileName().toString());
   157         return BaseFileManager.getKind(path.getFileName().toString());
   156     }
   158     }
   157 
   159 
   158     @Override
   160     @Override @DefinedBy(Api.COMPILER)
   159     public boolean isNameCompatible(String simpleName, Kind kind) {
   161     public boolean isNameCompatible(String simpleName, Kind kind) {
   160         simpleName.getClass();
   162         simpleName.getClass();
   161         // null check
   163         // null check
   162         if (kind == Kind.OTHER && getKind() != kind) {
   164         if (kind == Kind.OTHER && getKind() != kind) {
   163             return false;
   165             return false;
   175             }
   177             }
   176         }
   178         }
   177         return false;
   179         return false;
   178     }
   180     }
   179 
   181 
   180     @Override
   182     @Override @DefinedBy(Api.COMPILER)
   181     public NestingKind getNestingKind() {
   183     public NestingKind getNestingKind() {
   182         return null;
   184         return null;
   183     }
   185     }
   184 
   186 
   185     @Override
   187     @Override @DefinedBy(Api.COMPILER)
   186     public Modifier getAccessLevel() {
   188     public Modifier getAccessLevel() {
   187         return null;
   189         return null;
   188     }
   190     }
   189 
   191 
   190     @Override
   192     @Override @DefinedBy(Api.COMPILER)
   191     public URI toUri() {
   193     public URI toUri() {
   192         return path.toUri();
   194         return path.toUri();
   193     }
   195     }
   194 
   196 
   195     @Override
   197     @Override @DefinedBy(Api.COMPILER)
   196     public String getName() {
   198     public String getName() {
   197         return path.toString();
   199         return path.toString();
   198     }
   200     }
   199 
   201 
   200     @Override
   202     @Override @DefinedBy(Api.COMPILER)
   201     public InputStream openInputStream() throws IOException {
   203     public InputStream openInputStream() throws IOException {
   202         return Files.newInputStream(path);
   204         return Files.newInputStream(path);
   203     }
   205     }
   204 
   206 
   205     @Override
   207     @Override @DefinedBy(Api.COMPILER)
   206     public OutputStream openOutputStream() throws IOException {
   208     public OutputStream openOutputStream() throws IOException {
   207         fileManager.flushCache(this);
   209         fileManager.flushCache(this);
   208         ensureParentDirectoriesExist();
   210         ensureParentDirectoriesExist();
   209         return Files.newOutputStream(path);
   211         return Files.newOutputStream(path);
   210     }
   212     }
   211 
   213 
   212     @Override
   214     @Override @DefinedBy(Api.COMPILER)
   213     public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
   215     public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
   214         CharsetDecoder decoder = fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
   216         CharsetDecoder decoder = fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
   215         return new InputStreamReader(openInputStream(), decoder);
   217         return new InputStreamReader(openInputStream(), decoder);
   216     }
   218     }
   217 
   219 
   218     @Override
   220     @Override @DefinedBy(Api.COMPILER)
   219     public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   221     public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   220         CharBuffer cb = fileManager.getCachedContent(this);
   222         CharBuffer cb = fileManager.getCachedContent(this);
   221         if (cb == null) {
   223         if (cb == null) {
   222             try (InputStream in = openInputStream()) {
   224             try (InputStream in = openInputStream()) {
   223                 ByteBuffer bb = fileManager.makeByteBuffer(in);
   225                 ByteBuffer bb = fileManager.makeByteBuffer(in);
   234             }
   236             }
   235         }
   237         }
   236         return cb;
   238         return cb;
   237     }
   239     }
   238 
   240 
   239     @Override
   241     @Override @DefinedBy(Api.COMPILER)
   240     public Writer openWriter() throws IOException {
   242     public Writer openWriter() throws IOException {
   241         fileManager.flushCache(this);
   243         fileManager.flushCache(this);
   242         ensureParentDirectoriesExist();
   244         ensureParentDirectoriesExist();
   243         return new OutputStreamWriter(Files.newOutputStream(path), fileManager.getEncodingName());
   245         return new OutputStreamWriter(Files.newOutputStream(path), fileManager.getEncodingName());
   244     }
   246     }
   245 
   247 
   246     @Override
   248     @Override @DefinedBy(Api.COMPILER)
   247     public long getLastModified() {
   249     public long getLastModified() {
   248         try {
   250         try {
   249             return Files.getLastModifiedTime(path).toMillis();
   251             return Files.getLastModifiedTime(path).toMillis();
   250         } catch (IOException e) {
   252         } catch (IOException e) {
   251             return -1;
   253             return -1;
   252         }
   254         }
   253     }
   255     }
   254 
   256 
   255     @Override
   257     @Override @DefinedBy(Api.COMPILER)
   256     public boolean delete() {
   258     public boolean delete() {
   257         try {
   259         try {
   258             Files.delete(path);
   260             Files.delete(path);
   259             return true;
   261             return true;
   260         } catch (IOException e) {
   262         } catch (IOException e) {