blob: 093c129add9d83c3e3e76e5ecb7981c29e888cfa [file] [log] [blame]
Sebastien Hertzb7054ba2014-03-13 11:52:31 +01001/*
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");
Mathieu Chartierf8322842014-05-16 10:59:25 -0700110 StackHandleScope<4> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700111 Handle<mirror::ClassLoader> class_loader(
112 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100113
Mathieu Chartierf8322842014-05-16 10:59:25 -0700114 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);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100120
Mathieu Chartierf8322842014-05-16 10:59:25 -0700121 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());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100130
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200131 EXPECT_EQ(2U, proxy_class->NumDirectInterfaces()); // Interfaces$I and Interfaces$J.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700132 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());
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200136 EXPECT_EQ(nullptr, proxy_class->GetSourceFile());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100137}
138
Sebastien Hertz80989a62014-04-01 14:39:44 +0200139// Creates a proxy class and check FieldHelper works correctly.
140TEST_F(ProxyTest, ProxyFieldHelper) {
141 ScopedObjectAccess soa(Thread::Current());
142 jobject jclass_loader = LoadDex("Interfaces");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700143 StackHandleScope<9> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700144 Handle<mirror::ClassLoader> class_loader(
145 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Sebastien Hertz80989a62014-04-01 14:39:44 +0200146
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700147 Handle<mirror::Class> I(hs.NewHandle(
148 class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
149 Handle<mirror::Class> J(hs.NewHandle(
150 class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
151 ASSERT_TRUE(I.Get() != nullptr);
152 ASSERT_TRUE(J.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200153
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700154 Handle<mirror::Class> proxyClass;
155 {
156 std::vector<mirror::Class*> interfaces;
157 interfaces.push_back(I.Get());
158 interfaces.push_back(J.Get());
159 proxyClass = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces));
160 }
161
162 ASSERT_TRUE(proxyClass.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200163 ASSERT_TRUE(proxyClass->IsProxyClass());
164 ASSERT_TRUE(proxyClass->IsInitialized());
165
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700166 Handle<mirror::ObjectArray<mirror::ArtField>> instance_fields(
167 hs.NewHandle(proxyClass->GetIFields()));
168 EXPECT_TRUE(instance_fields.Get() == nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200169
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700170 Handle<mirror::ObjectArray<mirror::ArtField>> static_fields(
171 hs.NewHandle(proxyClass->GetSFields()));
172 ASSERT_TRUE(static_fields.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200173 ASSERT_EQ(2, static_fields->GetLength());
174
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700175 Handle<mirror::Class> interfacesFieldClass(
176 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;")));
177 ASSERT_TRUE(interfacesFieldClass.Get() != nullptr);
178 Handle<mirror::Class> throwsFieldClass(
179 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;")));
180 ASSERT_TRUE(throwsFieldClass.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200181
182 // Test "Class[] interfaces" field.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700183 FieldHelper fh(hs.NewHandle(static_fields->Get(0)));
184 EXPECT_EQ("interfaces", std::string(fh.GetField()->GetName()));
185 EXPECT_EQ("[Ljava/lang/Class;", std::string(fh.GetField()->GetTypeDescriptor()));
186 EXPECT_EQ(interfacesFieldClass.Get(), fh.GetType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200187 EXPECT_EQ("L$Proxy1234;", std::string(fh.GetDeclaringClassDescriptor()));
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700188 EXPECT_FALSE(fh.GetField()->IsPrimitiveType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200189
190 // Test "Class[][] throws" field.
191 fh.ChangeField(static_fields->Get(1));
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700192 EXPECT_EQ("throws", std::string(fh.GetField()->GetName()));
193 EXPECT_EQ("[[Ljava/lang/Class;", std::string(fh.GetField()->GetTypeDescriptor()));
194 EXPECT_EQ(throwsFieldClass.Get(), fh.GetType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200195 EXPECT_EQ("L$Proxy1234;", std::string(fh.GetDeclaringClassDescriptor()));
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700196 EXPECT_FALSE(fh.GetField()->IsPrimitiveType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200197}
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100198
199} // namespace art