blob: 76484258f75b16ce09763bd6e55663b72d872c33 [file] [log] [blame]
Elliott Hughesd369bb72011-09-12 14:41:14 -07001/*
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
23namespace art {
24
25namespace {
26
Elliott Hughes6bdc3b22011-09-16 19:24:10 -070027jboolean Class_desiredAssertionStatus(JNIEnv* env, jobject javaThis) {
28 return JNI_FALSE;
29}
30
31jobject 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 Hughesd369bb72011-09-12 14:41:14 -070037jclass 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 Hughesd369bb72011-09-12 14:41:14 -070056}
57
58jobjectArray Class_getDeclaredClasses(JNIEnv* env, jclass java_lang_Class_class, jclass c, jboolean publicOnly) {
Elliott Hughes6bdc3b22011-09-16 19:24:10 -070059 UNIMPLEMENTED(WARNING) << "needs annotations";
Elliott Hughesd369bb72011-09-12 14:41:14 -070060 return env->NewObjectArray(0, java_lang_Class_class, NULL);
61}
62
Elliott Hughes6bdc3b22011-09-16 19:24:10 -070063jclass Class_getDeclaringClass(JNIEnv* env, jobject javaThis) {
64 UNIMPLEMENTED(WARNING) << "needs annotations";
65 return NULL;
66}
67
68jobject Class_getEnclosingConstructor(JNIEnv* env, jobject javaThis) {
69 UNIMPLEMENTED(WARNING) << "needs annotations";
70 return NULL;
71}
72
73jobject 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 */
87jstring 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
120jclass 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
126jboolean Class_isAnonymousClass(JNIEnv* env, jobject javaThis) {
127 UNIMPLEMENTED(WARNING) << "needs annotations";
128 return JNI_FALSE;
129}
130
131jboolean Class_isInterface(JNIEnv* env, jobject javaThis) {
132 Class* c = Decode<Class*>(env, javaThis);
133 return c->IsInterface();
134}
135
136jboolean Class_isPrimitive(JNIEnv* env, jobject javaThis) {
137 Class* c = Decode<Class*>(env, javaThis);
138 return c->IsPrimitive();
139}
140
Elliott Hughesd369bb72011-09-12 14:41:14 -0700141static JNINativeMethod gMethods[] = {
142 //NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700143 NATIVE_METHOD(Class, desiredAssertionStatus, "()Z"),
144 NATIVE_METHOD(Class, getClassLoader, "(Ljava/lang/Class;)Ljava/lang/ClassLoader;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700145 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 Hughes6bdc3b22011-09-16 19:24:10 -0700154 NATIVE_METHOD(Class, getDeclaringClass, "()Ljava/lang/Class;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700155 //NATIVE_METHOD(Class, getEnclosingClass, "()Ljava/lang/Class;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700156 NATIVE_METHOD(Class, getEnclosingConstructor, "()Ljava/lang/reflect/Constructor;"),
157 NATIVE_METHOD(Class, getEnclosingMethod, "()Ljava/lang/reflect/Method;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700158 //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 Hughes6bdc3b22011-09-16 19:24:10 -0700161 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700162 //NATIVE_METHOD(Class, getSignatureAnnotation, "()[Ljava/lang/Object;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700163 NATIVE_METHOD(Class, getSuperclass, "()Ljava/lang/Class;"),
164 NATIVE_METHOD(Class, isAnonymousClass, "()Z"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700165 //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 Hughes6bdc3b22011-09-16 19:24:10 -0700168 NATIVE_METHOD(Class, isInterface, "()Z"),
169 NATIVE_METHOD(Class, isPrimitive, "()Z"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700170 //NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"),
171};
172
173} // namespace
174
175void register_java_lang_Class(JNIEnv* env) {
176 jniRegisterNativeMethods(env, "java/lang/Class", gMethods, NELEM(gMethods));
177}
178
179} // namespace art