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"); |
| 110 | SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(jclass_loader)); |
| 111 | |
| 112 | mirror::Class* I = class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader); |
| 113 | mirror::Class* J = class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader); |
| 114 | ASSERT_TRUE(I != nullptr); |
| 115 | ASSERT_TRUE(J != nullptr); |
| 116 | std::vector<mirror::Class*> interfaces; |
| 117 | interfaces.push_back(I); |
| 118 | interfaces.push_back(J); |
| 119 | |
| 120 | mirror::Class* proxyClass = GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces); |
| 121 | ASSERT_TRUE(proxyClass != nullptr); |
| 122 | ASSERT_TRUE(proxyClass->IsProxyClass()); |
| 123 | |
| 124 | mirror::Class* javaIoSerializable = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;"); |
| 125 | ASSERT_TRUE(javaIoSerializable != nullptr); |
| 126 | |
| 127 | // Check ClassHelper for proxy. |
| 128 | ClassHelper kh(proxyClass); |
| 129 | EXPECT_EQ(kh.NumDirectInterfaces(), 3U); // java.io.Serializable, Interfaces$I and Interfaces$J. |
| 130 | EXPECT_EQ(javaIoSerializable, kh.GetDirectInterface(0)); |
| 131 | EXPECT_EQ(I, kh.GetDirectInterface(1)); |
| 132 | EXPECT_EQ(J, kh.GetDirectInterface(2)); |
| 133 | std::string proxyClassDescriptor(kh.GetDescriptor()); |
| 134 | EXPECT_EQ("L$Proxy1234;", proxyClassDescriptor); |
| 135 | // EXPECT_EQ(nullptr, kh.GetSourceFile()); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | } // namespace art |