test/tools/javac/tree/MakeLiteralTest.java

Thu, 25 Oct 2012 11:09:36 -0700

author
jjg
date
Thu, 25 Oct 2012 11:09:36 -0700
changeset 1374
c002fdee76fd
parent 788
8ec3a824f925
child 2525
2eb010b6cb22
permissions
-rw-r--r--

7200915: convert TypeTags from a series of small ints to an enum
Reviewed-by: jjg, mcimadamore
Contributed-by: vicente.romero@oracle.com

     1 /*
     2  * Copyright (c) 2010, 2012, 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.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 6504896
    27  * @summary TreeMaker.Literal(Object) does not support Booleans
    28  */
    30 import com.sun.tools.javac.code.Type;
    31 import com.sun.tools.javac.code.TypeTag;
    32 import com.sun.tools.javac.code.Symtab;
    33 import com.sun.tools.javac.code.Types;
    34 import com.sun.tools.javac.file.JavacFileManager;
    35 import com.sun.tools.javac.tree.JCTree.JCLiteral;
    36 import com.sun.tools.javac.util.Context;
    37 import com.sun.tools.javac.tree.TreeMaker;
    38 import static com.sun.tools.javac.code.TypeTag.*;
    40 public class MakeLiteralTest {
    41     public static void main(String... args) throws Exception {
    42         new MakeLiteralTest().run();
    43     }
    45     void run() throws Exception {
    46         Context context = new Context();
    47         JavacFileManager.preRegister(context);
    48         Symtab syms = Symtab.instance(context);
    49         maker = TreeMaker.instance(context);
    50         types = Types.instance(context);
    52         test("abc",                     CLASS,      syms.stringType,    "abc");
    53         test(Boolean.FALSE,             BOOLEAN,    syms.booleanType,   Integer.valueOf(0));
    54         test(Boolean.TRUE,              BOOLEAN,    syms.booleanType,   Integer.valueOf(1));
    55         test(Byte.valueOf((byte) 1),    BYTE,       syms.byteType,      Byte.valueOf((byte) 1));
    56         test(Character.valueOf('a'),    CHAR,       syms.charType,      Integer.valueOf('a'));
    57         test(Double.valueOf(1d),        DOUBLE,     syms.doubleType,    Double.valueOf(1d));
    58         test(Float.valueOf(1f),         FLOAT,      syms.floatType,     Float.valueOf(1f));
    59         test(Integer.valueOf(1),        INT,        syms.intType,       Integer.valueOf(1));
    60         test(Long.valueOf(1),           LONG,       syms.longType,      Long.valueOf(1));
    61         test(Short.valueOf((short) 1),  SHORT,      syms.shortType,     Short.valueOf((short) 1));
    63         if (errors > 0)
    64             throw new Exception(errors + " errors found");
    65     }
    67     void test(Object value, TypeTag tag, Type type, Object constValue) {
    68         JCLiteral l = maker.Literal(value);
    69         if (!l.type.hasTag(tag))
    70             error("unexpected tag: " + l.getTag() + ": expected: " + tag);
    71         if (!types.isSameType(l.type, type))
    72             error("unexpected type: " + l.type + ": expected: " + type);
    73         if (l.type.constValue().getClass() != constValue.getClass()
    74                 || !constValue.equals(l.type.constValue()))  {
    75             error("unexpected const value: "
    76                     + l.type.constValue().getClass() + " " + l.type.constValue()
    77                     + ": expected:" + constValue.getClass() + " " + constValue);
    78         }
    79     }
    81     void error(String msg) {
    82         System.err.println("Error: " + msg);
    83         errors++;
    84     }
    86     TreeMaker maker;
    87     Types types;
    88     int errors;
    89 }

mercurial