test/tools/javac/tree/MakeLiteralTest.java

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

mercurial