Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "jni_internal.h" |
| 18 | #include "class_linker.h" |
| 19 | #include "object.h" |
| 20 | |
| 21 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | namespace { |
| 26 | |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame^] | 27 | jboolean Class_desiredAssertionStatus(JNIEnv* env, jobject javaThis) { |
| 28 | return JNI_FALSE; |
| 29 | } |
| 30 | |
| 31 | jobject Class_getClassLoader(JNIEnv* env, jclass, jobject javaClass) { |
| 32 | Class* c = Decode<Class*>(env, javaClass); |
| 33 | Object* result = reinterpret_cast<Object*>(const_cast<ClassLoader*>(c->GetClassLoader())); |
| 34 | return AddLocalReference<jobject>(env, result); |
| 35 | } |
| 36 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 37 | jclass Class_getComponentType(JNIEnv* env, jobject javaThis) { |
| 38 | Class* c = Decode<Class*>(env, javaThis); |
| 39 | if (!c->IsArrayClass()) { |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * We can't just return c->GetComponentType(), because that gives |
| 45 | * us the base type (e.g. X[][][] returns X). If this is a multi- |
| 46 | * dimensional array, we have to do the lookup by name. |
| 47 | */ |
| 48 | Class* result; |
| 49 | std::string descriptor(c->GetDescriptor()->ToModifiedUtf8()); |
| 50 | if (descriptor[1] == '[') { |
| 51 | result = Runtime::Current()->GetClassLinker()->FindClass(descriptor.c_str() + 1, c->GetClassLoader()); |
| 52 | } else { |
| 53 | result = c->GetComponentType(); |
| 54 | } |
| 55 | return AddLocalReference<jclass>(env, result); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | jobjectArray Class_getDeclaredClasses(JNIEnv* env, jclass java_lang_Class_class, jclass c, jboolean publicOnly) { |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame^] | 59 | UNIMPLEMENTED(WARNING) << "needs annotations"; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 60 | return env->NewObjectArray(0, java_lang_Class_class, NULL); |
| 61 | } |
| 62 | |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame^] | 63 | jclass Class_getDeclaringClass(JNIEnv* env, jobject javaThis) { |
| 64 | UNIMPLEMENTED(WARNING) << "needs annotations"; |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | jobject Class_getEnclosingConstructor(JNIEnv* env, jobject javaThis) { |
| 69 | UNIMPLEMENTED(WARNING) << "needs annotations"; |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | jobject Class_getEnclosingMethod(JNIEnv* env, jobject javaThis) { |
| 74 | UNIMPLEMENTED(WARNING) << "needs annotations"; |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * private native String getNameNative() |
| 80 | * |
| 81 | * Return the class' name. The exact format is bizarre, but it's the specified |
| 82 | * behavior: keywords for primitive types, regular "[I" form for primitive |
| 83 | * arrays (so "int" but "[I"), and arrays of reference types written |
| 84 | * between "L" and ";" but with dots rather than slashes (so "java.lang.String" |
| 85 | * but "[Ljava.lang.String;"). Madness. |
| 86 | */ |
| 87 | jstring Class_getNameNative(JNIEnv* env, jobject javaThis) { |
| 88 | Class* c = Decode<Class*>(env, javaThis); |
| 89 | std::string descriptor(c->GetDescriptor()->ToModifiedUtf8()); |
| 90 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 91 | // The descriptor indicates that this is the class for |
| 92 | // a primitive type; special-case the return value. |
| 93 | const char* name = NULL; |
| 94 | switch (descriptor[0]) { |
| 95 | case 'Z': name = "boolean"; break; |
| 96 | case 'B': name = "byte"; break; |
| 97 | case 'C': name = "char"; break; |
| 98 | case 'S': name = "short"; break; |
| 99 | case 'I': name = "int"; break; |
| 100 | case 'J': name = "long"; break; |
| 101 | case 'F': name = "float"; break; |
| 102 | case 'D': name = "double"; break; |
| 103 | case 'V': name = "void"; break; |
| 104 | default: |
| 105 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 106 | } |
| 107 | return env->NewStringUTF(name); |
| 108 | } |
| 109 | |
| 110 | // Convert the UTF-8 name to a java.lang.String. The |
| 111 | // name must use '.' to separate package components. |
| 112 | if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') { |
| 113 | descriptor.erase(0, 1); |
| 114 | descriptor.erase(descriptor.size() - 1); |
| 115 | } |
| 116 | std::replace(descriptor.begin(), descriptor.end(), '/', '.'); |
| 117 | return env->NewStringUTF(descriptor.c_str()); |
| 118 | } |
| 119 | |
| 120 | jclass Class_getSuperclass(JNIEnv* env, jobject javaThis) { |
| 121 | Class* c = Decode<Class*>(env, javaThis); |
| 122 | Class* result = c->GetSuperClass(); |
| 123 | return AddLocalReference<jclass>(env, result); |
| 124 | } |
| 125 | |
| 126 | jboolean Class_isAnonymousClass(JNIEnv* env, jobject javaThis) { |
| 127 | UNIMPLEMENTED(WARNING) << "needs annotations"; |
| 128 | return JNI_FALSE; |
| 129 | } |
| 130 | |
| 131 | jboolean Class_isInterface(JNIEnv* env, jobject javaThis) { |
| 132 | Class* c = Decode<Class*>(env, javaThis); |
| 133 | return c->IsInterface(); |
| 134 | } |
| 135 | |
| 136 | jboolean Class_isPrimitive(JNIEnv* env, jobject javaThis) { |
| 137 | Class* c = Decode<Class*>(env, javaThis); |
| 138 | return c->IsPrimitive(); |
| 139 | } |
| 140 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 141 | static JNINativeMethod gMethods[] = { |
| 142 | //NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame^] | 143 | NATIVE_METHOD(Class, desiredAssertionStatus, "()Z"), |
| 144 | NATIVE_METHOD(Class, getClassLoader, "(Ljava/lang/Class;)Ljava/lang/ClassLoader;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 145 | NATIVE_METHOD(Class, getComponentType, "()Ljava/lang/Class;"), |
| 146 | //NATIVE_METHOD(Class, getDeclaredAnnotation, "(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"), |
| 147 | //NATIVE_METHOD(Class, getDeclaredAnnotations, "()[Ljava/lang/annotation/Annotation;"), |
| 148 | NATIVE_METHOD(Class, getDeclaredClasses, "(Ljava/lang/Class;Z)[Ljava/lang/Class;"), |
| 149 | //NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"), |
| 150 | //NATIVE_METHOD(Class, getDeclaredConstructors, "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Constructor;"), |
| 151 | //NATIVE_METHOD(Class, getDeclaredField, "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field;"), |
| 152 | //NATIVE_METHOD(Class, getDeclaredFields, "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Field;"), |
| 153 | //NATIVE_METHOD(Class, getDeclaredMethods, "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Method;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame^] | 154 | NATIVE_METHOD(Class, getDeclaringClass, "()Ljava/lang/Class;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 155 | //NATIVE_METHOD(Class, getEnclosingClass, "()Ljava/lang/Class;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame^] | 156 | NATIVE_METHOD(Class, getEnclosingConstructor, "()Ljava/lang/reflect/Constructor;"), |
| 157 | NATIVE_METHOD(Class, getEnclosingMethod, "()Ljava/lang/reflect/Method;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 158 | //NATIVE_METHOD(Class, getInnerClassName, "()Ljava/lang/String;"), |
| 159 | //NATIVE_METHOD(Class, getInterfaces, "()[Ljava/lang/Class;"), |
| 160 | //NATIVE_METHOD(Class, getModifiers, "(Ljava/lang/Class;Z)I"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame^] | 161 | NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 162 | //NATIVE_METHOD(Class, getSignatureAnnotation, "()[Ljava/lang/Object;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame^] | 163 | NATIVE_METHOD(Class, getSuperclass, "()Ljava/lang/Class;"), |
| 164 | NATIVE_METHOD(Class, isAnonymousClass, "()Z"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 165 | //NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"), |
| 166 | //NATIVE_METHOD(Class, isDeclaredAnnotationPresent, "(Ljava/lang/Class;)Z"), |
| 167 | //NATIVE_METHOD(Class, isInstance, "(Ljava/lang/Object;)Z"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame^] | 168 | NATIVE_METHOD(Class, isInterface, "()Z"), |
| 169 | NATIVE_METHOD(Class, isPrimitive, "()Z"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 170 | //NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"), |
| 171 | }; |
| 172 | |
| 173 | } // namespace |
| 174 | |
| 175 | void register_java_lang_Class(JNIEnv* env) { |
| 176 | jniRegisterNativeMethods(env, "java/lang/Class", gMethods, NELEM(gMethods)); |
| 177 | } |
| 178 | |
| 179 | } // namespace art |