langtools/src/share/classes/com/sun/tools/apt/mirror/declaration/Constants.java
author mcimadamore
Tue, 13 Jan 2009 13:27:14 +0000
changeset 1789 7ac8c0815000
parent 10 06bc494ca11e
child 3378 22011d9a9398
permissions -rw-r--r--
6765045: Remove rawtypes warnings from langtools Summary: Removed all occurrences of rawtypes warnings from langtools Reviewed-by: jjg, bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
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
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.apt.mirror.declaration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.Collection;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.mirror.declaration.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.mirror.type.TypeMirror;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.apt.mirror.type.TypeMirrorImpl;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.code.Type;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * Utility class for operating on constant expressions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
class Constants {
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
     * Converts a constant in javac's internal representation (in which
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
     * boolean, char, byte, short, and int are each represented by an Integer)
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
     * into standard representation.  Other values (including null) are
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
     * returned unchanged.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    static Object decodeConstant(Object value, Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        if (value instanceof Integer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
            int i = ((Integer) value).intValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
            switch (type.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
            case BOOLEAN:  return Boolean.valueOf(i != 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
            case CHAR:     return Character.valueOf((char) i);
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
            case BYTE:     return Byte.valueOf((byte) i);
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
            case SHORT:    return Short.valueOf((short) i);
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        return value;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * Returns a formatter for generating the text of constant
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     * expressions.  Equivalent to
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     * <tt>getFormatter(new StringBuilder())</tt>.
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    static Formatter getFormatter() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        return new Formatter(new StringBuilder());
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * Returns a formatter for generating the text of constant
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * expressions.  Also generates the text of constant
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     * "pseudo-expressions" for annotations and array-valued
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     * annotation elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * @param buf  where the expression is written
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    static Formatter getFormatter(StringBuilder buf) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        return new Formatter(buf);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * Utility class used to generate the text of constant
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     * expressions.  Also generates the text of constant
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * "pseudo-expressions" for annotations and array-valued
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * annotation elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    static class Formatter {
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        private StringBuilder buf;      // where the output goes
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        private Formatter(StringBuilder buf) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
            this.buf = buf;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            return buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
         * Appends a constant whose type is not statically known
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
         * by dispatching to the appropriate overloaded append method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        void append(Object val) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            if (val instanceof String) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
                append((String) val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            } else if (val instanceof Character) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
                append((Character) val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            } else if (val instanceof Boolean) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                append((Boolean) val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            } else if (val instanceof Byte) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
                append((Byte) val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            } else if (val instanceof Short) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                append((Short) val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            } else if (val instanceof Integer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
                append((Integer) val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            } else if (val instanceof Long) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                append((Long) val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            } else if (val instanceof Float) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                append((Float) val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            } else if (val instanceof Double) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                append((Double) val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            } else if (val instanceof TypeMirror) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                append((TypeMirrorImpl) val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            } else if (val instanceof EnumConstantDeclaration) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                append((EnumConstantDeclarationImpl) val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            } else if (val instanceof AnnotationMirror) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                append((AnnotationMirrorImpl) val);
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
   133
            } else if (val instanceof Collection<?>) {
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
   134
                append((Collection<?>) val);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                appendUnquoted(val.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
         * Appends a string, escaped (as needed) and quoted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        void append(String val) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
            buf.append('"');
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            appendUnquoted(val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            buf.append('"');
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
         * Appends a Character, escaped (as needed) and quoted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        void append(Character val) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            buf.append('\'');
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            appendUnquoted(val.charValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            buf.append('\'');
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        void append(Boolean val) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            buf.append(val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        void append(Byte val) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            buf.append(String.format("0x%02x", val));
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        void append(Short val) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            buf.append(val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        void append(Integer val) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            buf.append(val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        void append(Long val) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            buf.append(val).append('L');
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        void append(Float val) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
            if (val.isNaN()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                buf.append("0.0f/0.0f");
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
            } else if (val.isInfinite()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
                if (val.floatValue() < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                    buf.append('-');
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                buf.append("1.0f/0.0f");
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                buf.append(val).append('f');
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        void append(Double val) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            if (val.isNaN()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                buf.append("0.0/0.0");
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            } else if (val.isInfinite()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                if (val.doubleValue() < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                    buf.append('-');
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                buf.append("1.0/0.0");
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                buf.append(val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
         * Appends the class literal corresponding to a type.  Should
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
         * only be invoked for types that have an associated literal.
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
         * e.g:  "java.lang.String.class"
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
         *       "boolean.class"
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
         *       "int[].class"
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        void append(TypeMirrorImpl t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
            appendUnquoted(t.type.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            buf.append(".class");
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
         * Appends the fully qualified name of an enum constant.
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
         * e.g:  "java.math.RoundingMode.UP"
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        void append(EnumConstantDeclarationImpl e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            appendUnquoted(e.sym.enclClass() + "." + e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
         * Appends the text of an annotation pseudo-expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
         * e.g:  "@pkg.Format(linesep='\n')"
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        void append(AnnotationMirrorImpl anno) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            appendUnquoted(anno.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
         * Appends the elements of a collection, enclosed within braces
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
         * and separated by ", ".  Useful for array-valued annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
         * elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
         */
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
   237
        void append(Collection<?> vals) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            buf.append('{');
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
            for (Object val : vals) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                if (first) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                    buf.append(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                append(((AnnotationValue) val).getValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            buf.append('}');
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
         * For each char of a string, append using appendUnquoted(char).
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        private void appendUnquoted(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            for (char c : s.toCharArray()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                appendUnquoted(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
         * Appends a char (unquoted), using escapes for those that are not
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
         * printable ASCII.  We don't know what is actually printable in
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
         * the locale in which this result will be used, so ASCII is our
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
         * best guess as to the least common denominator.
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        private void appendUnquoted(char c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
            switch (c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            case '\b': buf.append("\\b");  break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            case '\t': buf.append("\\t");  break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            case '\n': buf.append("\\n");  break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
            case '\f': buf.append("\\f");  break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            case '\r': buf.append("\\r");  break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            case '\"': buf.append("\\\""); break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            case '\'': buf.append("\\\'"); break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            case '\\': buf.append("\\\\"); break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                if (isPrintableAscii(c)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                    buf.append(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                    buf.append(String.format("\\u%04x", (int) c));
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
         * Is c a printable ASCII character?
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        private static boolean isPrintableAscii(char c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            return c >= ' ' && c <= '~';
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
}