author | alundblad |
Wed, 13 Aug 2014 14:44:59 +0200 | |
changeset 26098 | 32588700060b |
parent 22448 | a85fbad9d687 |
permissions | -rw-r--r-- |
15368
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
1 |
/* |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
22448
diff
changeset
|
2 |
* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. |
15368
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
4 |
* |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
10 |
* |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
15 |
* accompanied this code). |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
16 |
* |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
20 |
* |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
23 |
* questions. |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
24 |
*/ |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
25 |
package com.sun.tools.sjavac.comp; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
26 |
|
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
27 |
import java.io.*; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
28 |
import javax.tools.JavaFileObject; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
29 |
|
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
30 |
/** |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
31 |
* The SmartWriter will cache the written data and when the writer is closed, |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
32 |
* then it will compare the cached data with the old_content string. |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
33 |
* If different, then it will write all the new content to the file. |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
34 |
* If not, the file is not touched. |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
35 |
* |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
36 |
* <p><b>This is NOT part of any supported API. |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
22448
diff
changeset
|
37 |
* If you write code that depends on this, you do so at your own risk. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
22448
diff
changeset
|
38 |
* This code and its internal interfaces are subject to change or |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
22448
diff
changeset
|
39 |
* deletion without notice.</b> |
15368
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
40 |
*/ |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
41 |
public class SmartWriter extends Writer { |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
42 |
|
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
43 |
String name; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
44 |
JavaFileObject file; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
45 |
String oldContent; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
46 |
StringWriter newContent = new StringWriter(); |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
47 |
PrintWriter stdout; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
48 |
boolean closed; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
49 |
public SmartWriter(JavaFileObject f, String s, String n, PrintWriter pw) { |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
50 |
name = n; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
51 |
file = f; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
52 |
oldContent = s; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
53 |
newContent = new StringWriter(); |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
54 |
stdout = pw; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
55 |
closed = false; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
56 |
} |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
57 |
|
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
58 |
public void write(char[] chars, int i, int i1) |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
59 |
{ |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
60 |
newContent.write(chars, i, i1); |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
61 |
} |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
62 |
|
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
63 |
public void close() throws IOException { |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
64 |
if (closed) return; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
65 |
closed = true; |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
66 |
String s = newContent.toString(); |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
67 |
if (!oldContent.equals(s)) { |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
68 |
int p = file.getName().lastIndexOf(File.separatorChar); |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
69 |
try (Writer writer = file.openWriter()) { |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
70 |
writer.write(s); |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
71 |
} |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
72 |
stdout.println("Writing "+file.getName().substring(p+1)); |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
73 |
} |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
74 |
} |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
75 |
|
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
76 |
public void flush() throws IOException { |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
77 |
} |
2577ddb7e710
8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff
changeset
|
78 |
} |