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 "reflection.h" 24 #include "scoped_fast_native_object_access.h" 25 #include "well_known_classes.h" 26 27 namespace art { 28 29 /* 30 * We get here through Constructor.newInstance(). The Constructor object 31 * would not be available if the constructor weren't public (per the 32 * definition of Class.getConstructor), so we can skip the method access 33 * check. We can also safely assume the constructor isn't associated 34 * with an interface, array, or primitive class. 35 */ 36 static jobject Constructor_newInstance(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs, 37 jboolean accessible) { 38 ScopedFastNativeObjectAccess soa(env); 39 mirror::ArtMethod* m = mirror::ArtMethod::FromReflectedMethod(soa, javaMethod); 40 StackHandleScope<1> hs(soa.Self()); 41 Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass())); 42 if (UNLIKELY(c->IsAbstract())) { 43 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); 44 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/InstantiationException;", 45 "Can't instantiate %s %s", 46 c->IsInterface() ? "interface" : "abstract class", 47 PrettyDescriptor(c.Get()).c_str()); 48 return nullptr; 49 } 50 51 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) { 52 DCHECK(soa.Self()->IsExceptionPending()); 53 return nullptr; 54 } 55 56 bool movable = true; 57 if (!kMovingMethods && c->IsArtMethodClass()) { 58 movable = false; 59 } else if (!kMovingFields && c->IsArtFieldClass()) { 60 movable = false; 61 } else if (!kMovingClasses && c->IsClassClass()) { 62 movable = false; 63 } 64 mirror::Object* receiver = 65 movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self()); 66 if (receiver == nullptr) { 67 return nullptr; 68 } 69 70 jobject javaReceiver = soa.AddLocalReference<jobject>(receiver); 71 InvokeMethod(soa, javaMethod, javaReceiver, javaArgs, (accessible == JNI_TRUE)); 72 73 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod. 74 return javaReceiver; 75 } 76 77 static JNINativeMethod gMethods[] = { 78 NATIVE_METHOD(Constructor, newInstance, "!([Ljava/lang/Object;Z)Ljava/lang/Object;"), 79 }; 80 81 void register_java_lang_reflect_Constructor(JNIEnv* env) { 82 REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor"); 83 } 84 85 } // namespace art 86