[vmkit-commits] [PATCH] Impl JVM_GetClassDeclared{Fields, Methods, Constructors}

Will Dietz wdietz2 at illinois.edu
Thu Nov 3 07:17:44 PDT 2011


Inlined below.

Mostly copied from equivalent Classpath implementations, only the
OpenJDK constructors are different.

~Will

>From 0821f3663e504c431f96a52070e9983afd87d4a7 Mon Sep 17 00:00:00 2001
From: Will Dietz <w at wdtz.org>
Date: Mon, 31 Oct 2011 16:54:28 -0500
Subject: [PATCH 01/17] Impl JVM_GetClassDeclared{Fields,Methods,Constructors}

---
 lib/J3/ClassLib/OpenJDK/OpenJDK.inc |  228 ++++++++++++++++++++++++++++++++++-
 1 files changed, 225 insertions(+), 3 deletions(-)

diff --git a/lib/J3/ClassLib/OpenJDK/OpenJDK.inc
b/lib/J3/ClassLib/OpenJDK/OpenJDK.inc
index f4110b7..b724e73 100644
--- a/lib/J3/ClassLib/OpenJDK/OpenJDK.inc
+++ b/lib/J3/ClassLib/OpenJDK/OpenJDK.inc
@@ -1016,17 +1016,237 @@ JVM_GetClassAnnotations(JNIEnv *env, jclass cls) {

 JNIEXPORT jobjectArray JNICALL
 JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly) {
-  NYI();
+  ArrayObject* ret = 0;
+  JavaObject* tmp = 0;
+  JavaString* str = 0;
+  JavaObject * Cl = 0;
+  llvm_gcroot(Cl, 0);
+  llvm_gcroot(ret, 0);
+  llvm_gcroot(tmp, 0);
+  llvm_gcroot(str, 0);
+
+  BEGIN_JNI_EXCEPTION
+
+  Cl = *(JavaObject**)ofClass;
+
+  Jnjvm* vm = JavaThread::get()->getJVM();
+  UserCommonClass* cl = UserCommonClass::resolvedImplClass(vm, Cl, false);
+  Classpath* upcalls = vm->upcalls;
+
+  if (cl->isArray() || cl->isPrimitive()) {
+    ret = (ArrayObject*)upcalls->methodArrayClass->doNew(0, vm);
+  } else {
+    UserClass* realCl = cl->asClass();
+    JnjvmClassLoader* classLoader = cl->classLoader;
+    uint32 size = 0;
+
+    for (uint32 i = 0; i < realCl->nbVirtualMethods + realCl->nbStaticMethods;
+         ++i) {
+      JavaMethod* meth = &realCl->virtualMethods[i];
+      bool pub = isPublic(meth->access);
+      if (!(meth->name->equals(classLoader->bootstrapLoader->initName)) &&
+          (!publicOnly || pub)) {
+        ++size;
+      }
+    }
+
+
+    ret = (ArrayObject*)upcalls->methodArrayClass->doNew(size, vm);
+
+    sint32 index = 0;
+    for (uint32 i = 0; i < realCl->nbVirtualMethods + realCl->nbStaticMethods;
+         ++i) {
+      JavaMethod* meth = &realCl->virtualMethods[i];
+      bool pub = isPublic(meth->access);
+      if (!(meth->name->equals(classLoader->bootstrapLoader->initName)) &&
+          (!publicOnly || pub)) {
+        // TODO: check parameter types
+        UserClass* Meth = vm->upcalls->newMethod;
+        tmp = Meth->doNew(vm);
+        str = vm->internalUTF8ToStr(meth->name);
+        JavaObject * pArr = meth->getParameterTypes(classLoader);
+        JavaObject * eArr = meth->getExceptionTypes(classLoader);
+        JavaObject * retTy = meth->getReturnType(classLoader);
+        upcalls->initMethod->invokeIntSpecial(vm, Meth, tmp,
+          &Cl,          /* declaring class */
+          &str,         /* name */
+          &pArr,        /* parameter types */
+          &retTy,       /* return type */
+          &eArr,        /* exceptions */
+          meth->access, /* modifiers */
+          i,            /* slot */
+          NULL,         /* signature */
+          NULL,         /* annotations */
+          NULL,         /* parameter annotations */
+          NULL,         /* default annotations */
+          i);
+        ArrayObject::setElement(ret, tmp, index);
+        index++;
+      }
+    }
+  }
+
+  RETURN_FROM_JNI((jobjectArray)th->pushJNIRef(ret));
+
+  END_JNI_EXCEPTION
+
+  return 0;
 }

 JNIEXPORT jobjectArray JNICALL
 JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly) {
-  NYI();
+  ArrayObject* ret = 0;
+  JavaObject* tmp = 0;
+  JavaString* name = 0;
+  JavaObject*const*  type = 0;
+  JavaString* sign = 0;
+  JavaObject* annArray = 0;
+  llvm_gcroot(ret, 0);
+  llvm_gcroot(tmp, 0);
+  llvm_gcroot(name, 0);
+  llvm_gcroot(type, 0);
+  llvm_gcroot(sign, 0);
+  llvm_gcroot(annArray, 0);
+
+  BEGIN_JNI_EXCEPTION
+
+  JavaObject * Cl = *(JavaObject**)ofClass;
+  llvm_gcroot(Cl, 0);
+
+  Jnjvm* vm = JavaThread::get()->getJVM();
+  UserCommonClass* cl = UserCommonClass::resolvedImplClass(vm, Cl, false);
+
+  if (!cl->isClass()) {
+    ret = (ArrayObject*)vm->upcalls->fieldArrayClass->doNew(0, vm);
+  } else {
+    UserClass* realCl = cl->asClass();
+    uint32 size = 0;
+    for (uint32 i = 0; i < realCl->nbVirtualFields + realCl->nbStaticFields;
+         ++i) {
+      JavaField* field = &realCl->virtualFields[i];
+      if (!publicOnly || isPublic(field->access)) {
+        ++size;
+      }
+    }
+
+
+    ret = (ArrayObject*)vm->upcalls->fieldArrayClass->doNew(size, vm);
+
+    sint32 index = 0;
+    for (uint32 i = 0; i < realCl->nbVirtualFields + realCl->nbStaticFields;
+         ++i) {
+      JavaField* field = &realCl->virtualFields[i];
+      if (!publicOnly || isPublic(field->access)) {
+        // TODO: check parameter types
+        UserClass* Field = vm->upcalls->newField;
+        tmp = Field->doNew(vm);
+        name = vm->internalUTF8ToStr(field->name);
+
+        //type->Class
+        UserCommonClass * fieldCl =
field->getSignature()->assocClass(cl->classLoader);
+        assert(fieldCl);
+        type = fieldCl->getClassDelegateePtr(vm);
+
+        // TODO:Implement these!
+        sign = NULL;
+        annArray = NULL;
+
+        //Convert to string
+
+        /* java.reflect.Field(
+         *   Class declaringClass,
+         *   String name,
+         *   Class type,
+         *   int modifiers,
+         *   int slot,
+         *   String signature,
+         *   byte[] annotations)
+         */
+        vm->upcalls->initField->invokeIntSpecial(vm, Field, tmp,
+          &Cl,
+          &name,
+          type,
+          field->access,
+          i,
+          sign,
+          annArray);
+        ArrayObject::setElement(ret, tmp, index);
+        index++;
+      }
+    }
+  }
+  RETURN_FROM_JNI((jobjectArray)th->pushJNIRef(ret));
+
+  END_JNI_EXCEPTION
+
+  return 0;
 }

 JNIEXPORT jobjectArray JNICALL
 JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass,
