aoqi@0: /* aoqi@0: * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.javac.util; aoqi@0: aoqi@0: import com.sun.tools.javac.code.Type; aoqi@0: aoqi@0: /** aoqi@0: * Utilities for operating on constant values. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: */ aoqi@0: public class Constants { aoqi@0: aoqi@0: /** aoqi@0: * Converts a constant in internal representation (in which aoqi@0: * boolean, char, byte, short, and int are each represented by an aoqi@0: * Integer) into standard representation. Other values (including aoqi@0: * null) are returned unchanged. aoqi@0: */ aoqi@0: public static Object decode(Object value, Type type) { aoqi@0: if (value instanceof Integer) { aoqi@0: int i = (Integer) value; aoqi@0: switch (type.getTag()) { aoqi@0: case BOOLEAN: return i != 0; aoqi@0: case CHAR: return (char) i; aoqi@0: case BYTE: return (byte) i; aoqi@0: case SHORT: return (short) i; aoqi@0: } aoqi@0: } aoqi@0: return value; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a string representation of a constant value (given in aoqi@0: * internal representation), quoted and formatted as in Java source. aoqi@0: */ aoqi@0: public static String format(Object value, Type type) { aoqi@0: value = decode(value, type); aoqi@0: switch (type.getTag()) { aoqi@0: case BYTE: return formatByte((Byte) value); aoqi@0: case LONG: return formatLong((Long) value); aoqi@0: case FLOAT: return formatFloat((Float) value); aoqi@0: case DOUBLE: return formatDouble((Double) value); aoqi@0: case CHAR: return formatChar((Character) value); aoqi@0: } aoqi@0: if (value instanceof String) aoqi@0: return formatString((String) value); aoqi@0: return value + ""; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a string representation of a constant value (given in aoqi@0: * standard wrapped representation), quoted and formatted as in aoqi@0: * Java source. aoqi@0: */ aoqi@0: public static String format(Object value) { aoqi@0: if (value instanceof Byte) return formatByte((Byte) value); aoqi@0: if (value instanceof Short) return formatShort((Short) value); aoqi@0: if (value instanceof Long) return formatLong((Long) value); aoqi@0: if (value instanceof Float) return formatFloat((Float) value); aoqi@0: if (value instanceof Double) return formatDouble((Double) value); aoqi@0: if (value instanceof Character) return formatChar((Character) value); aoqi@0: if (value instanceof String) return formatString((String) value); aoqi@0: if (value instanceof Integer || aoqi@0: value instanceof Boolean) return value.toString(); aoqi@0: else aoqi@0: throw new IllegalArgumentException("Argument is not a primitive type or a string; it " + aoqi@0: ((value == null) ? aoqi@0: "is a null value." : aoqi@0: "has class " + aoqi@0: value.getClass().getName()) + "." ); aoqi@0: } aoqi@0: aoqi@0: private static String formatByte(byte b) { aoqi@0: return String.format("(byte)0x%02x", b); aoqi@0: } aoqi@0: aoqi@0: private static String formatShort(short s) { aoqi@0: return String.format("(short)%d", s); aoqi@0: } aoqi@0: aoqi@0: private static String formatLong(long lng) { aoqi@0: return lng + "L"; aoqi@0: } aoqi@0: aoqi@0: private static String formatFloat(float f) { aoqi@0: if (Float.isNaN(f)) aoqi@0: return "0.0f/0.0f"; aoqi@0: else if (Float.isInfinite(f)) aoqi@0: return (f < 0) ? "-1.0f/0.0f" : "1.0f/0.0f"; aoqi@0: else aoqi@0: return f + "f"; aoqi@0: } aoqi@0: aoqi@0: private static String formatDouble(double d) { aoqi@0: if (Double.isNaN(d)) aoqi@0: return "0.0/0.0"; aoqi@0: else if (Double.isInfinite(d)) aoqi@0: return (d < 0) ? "-1.0/0.0" : "1.0/0.0"; aoqi@0: else aoqi@0: return d + ""; aoqi@0: } aoqi@0: aoqi@0: private static String formatChar(char c) { aoqi@0: return '\'' + Convert.quote(c) + '\''; aoqi@0: } aoqi@0: aoqi@0: private static String formatString(String s) { aoqi@0: return '"' + Convert.quote(s) + '"'; aoqi@0: } aoqi@0: }