blob: 8517e349692dc7a22685bf2826b703084add372e [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 Chartiereb8167a2014-05-07 15:43:14 -0700110 StackHandleScope<1> hs(soa.Self());
111 Handle<mirror::ClassLoader> class_loader(
112 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100113
114 mirror::Class* I = class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader);
115 mirror::Class* J = class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader);
116 ASSERT_TRUE(I != nullptr);
117 ASSERT_TRUE(J != nullptr);
118 std::vector<mirror::Class*> interfaces;
119 interfaces.push_back(I);
120 interfaces.push_back(J);
121
122 mirror::Class* proxyClass = GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces);
123 ASSERT_TRUE(proxyClass != nullptr);
124 ASSERT_TRUE(proxyClass->IsProxyClass());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200125 ASSERT_TRUE(proxyClass->IsInitialized());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100126
127 // Check ClassHelper for proxy.
128 ClassHelper kh(proxyClass);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200129 EXPECT_EQ(kh.NumDirectInterfaces(), 2U); // Interfaces$I and Interfaces$J.
130 EXPECT_EQ(I, kh.GetDirectInterface(0));
131 EXPECT_EQ(J, kh.GetDirectInterface(1));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100132 std::string proxyClassDescriptor(kh.GetDescriptor());
133 EXPECT_EQ("L$Proxy1234;", proxyClassDescriptor);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100134}
135
Sebastien Hertz80989a62014-04-01 14:39:44 +0200136// Creates a proxy class and check FieldHelper works correctly.
137TEST_F(ProxyTest, ProxyFieldHelper) {
138 ScopedObjectAccess soa(Thread::Current());
139 jobject jclass_loader = LoadDex("Interfaces");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700140 StackHandleScope<1> hs(soa.Self());
141 Handle<mirror::ClassLoader> class_loader(
142 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Sebastien Hertz80989a62014-04-01 14:39:44 +0200143
144 mirror::Class* I = class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader);
145 mirror::Class* J = class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader);
146 ASSERT_TRUE(I != nullptr);
147 ASSERT_TRUE(J != nullptr);
148 std::vector<mirror::Class*> interfaces;
149 interfaces.push_back(I);
150 interfaces.push_back(J);
151
152 mirror::Class* proxyClass = GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces);
153 ASSERT_TRUE(proxyClass != nullptr);
154 ASSERT_TRUE(proxyClass->IsProxyClass());
155 ASSERT_TRUE(proxyClass->IsInitialized());
156
157 mirror::ObjectArray<mirror::ArtField>* instance_fields = proxyClass->GetIFields();
158 EXPECT_TRUE(instance_fields == nullptr);
159
160 mirror::ObjectArray<mirror::ArtField>* static_fields = proxyClass->GetSFields();
161 ASSERT_TRUE(static_fields != nullptr);
162 ASSERT_EQ(2, static_fields->GetLength());
163
164 mirror::Class* interfacesFieldClass = class_linker_->FindSystemClass(soa.Self(),
165 "[Ljava/lang/Class;");
166 ASSERT_TRUE(interfacesFieldClass != nullptr);
167 mirror::Class* throwsFieldClass = class_linker_->FindSystemClass(soa.Self(),
168 "[[Ljava/lang/Class;");
169 ASSERT_TRUE(throwsFieldClass != nullptr);
170
171 // Test "Class[] interfaces" field.
172 FieldHelper fh(static_fields->Get(0));
173 EXPECT_EQ("interfaces", std::string(fh.GetName()));
174 EXPECT_EQ("[Ljava/lang/Class;", std::string(fh.GetTypeDescriptor()));
175 EXPECT_EQ(interfacesFieldClass, fh.GetType());
176 EXPECT_EQ("L$Proxy1234;", std::string(fh.GetDeclaringClassDescriptor()));
177 EXPECT_FALSE(fh.IsPrimitiveType());
178
179 // Test "Class[][] throws" field.
180 fh.ChangeField(static_fields->Get(1));
181 EXPECT_EQ("throws", std::string(fh.GetName()));
182 EXPECT_EQ("[[Ljava/lang/Class;", std::string(fh.GetTypeDescriptor()));
183 EXPECT_EQ(throwsFieldClass, fh.GetType());
184 EXPECT_EQ("L$Proxy1234;", std::string(fh.GetDeclaringClassDescriptor()));
185 EXPECT_FALSE(fh.IsPrimitiveType());
186}
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100187
188} // namespace art