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