src/share/classes/com/sun/tools/javac/file/RegularFileObject.java

changeset 424
86b773b7cb40
parent 418
4776a869fdfa
child 554
9d9f26857129
     1.1 --- a/src/share/classes/com/sun/tools/javac/file/RegularFileObject.java	Tue Oct 13 15:26:30 2009 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/file/RegularFileObject.java	Wed Oct 14 15:41:28 2009 -0700
     1.3 @@ -33,6 +33,8 @@
     1.4  import java.io.OutputStream;
     1.5  import java.io.OutputStreamWriter;
     1.6  import java.io.Writer;
     1.7 +import java.lang.ref.Reference;
     1.8 +import java.lang.ref.SoftReference;
     1.9  import java.net.URI;
    1.10  import java.nio.ByteBuffer;
    1.11  import java.nio.CharBuffer;
    1.12 @@ -53,7 +55,8 @@
    1.13       */
    1.14      private boolean hasParents = false;
    1.15      private String name;
    1.16 -    final File f;
    1.17 +    final File file;
    1.18 +    private Reference<File> absFileRef;
    1.19  
    1.20      public RegularFileObject(JavacFileManager fileManager, File f) {
    1.21          this(fileManager, f.getName(), f);
    1.22 @@ -65,17 +68,17 @@
    1.23              throw new IllegalArgumentException("directories not supported");
    1.24          }
    1.25          this.name = name;
    1.26 -        this.f = f;
    1.27 +        this.file = f;
    1.28      }
    1.29  
    1.30      @Override
    1.31      public URI toUri() {
    1.32 -        return f.toURI().normalize();
    1.33 +        return file.toURI().normalize();
    1.34      }
    1.35  
    1.36      @Override
    1.37      public String getName() {
    1.38 -        return f.getPath();
    1.39 +        return file.getPath();
    1.40      }
    1.41  
    1.42      @Override
    1.43 @@ -90,20 +93,20 @@
    1.44  
    1.45      @Override
    1.46      public InputStream openInputStream() throws IOException {
    1.47 -        return new FileInputStream(f);
    1.48 +        return new FileInputStream(file);
    1.49      }
    1.50  
    1.51      @Override
    1.52      public OutputStream openOutputStream() throws IOException {
    1.53          ensureParentDirectoriesExist();
    1.54 -        return new FileOutputStream(f);
    1.55 +        return new FileOutputStream(file);
    1.56      }
    1.57  
    1.58      @Override
    1.59      public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
    1.60          CharBuffer cb = fileManager.getCachedContent(this);
    1.61          if (cb == null) {
    1.62 -            InputStream in = new FileInputStream(f);
    1.63 +            InputStream in = new FileInputStream(file);
    1.64              try {
    1.65                  ByteBuffer bb = fileManager.makeByteBuffer(in);
    1.66                  JavaFileObject prev = fileManager.log.useSource(this);
    1.67 @@ -126,17 +129,17 @@
    1.68      @Override
    1.69      public Writer openWriter() throws IOException {
    1.70          ensureParentDirectoriesExist();
    1.71 -        return new OutputStreamWriter(new FileOutputStream(f), fileManager.getEncodingName());
    1.72 +        return new OutputStreamWriter(new FileOutputStream(file), fileManager.getEncodingName());
    1.73      }
    1.74  
    1.75      @Override
    1.76      public long getLastModified() {
    1.77 -        return f.lastModified();
    1.78 +        return file.lastModified();
    1.79      }
    1.80  
    1.81      @Override
    1.82      public boolean delete() {
    1.83 -        return f.delete();
    1.84 +        return file.delete();
    1.85      }
    1.86  
    1.87      @Override
    1.88 @@ -146,7 +149,7 @@
    1.89  
    1.90      @Override
    1.91      protected String inferBinaryName(Iterable<? extends File> path) {
    1.92 -        String fPath = f.getPath();
    1.93 +        String fPath = file.getPath();
    1.94          //System.err.println("RegularFileObject " + file + " " +r.getPath());
    1.95          for (File dir: path) {
    1.96              //System.err.println("dir: " + dir);
    1.97 @@ -178,7 +181,7 @@
    1.98          if (name.equalsIgnoreCase(n)) {
    1.99              try {
   1.100                  // allow for Windows
   1.101 -                return f.getCanonicalFile().getName().equals(n);
   1.102 +                return file.getCanonicalFile().getName().equals(n);
   1.103              } catch (IOException e) {
   1.104              }
   1.105          }
   1.106 @@ -187,7 +190,7 @@
   1.107  
   1.108      private void ensureParentDirectoriesExist() throws IOException {
   1.109          if (!hasParents) {
   1.110 -            File parent = f.getParentFile();
   1.111 +            File parent = file.getParentFile();
   1.112              if (parent != null && !parent.exists()) {
   1.113                  if (!parent.mkdirs()) {
   1.114                      if (!parent.exists() || !parent.isDirectory()) {
   1.115 @@ -199,21 +202,34 @@
   1.116          }
   1.117      }
   1.118  
   1.119 +    /**
   1.120 +     * Check if two file objects are equal.
   1.121 +     * Two RegularFileObjects are equal if the absolute paths of the underlying
   1.122 +     * files are equal.
   1.123 +     */
   1.124      @Override
   1.125      public boolean equals(Object other) {
   1.126 -        if (!(other instanceof RegularFileObject)) {
   1.127 +        if (this == other)
   1.128 +            return true;
   1.129 +
   1.130 +        if (!(other instanceof RegularFileObject))
   1.131              return false;
   1.132 -        }
   1.133 +
   1.134          RegularFileObject o = (RegularFileObject) other;
   1.135 -        try {
   1.136 -            return f.equals(o.f) || f.getCanonicalFile().equals(o.f.getCanonicalFile());
   1.137 -        } catch (IOException e) {
   1.138 -            return false;
   1.139 -        }
   1.140 +        return getAbsoluteFile().equals(o.getAbsoluteFile());
   1.141      }
   1.142  
   1.143      @Override
   1.144      public int hashCode() {
   1.145 -        return f.hashCode();
   1.146 +        return getAbsoluteFile().hashCode();
   1.147 +    }
   1.148 +
   1.149 +    private File getAbsoluteFile() {
   1.150 +        File absFile = (absFileRef == null ? null : absFileRef.get());
   1.151 +        if (absFile == null) {
   1.152 +            absFile = file.getAbsoluteFile();
   1.153 +            absFileRef = new SoftReference<File>(absFile);
   1.154 +        }
   1.155 +        return absFile;
   1.156      }
   1.157  }

mercurial