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