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