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