6504896: TreeMaker.Literal(Object) does not support Booleans

Fri, 10 Dec 2010 07:38:28 -0800

author
jjg
date
Fri, 10 Dec 2010 07:38:28 -0800
changeset 788
8ec3a824f925
parent 787
b1c98bfd4709
child 789
878c8f760ded
child 790
fe43a7efd273
child 793
ffbf2b2a8611

6504896: TreeMaker.Literal(Object) does not support Booleans
Reviewed-by: mcimadamore

src/share/classes/com/sun/tools/javac/parser/JavacParser.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/tree/TreeMaker.java file | annotate | diff | comparison | revisions
test/tools/javac/tree/MakeLiteralTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Fri Dec 10 15:24:17 2010 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Fri Dec 10 07:38:28 2010 -0800
     1.3 @@ -498,7 +498,7 @@
     1.4              try {
     1.5                  n = Float.valueOf(proper);
     1.6              } catch (NumberFormatException ex) {
     1.7 -                // error already repoted in scanner
     1.8 +                // error already reported in scanner
     1.9                  n = Float.NaN;
    1.10              }
    1.11              if (n.floatValue() == 0.0f && !isZero(proper))
     2.1 --- a/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java	Fri Dec 10 15:24:17 2010 +0000
     2.2 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java	Fri Dec 10 07:38:28 2010 -0800
     2.3 @@ -734,8 +734,9 @@
     2.4              result = Literal(BYTE, value).
     2.5                  setType(syms.byteType.constType(value));
     2.6          } else if (value instanceof Character) {
     2.7 +            int v = (int) (((Character) value).toString().charAt(0));
     2.8              result = Literal(CHAR, value).
     2.9 -                setType(syms.charType.constType(value));
    2.10 +                setType(syms.charType.constType(v));
    2.11          } else if (value instanceof Double) {
    2.12              result = Literal(DOUBLE, value).
    2.13                  setType(syms.doubleType.constType(value));
    2.14 @@ -745,6 +746,10 @@
    2.15          } else if (value instanceof Short) {
    2.16              result = Literal(SHORT, value).
    2.17                  setType(syms.shortType.constType(value));
    2.18 +        } else if (value instanceof Boolean) {
    2.19 +            int v = ((Boolean) value) ? 1 : 0;
    2.20 +            result = Literal(BOOLEAN, v).
    2.21 +                setType(syms.booleanType.constType(v));
    2.22          } else {
    2.23              throw new AssertionError(value);
    2.24          }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/tree/MakeLiteralTest.java	Fri Dec 10 07:38:28 2010 -0800
     3.3 @@ -0,0 +1,90 @@
     3.4 +
     3.5 +
     3.6 +/*
     3.7 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     3.8 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.9 + *
    3.10 + * This code is free software; you can redistribute it and/or modify it
    3.11 + * under the terms of the GNU General Public License version 2 only, as
    3.12 + * published by the Free Software Foundation.
    3.13 + *
    3.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.17 + * version 2 for more details (a copy is included in the LICENSE file that
    3.18 + * accompanied this code).
    3.19 + *
    3.20 + * You should have received a copy of the GNU General Public License version
    3.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.23 + *
    3.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.25 + * or visit www.oracle.com if you need additional information or have any
    3.26 + * questions.
    3.27 + */
    3.28 +
    3.29 +/*
    3.30 + * @test
    3.31 + * @bug 6504896
    3.32 + * @summary TreeMaker.Literal(Object) does not support Booleans
    3.33 + */
    3.34 +
    3.35 +import com.sun.tools.javac.code.Type;
    3.36 +import com.sun.tools.javac.code.Symtab;
    3.37 +import com.sun.tools.javac.code.Types;
    3.38 +import com.sun.tools.javac.file.JavacFileManager;
    3.39 +import com.sun.tools.javac.tree.JCTree.JCLiteral;
    3.40 +import com.sun.tools.javac.util.Context;
    3.41 +import com.sun.tools.javac.tree.TreeMaker;
    3.42 +import static com.sun.tools.javac.code.TypeTags.*;
    3.43 +
    3.44 +public class MakeLiteralTest {
    3.45 +    public static void main(String... args) throws Exception {
    3.46 +        new MakeLiteralTest().run();
    3.47 +    }
    3.48 +
    3.49 +    void run() throws Exception {
    3.50 +        Context context = new Context();
    3.51 +        JavacFileManager.preRegister(context);
    3.52 +        Symtab syms = Symtab.instance(context);
    3.53 +        maker = TreeMaker.instance(context);
    3.54 +        types = Types.instance(context);
    3.55 +
    3.56 +        test("abc",                     CLASS,      syms.stringType,    "abc");
    3.57 +        test(Boolean.FALSE,             BOOLEAN,    syms.booleanType,   Integer.valueOf(0));
    3.58 +        test(Boolean.TRUE,              BOOLEAN,    syms.booleanType,   Integer.valueOf(1));
    3.59 +        test(Byte.valueOf((byte) 1),    BYTE,       syms.byteType,      Byte.valueOf((byte) 1));
    3.60 +        test(Character.valueOf('a'),    CHAR,       syms.charType,      Integer.valueOf('a'));
    3.61 +        test(Double.valueOf(1d),        DOUBLE,     syms.doubleType,    Double.valueOf(1d));
    3.62 +        test(Float.valueOf(1f),         FLOAT,      syms.floatType,     Float.valueOf(1f));
    3.63 +        test(Integer.valueOf(1),        INT,        syms.intType,       Integer.valueOf(1));
    3.64 +        test(Long.valueOf(1),           LONG,       syms.longType,      Long.valueOf(1));
    3.65 +        test(Short.valueOf((short) 1),  SHORT,      syms.shortType,     Short.valueOf((short) 1));
    3.66 +
    3.67 +        if (errors > 0)
    3.68 +            throw new Exception(errors + " errors found");
    3.69 +    }
    3.70 +
    3.71 +    void test(Object value, int tag, Type type, Object constValue) {
    3.72 +        JCLiteral l = maker.Literal(value);
    3.73 +        if (l.type.tag != tag)
    3.74 +            error("unexpected tag: " + l.getTag() + ": expected: " + tag);
    3.75 +        if (!types.isSameType(l.type, type))
    3.76 +            error("unexpected type: " + l.type + ": expected: " + type);
    3.77 +        if (l.type.constValue().getClass() != constValue.getClass()
    3.78 +                || !constValue.equals(l.type.constValue()))  {
    3.79 +            error("unexpected const value: "
    3.80 +                    + l.type.constValue().getClass() + " " + l.type.constValue()
    3.81 +                    + ": expected:" + constValue.getClass() + " " + constValue);
    3.82 +        }
    3.83 +    }
    3.84 +
    3.85 +    void error(String msg) {
    3.86 +        System.err.println("Error: " + msg);
    3.87 +        errors++;
    3.88 +    }
    3.89 +
    3.90 +    TreeMaker maker;
    3.91 +    Types types;
    3.92 +    int errors;
    3.93 +}

mercurial