test/tools/javac/tree/MakeLiteralTest.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 788
8ec3a824f925
child 1374
c002fdee76fd
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

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

mercurial