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 "class_linker.h" 18 #include "jni_internal.h" 19 #include "mirror/art_method.h" 20 #include "mirror/art_method-inl.h" 21 #include "mirror/class-inl.h" 22 #include "mirror/object-inl.h" 23 #include "object_utils.h" 24 #include "reflection.h" 25 #include "scoped_thread_state_change.h" 26 #include "well_known_classes.h" 27 28 namespace art { 29 30 /* 31 * We get here through Constructor.newInstance(). The Constructor object 32 * would not be available if the constructor weren't public (per the 33 * definition of Class.getConstructor), so we can skip the method access 34 * check. We can also safely assume the constructor isn't associated 35 * with an interface, array, or primitive class. 36 */ 37 static jobject Constructor_newInstance(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) { 38 ScopedObjectAccess soa(env); 39 jobject art_method = soa.Env()->GetObjectField( 40 javaMethod, WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod); 41 42 mirror::ArtMethod* m = soa.Decode<mirror::Object*>(art_method)->AsArtMethod(); 43 mirror::Class* c = m->GetDeclaringClass(); 44 if (UNLIKELY(c->IsAbstract())) { 45 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); 46 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/InstantiationException;", 47 "Can't instantiate %s %s", 48 c->IsInterface() ? "interface" : "abstract class", 49 PrettyDescriptor(c).c_str()); 50 return NULL; 51 } 52 53 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) { 54 DCHECK(soa.Self()->IsExceptionPending()); 55 return NULL; 56 } 57 58 mirror::Object* receiver = c->AllocObject(soa.Self()); 59 if (receiver == NULL) { 60 return NULL; 61 } 62 63 jobject javaReceiver = soa.AddLocalReference<jobject>(receiver); 64 InvokeMethod(soa, javaMethod, javaReceiver, javaArgs); 65 66 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod. 67 return javaReceiver; 68 } 69 70 static JNINativeMethod gMethods[] = { 71 NATIVE_METHOD(Constructor, newInstance, "([Ljava/lang/Object;)Ljava/lang/Object;"), 72 }; 73 74 void register_java_lang_reflect_Constructor(JNIEnv* env) { 75 REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor"); 76 } 77 78 } // namespace art 79