001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javac.util;
027:
028: import com.sun.tools.javac.code.Type;
029:
030: import static com.sun.tools.javac.code.TypeTags.*;
031:
032: /**
033: * Utilities for operating on constant values.
034: *
035: * <p><b>This is NOT part of any API supported by Sun Microsystems. If
036: * you write code that depends on this, you do so at your own risk.
037: * This code and its internal interfaces are subject to change or
038: * deletion without notice.</b>
039: */
040: @Version("@(#)Constants.java 1.7 07/05/05")
041: public class Constants {
042:
043: /**
044: * Converts a constant in internal representation (in which
045: * boolean, char, byte, short, and int are each represented by an
046: * Integer) into standard representation. Other values (including
047: * null) are returned unchanged.
048: */
049: public static Object decode(Object value, Type type) {
050: if (value instanceof Integer) {
051: int i = (Integer) value;
052: switch (type.tag) {
053: case BOOLEAN:
054: return i != 0;
055: case CHAR:
056: return (char) i;
057: case BYTE:
058: return (byte) i;
059: case SHORT:
060: return (short) i;
061: }
062: }
063: return value;
064: }
065:
066: /**
067: * Returns a string representation of a constant value (given in
068: * internal representation), quoted and formatted as in Java source.
069: */
070: public static String format(Object value, Type type) {
071: value = decode(value, type);
072: switch (type.tag) {
073: case BYTE:
074: return formatByte((Byte) value);
075: case LONG:
076: return formatLong((Long) value);
077: case FLOAT:
078: return formatFloat((Float) value);
079: case DOUBLE:
080: return formatDouble((Double) value);
081: case CHAR:
082: return formatChar((Character) value);
083: }
084: if (value instanceof String)
085: return formatString((String) value);
086: return value + "";
087: }
088:
089: /**
090: * Returns a string representation of a constant value (given in
091: * standard wrapped representation), quoted and formatted as in
092: * Java source.
093: */
094: public static String format(Object value) {
095: if (value instanceof Byte)
096: return formatByte((Byte) value);
097: if (value instanceof Long)
098: return formatLong((Long) value);
099: if (value instanceof Float)
100: return formatFloat((Float) value);
101: if (value instanceof Double)
102: return formatDouble((Double) value);
103: if (value instanceof Character)
104: return formatChar((Character) value);
105: if (value instanceof String)
106: return formatString((String) value);
107: return value + "";
108: }
109:
110: private static String formatByte(byte b) {
111: return String.format("0x%02x", b);
112: }
113:
114: private static String formatLong(long lng) {
115: return lng + "L";
116: }
117:
118: private static String formatFloat(float f) {
119: if (Float.isNaN(f))
120: return "0.0f/0.0f";
121: else if (Float.isInfinite(f))
122: return (f < 0) ? "-1.0f/0.0f" : "1.0f/0.0f";
123: else
124: return f + "f";
125: }
126:
127: private static String formatDouble(double d) {
128: if (Double.isNaN(d))
129: return "0.0/0.0";
130: else if (Double.isInfinite(d))
131: return (d < 0) ? "-1.0/0.0" : "1.0/0.0";
132: else
133: return d + "";
134: }
135:
136: private static String formatChar(char c) {
137: return '\'' + Convert.quote(c) + '\'';
138: }
139:
140: private static String formatString(String s) {
141: return '"' + Convert.quote(s) + '"';
142: }
143: }
|