proxy_test.cc revision daaf3265806eb2eadb2e03302bd68022fab5ca28
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 <jni.h> 18#include <vector> 19 20#include "common_compiler_test.h" 21#include "mirror/art_field-inl.h" 22#include "scoped_thread_state_change.h" 23 24namespace art { 25 26class ProxyTest : public CommonCompilerTest { 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. 108TEST_F(ProxyTest, ProxyClassHelper) { 109 ScopedObjectAccess soa(Thread::Current()); 110 jobject jclass_loader = LoadDex("Interfaces"); 111 StackHandleScope<4> hs(soa.Self()); 112 Handle<mirror::ClassLoader> class_loader( 113 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader))); 114 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); 121 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()); 131 132 EXPECT_EQ(2U, proxy_class->NumDirectInterfaces()); // Interfaces$I and Interfaces$J. 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)); 135 std::string temp; 136 const char* proxy_class_descriptor = proxy_class->GetDescriptor(&temp); 137 EXPECT_STREQ("L$Proxy1234;", proxy_class_descriptor); 138 EXPECT_EQ(nullptr, proxy_class->GetSourceFile()); 139} 140 141// Creates a proxy class and check FieldHelper works correctly. 142TEST_F(ProxyTest, ProxyFieldHelper) { 143 ScopedObjectAccess soa(Thread::Current()); 144 jobject jclass_loader = LoadDex("Interfaces"); 145 StackHandleScope<9> hs(soa.Self()); 146 Handle<mirror::ClassLoader> class_loader( 147 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader))); 148 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); 155 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); 165 ASSERT_TRUE(proxyClass->IsProxyClass()); 166 ASSERT_TRUE(proxyClass->IsInitialized()); 167 168 Handle<mirror::ObjectArray<mirror::ArtField>> instance_fields( 169 hs.NewHandle(proxyClass->GetIFields())); 170 EXPECT_TRUE(instance_fields.Get() == nullptr); 171 172 Handle<mirror::ObjectArray<mirror::ArtField>> static_fields( 173 hs.NewHandle(proxyClass->GetSFields())); 174 ASSERT_TRUE(static_fields.Get() != nullptr); 175 ASSERT_EQ(2, static_fields->GetLength()); 176 177 Handle<mirror::Class> interfacesFieldClass( 178 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;"))); 179 ASSERT_TRUE(interfacesFieldClass.Get() != nullptr); 180 Handle<mirror::Class> throwsFieldClass( 181 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;"))); 182 ASSERT_TRUE(throwsFieldClass.Get() != nullptr); 183 184 // Test "Class[] interfaces" field. 185 MutableHandle<mirror::ArtField> fhandle = hs.NewHandle(static_fields->Get(0)); 186 EXPECT_EQ("interfaces", std::string(fhandle->GetName())); 187 EXPECT_EQ("[Ljava/lang/Class;", std::string(fhandle->GetTypeDescriptor())); 188 EXPECT_EQ(interfacesFieldClass.Get(), fhandle->GetType<true>()); 189 std::string temp; 190 EXPECT_EQ("L$Proxy1234;", std::string(fhandle->GetDeclaringClass()->GetDescriptor(&temp))); 191 EXPECT_FALSE(fhandle->IsPrimitiveType()); 192 193 // Test "Class[][] throws" field. 194 fhandle.Assign(static_fields->Get(1)); 195 EXPECT_EQ("throws", std::string(fhandle->GetName())); 196 EXPECT_EQ("[[Ljava/lang/Class;", std::string(fhandle->GetTypeDescriptor())); 197 EXPECT_EQ(throwsFieldClass.Get(), fhandle->GetType<true>()); 198 EXPECT_EQ("L$Proxy1234;", std::string(fhandle->GetDeclaringClass()->GetDescriptor(&temp))); 199 EXPECT_FALSE(fhandle->IsPrimitiveType()); 200} 201 202} // namespace art 203