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