blob: fe5da9899f721dff22102b72ec45b95e516cc534 [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"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070019#include "class_loader.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070020#include "object.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070021#include "ScopedUtfChars.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070022
23#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
24
25namespace art {
26
27namespace {
28
Brian Carlstromf91c8c32011-09-21 17:30:34 -070029// "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
30jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) {
31 ScopedUtfChars name(env, javaName);
32 if (name.c_str() == NULL) {
33 return NULL;
34 }
35
36 // We need to validate and convert the name (from x.y.z to x/y/z). This
37 // is especially handy for array types, since we want to avoid
38 // auto-generating bogus array classes.
39 if (!IsValidClassName(name.c_str(), true, true)) {
40 Thread::Current()->ThrowNewException("Ljava/lang/ClassNotFoundException;",
41 "Invalid name: %s", name.c_str());
42 return NULL;
43 }
44
45 std::string descriptor(DotToDescriptor(name.c_str()));
46 Object* loader = Decode<Object*>(env, javaLoader);
47 ClassLoader* class_loader = down_cast<ClassLoader*>(loader);
48 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
49 Class* c = class_linker->FindClass(descriptor.c_str(), class_loader);
50 if (initialize) {
51 class_linker->EnsureInitialized(c, true);
52 }
53 return AddLocalReference<jclass>(env, c);
54}
55
Elliott Hughes6bdc3b22011-09-16 19:24:10 -070056jboolean Class_desiredAssertionStatus(JNIEnv* env, jobject javaThis) {
57 return JNI_FALSE;
58}
59
60jobject Class_getClassLoader(JNIEnv* env, jclass, jobject javaClass) {
61 Class* c = Decode<Class*>(env, javaClass);
62 Object* result = reinterpret_cast<Object*>(const_cast<ClassLoader*>(c->GetClassLoader()));
63 return AddLocalReference<jobject>(env, result);
64}
65
Elliott Hughesd369bb72011-09-12 14:41:14 -070066jclass Class_getComponentType(JNIEnv* env, jobject javaThis) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070067 return AddLocalReference<jclass>(env, Decode<Class*>(env, javaThis)->GetComponentType());
Elliott Hughesd369bb72011-09-12 14:41:14 -070068}
69
70jobjectArray Class_getDeclaredClasses(JNIEnv* env, jclass java_lang_Class_class, jclass c, jboolean publicOnly) {
Elliott Hughes6bdc3b22011-09-16 19:24:10 -070071 UNIMPLEMENTED(WARNING) << "needs annotations";
Elliott Hughesd369bb72011-09-12 14:41:14 -070072 return env->NewObjectArray(0, java_lang_Class_class, NULL);
73}
74
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -070075jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass,
76 jclass jklass, jstring jname, jobjectArray jsignature) {
77 Class* klass = Decode<Class*>(env, jklass);
78 DCHECK(klass->IsClass());
79 String* name = Decode<String*>(env, jname);
80 DCHECK(name->IsString());
81 Object* signature_obj = Decode<Object*>(env, jsignature);
82 DCHECK(signature_obj->IsArrayInstance());
Brian Carlstrom03c99df2011-09-18 10:52:00 -070083 // check that this is a Class[] by checking that component type is Class
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -070084 // foo->GetClass()->GetClass() is an idiom for getting java.lang.Class from an arbitrary object
85 DCHECK(signature_obj->GetClass()->GetComponentType() == signature_obj->GetClass()->GetClass());
86 ObjectArray<Class>* signature = down_cast<ObjectArray<Class>*>(signature_obj);
87
88 std::string name_string = name->ToModifiedUtf8();
89 std::string signature_string;
90 signature_string += "(";
91 for (int i = 0; i < signature->GetLength(); i++) {
92 Class* argument_class = signature->Get(0);
93 if (argument_class == NULL) {
94 UNIMPLEMENTED(FATAL) << "throw null pointer exception?";
95 }
96 signature_string += argument_class->GetDescriptor()->ToModifiedUtf8();
97 }
98 signature_string += ")";
99
100 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
101 Method* method = klass->GetVirtualMethod(i);
102 if (!method->GetName()->Equals(name)) {
103 continue;
104 }
105 std::string method_signature = method->GetSignature()->ToModifiedUtf8();
106 if (!StringPiece(method_signature).starts_with(signature_string)) {
107 continue;
108 }
109 return AddLocalReference<jobject>(env, method);
110 }
111
Brian Carlstrom03c99df2011-09-18 10:52:00 -0700112 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
113 Method* method = klass->GetDirectMethod(i);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700114 if (!method->GetName()->Equals(name)) {
115 continue;
116 }
117 std::string method_signature = method->GetSignature()->ToModifiedUtf8();
118 if (!StringPiece(method_signature).starts_with(signature_string)) {
119 continue;
120 }
121 return AddLocalReference<jobject>(env, method);
122 }
123
124 return NULL;
125}
126
127jobject Class_getDeclaredField(JNIEnv* env, jclass, jclass jklass, jobject jname) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700128 Class* klass = Decode<Class*>(env, jklass);
129 DCHECK(klass->IsClass());
130 String* name = Decode<String*>(env, jname);
131 DCHECK(name->IsString());
132
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700133 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700134 Field* f = klass->GetInstanceField(i);
135 if (f->GetName()->Equals(name)) {
136 return AddLocalReference<jclass>(env, f);
137 }
138 }
139 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
140 Field* f = klass->GetStaticField(i);
141 if (f->GetName()->Equals(name)) {
142 return AddLocalReference<jclass>(env, f);
143 }
144 }
145 return NULL;
146}
147
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700148jclass Class_getDeclaringClass(JNIEnv* env, jobject javaThis) {
149 UNIMPLEMENTED(WARNING) << "needs annotations";
150 return NULL;
151}
152
153jobject Class_getEnclosingConstructor(JNIEnv* env, jobject javaThis) {
154 UNIMPLEMENTED(WARNING) << "needs annotations";
155 return NULL;
156}
157
158jobject Class_getEnclosingMethod(JNIEnv* env, jobject javaThis) {
159 UNIMPLEMENTED(WARNING) << "needs annotations";
160 return NULL;
161}
162
163/*
164 * private native String getNameNative()
165 *
166 * Return the class' name. The exact format is bizarre, but it's the specified
167 * behavior: keywords for primitive types, regular "[I" form for primitive
168 * arrays (so "int" but "[I"), and arrays of reference types written
169 * between "L" and ";" but with dots rather than slashes (so "java.lang.String"
170 * but "[Ljava.lang.String;"). Madness.
171 */
172jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
173 Class* c = Decode<Class*>(env, javaThis);
174 std::string descriptor(c->GetDescriptor()->ToModifiedUtf8());
175 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
176 // The descriptor indicates that this is the class for
177 // a primitive type; special-case the return value.
178 const char* name = NULL;
179 switch (descriptor[0]) {
180 case 'Z': name = "boolean"; break;
181 case 'B': name = "byte"; break;
182 case 'C': name = "char"; break;
183 case 'S': name = "short"; break;
184 case 'I': name = "int"; break;
185 case 'J': name = "long"; break;
186 case 'F': name = "float"; break;
187 case 'D': name = "double"; break;
188 case 'V': name = "void"; break;
189 default:
190 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
191 }
192 return env->NewStringUTF(name);
193 }
194
195 // Convert the UTF-8 name to a java.lang.String. The
196 // name must use '.' to separate package components.
197 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
198 descriptor.erase(0, 1);
199 descriptor.erase(descriptor.size() - 1);
200 }
201 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
202 return env->NewStringUTF(descriptor.c_str());
203}
204
205jclass Class_getSuperclass(JNIEnv* env, jobject javaThis) {
206 Class* c = Decode<Class*>(env, javaThis);
207 Class* result = c->GetSuperClass();
208 return AddLocalReference<jclass>(env, result);
209}
210
211jboolean Class_isAnonymousClass(JNIEnv* env, jobject javaThis) {
212 UNIMPLEMENTED(WARNING) << "needs annotations";
213 return JNI_FALSE;
214}
215
216jboolean Class_isInterface(JNIEnv* env, jobject javaThis) {
217 Class* c = Decode<Class*>(env, javaThis);
218 return c->IsInterface();
219}
220
221jboolean Class_isPrimitive(JNIEnv* env, jobject javaThis) {
222 Class* c = Decode<Class*>(env, javaThis);
223 return c->IsPrimitive();
224}
225
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700226bool CheckClassAccess(const Class* access_from, const Class* klass) {
227 if (klass->IsPublic()) {
228 return true;
229 }
230 return access_from->IsInSamePackage(klass);
231}
232
233// Validate method/field access.
234bool CheckMemberAccess(const Class* access_from, const Class* access_to, uint32_t member_flags) {
235 // quick accept for public access */
236 if (member_flags & kAccPublic) {
237 return true;
238 }
239
240 // quick accept for access from same class
241 if (access_from == access_to) {
242 return true;
243 }
244
245 // quick reject for private access from another class
246 if (member_flags & kAccPrivate) {
247 return false;
248 }
249
250 // Semi-quick test for protected access from a sub-class, which may or
251 // may not be in the same package.
252 if (member_flags & kAccProtected) {
253 if (access_from->IsSubClass(access_to)) {
254 return true;
255 }
256 }
257
258 // Allow protected and private access from other classes in the same
259 return access_from->IsInSamePackage(access_to);
260}
261
262jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) {
263 Class* c = Decode<Class*>(env, javaThis);
264 if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) {
265 Thread::Current()->ThrowNewException("Ljava/lang/InstantiationException;",
266 "Class %s can not be instantiated", PrettyDescriptor(c->GetDescriptor()).c_str());
267 return NULL;
268 }
269
270 Method* init = c->FindDirectMethod("<init>", "()V");
271 if (init == NULL) {
272 Thread::Current()->ThrowNewException("Ljava/lang/InstantiationException;",
273 "Class %s has no default <init>()V constructor", PrettyDescriptor(c->GetDescriptor()).c_str());
274 return NULL;
275 }
276
277 // Verify access from the call site.
278 //
279 // First, make sure the method invoking Class.newInstance() has permission
280 // to access the class.
281 //
282 // Second, make sure it has permission to invoke the constructor. The
283 // constructor must be public or, if the caller is in the same package,
284 // have package scope.
285 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
286 Frame frame = Thread::Current()->GetTopOfStack();
287 frame.Next();
288 frame.Next();
289 Method* caller_caller = frame.GetMethod();
290 Class* caller_class = caller_caller->GetDeclaringClass();
291
292 if (!CheckClassAccess(c, caller_class)) {
293 Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessException;",
294 "Class %s is not accessible from class %s",
295 PrettyDescriptor(c->GetDescriptor()).c_str(),
296 PrettyDescriptor(caller_class->GetDescriptor()).c_str());
297 return NULL;
298 }
299 if (!CheckMemberAccess(caller_class, init->GetDeclaringClass(), init->GetAccessFlags())) {
300 Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessException;",
301 "%s is not accessible from class %s",
302 PrettyMethod(init).c_str(),
303 PrettyDescriptor(caller_class->GetDescriptor()).c_str());
304 return NULL;
305 }
306
307 Object* new_obj = c->AllocObject();
308 if (new_obj == NULL) {
309 DCHECK(Thread::Current()->IsExceptionPending());
310 return NULL;
311 }
312
313 // invoke constructor; unlike reflection calls, we don't wrap exceptions
314 jclass jklass = AddLocalReference<jclass>(env, c);
315 jmethodID mid = EncodeMethod(init);
316 return env->NewObject(jklass, mid);
317}
318
Elliott Hughesd369bb72011-09-12 14:41:14 -0700319static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700320 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700321 NATIVE_METHOD(Class, desiredAssertionStatus, "()Z"),
322 NATIVE_METHOD(Class, getClassLoader, "(Ljava/lang/Class;)Ljava/lang/ClassLoader;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700323 NATIVE_METHOD(Class, getComponentType, "()Ljava/lang/Class;"),
324 //NATIVE_METHOD(Class, getDeclaredAnnotation, "(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
325 //NATIVE_METHOD(Class, getDeclaredAnnotations, "()[Ljava/lang/annotation/Annotation;"),
326 NATIVE_METHOD(Class, getDeclaredClasses, "(Ljava/lang/Class;Z)[Ljava/lang/Class;"),
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700327 NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700328 //NATIVE_METHOD(Class, getDeclaredConstructors, "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Constructor;"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700329 NATIVE_METHOD(Class, getDeclaredField, "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700330 //NATIVE_METHOD(Class, getDeclaredFields, "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Field;"),
331 //NATIVE_METHOD(Class, getDeclaredMethods, "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Method;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700332 NATIVE_METHOD(Class, getDeclaringClass, "()Ljava/lang/Class;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700333 //NATIVE_METHOD(Class, getEnclosingClass, "()Ljava/lang/Class;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700334 NATIVE_METHOD(Class, getEnclosingConstructor, "()Ljava/lang/reflect/Constructor;"),
335 NATIVE_METHOD(Class, getEnclosingMethod, "()Ljava/lang/reflect/Method;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700336 //NATIVE_METHOD(Class, getInnerClassName, "()Ljava/lang/String;"),
337 //NATIVE_METHOD(Class, getInterfaces, "()[Ljava/lang/Class;"),
338 //NATIVE_METHOD(Class, getModifiers, "(Ljava/lang/Class;Z)I"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700339 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700340 //NATIVE_METHOD(Class, getSignatureAnnotation, "()[Ljava/lang/Object;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700341 NATIVE_METHOD(Class, getSuperclass, "()Ljava/lang/Class;"),
342 NATIVE_METHOD(Class, isAnonymousClass, "()Z"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700343 //NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"),
344 //NATIVE_METHOD(Class, isDeclaredAnnotationPresent, "(Ljava/lang/Class;)Z"),
345 //NATIVE_METHOD(Class, isInstance, "(Ljava/lang/Object;)Z"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700346 NATIVE_METHOD(Class, isInterface, "()Z"),
347 NATIVE_METHOD(Class, isPrimitive, "()Z"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700348 NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700349};
350
351} // namespace
352
353void register_java_lang_Class(JNIEnv* env) {
354 jniRegisterNativeMethods(env, "java/lang/Class", gMethods, NELEM(gMethods));
355}
356
357} // namespace art