langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/nio/PathFileObject.java
changeset 27579 d1a63c99cdd5
parent 26266 2d24bda701dc
child 29291 076c277565f7
equal deleted inserted replaced
27578:d61af14a5cf7 27579:d1a63c99cdd5
     1 /*
     1 /*
     2  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    60  *  <p><b>This is NOT part of any supported API.
    60  *  <p><b>This is NOT part of any supported API.
    61  *  If you write code that depends on this, you do so at your own risk.
    61  *  If you write code that depends on this, you do so at your own risk.
    62  *  This code and its internal interfaces are subject to change or
    62  *  This code and its internal interfaces are subject to change or
    63  *  deletion without notice.</b>
    63  *  deletion without notice.</b>
    64  */
    64  */
    65 abstract class PathFileObject implements JavaFileObject {
    65 public abstract class PathFileObject implements JavaFileObject {
    66     private JavacPathFileManager fileManager;
    66     private final BaseFileManager fileManager;
    67     private Path path;
    67     private final Path path;
    68 
    68 
    69     /**
    69     /**
    70      * Create a PathFileObject within a directory, such that the binary name
    70      * Create a PathFileObject within a directory, such that the binary name
    71      * can be inferred from the relationship to the parent directory.
    71      * can be inferred from the relationship to the parent directory.
    72      */
    72      */
    73     static PathFileObject createDirectoryPathFileObject(JavacPathFileManager fileManager,
    73     static PathFileObject createDirectoryPathFileObject(BaseFileManager fileManager,
    74             final Path path, final Path dir) {
    74             final Path path, final Path dir) {
    75         return new PathFileObject(fileManager, path) {
    75         return new PathFileObject(fileManager, path) {
    76             @Override
    76             @Override
    77             String inferBinaryName(Iterable<? extends Path> paths) {
    77             public String inferBinaryName(Iterable<? extends Path> paths) {
    78                 return toBinaryName(dir.relativize(path));
    78                 return toBinaryName(dir.relativize(path));
    79             }
    79             }
    80         };
    80         };
    81     }
    81     }
    82 
    82 
    83     /**
    83     /**
    84      * Create a PathFileObject in a file system such as a jar file, such that
    84      * Create a PathFileObject in a file system such as a jar file, such that
    85      * the binary name can be inferred from its position within the filesystem.
    85      * the binary name can be inferred from its position within the filesystem.
    86      */
    86      */
    87     static PathFileObject createJarPathFileObject(JavacPathFileManager fileManager,
    87     public static PathFileObject createJarPathFileObject(BaseFileManager fileManager,
    88             final Path path) {
    88             final Path path) {
    89         return new PathFileObject(fileManager, path) {
    89         return new PathFileObject(fileManager, path) {
    90             @Override
    90             @Override
    91             String inferBinaryName(Iterable<? extends Path> paths) {
    91             public String inferBinaryName(Iterable<? extends Path> paths) {
    92                 return toBinaryName(path);
    92                 return toBinaryName(path);
       
    93             }
       
    94         };
       
    95     }
       
    96 
       
    97     /**
       
    98      * Create a PathFileObject in a modular file system, such as jrt:, such that
       
    99      * the binary name can be inferred from its position within the filesystem.
       
   100      */
       
   101     public static PathFileObject createJRTPathFileObject(BaseFileManager fileManager,
       
   102             final Path path) {
       
   103         return new PathFileObject(fileManager, path) {
       
   104             @Override
       
   105             public String inferBinaryName(Iterable<? extends Path> paths) {
       
   106                 // use subpath to ignore the leading component containing the module name
       
   107                 return toBinaryName(path.subpath(1, path.getNameCount()));
    93             }
   108             }
    94         };
   109         };
    95     }
   110     }
    96 
   111 
    97     /**
   112     /**
    98      * Create a PathFileObject whose binary name can be inferred from the
   113      * Create a PathFileObject whose binary name can be inferred from the
    99      * relative path to a sibling.
   114      * relative path to a sibling.
   100      */
   115      */
   101     static PathFileObject createSiblingPathFileObject(JavacPathFileManager fileManager,
   116     static PathFileObject createSiblingPathFileObject(BaseFileManager fileManager,
   102             final Path path, final String relativePath) {
   117             final Path path, final String relativePath) {
   103         return new PathFileObject(fileManager, path) {
   118         return new PathFileObject(fileManager, path) {
   104             @Override
   119             @Override
   105             String inferBinaryName(Iterable<? extends Path> paths) {
   120             public String inferBinaryName(Iterable<? extends Path> paths) {
   106                 return toBinaryName(relativePath, "/");
   121                 return toBinaryName(relativePath, "/");
   107             }
   122             }
   108         };
   123         };
   109     }
   124     }
   110 
   125 
   111     /**
   126     /**
   112      * Create a PathFileObject whose binary name might be inferred from its
   127      * Create a PathFileObject whose binary name might be inferred from its
   113      * position on a search path.
   128      * position on a search path.
   114      */
   129      */
   115     static PathFileObject createSimplePathFileObject(JavacPathFileManager fileManager,
   130     static PathFileObject createSimplePathFileObject(BaseFileManager fileManager,
   116             final Path path) {
   131             final Path path) {
   117         return new PathFileObject(fileManager, path) {
   132         return new PathFileObject(fileManager, path) {
   118             @Override
   133             @Override
   119             String inferBinaryName(Iterable<? extends Path> paths) {
   134             public String inferBinaryName(Iterable<? extends Path> paths) {
   120                 Path absPath = path.toAbsolutePath();
   135                 Path absPath = path.toAbsolutePath();
   121                 for (Path p: paths) {
   136                 for (Path p: paths) {
   122                     Path ap = p.toAbsolutePath();
   137                     Path ap = p.toAbsolutePath();
   123                     if (absPath.startsWith(ap)) {
   138                     if (absPath.startsWith(ap)) {
   124                         try {
   139                         try {
   133                 return null;
   148                 return null;
   134             }
   149             }
   135         };
   150         };
   136     }
   151     }
   137 
   152 
   138     protected PathFileObject(JavacPathFileManager fileManager, Path path) {
   153     protected PathFileObject(BaseFileManager fileManager, Path path) {
   139         fileManager.getClass(); // null check
   154         fileManager.getClass(); // null check
   140         path.getClass();        // null check
   155         path.getClass();        // null check
   141         this.fileManager = fileManager;
   156         this.fileManager = fileManager;
   142         this.path = path;
   157         this.path = path;
   143     }
   158     }
   144 
   159 
   145     abstract String inferBinaryName(Iterable<? extends Path> paths);
   160     public abstract String inferBinaryName(Iterable<? extends Path> paths);
   146 
   161 
   147     /**
   162     /**
   148      * Return the Path for this object.
   163      * Return the Path for this object.
   149      * @return the Path for this object.
   164      * @return the Path for this object.
   150      */
   165      */
   151     Path getPath() {
   166     public Path getPath() {
   152         return path;
   167         return path;
   153     }
   168     }
   154 
   169 
   155     @Override @DefinedBy(Api.COMPILER)
   170     @Override @DefinedBy(Api.COMPILER)
   156     public Kind getKind() {
   171     public Kind getKind() {