jboolean publicOnly) {
-  NYI();
+  ArrayObject* ret = 0;
+  JavaObject* tmp = 0;
+  JavaObject* Cl = 0;
+  llvm_gcroot(Cl, 0);
+  llvm_gcroot(ret, 0);
+  llvm_gcroot(tmp, 0);
+
+  BEGIN_JNI_EXCEPTION
+
+  Cl = *(JavaObject**)ofClass;
+
+  Jnjvm* vm = JavaThread::get()->getJVM();
+  UserCommonClass* cl = UserCommonClass::resolvedImplClass(vm, Cl, false);
+
+  if (cl->isArray() || cl->isInterface() || cl->isPrimitive()) {
+    ret = (ArrayObject*)vm->upcalls->constructorArrayClass->doNew(0, vm);
+  } else {
+    UserClass* realCl = cl->asClass();;
+    JnjvmClassLoader* classLoader = cl->classLoader;
+    uint32 size = 0;
+
+    for (uint32 i = 0; i < realCl->nbVirtualMethods; ++i) {
+      JavaMethod* meth = &realCl->virtualMethods[i];
+      bool pub = isPublic(meth->access);
+      if (meth->name->equals(classLoader->bootstrapLoader->initName) &&
+          (!publicOnly || pub)) {
+        ++size;
+      }
+    }
+
+    ret = (ArrayObject*)vm->upcalls->constructorArrayClass->doNew(size, vm);
+
+    sint32 index = 0;
+    for (uint32 i = 0; i < realCl->nbVirtualMethods; ++i) {
+      JavaMethod* meth = &realCl->virtualMethods[i];
+      bool pub = isPublic(meth->access);
+      if (meth->name->equals(classLoader->bootstrapLoader->initName) &&
+          (!publicOnly || pub)) {
+        UserClass* Cons = vm->upcalls->newConstructor;
+        JavaObject * pArr = meth->getParameterTypes(classLoader);
+        JavaObject * eArr = meth->getExceptionTypes(classLoader);
+        tmp = Cons->doNew(vm);
+        vm->upcalls->initConstructor->invokeIntSpecial(vm, Cons, tmp,
+          &Cl,          /* declaringClass */
+          &pArr,        /* parameterTypes */
+          &eArr,        /* checkedExceptions */
+          meth->access, /* modifiers */
+          i,            /* slot */
+          NULL,         /* String signature */
+          NULL,         /* annotations */
+          NULL          /* parameterAnnotations */
+        );
+        ArrayObject::setElement(ret, tmp, index);
+        index++;
+      }
+    }
+  }
+
+  RETURN_FROM_JNI((jobjectArray)th->pushJNIRef(ret));
+
+  END_JNI_EXCEPTION
+
+  return 0;
 }

 /* Differs from JVM_GetClassModifiers in treatment of inner classes.
@@ -1170,6 +1390,8 @@ JVM_DoPrivileged(JNIEnv *env, jclass cls,
   RETURN_FROM_JNI((jobject)th->pushJNIRef(res));

   END_JNI_EXCEPTION
+
+  return NULL;
 }

 JNIEXPORT jobject JNICALL
-- 
1.7.5.1



More information about the vmkit-commits mailing list