langtools/src/share/classes/com/sun/tools/javac/util/Constants.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
child 14359 d4099818ab70
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3994
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3994
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3994
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3994
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3994
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3994
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.javac.code.Type;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 * Utilities for operating on constant values.
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    35
 * <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
    36
 * If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
public class Constants {
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
     * Converts a constant in internal representation (in which
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
     * boolean, char, byte, short, and int are each represented by an
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
     * Integer) into standard representation.  Other values (including
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
     * null) are returned unchanged.
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    public static Object decode(Object value, Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        if (value instanceof Integer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
            int i = (Integer) value;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
            switch (type.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
            case BOOLEAN:  return i != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
            case CHAR:     return (char) i;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
            case BYTE:     return (byte) i;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
            case SHORT:    return (short) i;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        return value;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * Returns a string representation of a constant value (given in
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * internal representation), quoted and formatted as in Java source.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    public static String format(Object value, Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        value = decode(value, type);
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        switch (type.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        case BYTE:      return formatByte((Byte) value);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        case LONG:      return formatLong((Long) value);
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        case FLOAT:     return formatFloat((Float) value);
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        case DOUBLE:    return formatDouble((Double) value);
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        case CHAR:      return formatChar((Character) value);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        if (value instanceof String)
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            return formatString((String) value);
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        return value + "";
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     * Returns a string representation of a constant value (given in
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * standard wrapped representation), quoted and formatted as in
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * Java source.
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    public static String format(Object value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        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
    86
        if (value instanceof Short)     return formatShort((Short) value);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        if (value instanceof Long)      return formatLong((Long) value);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        if (value instanceof Float)     return formatFloat((Float) value);
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        if (value instanceof Double)    return formatDouble((Double) value);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        if (value instanceof Character) return formatChar((Character) value);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        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
    92
        if (value instanceof Integer ||
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 10
diff changeset
    93
            value instanceof Boolean)   return value.toString();
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 10
diff changeset
    94
        else
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 10
diff changeset
    95
            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
    96
                                               ((value == null) ?
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 10
diff changeset
    97
                                                "is a null value." :
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 10
diff changeset
    98
                                                "has class " +
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 10
diff changeset
    99
                                                value.getClass().getName()) + "." );
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    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
   103
        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
   104
    }
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 10
diff changeset
   105
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 10
diff changeset
   106
    private static String formatShort(short s) {
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 10
diff changeset
   107
        return String.format("(short)%d", s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    private static String formatLong(long lng) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        return lng + "L";
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    private static String formatFloat(float f) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        if (Float.isNaN(f))
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            return "0.0f/0.0f";
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        else if (Float.isInfinite(f))
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            return (f < 0) ? "-1.0f/0.0f" : "1.0f/0.0f";
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            return f + "f";
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    private static String formatDouble(double d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        if (Double.isNaN(d))
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            return "0.0/0.0";
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        else if (Double.isInfinite(d))
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            return (d < 0) ? "-1.0/0.0" : "1.0/0.0";
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            return d + "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    private static String formatChar(char c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        return '\'' + Convert.quote(c) + '\'';
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    private static String formatString(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        return '"' + Convert.quote(s) + '"';
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
}