Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 17 | #include <jni.h> |
| 18 | #include <vector> |
| 19 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 20 | #include "art_field-inl.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 21 | #include "class_linker-inl.h" |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 22 | #include "common_compiler_test.h" |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame^] | 23 | #include "mirror/field-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 24 | #include "mirror/method.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change.h" |
| 26 | |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 27 | namespace art { |
| 28 | |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 29 | class ProxyTest : public CommonCompilerTest { |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 30 | public: |
| 31 | // Generate a proxy class with the given name and interfaces. This is a simplification from what |
| 32 | // libcore does to fit to our test needs. We do not check for duplicated interfaces or methods and |
| 33 | // we do not declare exceptions. |
| 34 | mirror::Class* GenerateProxyClass(ScopedObjectAccess& soa, jobject jclass_loader, |
| 35 | const char* className, |
| 36 | const std::vector<mirror::Class*>& interfaces) |
| 37 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 38 | mirror::Class* javaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
| 39 | CHECK(javaLangObject != nullptr); |
| 40 | |
| 41 | jclass javaLangClass = soa.AddLocalReference<jclass>(mirror::Class::GetJavaLangClass()); |
| 42 | |
| 43 | // Builds the interfaces array. |
| 44 | jobjectArray proxyClassInterfaces = soa.Env()->NewObjectArray(interfaces.size(), javaLangClass, |
| 45 | nullptr); |
| 46 | soa.Self()->AssertNoPendingException(); |
| 47 | for (size_t i = 0; i < interfaces.size(); ++i) { |
| 48 | soa.Env()->SetObjectArrayElement(proxyClassInterfaces, i, |
| 49 | soa.AddLocalReference<jclass>(interfaces[i])); |
| 50 | } |
| 51 | |
| 52 | // Builds the method array. |
| 53 | jsize methods_count = 3; // Object.equals, Object.hashCode and Object.toString. |
| 54 | for (mirror::Class* interface : interfaces) { |
| 55 | mirror::ObjectArray<mirror::ArtMethod>* virtual_methods = interface->GetVirtualMethods(); |
| 56 | methods_count += (virtual_methods == nullptr) ? 0 : virtual_methods->GetLength(); |
| 57 | } |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 58 | jobjectArray proxyClassMethods = soa.Env()->NewObjectArray( |
| 59 | methods_count, soa.AddLocalReference<jclass>(mirror::Method::StaticClass()), nullptr); |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 60 | soa.Self()->AssertNoPendingException(); |
| 61 | |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 62 | jsize array_index = 0; |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 63 | // Fill the method array |
| 64 | mirror::ArtMethod* method = javaLangObject->FindDeclaredVirtualMethod( |
| 65 | "equals", "(Ljava/lang/Object;)Z"); |
| 66 | CHECK(method != nullptr); |
| 67 | soa.Env()->SetObjectArrayElement( |
| 68 | proxyClassMethods, array_index++, soa.AddLocalReference<jobject>( |
| 69 | mirror::Method::CreateFromArtMethod(soa.Self(), method))); |
| 70 | method = javaLangObject->FindDeclaredVirtualMethod("hashCode", "()I"); |
| 71 | CHECK(method != nullptr); |
| 72 | soa.Env()->SetObjectArrayElement( |
| 73 | proxyClassMethods, array_index++, soa.AddLocalReference<jobject>( |
| 74 | mirror::Method::CreateFromArtMethod(soa.Self(), method))); |
| 75 | method = javaLangObject->FindDeclaredVirtualMethod("toString", "()Ljava/lang/String;"); |
| 76 | CHECK(method != nullptr); |
| 77 | soa.Env()->SetObjectArrayElement( |
| 78 | proxyClassMethods, array_index++, soa.AddLocalReference<jobject>( |
| 79 | mirror::Method::CreateFromArtMethod(soa.Self(), method))); |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 80 | // Now adds all interfaces virtual methods. |
| 81 | for (mirror::Class* interface : interfaces) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 82 | for (int32_t i = 0, count = interface->NumVirtualMethods(); i < count; ++i) { |
| 83 | soa.Env()->SetObjectArrayElement( |
| 84 | proxyClassMethods, array_index++, soa.AddLocalReference<jobject>( |
| 85 | mirror::Method::CreateFromArtMethod(soa.Self(), interface->GetVirtualMethod(i)))); |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | CHECK_EQ(array_index, methods_count); |
| 89 | |
| 90 | // Builds an empty exception array. |
| 91 | jobjectArray proxyClassThrows = soa.Env()->NewObjectArray(0, javaLangClass, nullptr); |
| 92 | soa.Self()->AssertNoPendingException(); |
| 93 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 94 | mirror::Class* proxyClass = class_linker_->CreateProxyClass( |
| 95 | soa, soa.Env()->NewStringUTF(className), proxyClassInterfaces, jclass_loader, |
| 96 | proxyClassMethods, proxyClassThrows); |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 97 | soa.Self()->AssertNoPendingException(); |
| 98 | return proxyClass; |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | // Creates a proxy class and check ClassHelper works correctly. |
| 103 | TEST_F(ProxyTest, ProxyClassHelper) { |
| 104 | ScopedObjectAccess soa(Thread::Current()); |
| 105 | jobject jclass_loader = LoadDex("Interfaces"); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 106 | StackHandleScope<4> hs(soa.Self()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 107 | Handle<mirror::ClassLoader> class_loader( |
| 108 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader))); |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 109 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 110 | Handle<mirror::Class> I(hs.NewHandle( |
| 111 | class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader))); |
| 112 | Handle<mirror::Class> J(hs.NewHandle( |
| 113 | class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader))); |
| 114 | ASSERT_TRUE(I.Get() != nullptr); |
| 115 | ASSERT_TRUE(J.Get() != nullptr); |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 116 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 117 | std::vector<mirror::Class*> interfaces; |
| 118 | interfaces.push_back(I.Get()); |
| 119 | interfaces.push_back(J.Get()); |
| 120 | Handle<mirror::Class> proxy_class(hs.NewHandle( |
| 121 | GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces))); |
| 122 | interfaces.clear(); // Don't least possibly stale objects in the array as good practice. |
| 123 | ASSERT_TRUE(proxy_class.Get() != nullptr); |
| 124 | ASSERT_TRUE(proxy_class->IsProxyClass()); |
| 125 | ASSERT_TRUE(proxy_class->IsInitialized()); |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 126 | |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 127 | EXPECT_EQ(2U, proxy_class->NumDirectInterfaces()); // Interfaces$I and Interfaces$J. |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 128 | EXPECT_EQ(I.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class, 0)); |
| 129 | EXPECT_EQ(J.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class, 1)); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 130 | std::string temp; |
| 131 | const char* proxy_class_descriptor = proxy_class->GetDescriptor(&temp); |
| 132 | EXPECT_STREQ("L$Proxy1234;", proxy_class_descriptor); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 133 | EXPECT_EQ(nullptr, proxy_class->GetSourceFile()); |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 136 | // Creates a proxy class and check FieldHelper works correctly. |
| 137 | TEST_F(ProxyTest, ProxyFieldHelper) { |
| 138 | ScopedObjectAccess soa(Thread::Current()); |
| 139 | jobject jclass_loader = LoadDex("Interfaces"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 140 | StackHandleScope<9> hs(soa.Self()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 141 | Handle<mirror::ClassLoader> class_loader( |
| 142 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader))); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 143 | |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 144 | Handle<mirror::Class> I(hs.NewHandle( |
| 145 | class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader))); |
| 146 | Handle<mirror::Class> J(hs.NewHandle( |
| 147 | class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader))); |
| 148 | ASSERT_TRUE(I.Get() != nullptr); |
| 149 | ASSERT_TRUE(J.Get() != nullptr); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 150 | |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 151 | Handle<mirror::Class> proxyClass; |
| 152 | { |
| 153 | std::vector<mirror::Class*> interfaces; |
| 154 | interfaces.push_back(I.Get()); |
| 155 | interfaces.push_back(J.Get()); |
| 156 | proxyClass = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces)); |
| 157 | } |
| 158 | |
| 159 | ASSERT_TRUE(proxyClass.Get() != nullptr); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 160 | ASSERT_TRUE(proxyClass->IsProxyClass()); |
| 161 | ASSERT_TRUE(proxyClass->IsInitialized()); |
| 162 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 163 | ArtField* instance_fields = proxyClass->GetIFields(); |
| 164 | EXPECT_TRUE(instance_fields == nullptr); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 165 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 166 | ArtField* static_fields = proxyClass->GetSFields(); |
| 167 | ASSERT_TRUE(static_fields != nullptr); |
| 168 | ASSERT_EQ(2u, proxyClass->NumStaticFields()); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 169 | |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 170 | Handle<mirror::Class> interfacesFieldClass( |
| 171 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;"))); |
| 172 | ASSERT_TRUE(interfacesFieldClass.Get() != nullptr); |
| 173 | Handle<mirror::Class> throwsFieldClass( |
| 174 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;"))); |
| 175 | ASSERT_TRUE(throwsFieldClass.Get() != nullptr); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 176 | |
| 177 | // Test "Class[] interfaces" field. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 178 | ArtField* field = &static_fields[0]; |
| 179 | EXPECT_STREQ("interfaces", field->GetName()); |
| 180 | EXPECT_STREQ("[Ljava/lang/Class;", field->GetTypeDescriptor()); |
| 181 | EXPECT_EQ(interfacesFieldClass.Get(), field->GetType<true>()); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 182 | std::string temp; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 183 | EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp)); |
| 184 | EXPECT_FALSE(field->IsPrimitiveType()); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 185 | |
| 186 | // Test "Class[][] throws" field. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 187 | field = &static_fields[1]; |
| 188 | EXPECT_STREQ("throws", field->GetName()); |
| 189 | EXPECT_STREQ("[[Ljava/lang/Class;", field->GetTypeDescriptor()); |
| 190 | EXPECT_EQ(throwsFieldClass.Get(), field->GetType<true>()); |
| 191 | EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp)); |
| 192 | EXPECT_FALSE(field->IsPrimitiveType()); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 193 | } |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 194 | |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame^] | 195 | // Creates two proxy classes and check the art/mirror fields of their static fields. |
| 196 | TEST_F(ProxyTest, CheckArtMirrorFieldsOfProxyStaticFields) { |
| 197 | ScopedObjectAccess soa(Thread::Current()); |
| 198 | jobject jclass_loader = LoadDex("Interfaces"); |
| 199 | StackHandleScope<7> hs(soa.Self()); |
| 200 | Handle<mirror::ClassLoader> class_loader( |
| 201 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader))); |
| 202 | |
| 203 | Handle<mirror::Class> proxyClass0; |
| 204 | Handle<mirror::Class> proxyClass1; |
| 205 | { |
| 206 | std::vector<mirror::Class*> interfaces; |
| 207 | proxyClass0 = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy0", interfaces)); |
| 208 | proxyClass1 = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy1", interfaces)); |
| 209 | } |
| 210 | |
| 211 | ASSERT_TRUE(proxyClass0.Get() != nullptr); |
| 212 | ASSERT_TRUE(proxyClass0->IsProxyClass()); |
| 213 | ASSERT_TRUE(proxyClass0->IsInitialized()); |
| 214 | ASSERT_TRUE(proxyClass1.Get() != nullptr); |
| 215 | ASSERT_TRUE(proxyClass1->IsProxyClass()); |
| 216 | ASSERT_TRUE(proxyClass1->IsInitialized()); |
| 217 | |
| 218 | ArtField* static_fields0 = proxyClass0->GetSFields(); |
| 219 | ASSERT_TRUE(static_fields0 != nullptr); |
| 220 | ASSERT_EQ(2u, proxyClass0->NumStaticFields()); |
| 221 | ArtField* static_fields1 = proxyClass1->GetSFields(); |
| 222 | ASSERT_TRUE(static_fields1 != nullptr); |
| 223 | ASSERT_EQ(2u, proxyClass1->NumStaticFields()); |
| 224 | |
| 225 | EXPECT_EQ(static_fields0[0].GetDeclaringClass(), proxyClass0.Get()); |
| 226 | EXPECT_EQ(static_fields0[1].GetDeclaringClass(), proxyClass0.Get()); |
| 227 | EXPECT_EQ(static_fields1[0].GetDeclaringClass(), proxyClass1.Get()); |
| 228 | EXPECT_EQ(static_fields1[1].GetDeclaringClass(), proxyClass1.Get()); |
| 229 | |
| 230 | Handle<mirror::Field> field00 = |
| 231 | hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields0[0], true)); |
| 232 | Handle<mirror::Field> field01 = |
| 233 | hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields0[1], true)); |
| 234 | Handle<mirror::Field> field10 = |
| 235 | hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields1[0], true)); |
| 236 | Handle<mirror::Field> field11 = |
| 237 | hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields1[1], true)); |
| 238 | EXPECT_EQ(field00->GetArtField(), &static_fields0[0]); |
| 239 | EXPECT_EQ(field01->GetArtField(), &static_fields0[1]); |
| 240 | EXPECT_EQ(field10->GetArtField(), &static_fields1[0]); |
| 241 | EXPECT_EQ(field11->GetArtField(), &static_fields1[1]); |
| 242 | } |
| 243 | |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 244 | } // namespace art |