blob: b471293c09ce5284e6d6989eb365fd8b8ba46d33 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070020#include "art_field-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010021#include "class_linker-inl.h"
Jeff Haodb8a6642014-08-14 17:18:52 -070022#include "common_compiler_test.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070023#include "mirror/method.h"
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "scoped_thread_state_change.h"
25
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010026namespace art {
27
Jeff Haodb8a6642014-08-14 17:18:52 -070028class ProxyTest : public CommonCompilerTest {
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010029 public:
30 // Generate a proxy class with the given name and interfaces. This is a simplification from what
31 // libcore does to fit to our test needs. We do not check for duplicated interfaces or methods and
32 // we do not declare exceptions.
33 mirror::Class* GenerateProxyClass(ScopedObjectAccess& soa, jobject jclass_loader,
34 const char* className,
35 const std::vector<mirror::Class*>& interfaces)
36 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
37 mirror::Class* javaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
38 CHECK(javaLangObject != nullptr);
39
40 jclass javaLangClass = soa.AddLocalReference<jclass>(mirror::Class::GetJavaLangClass());
41
42 // Builds the interfaces array.
43 jobjectArray proxyClassInterfaces = soa.Env()->NewObjectArray(interfaces.size(), javaLangClass,
44 nullptr);
45 soa.Self()->AssertNoPendingException();
46 for (size_t i = 0; i < interfaces.size(); ++i) {
47 soa.Env()->SetObjectArrayElement(proxyClassInterfaces, i,
48 soa.AddLocalReference<jclass>(interfaces[i]));
49 }
50
51 // Builds the method array.
52 jsize methods_count = 3; // Object.equals, Object.hashCode and Object.toString.
53 for (mirror::Class* interface : interfaces) {
54 mirror::ObjectArray<mirror::ArtMethod>* virtual_methods = interface->GetVirtualMethods();
55 methods_count += (virtual_methods == nullptr) ? 0 : virtual_methods->GetLength();
56 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -070057 jobjectArray proxyClassMethods = soa.Env()->NewObjectArray(
58 methods_count, soa.AddLocalReference<jclass>(mirror::Method::StaticClass()), nullptr);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010059 soa.Self()->AssertNoPendingException();
60
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010061 jsize array_index = 0;
Mathieu Chartierfc58af42015-04-16 18:00:39 -070062 // Fill the method array
63 mirror::ArtMethod* method = javaLangObject->FindDeclaredVirtualMethod(
64 "equals", "(Ljava/lang/Object;)Z");
65 CHECK(method != nullptr);
66 soa.Env()->SetObjectArrayElement(
67 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
68 mirror::Method::CreateFromArtMethod(soa.Self(), method)));
69 method = javaLangObject->FindDeclaredVirtualMethod("hashCode", "()I");
70 CHECK(method != nullptr);
71 soa.Env()->SetObjectArrayElement(
72 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
73 mirror::Method::CreateFromArtMethod(soa.Self(), method)));
74 method = javaLangObject->FindDeclaredVirtualMethod("toString", "()Ljava/lang/String;");
75 CHECK(method != nullptr);
76 soa.Env()->SetObjectArrayElement(
77 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
78 mirror::Method::CreateFromArtMethod(soa.Self(), method)));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010079 // Now adds all interfaces virtual methods.
80 for (mirror::Class* interface : interfaces) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -070081 for (int32_t i = 0, count = interface->NumVirtualMethods(); i < count; ++i) {
82 soa.Env()->SetObjectArrayElement(
83 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
84 mirror::Method::CreateFromArtMethod(soa.Self(), interface->GetVirtualMethod(i))));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010085 }
86 }
87 CHECK_EQ(array_index, methods_count);
88
89 // Builds an empty exception array.
90 jobjectArray proxyClassThrows = soa.Env()->NewObjectArray(0, javaLangClass, nullptr);
91 soa.Self()->AssertNoPendingException();
92
Mathieu Chartierfc58af42015-04-16 18:00:39 -070093 mirror::Class* proxyClass = class_linker_->CreateProxyClass(
94 soa, soa.Env()->NewStringUTF(className), proxyClassInterfaces, jclass_loader,
95 proxyClassMethods, proxyClassThrows);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010096 soa.Self()->AssertNoPendingException();
97 return proxyClass;
98 }
99};
100
101// Creates a proxy class and check ClassHelper works correctly.
102TEST_F(ProxyTest, ProxyClassHelper) {
103 ScopedObjectAccess soa(Thread::Current());
104 jobject jclass_loader = LoadDex("Interfaces");
Mathieu Chartierf8322842014-05-16 10:59:25 -0700105 StackHandleScope<4> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700106 Handle<mirror::ClassLoader> class_loader(
107 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100108
Mathieu Chartierf8322842014-05-16 10:59:25 -0700109 Handle<mirror::Class> I(hs.NewHandle(
110 class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
111 Handle<mirror::Class> J(hs.NewHandle(
112 class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
113 ASSERT_TRUE(I.Get() != nullptr);
114 ASSERT_TRUE(J.Get() != nullptr);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100115
Mathieu Chartierf8322842014-05-16 10:59:25 -0700116 std::vector<mirror::Class*> interfaces;
117 interfaces.push_back(I.Get());
118 interfaces.push_back(J.Get());
119 Handle<mirror::Class> proxy_class(hs.NewHandle(
120 GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces)));
121 interfaces.clear(); // Don't least possibly stale objects in the array as good practice.
122 ASSERT_TRUE(proxy_class.Get() != nullptr);
123 ASSERT_TRUE(proxy_class->IsProxyClass());
124 ASSERT_TRUE(proxy_class->IsInitialized());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100125
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200126 EXPECT_EQ(2U, proxy_class->NumDirectInterfaces()); // Interfaces$I and Interfaces$J.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700127 EXPECT_EQ(I.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class, 0));
128 EXPECT_EQ(J.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class, 1));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700129 std::string temp;
130 const char* proxy_class_descriptor = proxy_class->GetDescriptor(&temp);
131 EXPECT_STREQ("L$Proxy1234;", proxy_class_descriptor);
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200132 EXPECT_EQ(nullptr, proxy_class->GetSourceFile());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100133}
134
Sebastien Hertz80989a62014-04-01 14:39:44 +0200135// Creates a proxy class and check FieldHelper works correctly.
136TEST_F(ProxyTest, ProxyFieldHelper) {
137 ScopedObjectAccess soa(Thread::Current());
138 jobject jclass_loader = LoadDex("Interfaces");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700139 StackHandleScope<9> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700140 Handle<mirror::ClassLoader> class_loader(
141 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Sebastien Hertz80989a62014-04-01 14:39:44 +0200142
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700143 Handle<mirror::Class> I(hs.NewHandle(
144 class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
145 Handle<mirror::Class> J(hs.NewHandle(
146 class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
147 ASSERT_TRUE(I.Get() != nullptr);
148 ASSERT_TRUE(J.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200149
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700150 Handle<mirror::Class> proxyClass;
151 {
152 std::vector<mirror::Class*> interfaces;
153 interfaces.push_back(I.Get());
154 interfaces.push_back(J.Get());
155 proxyClass = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces));
156 }
157
158 ASSERT_TRUE(proxyClass.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200159 ASSERT_TRUE(proxyClass->IsProxyClass());
160 ASSERT_TRUE(proxyClass->IsInitialized());
161
Mathieu Chartierc7853442015-03-27 14:35:38 -0700162 ArtField* instance_fields = proxyClass->GetIFields();
163 EXPECT_TRUE(instance_fields == nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200164
Mathieu Chartierc7853442015-03-27 14:35:38 -0700165 ArtField* static_fields = proxyClass->GetSFields();
166 ASSERT_TRUE(static_fields != nullptr);
167 ASSERT_EQ(2u, proxyClass->NumStaticFields());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200168
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700169 Handle<mirror::Class> interfacesFieldClass(
170 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;")));
171 ASSERT_TRUE(interfacesFieldClass.Get() != nullptr);
172 Handle<mirror::Class> throwsFieldClass(
173 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;")));
174 ASSERT_TRUE(throwsFieldClass.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200175
176 // Test "Class[] interfaces" field.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700177 ArtField* field = &static_fields[0];
178 EXPECT_STREQ("interfaces", field->GetName());
179 EXPECT_STREQ("[Ljava/lang/Class;", field->GetTypeDescriptor());
180 EXPECT_EQ(interfacesFieldClass.Get(), field->GetType<true>());
Ian Rogers08f1f502014-12-02 15:04:37 -0800181 std::string temp;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700182 EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
183 EXPECT_FALSE(field->IsPrimitiveType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200184
185 // Test "Class[][] throws" field.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700186 field = &static_fields[1];
187 EXPECT_STREQ("throws", field->GetName());
188 EXPECT_STREQ("[[Ljava/lang/Class;", field->GetTypeDescriptor());
189 EXPECT_EQ(throwsFieldClass.Get(), field->GetType<true>());
190 EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
191 EXPECT_FALSE(field->IsPrimitiveType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200192}
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100193
194} // namespace art