aboutsummaryrefslogtreecommitdiff
path: root/java/openjdk6/files/icedtea/openjdk/4111861-static_fields.patch
blob: 9612d7b0b577c689a68a990d96845f88a2c44c6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# HG changeset patch
# User jjg
# Date 1217887742 25200
#      Mon Aug 04 15:09:02 2008 -0700
# Node ID 6134c146043a3e9dd12ee73ca32ce56ac1c95e3a
# Parent  17dfaebe23044c48bcd5ed0730ce2358543ac459
4111861: static final field contents are not displayed
Reviewed-by: ksrini

diff -r 17dfaebe2304 -r 6134c146043a src/share/classes/com/sun/tools/javap/ClassWriter.java
--- langtools/src/share/classes/com/sun/tools/javap/ClassWriter.java	Tue May 19 11:43:50 2009 -0700
+++ langtools/src/share/classes/com/sun/tools/javap/ClassWriter.java	Mon Aug 04 15:09:02 2008 -0700
@@ -35,6 +35,7 @@
 import com.sun.tools.classfile.Code_attribute;
 import com.sun.tools.classfile.ConstantPool;
 import com.sun.tools.classfile.ConstantPoolException;
+import com.sun.tools.classfile.ConstantValue_attribute;
 import com.sun.tools.classfile.Descriptor;
 import com.sun.tools.classfile.DescriptorException;
 import com.sun.tools.classfile.Exceptions_attribute;
@@ -189,6 +190,14 @@
         }
         print(" ");
         print(getFieldName(f));
+        if (options.showConstants && !options.compat) { // BUG 4111861 print static final field contents
+            Attribute a = f.attributes.get(Attribute.ConstantValue);
+            if (a instanceof ConstantValue_attribute) {
+                print(" = ");
+                ConstantValue_attribute cv = (ConstantValue_attribute) a;
+                print(getConstantValue(f.descriptor, cv.constantvalue_index));
+            }
+        }
         print(";");
         println();
 
@@ -485,6 +494,81 @@
         }
     }
 
+    /**
+     * Get the value of an entry in the constant pool as a Java constant.
+     * Characters and booleans are represented by CONSTANT_Intgere entries.
+     * Character and string values are processed to escape characters outside
+     * the basic printable ASCII set.
+     * @param d the descriptor, giving the expected type of the constant
+     * @param index the index of the value in the constant pool
+     * @return a printable string containing the value of the constant.
+     */
+    String getConstantValue(Descriptor d, int index) {
+        try {
+            ConstantPool.CPInfo cpInfo = constant_pool.get(index);
+
+            switch (cpInfo.getTag()) {
+                case ConstantPool.CONSTANT_Integer: {
+                    ConstantPool.CONSTANT_Integer_info info =
+                            (ConstantPool.CONSTANT_Integer_info) cpInfo;
+                    String t = d.getValue(constant_pool);
+                    if (t.equals("C")) { // character
+                        return getConstantCharValue((char) info.value);
+                    } else if (t.equals("Z")) { // boolean
+                        return String.valueOf(info.value == 1);
+                    } else { // other: assume integer
+                        return String.valueOf(info.value);
+                    }
+                }
+
+                case ConstantPool.CONSTANT_String: {
+                    ConstantPool.CONSTANT_String_info info =
+                            (ConstantPool.CONSTANT_String_info) cpInfo;
+                    return getConstantStringValue(info.getString());
+                }
+
+                default:
+                    return constantWriter.stringValue(cpInfo);
+            }
+        } catch (ConstantPoolException e) {
+            return "#" + index;
+        }
+    }
+
+    private String getConstantCharValue(char c) {
+        StringBuilder sb = new StringBuilder();
+        sb.append('\'');
+        sb.append(esc(c, '\''));
+        sb.append('\'');
+        return sb.toString();
+    }
+
+    private String getConstantStringValue(String s) {
+        StringBuilder sb = new StringBuilder();
+        sb.append("\"");
+        for (int i = 0; i < s.length(); i++) {
+            sb.append(esc(s.charAt(i), '"'));
+        }
+        sb.append("\"");
+        return sb.toString();
+    }
+
+    private String esc(char c, char quote) {
+        if (32 <= c && c <= 126 && c != quote)
+            return String.valueOf(c);
+        else switch (c) {
+            case '\b': return "\\b";
+            case '\n': return "\\n";
+            case '\t': return "\\t";
+            case '\f': return "\\f";
+            case '\r': return "\\r";
+            case '\\': return "\\\\";
+            case '\'': return "\\'";
+            case '\"': return "\\\"";
+            default:   return String.format("\\u%04x", (int) c);
+        }
+    }
+
     private Options options;
     private AttributeWriter attrWriter;
     private CodeWriter codeWriter;
