blob: 131e6c3632be7ac48819be2d212d970650145850 [file] [log] [blame]
Andreas Gampeab2f0d02017-01-05 17:23:45 -08001/* Copyright (C) 2016 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
32#include "ti_field.h"
33
34#include "art_jvmti.h"
35#include "art_field-inl.h"
36#include "base/enums.h"
Andreas Gampe188abab2017-02-16 10:34:05 -080037#include "dex_file_annotations.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080038#include "jni_internal.h"
Andreas Gampe188abab2017-02-16 10:34:05 -080039#include "mirror/object_array-inl.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080040#include "modifiers.h"
41#include "scoped_thread_state_change-inl.h"
42#include "thread-inl.h"
43
44namespace openjdkjvmti {
45
46// Note: For all these functions, we could do a check that the field actually belongs to the given
47// class. But the spec seems to assume a certain encoding of the field ID, and so doesn't
48// specify any errors.
49
50jvmtiError FieldUtil::GetFieldName(jvmtiEnv* env,
51 jclass klass,
52 jfieldID field,
53 char** name_ptr,
54 char** signature_ptr,
55 char** generic_ptr) {
56 if (klass == nullptr) {
57 return ERR(INVALID_CLASS);
58 }
59 if (field == nullptr) {
60 return ERR(INVALID_FIELDID);
61 }
62
63 art::ScopedObjectAccess soa(art::Thread::Current());
64 art::ArtField* art_field = art::jni::DecodeArtField(field);
65
66 JvmtiUniquePtr name_copy;
67 if (name_ptr != nullptr) {
68 const char* field_name = art_field->GetName();
69 if (field_name == nullptr) {
70 field_name = "<error>";
71 }
72 unsigned char* tmp;
73 jvmtiError ret = CopyString(env, field_name, &tmp);
74 if (ret != ERR(NONE)) {
75 return ret;
76 }
77 name_copy = MakeJvmtiUniquePtr(env, tmp);
78 *name_ptr = reinterpret_cast<char*>(tmp);
79 }
80
81 JvmtiUniquePtr signature_copy;
82 if (signature_ptr != nullptr) {
83 const char* sig = art_field->GetTypeDescriptor();
84 unsigned char* tmp;
85 jvmtiError ret = CopyString(env, sig, &tmp);
86 if (ret != ERR(NONE)) {
87 return ret;
88 }
89 signature_copy = MakeJvmtiUniquePtr(env, tmp);
90 *signature_ptr = reinterpret_cast<char*>(tmp);
91 }
92
93 // TODO: Support generic signature.
94 if (generic_ptr != nullptr) {
95 *generic_ptr = nullptr;
Andreas Gampe188abab2017-02-16 10:34:05 -080096 if (!art_field->GetDeclaringClass()->IsProxyClass()) {
97 art::mirror::ObjectArray<art::mirror::String>* str_array =
98 art::annotations::GetSignatureAnnotationForField(art_field);
99 if (str_array != nullptr) {
100 std::ostringstream oss;
101 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
102 oss << str_array->Get(i)->ToModifiedUtf8();
103 }
104 std::string output_string = oss.str();
105 unsigned char* tmp;
106 jvmtiError ret = CopyString(env, output_string.c_str(), &tmp);
107 if (ret != ERR(NONE)) {
108 return ret;
109 }
110 *generic_ptr = reinterpret_cast<char*>(tmp);
111 } else if (soa.Self()->IsExceptionPending()) {
112 // TODO: Should we report an error here?
113 soa.Self()->ClearException();
114 }
115 }
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800116 }
117
118 // Everything is fine, release the buffers.
119 name_copy.release();
120 signature_copy.release();
121
122 return ERR(NONE);
123}
124
125jvmtiError FieldUtil::GetFieldDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
126 jclass klass,
127 jfieldID field,
128 jclass* declaring_class_ptr) {
129 if (klass == nullptr) {
130 return ERR(INVALID_CLASS);
131 }
132 if (field == nullptr) {
133 return ERR(INVALID_FIELDID);
134 }
135 if (declaring_class_ptr == nullptr) {
136 return ERR(NULL_POINTER);
137 }
138
139 art::ScopedObjectAccess soa(art::Thread::Current());
140 art::ArtField* art_field = art::jni::DecodeArtField(field);
141 art::ObjPtr<art::mirror::Class> field_klass = art_field->GetDeclaringClass();
142
143 *declaring_class_ptr = soa.AddLocalReference<jclass>(field_klass);
144
145 return ERR(NONE);
146}
147
148jvmtiError FieldUtil::GetFieldModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
149 jclass klass,
150 jfieldID field,
151 jint* modifiers_ptr) {
152 if (klass == nullptr) {
153 return ERR(INVALID_CLASS);
154 }
155 if (field == nullptr) {
156 return ERR(INVALID_FIELDID);
157 }
158 if (modifiers_ptr == nullptr) {
159 return ERR(NULL_POINTER);
160 }
161
162 art::ScopedObjectAccess soa(art::Thread::Current());
163 art::ArtField* art_field = art::jni::DecodeArtField(field);
164 // Note: Keep this code in sync with Field.getModifiers.
165 uint32_t modifiers = art_field->GetAccessFlags() & 0xFFFF;
166
167 *modifiers_ptr = modifiers;
168 return ERR(NONE);
169}
170
171jvmtiError FieldUtil::IsFieldSynthetic(jvmtiEnv* env ATTRIBUTE_UNUSED,
172 jclass klass,
173 jfieldID field,
174 jboolean* is_synthetic_ptr) {
175 if (klass == nullptr) {
176 return ERR(INVALID_CLASS);
177 }
178 if (field == nullptr) {
179 return ERR(INVALID_FIELDID);
180 }
181 if (is_synthetic_ptr == nullptr) {
182 return ERR(NULL_POINTER);
183 }
184
185 art::ScopedObjectAccess soa(art::Thread::Current());
186 art::ArtField* art_field = art::jni::DecodeArtField(field);
187 uint32_t modifiers = art_field->GetAccessFlags();
188
189 *is_synthetic_ptr = ((modifiers & art::kAccSynthetic) != 0) ? JNI_TRUE : JNI_FALSE;
190 return ERR(NONE);
191}
192
193} // namespace openjdkjvmti