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