Ejemplo n.º 1
0
/*
 * Class:     java_lang_VMClassRegistry
 * Method:    getDeclaredClasses
 * Signature: (Ljava/lang/Class;)[Ljava/lang/Class;
 */
JNIEXPORT jobjectArray JNICALL Java_java_lang_VMClassRegistry_getDeclaredClasses
  (JNIEnv *jenv, jclass, jclass clazz)
{
    unsigned index, num_ic, num_res;
    Class_Handle clss, iclss;

    // get class and number of inner classes
    clss = jni_get_class_handle(jenv, clazz);
    num_ic = num_res = class_number_inner_classes(clss);

    // calculate number of declared classes
    for(index=0; index < num_ic; index++) {
        iclss = class_get_inner_class(clss, index);
        if (!iclss)
            return NULL;        
        if( !class_is_direct_member( clss, iclss ) )
            num_res--;
    }

    // create array
    jclass cclazz = struct_Class_to_java_lang_Class_Handle(VM_Global_State::loader_env->JavaLangClass_Class);
    jobjectArray res = NewObjectArray(jenv, num_res, cclazz, NULL);

    // set array
    for( index = 0, num_res = 0; index < num_ic; index++) {
        iclss = class_get_inner_class(clss, index);
        if( !class_is_direct_member( clss, iclss ) )
            continue;
        SetObjectArrayElement(jenv, res, num_res++, jni_class_from_handle(jenv, iclss));
    }
    return res;
}
Ejemplo n.º 2
0
void generateJavaLangClassInstances() {
	// Allocate Class[] containing only those classes that are referenced:
	javaLangClassArray = NewObjectArray((jint) numberOfAllClassInstanceInfo,
			CLASS_ID_java_lang_Class, NULL);

	if (javaLangClassArray != NULL) {
		// Don't GC our array of classes:
		heapProtect(javaLangClassArray, TRUE);

		int i;
		for (i = 0; i < numberOfAllClassInstanceInfo; i++) {
			// Simulate: Class cl  = new Class();
			// TODO NewObject !!!
			// TODO Lazy load?
			u2 size;
			getClassSize(CLASS_ID_java_lang_Class, &size);
			jobject jc = heapAllocObjectByStackableSize(size, CLASS_ID_java_lang_Class);
			SetIntField(jc, LINK_ID_java_lang_Class_aClassId_I, i);
			if (ExceptionCheck()) {
				break;
			}

			SetObjectArrayElement(javaLangClassArray, i, jc);
			if (ExceptionCheck()) {
				break;
			}
		}

		// Set the aAllClasses in java.lang.Class:
		jclass classInstance = getJavaLangClass(CLASS_ID_java_lang_Class);
		SetStaticObjectField(classInstance, LINK_ID_java_lang_Class_aAllClasses__Ljava_lang_Class_,
				javaLangClassArray);

		// No need of protection:
		heapProtect(javaLangClassArray, FALSE);
	}
	// consout("class init done\n");

}
Ejemplo n.º 3
0
void deprecated_generateJavaLangClassInstances() {
	int i;
	int j;
	int arrayLength = 0;

	// Calculate the length of the Class[] to generate:
	for (i = 0; i < numberOfAllClassReferences; i++) {
		// The candidate for java.lang.Class generation:
		u2 classId = allClassReferences[i].targetClassId;

		// Verify that it hasn't been generated before:
		BOOL found = FALSE;
		for (j = 0; j < i - 1 && !found; j++) {
			found = allClassReferences[j].targetClassId == classId;
		}

		if (!found) {
			arrayLength++;
		}
	}

	consoutli("arrayLength: %d\n", arrayLength);
	// Allocate Class[] containing only those classes that are referenced:
	javaLangClassArray = NewObjectArray((jint) arrayLength, CLASS_ID_java_lang_Class, NULL);

	if (javaLangClassArray != NULL) {
		// Don't GC our array of classes:
		heapProtect(javaLangClassArray, TRUE);

		int arrayIndex = 0;
		u2 aClassIdLinkId = LINK_ID_java_lang_Class_aClassId_I;

		jclass classInstance = NULL;

		for (i = 0; i < numberOfAllClassReferences; i++) {
			// The candidate for java.lang.Class generation:
			u2 classId = allClassReferences[i].targetClassId;

			// Verify that it hasn't been generated before:
			BOOL found = FALSE;
			for (j = 0; j < i - 1 && !found; j++) {
				found = allClassReferences[j].targetClassId == classId;
			}

			if (!found) {
				// The class instance does not exist yet:
				// Simulate: Class cl  = new Class();
				u2 size;
				getClassSize(CLASS_ID_java_lang_Class, &size);
				jobject jc = heapAllocObjectByStackableSize(size, CLASS_ID_java_lang_Class);
				SetIntField(jc, aClassIdLinkId, classId);
				if (ExceptionCheck()) {
					break;
				}

				consoutli("class id: %d\n", classId);
				if (classId == CLASS_ID_java_lang_Class) {
					classInstance = jc;
					consoutli("class: \n");
				}
				SetObjectArrayElement(javaLangClassArray, arrayIndex, jc);
				arrayIndex++;
			}
		}

		// Set the aAllClasses in java.lang.Class:
		SetStaticObjectField(classInstance, LINK_ID_java_lang_Class_aAllClasses__Ljava_lang_Class_,
				javaLangClassArray);
	}
	// consout("class init done\n");
}
Ejemplo n.º 4
0
/*
 * Class:     java_lang_VMClassRegistry
 * Method:    getSystemPackages
 * Signature: (I)[[Ljava/lang/String;
 */
