blob: 860d96c116cb418a12e2b5a9b6b7782a78c94c48 [file] [log] [blame]
David Brazdil1f9d3c32018-05-02 16:53:06 +01001/*
2 * Copyright (C) 2018 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#ifndef ART_RUNTIME_PROXY_TEST_H_
18#define ART_RUNTIME_PROXY_TEST_H_
19
20#include <jni.h>
21#include <vector>
22
23#include "art_method-inl.h"
24#include "class_linker-inl.h"
Vladimir Marko679730e2018-05-25 15:06:48 +010025#include "class_root.h"
David Brazdil1f9d3c32018-05-02 16:53:06 +010026#include "mirror/class-inl.h"
27#include "mirror/method.h"
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010028#include "obj_ptr-inl.h"
David Brazdil1f9d3c32018-05-02 16:53:06 +010029
30namespace art {
31namespace proxy_test {
32
33// Generate a proxy class with the given name and interfaces. This is a simplification from what
34// libcore does to fit to our test needs. We do not check for duplicated interfaces or methods and
35// we do not declare exceptions.
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010036ObjPtr<mirror::Class> GenerateProxyClass(ScopedObjectAccess& soa,
37 jobject jclass_loader,
38 ClassLinker* class_linker,
39 const char* className,
40 const std::vector<Handle<mirror::Class>>& interfaces)
David Brazdil1f9d3c32018-05-02 16:53:06 +010041 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoe9987b02018-05-22 16:26:43 +010042 StackHandleScope<1> hs(soa.Self());
43 Handle<mirror::Class> javaLangObject = hs.NewHandle(
44 class_linker->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
David Brazdil1f9d3c32018-05-02 16:53:06 +010045 CHECK(javaLangObject != nullptr);
46
47 jclass javaLangClass = soa.AddLocalReference<jclass>(mirror::Class::GetJavaLangClass());
48
49 // Builds the interfaces array.
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010050 jobjectArray proxyClassInterfaces =
51 soa.Env()->NewObjectArray(interfaces.size(), javaLangClass, /* initialElement */ nullptr);
David Brazdil1f9d3c32018-05-02 16:53:06 +010052 soa.Self()->AssertNoPendingException();
53 for (size_t i = 0; i < interfaces.size(); ++i) {
54 soa.Env()->SetObjectArrayElement(proxyClassInterfaces, i,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010055 soa.AddLocalReference<jclass>(interfaces[i].Get()));
David Brazdil1f9d3c32018-05-02 16:53:06 +010056 }
57
58 // Builds the method array.
59 jsize methods_count = 3; // Object.equals, Object.hashCode and Object.toString.
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010060 for (Handle<mirror::Class> interface : interfaces) {
David Brazdil1f9d3c32018-05-02 16:53:06 +010061 methods_count += interface->NumVirtualMethods();
62 }
63 jobjectArray proxyClassMethods = soa.Env()->NewObjectArray(
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010064 methods_count,
65 soa.AddLocalReference<jclass>(GetClassRoot<mirror::Method>()),
66 /* initialElement */ nullptr);
David Brazdil1f9d3c32018-05-02 16:53:06 +010067 soa.Self()->AssertNoPendingException();
68
69 jsize array_index = 0;
70 // Fill the method array
71 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
72 ArtMethod* method = javaLangObject->FindClassMethod(
73 "equals", "(Ljava/lang/Object;)Z", kRuntimePointerSize);
74 CHECK(method != nullptr);
75 CHECK(!method->IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +010076 CHECK(method->GetDeclaringClass() == javaLangObject.Get());
David Brazdil1f9d3c32018-05-02 16:53:06 +010077 DCHECK(!Runtime::Current()->IsActiveTransaction());
78 soa.Env()->SetObjectArrayElement(
79 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
80 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), method)));
81 method = javaLangObject->FindClassMethod("hashCode", "()I", kRuntimePointerSize);
82 CHECK(method != nullptr);
83 CHECK(!method->IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +010084 CHECK(method->GetDeclaringClass() == javaLangObject.Get());
David Brazdil1f9d3c32018-05-02 16:53:06 +010085 soa.Env()->SetObjectArrayElement(
86 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
87 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), method)));
88 method = javaLangObject->FindClassMethod(
89 "toString", "()Ljava/lang/String;", kRuntimePointerSize);
90 CHECK(method != nullptr);
91 CHECK(!method->IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +010092 CHECK(method->GetDeclaringClass() == javaLangObject.Get());
David Brazdil1f9d3c32018-05-02 16:53:06 +010093 soa.Env()->SetObjectArrayElement(
94 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
95 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), method)));
96 // Now adds all interfaces virtual methods.
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010097 for (Handle<mirror::Class> interface : interfaces) {
David Brazdil1f9d3c32018-05-02 16:53:06 +010098 for (auto& m : interface->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
99 soa.Env()->SetObjectArrayElement(
100 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
101 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), &m)));
102 }
103 }
104 CHECK_EQ(array_index, methods_count);
105
106 // Builds an empty exception array.
107 jobjectArray proxyClassThrows = soa.Env()->NewObjectArray(0, javaLangClass, nullptr);
108 soa.Self()->AssertNoPendingException();
109
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100110 ObjPtr<mirror::Class> proxyClass = class_linker->CreateProxyClass(
111 soa,
112 soa.Env()->NewStringUTF(className),
113 proxyClassInterfaces,
114 jclass_loader,
115 proxyClassMethods,
116 proxyClassThrows);
David Brazdil1f9d3c32018-05-02 16:53:06 +0100117 soa.Self()->AssertNoPendingException();
118 return proxyClass;
119}
120
121} // namespace proxy_test
122} // namespace art
123
124#endif // ART_RUNTIME_PROXY_TEST_H_