src/share/classes/com/sun/tools/javac/util/Constants.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 581
f2fdd52e4e87
child 1374
c002fdee76fd
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

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

mercurial