jjg@788: /* jjg@1374: * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. jjg@788: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@788: * jjg@788: * This code is free software; you can redistribute it and/or modify it jjg@788: * under the terms of the GNU General Public License version 2 only, as jjg@788: * published by the Free Software Foundation. jjg@788: * jjg@788: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@788: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@788: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@788: * version 2 for more details (a copy is included in the LICENSE file that jjg@788: * accompanied this code). jjg@788: * jjg@788: * You should have received a copy of the GNU General Public License version jjg@788: * 2 along with this work; if not, write to the Free Software Foundation, jjg@788: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@788: * jjg@788: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@788: * or visit www.oracle.com if you need additional information or have any jjg@788: * questions. jjg@788: */ jjg@788: jjg@788: /* jjg@788: * @test jjg@788: * @bug 6504896 jjg@788: * @summary TreeMaker.Literal(Object) does not support Booleans jjg@788: */ jjg@788: jjg@788: import com.sun.tools.javac.code.Type; jjg@1374: import com.sun.tools.javac.code.TypeTag; jjg@788: import com.sun.tools.javac.code.Symtab; jjg@788: import com.sun.tools.javac.code.Types; jjg@788: import com.sun.tools.javac.file.JavacFileManager; jjg@788: import com.sun.tools.javac.tree.JCTree.JCLiteral; jjg@788: import com.sun.tools.javac.util.Context; jjg@788: import com.sun.tools.javac.tree.TreeMaker; jjg@1374: import static com.sun.tools.javac.code.TypeTag.*; jjg@788: jjg@788: public class MakeLiteralTest { jjg@788: public static void main(String... args) throws Exception { jjg@788: new MakeLiteralTest().run(); jjg@788: } jjg@788: jjg@788: void run() throws Exception { jjg@788: Context context = new Context(); jjg@788: JavacFileManager.preRegister(context); jjg@788: Symtab syms = Symtab.instance(context); jjg@788: maker = TreeMaker.instance(context); jjg@788: types = Types.instance(context); jjg@788: jjg@788: test("abc", CLASS, syms.stringType, "abc"); jjg@788: test(Boolean.FALSE, BOOLEAN, syms.booleanType, Integer.valueOf(0)); jjg@788: test(Boolean.TRUE, BOOLEAN, syms.booleanType, Integer.valueOf(1)); jjg@788: test(Byte.valueOf((byte) 1), BYTE, syms.byteType, Byte.valueOf((byte) 1)); jjg@788: test(Character.valueOf('a'), CHAR, syms.charType, Integer.valueOf('a')); jjg@788: test(Double.valueOf(1d), DOUBLE, syms.doubleType, Double.valueOf(1d)); jjg@788: test(Float.valueOf(1f), FLOAT, syms.floatType, Float.valueOf(1f)); jjg@788: test(Integer.valueOf(1), INT, syms.intType, Integer.valueOf(1)); jjg@788: test(Long.valueOf(1), LONG, syms.longType, Long.valueOf(1)); jjg@788: test(Short.valueOf((short) 1), SHORT, syms.shortType, Short.valueOf((short) 1)); jjg@788: jjg@788: if (errors > 0) jjg@788: throw new Exception(errors + " errors found"); jjg@788: } jjg@788: jjg@1374: void test(Object value, TypeTag tag, Type type, Object constValue) { jjg@788: JCLiteral l = maker.Literal(value); jjg@1374: if (!l.type.hasTag(tag)) jjg@788: error("unexpected tag: " + l.getTag() + ": expected: " + tag); jjg@788: if (!types.isSameType(l.type, type)) jjg@788: error("unexpected type: " + l.type + ": expected: " + type); jjg@788: if (l.type.constValue().getClass() != constValue.getClass() jjg@788: || !constValue.equals(l.type.constValue())) { jjg@788: error("unexpected const value: " jjg@788: + l.type.constValue().getClass() + " " + l.type.constValue() jjg@788: + ": expected:" + constValue.getClass() + " " + constValue); jjg@788: } jjg@788: } jjg@788: jjg@788: void error(String msg) { jjg@788: System.err.println("Error: " + msg); jjg@788: errors++; jjg@788: } jjg@788: jjg@788: TreeMaker maker; jjg@788: Types types; jjg@788: int errors; jjg@788: }