|
1 /* |
|
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
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 |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.sjavac.comp; |
|
27 |
|
28 import java.io.*; |
|
29 import java.net.URI; |
|
30 import javax.lang.model.element.Modifier; |
|
31 import javax.lang.model.element.NestingKind; |
|
32 import javax.tools.JavaFileObject; |
|
33 |
|
34 /** |
|
35 * The SmartFileObject will return an outputstream that cache the written data |
|
36 * and compare the new content with the old content on disk. Only if they differ, |
|
37 * will the file be updated. |
|
38 * |
|
39 * <p><b>This is NOT part of any supported API. |
|
40 * If you write code that depends on this, you do so at your own |
|
41 * risk. This code and its internal interfaces are subject to change |
|
42 * or deletion without notice.</b></p> |
|
43 */ |
|
44 public class SmartFileObject implements JavaFileObject { |
|
45 |
|
46 JavaFileObject file; |
|
47 PrintWriter stdout; |
|
48 |
|
49 public SmartFileObject(JavaFileObject r, PrintWriter pw) { |
|
50 file = r; |
|
51 stdout = pw; |
|
52 } |
|
53 |
|
54 @Override |
|
55 public boolean equals(Object other) { |
|
56 return file.equals(other); |
|
57 } |
|
58 |
|
59 @Override |
|
60 public int hashCode() { |
|
61 return file.hashCode(); |
|
62 } |
|
63 |
|
64 public Kind getKind() { |
|
65 return file.getKind(); |
|
66 } |
|
67 |
|
68 public boolean isNameCompatible(String simpleName, Kind kind) { |
|
69 return file.isNameCompatible(simpleName, kind); |
|
70 } |
|
71 |
|
72 public URI toUri() { |
|
73 return file.toUri(); |
|
74 } |
|
75 |
|
76 public String getName() { |
|
77 return file.getName(); |
|
78 } |
|
79 |
|
80 public InputStream openInputStream() throws IOException { |
|
81 return file.openInputStream(); |
|
82 } |
|
83 |
|
84 public OutputStream openOutputStream() throws IOException { |
|
85 return file.openOutputStream(); |
|
86 } |
|
87 |
|
88 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
|
89 return file.getCharContent(ignoreEncodingErrors); |
|
90 } |
|
91 |
|
92 static String lineseparator = System.getProperty("line.separator"); |
|
93 |
|
94 public Writer openWriter() throws IOException { |
|
95 StringBuilder s = new StringBuilder(); |
|
96 try (BufferedReader r = new BufferedReader(file.openReader(true))) { |
|
97 while (r.ready()) { |
|
98 s.append(r.readLine()+lineseparator); |
|
99 } |
|
100 } catch (FileNotFoundException e) { |
|
101 // Perfectly ok. |
|
102 } |
|
103 return new SmartWriter(file, s.toString(), file.getName(), stdout); |
|
104 } |
|
105 |
|
106 public long getLastModified() { |
|
107 return file.getLastModified(); |
|
108 } |
|
109 |
|
110 public boolean delete() { |
|
111 return file.delete(); |
|
112 } |
|
113 |
|
114 public Modifier getAccessLevel() { |
|
115 return file.getAccessLevel(); |
|
116 } |
|
117 |
|
118 public NestingKind getNestingKind() { |
|
119 return file.getNestingKind(); |
|
120 } |
|
121 |
|
122 public Reader openReader(boolean ignoreEncodingErrors) throws IOException { |
|
123 return file.openReader(ignoreEncodingErrors); |
|
124 } |
|
125 |
|
126 } |