Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4027518

NewObject allows to call any method as class constructor

XMLWordPrintable

    • sparc
    • solaris_2.5



      Name: laC46010 Date: 01/23/97


      JNI spec says:

      "jobject NewObject(JNIEnv *env, jclass clazz,
      jmethodID methodID, ...);

      Constructs a new Java object. The method ID indicates which constructor
      method to invoke. This ID must be obtained by calling GetMethodID()
      with <init> as the method name and void (V) as the return type."

      The test below shows that NewObject allows to call any class method
      as class constructor. JNI should check correctness of the argument in this case.

      To run the test, set environment variables JH (Java home) and CC (C compiler)
      and run "jnirun" script.

      >jnirun
      Method cl.nc is called as constructor.
      void nc() was invoked. picnst = 4

      ::::::::::::::
      jnirun
      ::::::::::::::
      #!/usr/bin/csh

      #setenv JH /export/ld32/dest/jdk1.1beta3/solaris
      #setenv CC /export/ld4/set/dist/sparc-S2/SC4.2/bin/cc

      $JH/bin/javac -d . *[1-9].java
      $CC -KPIC -G -I$JH/include -I$JH/include/solaris -o libtest.so *.c
      setenv LD_LIBRARY_PATH ".:$LD_LIBRARY_PATH"
      setenv CLASSPATH .
      $JH/bin/java javasoft.sqe.tests.vm.nobj.nobj.nobj00101
      ::::::::::::::
      nobj00101.c
      ::::::::::::::
      /* Ident: %Z%%M% %I% %E% */
      /* Copyright %G% Sun Microsystems, Inc. All Rights Reserved */

      #include <stdio.h>
      #include "native.h"

      #ifdef __cplusplus
      extern "C" {
      #endif

      #ifdef __cplusplus
      #define JNI_ENV_ARG(x, y) y
      #define JNI_ENV_PTR(x) x
      #else
      #define JNI_ENV_ARG(x,y) x, y
      #define JNI_ENV_PTR(x) (*x)
      #endif

      JNIEXPORT jobject JNICALL Java_javasoft_sqe_tests_vm_nobj_nobj_nobj00101_nobj00101nm(
      JNIEnv *env, jobject mainObject) {
      jclass jc;
      jmethodID jmi;
      jc = JNI_ENV_PTR(env) ->
      FindClass(JNI_ENV_ARG(env, "javasoft/sqe/tests/vm/nobj/nobj/cl" ));
      if ( jc == NULL) {
      printf("Cannot find class: javasoft/sqe/tests/vm/nobj/nobj/cl \n");
      return NULL;
      }
      jmi = JNI_ENV_PTR(env) ->
      GetMethodID(JNI_ENV_ARG(env,jc),"nc","()V");
      if ( jmi == NULL) {
      printf("Cannot find method nc for javasoft/sqe/tests/vm/nobj/nobj/cl \n");
      return NULL;
      }

      return (JNI_ENV_PTR(env) -> NewObject(JNI_ENV_ARG(env, jc),jmi));
      }


      #ifdef __cplusplus
      }
      #endif



      ::::::::::::::
      nobj00101.java
      ::::::::::::::
      // Ident: %Z%%M% %I% %E%
      // Copyright %G% Sun Microsystems, Inc. All Rights Reserved
                                   
      package javasoft.sqe.tests.vm.nobj.nobj;

      import java.io.PrintStream;

      class cl {
        public int picnst = 5;
        

        public void nc () {
          picnst = 4;
          System.out.println("Method cl.nc is called as constructor.");
        }
      }

      public class nobj00101 {

        native Object nobj00101nm() throws Exception;

        static {
          System.loadLibrary("test");
        }
        public static void main(String argv[]) {
          System.exit(run() + 95/*STATUS_TEMP*/);
        }

        public static int run() {
          nobj00101 tob = new nobj00101();
          cl cls0;
          try {
            cls0 = (cl)tob.nobj00101nm();
            if (cls0 == null) {
              System.out.println("Object of class cl cannot be constructed");
              return 0/*STATUS_PASSED*/;
            }
            else {
              if (cls0.picnst == 4) {
                System.out.println("void nc() was invoked. picnst = " + cls0.picnst);
                return 2/*STATUS_FAILED*/;
              }
              if (cls0.picnst == 5) {
                System.out.println("default constructor was invoked . picnst = " + cls0.picnst);
                return 2/*STATUS_FAILED*/;
              }
            }
          } catch (Throwable e) {
            System.out.println("Exception caught: " + e);
            return 0/*STATUS_PASSED*/;
          }
            return 0/*STATUS_PASSED*/;
        }
      }

      ======================================================================

            sliangsunw Sheng Liang (Inactive)
            leosunw Leo Leo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: