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