blob: 57eb3f79b61fdc74c76963538375563b1ec2670c [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
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010017#include <jni.h>
18#include <vector>
19
Ian Rogerse63db272014-07-15 15:36:11 -070020#include "common_compiler_test.h"
21#include "mirror/art_field-inl.h"
22#include "scoped_thread_state_change.h"
23
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010024namespace 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");
Mathieu Chartierf8322842014-05-16 10:59:25 -0700111 StackHandleScope<4> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700112 Handle<mirror::ClassLoader> class_loader(
113 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100114
Mathieu Chartierf8322842014-05-16 10:59:25 -0700115 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);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100121
Mathieu Chartierf8322842014-05-16 10:59:25 -0700122 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());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100131
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200132 EXPECT_EQ(2U, proxy_class->NumDirectInterfaces()); // Interfaces$I and Interfaces$J.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700133 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 proxy_class_descriptor(proxy_class->GetDescriptor());
136 EXPECT_STREQ("L$Proxy1234;", proxy_class_descriptor.c_str());
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200137 EXPECT_EQ(nullptr, proxy_class->GetSourceFile());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100138}
139
Sebastien Hertz80989a62014-04-01 14:39:44 +0200140// Creates a proxy class and check FieldHelper works correctly.
141TEST_F(ProxyTest, ProxyFieldHelper) {
142 ScopedObjectAccess soa(Thread::Current());
143 jobject jclass_loader = LoadDex("Interfaces");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700144 StackHandleScope<9> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700145 Handle<mirror::ClassLoader> class_loader(
146 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Sebastien Hertz80989a62014-04-01 14:39:44 +0200147
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700148 Handle<mirror::Class> I(hs.NewHandle(
149 class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
150 Handle<mirror::Class> J(hs.NewHandle(
151 class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
152 ASSERT_TRUE(I.Get() != nullptr);
153 ASSERT_TRUE(J.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200154
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700155 Handle<mirror::Class> proxyClass;
156 {
157 std::vector<mirror::Class*> interfaces;
158 interfaces.push_back(I.Get());
159 interfaces.push_back(J.Get());
160 proxyClass = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces));
161 }
162
163 ASSERT_TRUE(proxyClass.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200164 ASSERT_TRUE(proxyClass->IsProxyClass());
165 ASSERT_TRUE(proxyClass->IsInitialized());
166
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700167 Handle<mirror::ObjectArray<mirror::ArtField>> instance_fields(
168 hs.NewHandle(proxyClass->GetIFields()));
169 EXPECT_TRUE(instance_fields.Get() == nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200170
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700171 Handle<mirror::ObjectArray<mirror::ArtField>> static_fields(
172 hs.NewHandle(proxyClass->GetSFields()));
173 ASSERT_TRUE(static_fields.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200174 ASSERT_EQ(2, static_fields->GetLength());
175
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700176 Handle<mirror::Class> interfacesFieldClass(
177 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;")));
178 ASSERT_TRUE(interfacesFieldClass.Get() != nullptr);
179 Handle<mirror::Class> throwsFieldClass(
180 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;")));
181 ASSERT_TRUE(throwsFieldClass.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200182
183 // Test "Class[] interfaces" field.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700184 FieldHelper fh(hs.NewHandle(static_fields->Get(0)));
185 EXPECT_EQ("interfaces", std::string(fh.GetField()->GetName()));
186 EXPECT_EQ("[Ljava/lang/Class;", std::string(fh.GetField()->GetTypeDescriptor()));
187 EXPECT_EQ(interfacesFieldClass.Get(), fh.GetType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200188 EXPECT_EQ("L$Proxy1234;", std::string(fh.GetDeclaringClassDescriptor()));
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700189 EXPECT_FALSE(fh.GetField()->IsPrimitiveType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200190
191 // Test "Class[][] throws" field.
192 fh.ChangeField(static_fields->Get(1));
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700193 EXPECT_EQ("throws", std::string(fh.GetField()->GetName()));
194 EXPECT_EQ("[[Ljava/lang/Class;", std::string(fh.GetField()->GetTypeDescriptor()));
195 EXPECT_EQ(throwsFieldClass.Get(), fh.GetType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200196 EXPECT_EQ("L$Proxy1234;", std::string(fh.GetDeclaringClassDescriptor()));
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700197 EXPECT_FALSE(fh.GetField()->IsPrimitiveType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200198}
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100199
200} // namespace art