JNIEXPORT jobjectArray JNICALL Java_java_lang_VMClassRegistry_getSystemPackages
  (JNIEnv *jenv, jclass, jint len)
{
    Global_Env* genv = VM_Global_State::loader_env;
    ClassLoader* cl = static_cast<ClassLoader*>
        (genv->bootstrap_class_loader);
    Package_Table* ptab = cl->getPackageTable();
    cl->Lock();
    unsigned p_num = (unsigned)ptab->size();
    if (p_num == (unsigned)len) 
    {
        cl->Unlock();
        return NULL;
    }
    const char ** pkgs = (const char **)STD_MALLOC(p_num * 2 * sizeof(char*));
    size_t buf_len = 0;
    unsigned index = 0;
    for (Package_Table::const_iterator it = ptab->begin(), end = ptab->end(); 
        it != end; ++it)
    {
        const char* name = pkgs[index++] = (*it).first->bytes;
        pkgs[index++] = (*it).second->get_jar();
        size_t name_len = (*it).first->len;
        if (name_len > buf_len) {
            buf_len = name_len;
        }
    }
    cl->Unlock();

    jclass string_class = struct_Class_to_java_lang_Class_Handle(genv->JavaLangString_Class);
    static Class* aos = genv->LoadCoreClass("[Ljava/lang/String;");
    jclass string_array_class = struct_Class_to_java_lang_Class_Handle(aos);
    assert(string_class);
    assert(string_array_class);
        
    jobjectArray result = NewObjectArray(jenv, p_num, string_array_class, NULL);
    if (result) 
    {
        char* buf = (char*)STD_MALLOC(buf_len + 1);
        p_num *= 2;
        for (index = 0; index < p_num; )
        {
            jobjectArray pair = NewObjectArray(jenv, 2, string_class, NULL);
            if (!pair) {
                break;
            }
            SetObjectArrayElement(jenv, result, index/2, pair);

            char* name = strcpy(buf, pkgs[index++]);
            for (char* c = name; *c != '\0'; ++c) {
                if (*c == '/') {
                    *c = '.';
                }
            }
            jstring jname = NewStringUTF(jenv, name);
            if (!jname) {
                break;
            }
            SetObjectArrayElement(jenv, pair, 0, jname);

            const char * jar = pkgs[index++];
            if (jar) {
                jstring js = NewStringUTF(jenv, jar);
                if (!js) break;
                SetObjectArrayElement(jenv, pair, 1, js);
            }
        }
        STD_FREE(buf);
    }

    STD_FREE(pkgs);
    
    assert(result || exn_raised());
    return result;
}