proxy_test.cc revision 4206eb5d86d3a2406361e59b2018152b2485cced
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 23namespace art { 24 25class 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. 107TEST_F(ProxyTest, ProxyClassHelper) { 108 ScopedObjectAccess soa(Thread::Current()); 109 jobject jclass_loader = LoadDex("Interfaces"); 110 StackHandleScope<4> hs(soa.Self()); 111 Handle<mirror::ClassLoader> class_loader( 112 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader))); 113 114 Handle<mirror::Class> I(hs.NewHandle( 115 class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader))); 116 Handle<mirror::Class> J(hs.NewHandle( 117 class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader))); 118 ASSERT_TRUE(I.Get() != nullptr); 119 ASSERT_TRUE(J.Get() != nullptr); 120 121 std::vector<mirror::Class*> interfaces; 122 interfaces.push_back(I.Get()); 123 interfaces.push_back(J.Get()); 124 Handle<mirror::Class> proxy_class(hs.NewHandle( 125 GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces))); 126 interfaces.clear(); // Don't least possibly stale objects in the array as good practice. 127 ASSERT_TRUE(proxy_class.Get() != nullptr); 128 ASSERT_TRUE(proxy_class->IsProxyClass()); 129 ASSERT_TRUE(proxy_class->IsInitialized()); 130 131 EXPECT_EQ(2U, proxy_class->NumDirectInterfaces()); // Interfaces$I and Interfaces$J. 132 EXPECT_EQ(I.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class, 0)); 133 EXPECT_EQ(J.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class, 1)); 134 std::string proxy_class_descriptor(proxy_class->GetDescriptor()); 135 EXPECT_STREQ("L$Proxy1234;", proxy_class_descriptor.c_str()); 136 EXPECT_EQ(nullptr, proxy_class->GetSourceFile()); 137} 138 139// Creates a proxy class and check FieldHelper works correctly. 140TEST_F(ProxyTest, ProxyFieldHelper) { 141 ScopedObjectAccess soa(Thread::Current()); 142 jobject jclass_loader = LoadDex("Interfaces"); 143 StackHandleScope<1> hs(soa.Self()); 144 Handle<mirror::ClassLoader> class_loader( 145 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader))); 146 147 mirror::Class* I = class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader); 148 mirror::Class* J = class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader); 149 ASSERT_TRUE(I != nullptr); 150 ASSERT_TRUE(J != nullptr); 151 std::vector<mirror::Class*> interfaces; 152 interfaces.push_back(I); 153 interfaces.push_back(J); 154 155 mirror::Class* proxyClass = GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces); 156 ASSERT_TRUE(proxyClass != nullptr); 157 ASSERT_TRUE(proxyClass->IsProxyClass()); 158 ASSERT_TRUE(proxyClass->IsInitialized()); 159 160 mirror::ObjectArray<mirror::ArtField>* instance_fields = proxyClass->GetIFields(); 161 EXPECT_TRUE(instance_fields == nullptr); 162 163 mirror::ObjectArray<mirror::ArtField>* static_fields = proxyClass->GetSFields(); 164 ASSERT_TRUE(static_fields != nullptr); 165 ASSERT_EQ(2, static_fields->GetLength()); 166 167 mirror::Class* interfacesFieldClass = class_linker_->FindSystemClass(soa.Self(), 168 "[Ljava/lang/Class;"); 169 ASSERT_TRUE(interfacesFieldClass != nullptr); 170 mirror::Class* throwsFieldClass = class_linker_->FindSystemClass(soa.Self(), 171 "[[Ljava/lang/Class;"); 172 ASSERT_TRUE(throwsFieldClass != nullptr); 173 174 // Test "Class[] interfaces" field. 175 FieldHelper fh(static_fields->Get(0)); 176 EXPECT_EQ("interfaces", std::string(fh.GetName())); 177 EXPECT_EQ("[Ljava/lang/Class;", std::string(fh.GetTypeDescriptor())); 178 EXPECT_EQ(interfacesFieldClass, fh.GetType()); 179 EXPECT_EQ("L$Proxy1234;", std::string(fh.GetDeclaringClassDescriptor())); 180 EXPECT_FALSE(fh.IsPrimitiveType()); 181 182 // Test "Class[][] throws" field. 183 fh.ChangeField(static_fields->Get(1)); 184 EXPECT_EQ("throws", std::string(fh.GetName())); 185 EXPECT_EQ("[[Ljava/lang/Class;", std::string(fh.GetTypeDescriptor())); 186 EXPECT_EQ(throwsFieldClass, fh.GetType()); 187 EXPECT_EQ("L$Proxy1234;", std::string(fh.GetDeclaringClassDescriptor())); 188 EXPECT_FALSE(fh.IsPrimitiveType()); 189} 190 191} // namespace art 192