diff -r 17dfaebe2304 -r 6134c146043a src/share/classes/com/sun/tools/javap/JavapTask.java
--- langtools/src/share/classes/com/sun/tools/javap/JavapTask.java	Tue May 19 11:43:50 2009 -0700
+++ langtools/src/share/classes/com/sun/tools/javap/JavapTask.java	Mon Aug 04 15:09:02 2008 -0700
@@ -222,6 +222,12 @@
             void process(JavapTask task, String opt, String arg) {
                 task.options.ignoreSymbolFile = true;
             }
+        },
+
+        new Option(false, "-constants") {
+            void process(JavapTask task, String opt, String arg) {
+                task.options.showConstants = true;
+            }
         }
 
     };
diff -r 17dfaebe2304 -r 6134c146043a src/share/classes/com/sun/tools/javap/Options.java
--- langtools/src/share/classes/com/sun/tools/javap/Options.java	Tue May 19 11:43:50 2009 -0700
+++ langtools/src/share/classes/com/sun/tools/javap/Options.java	Mon Aug 04 15:09:02 2008 -0700
@@ -77,6 +77,7 @@
     public boolean showDisassembled;
     public boolean showInternalSignatures;
     public boolean showAllAttrs;
+    public boolean showConstants;
 
     public boolean compat;             // bug-for-bug compatibility mode with old javap
     public boolean jsr277;
diff -r 17dfaebe2304 -r 6134c146043a src/share/classes/com/sun/tools/javap/resources/javap.properties
--- langtools/src/share/classes/com/sun/tools/javap/resources/javap.properties	Tue May 19 11:43:50 2009 -0700
+++ langtools/src/share/classes/com/sun/tools/javap/resources/javap.properties	Mon Aug 04 15:09:02 2008 -0700
@@ -58,5 +58,9 @@
 main.opt.bootclasspath=\
 \  -bootclasspath <path>    Override location of bootstrap class files
 
+main.opt.constants=\
+\  -constants               Show static final constants
 
 
+
+
diff -r 17dfaebe2304 -r 6134c146043a test/tools/javap/4111861/A.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ langtools/test/tools/javap/4111861/A.java	Mon Aug 04 15:09:02 2008 -0700
@@ -0,0 +1,14 @@
+class A {
+    public static final int i = 42;
+    public static final boolean b = true;
+    public static final float f = 1.0f;
+    public static final double d = 1.0d;
+    public static final short s = 1;
+    public static final long l = 1l;
+    public static final char cA = 'A';
+    public static final char c0 = '\u0000';
+    public static final char cn = '\n';
+    public static final char cq1 = '\'';
+    public static final char cq2 = '"';
+    public static final java.lang.String t1 = "abc \u0000 \f\n\r\t'\"";
+}
diff -r 17dfaebe2304 -r 6134c146043a test/tools/javap/4111861/T4111861.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ langtools/test/tools/javap/4111861/T4111861.java	Mon Aug 04 15:09:02 2008 -0700
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+import java.io.*;
+
+/*
+ * @test
+ * @bug 4111861
+ * @summary static final field contents are not displayed
+ */
+public class T4111861 {
+    public static void main(String... args) throws Exception {
+        new T4111861().run();
+    }
+
+    void run() throws Exception {
+        File testSrc = new File(System.getProperty("test.src", "."));
+        File a_java = new File(testSrc, "A.java");
+        javac("-d", ".", a_java.getPath());
+
+        String out = javap("-classpath", ".", "-constants", "A");
+
+        String a = read(a_java);
+
+        if (!filter(out).equals(filter(read(a_java)))) {
+            System.out.println(out);
+            throw new Exception("unexpected output");
+        }
+    }
+
+    String javac(String... args) throws Exception {
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        int rc = com.sun.tools.javac.Main.compile(args, pw);
+        if (rc != 0)
+            throw new Exception("javac failed, rc=" + rc);
+        return sw.toString();
+    }
+
+    String javap(String... args) throws Exception {
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        int rc = com.sun.tools.javap.Main.run(args, pw);
+        if (rc != 0)
+            throw new Exception("javap failed, rc=" + rc);
+        return sw.toString();
+    }
+
+    String read(File f) throws IOException {
+        StringBuilder sb = new StringBuilder();
+        BufferedReader in = new BufferedReader(new FileReader(f));
+        try {
+            String line;
+            while ((line = in.readLine()) != null) {
+                sb.append(line);
+                sb.append('\n');
+            }
+        } finally {
+            in.close();
+        }
+        return sb.toString();
+    }
+
+    // return those lines beginning "public static final"
+    String filter(String s) throws IOException {
+        StringBuilder sb = new StringBuilder();
+        BufferedReader in = new BufferedReader(new StringReader(s));
+        try {
+            String line;
+            while ((line = in.readLine()) != null) {
+                if (line.indexOf("public static final") > 0) {
+                    sb.append(line);
+                    sb.append('\n');
+                }
+            }
+        } finally {
+            in.close();
+        }
+        return sb.toString();
+    }
+}