1 /*
   2  * Copyright (c) 2012, 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 import java.io.*;
  26 
  27 // Exercise JavaCritical native method signatures
  28 
  29 public class Generate {
  30     public static void main(String[] args) throws Exception {
  31         PrintStream j = new PrintStream(new FileOutputStream("JNITest.java"));
  32         generateJava(j);
  33         j.close();
  34         j = new PrintStream(new FileOutputStream("JNITest.c"));
  35         generateJNI(j);
  36         generateJavaCritical(j);
  37         j.close();
  38     }
  39 
  40     static void generateJava(PrintStream out) {
  41         out.println("public class JNITest {");
  42         for (int i = 0; i < 255; i++) {
  43             out.printf("  static native void nativeTest%d(", i);
  44             for (int b = 0; b < 8; b++) {
  45                 out.printf("%s a%d", (i & (1 << b)) == 0 ? "int" : "byte[]", b);
  46                 if (b < 7) {
  47                     out.print(", ");
  48                 } else {
  49                     out.println(");");
  50                 }
  51             }
  52         }
  53         out.println("  static byte[] array0 = new byte[1024];");
  54         out.println("  static {");
  55         out.println("    System.loadLibrary(\"jnitest\");");
  56         out.println("  }");
  57 
  58         out.println("  public static void main(String[] args) {");
  59         out.println("    for (int i = 0; i < 20000; i++) {");
  60         for (int i = 0; i < 255; i++) {
  61             out.printf("      nativeTest%d(", i);
  62             for (int b = 0; b < 8; b++) {
  63                 if ((i & (1 << b)) == 0) {
  64                     out.printf("(0xfeafd00f + %d)", b);
  65                 } else {
  66                     out.printf("array0");
  67                 }
  68                 if (b < 7) {
  69                     out.print(", ");
  70                 } else {
  71                     out.println(");");
  72                 }
  73             }
  74         }
  75         out.println("    }");
  76         out.println("  }");
  77         out.println("}");
  78     }
  79 
  80     static void generateJNI(PrintStream out) {
  81         out.println("#include \"JNITest.h\"");
  82         out.println();
  83         for (int i = 0; i < 255; i++) {
  84             out.printf("JNIEXPORT void JNICALL Java_JNITest_nativeTest%d(JNIEnv* env, jclass cl, ", i);
  85             for (int b = 0; b < 8; b++) {
  86                 out.printf("%s a%d", (i & (1 << b)) == 0 ? "jint" : "jbyteArray", b);
  87                 if (b < 7) {
  88                     out.print(", ");
  89                 } else {
  90                     out.println(") {\n}\n\n");
  91                 }
  92             }
  93         }
  94     }
  95 
  96     static void generateJavaCritical(PrintStream out) {
  97         for (int i = 0; i < 255; i++) {
  98             out.printf("JNIEXPORT void JNICALL JavaCritical_JNITest_nativeTest%d(", i);
  99             for (int b = 0; b < 8; b++) {
 100                 if ((i & (1 << b)) == 0) {
 101                     out.printf("%s a%d", "jint", b);
 102                 } else {
 103                     out.printf("int a%dlen, %s a%d", b, "jbyte*", b);
 104                 }
 105                 if (b < 7) {
 106                     out.print(", ");
 107                 } else {
 108                     out.println(") {\n");
 109                 }
 110             }
 111             for (int b = 0; b < 8; b++) {
 112                 if ((i & (1 << b)) == 0) {
 113                     out.printf("  if (a%d != 0x%x) fprintf(stderr, \"a%d %%d != %d in nativeTest%d\\n\", a%d);\n",
 114                                b, b + 0xfeafd00f, b, b + 0xfeafd00f, i, b);
 115                 } else {
 116                     out.printf("  a%d[0] = 1;\n", b);
 117                 }
 118             }
 119             out.println("}\n\n");
 120 
 121         }
 122     }
 123 }