ConstMacroTest.java revision 3294:9adfb22ff08f
1/* 2 * Copyright (c) 2013, 2016, 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 */ 23 24/* 25 * @test 26 * @bug 4786406 4781221 4780341 6214324 27 * @summary Validates rewritten javah handling of class defined constants and 28 * ensures that the appropriate macro definitions are placed in the generated 29 * header file. 30 * @library /tools/lib 31 * @modules jdk.compiler/com.sun.tools.javac.api 32 * jdk.compiler/com.sun.tools.javac.file 33 * jdk.compiler/com.sun.tools.javac.main 34 * jdk.jdeps/com.sun.tools.javap 35 * @build ToolBox 36 * @run main ConstMacroTest 37 */ 38 39import java.io.*; 40import java.util.List; 41 42// Original test: test/tools/javah/ConstMacroTest.sh 43public class ConstMacroTest { 44 45 private static final String subClassConstsGoldenFileTemplate = 46 "/* DO NOT EDIT THIS FILE - it is machine generated */\n" + 47 "#include <jni.h>\n" + 48 "/* Header for class SubClassConsts */\n" + 49 "\n" + 50 "#ifndef _Included_SubClassConsts\n" + 51 "#define _Included_SubClassConsts\n" + 52 "#ifdef __cplusplus\n" + 53 "extern \"C\" {\n" + 54 "#endif\n" + 55 "#undef SubClassConsts_serialVersionUID\n" + 56 "#define SubClassConsts_serialVersionUID 6733861379283244755%s\n" + 57 "#undef SubClassConsts_SUPER_INT_CONSTANT\n" + 58 "#define SubClassConsts_SUPER_INT_CONSTANT 3L\n" + 59 "#undef SubClassConsts_SUPER_FLOAT_CONSTANT\n" + 60 "#define SubClassConsts_SUPER_FLOAT_CONSTANT 99.3f\n" + 61 "#undef SubClassConsts_SUPER_DOUBLE_CONSTANT\n" + 62 "#define SubClassConsts_SUPER_DOUBLE_CONSTANT 33.2\n" + 63 "#undef SubClassConsts_SUPER_BOOLEAN_CONSTANT\n" + 64 "#define SubClassConsts_SUPER_BOOLEAN_CONSTANT 0L\n" + 65 "#undef SubClassConsts_SUB_INT_CONSTANT\n" + 66 "#define SubClassConsts_SUB_INT_CONSTANT 2L\n" + 67 "#undef SubClassConsts_SUB_DOUBLE_CONSTANT\n" + 68 "#define SubClassConsts_SUB_DOUBLE_CONSTANT 2.25\n" + 69 "#undef SubClassConsts_SUB_FLOAT_CONSTANT\n" + 70 "#define SubClassConsts_SUB_FLOAT_CONSTANT 7.9f\n" + 71 "#undef SubClassConsts_SUB_BOOLEAN_CONSTANT\n" + 72 "#define SubClassConsts_SUB_BOOLEAN_CONSTANT 1L\n" + 73 "#ifdef __cplusplus\n" + 74 "}\n" + 75 "#endif\n" + 76 "#endif"; 77 78 public static void main(String[] args) throws Exception { 79 ToolBox tb = new ToolBox(); 80 81 tb.new JavahTask() 82 .classpath(ToolBox.testClasses) 83 .classes("SubClassConsts") 84 .run(); 85 86 String longSuffix = tb.isWindows() ? "i64" : "LL"; 87 List<String> subClassConstsGoldenFile = tb.split( 88 String.format(subClassConstsGoldenFileTemplate, longSuffix), "\n"); 89 90 List<String> subClassConstsFile = tb.readAllLines("SubClassConsts.h"); 91 92 tb.checkEqual(subClassConstsFile, subClassConstsGoldenFile); 93 } 94 95} 96 97class SuperClassConsts implements Serializable { 98 // Define class constant values, base class is serializable 99 private static final long serialVersionUID = 6733861379283244755L; 100 public static final int SUPER_INT_CONSTANT = 3; 101 public final static float SUPER_FLOAT_CONSTANT = 99.3f; 102 public final static double SUPER_DOUBLE_CONSTANT = 33.2; 103 public final static boolean SUPER_BOOLEAN_CONSTANT = false; 104 // A token instance field 105 int instanceField; 106 107 public native int numValues(); 108} 109 110class SubClassConsts extends SuperClassConsts { 111 private final static int SUB_INT_CONSTANT = 2; 112 private final static double SUB_DOUBLE_CONSTANT = 2.25; 113 private final static float SUB_FLOAT_CONSTANT = 7.90f; 114 private final static boolean SUB_BOOLEAN_CONSTANT = true; 115} 116