author | briangoetz |
Wed, 18 Dec 2013 10:29:25 -0500 | |
changeset 22159 | 682da512ec17 |
parent 14359 | d4099818ab70 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
5847
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.util; |
|
27 |
||
28 |
import com.sun.tools.javac.code.Type; |
|
29 |
||
30 |
/** |
|
31 |
* Utilities for operating on constant values. |
|
32 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
33 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
34 |
* If you write code that depends on this, you do so at your own risk. |
10 | 35 |
* This code and its internal interfaces are subject to change or |
36 |
* deletion without notice.</b> |
|
37 |
*/ |
|
38 |
public class Constants { |
|
39 |
||
40 |
/** |
|
41 |
* Converts a constant in internal representation (in which |
|
42 |
* boolean, char, byte, short, and int are each represented by an |
|
43 |
* Integer) into standard representation. Other values (including |
|
44 |
* null) are returned unchanged. |
|
45 |
*/ |
|
46 |
public static Object decode(Object value, Type type) { |
|
47 |
if (value instanceof Integer) { |
|
48 |
int i = (Integer) value; |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
5847
diff
changeset
|
49 |
switch (type.getTag()) { |
10 | 50 |
case BOOLEAN: return i != 0; |
51 |
case CHAR: return (char) i; |
|
52 |
case BYTE: return (byte) i; |
|
53 |
case SHORT: return (short) i; |
|
54 |
} |
|
55 |
} |
|
56 |
return value; |
|
57 |
} |
|
58 |
||
59 |
/** |
|
60 |
* Returns a string representation of a constant value (given in |
|
61 |
* internal representation), quoted and formatted as in Java source. |
|
62 |
*/ |
|
63 |
public static String format(Object value, Type type) { |
|
64 |
value = decode(value, type); |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
5847
diff
changeset
|
65 |
switch (type.getTag()) { |
10 | 66 |
case BYTE: return formatByte((Byte) value); |
67 |
case LONG: return formatLong((Long) value); |
|
68 |
case FLOAT: return formatFloat((Float) value); |
|
69 |
case DOUBLE: return formatDouble((Double) value); |
|
70 |
case CHAR: return formatChar((Character) value); |
|
71 |
} |
|
72 |
if (value instanceof String) |
|
73 |
return formatString((String) value); |
|
74 |
return value + ""; |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Returns a string representation of a constant value (given in |
|
79 |
* standard wrapped representation), quoted and formatted as in |
|
80 |
* Java source. |
|
81 |
*/ |
|
82 |
public static String format(Object value) { |
|
83 |
if (value instanceof Byte) return formatByte((Byte) value); |
|
3994
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
84 |
if (value instanceof Short) return formatShort((Short) value); |
10 | 85 |
if (value instanceof Long) return formatLong((Long) value); |
86 |
if (value instanceof Float) return formatFloat((Float) value); |
|
87 |
if (value instanceof Double) return formatDouble((Double) value); |
|
88 |
if (value instanceof Character) return formatChar((Character) value); |
|
89 |
if (value instanceof String) return formatString((String) value); |
|
3994
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
90 |
if (value instanceof Integer || |
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
91 |
value instanceof Boolean) return value.toString(); |
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
92 |
else |
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
93 |
throw new IllegalArgumentException("Argument is not a primitive type or a string; it " + |
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
94 |
((value == null) ? |
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
95 |
"is a null value." : |
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
96 |
"has class " + |
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
97 |
value.getClass().getName()) + "." ); |
10 | 98 |
} |
99 |
||
100 |
private static String formatByte(byte b) { |
|
3994
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
101 |
return String.format("(byte)0x%02x", b); |
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
102 |
} |
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
103 |
|
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
104 |
private static String formatShort(short s) { |
7df1ecd5eadb
6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents:
10
diff
changeset
|
105 |
return String.format("(short)%d", s); |
10 | 106 |
} |
107 |
||
108 |
private static String formatLong(long lng) { |
|
109 |
return lng + "L"; |
|
110 |
} |
|
111 |
||
112 |
private static String formatFloat(float f) { |
|
113 |
if (Float.isNaN(f)) |
|
114 |
return "0.0f/0.0f"; |
|
115 |
else if (Float.isInfinite(f)) |
|
116 |
return (f < 0) ? "-1.0f/0.0f" : "1.0f/0.0f"; |
|
117 |
else |
|
118 |
return f + "f"; |
|
119 |
} |
|
120 |
||
121 |
private static String formatDouble(double d) { |
|
122 |
if (Double.isNaN(d)) |
|
123 |
return "0.0/0.0"; |
|
124 |
else if (Double.isInfinite(d)) |
|
125 |
return (d < 0) ? "-1.0/0.0" : "1.0/0.0"; |
|
126 |
else |
|
127 |
return d + ""; |
|
128 |
} |
|
129 |
||
130 |
private static String formatChar(char c) { |
|
131 |
return '\'' + Convert.quote(c) + '\''; |
|
132 |
} |
|
133 |
||
134 |
private static String formatString(String s) { |
|
135 |
return '"' + Convert.quote(s) + '"'; |
|
136 |
} |
|
137 |
} |