1 /* 2 * Copyright (C) 2016 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 "method_type.h" 18 19 #include <string> 20 #include <vector> 21 22 #include "class-inl.h" 23 #include "class_linker-inl.h" 24 #include "class_loader.h" 25 #include "common_runtime_test.h" 26 #include "handle_scope-inl.h" 27 #include "object_array-inl.h" 28 #include "scoped_thread_state_change-inl.h" 29 30 namespace art { 31 namespace mirror { 32 33 class MethodTypeTest : public CommonRuntimeTest {}; 34 35 static std::string FullyQualifiedType(const std::string& shorthand) { 36 return "Ljava/lang/" + shorthand + ";"; 37 } 38 39 static mirror::MethodType* CreateMethodType(const std::string& return_type, 40 const std::vector<std::string>& param_types) { 41 CHECK_LT(param_types.size(), 3u); 42 43 Runtime* const runtime = Runtime::Current(); 44 ClassLinker* const class_linker = runtime->GetClassLinker(); 45 Thread* const self = Thread::Current(); 46 47 ScopedObjectAccess soa(self); 48 StackHandleScope<5> hs(soa.Self()); 49 50 Handle<mirror::ClassLoader> boot_class_loader = hs.NewHandle<mirror::ClassLoader>(nullptr); 51 52 Handle<mirror::Class> return_clazz = hs.NewHandle(class_linker->FindClass( 53 soa.Self(), FullyQualifiedType(return_type).c_str(), boot_class_loader)); 54 CHECK(return_clazz != nullptr); 55 56 ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); 57 mirror::Class* class_array_type = class_linker->FindArrayClass(self, &class_type); 58 Handle<mirror::ObjectArray<mirror::Class>> param_classes = hs.NewHandle( 59 mirror::ObjectArray<mirror::Class>::Alloc(self, class_array_type, param_types.size())); 60 61 for (uint32_t i = 0; i < param_types.size(); ++i) { 62 Handle<mirror::Class> param = hs.NewHandle(class_linker->FindClass( 63 soa.Self(), FullyQualifiedType(param_types[i]).c_str(), boot_class_loader)); 64 param_classes->Set(i, param.Get()); 65 } 66 67 return mirror::MethodType::Create(self, return_clazz, param_classes); 68 } 69 70 71 TEST_F(MethodTypeTest, IsExactMatch) { 72 ScopedObjectAccess soa(Thread::Current()); 73 { 74 StackHandleScope<2> hs(soa.Self()); 75 Handle<mirror::MethodType> mt1 = hs.NewHandle(CreateMethodType("String", { "Integer" })); 76 Handle<mirror::MethodType> mt2 = hs.NewHandle(CreateMethodType("String", { "Integer" })); 77 ASSERT_TRUE(mt1->IsExactMatch(mt2.Get())); 78 } 79 80 // Mismatched return type. 81 { 82 StackHandleScope<2> hs(soa.Self()); 83 Handle<mirror::MethodType> mt1 = hs.NewHandle(CreateMethodType("String", { "Integer" })); 84 Handle<mirror::MethodType> mt2 = hs.NewHandle(CreateMethodType("Integer", { "Integer" })); 85 ASSERT_FALSE(mt1->IsExactMatch(mt2.Get())); 86 } 87 88 // Mismatched param types. 89 { 90 StackHandleScope<2> hs(soa.Self()); 91 Handle<mirror::MethodType> mt1 = hs.NewHandle(CreateMethodType("String", { "Integer" })); 92 Handle<mirror::MethodType> mt2 = hs.NewHandle(CreateMethodType("String", { "String" })); 93 ASSERT_FALSE(mt1->IsExactMatch(mt2.Get())); 94 } 95 96 // Wrong number of param types. 97 { 98 StackHandleScope<2> hs(soa.Self()); 99 Handle<mirror::MethodType> mt1 = hs.NewHandle( 100 CreateMethodType("String", { "String", "String" })); 101 Handle<mirror::MethodType> mt2 = hs.NewHandle(CreateMethodType("String", { "String" })); 102 ASSERT_FALSE(mt1->IsExactMatch(mt2.Get())); 103 } 104 } 105 106 } // namespace mirror 107 } // namespace art 108