blob: 5763479fe32b18842a809559ede986e050f66d21 [file] [log] [blame]
David Sehr9323e6e2016-09-13 08:58:35 -07001/*
2 * Copyright (C) 2016 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 "dex_file_annotations.h"
18
19#include <stdlib.h>
20
21#include "art_field-inl.h"
22#include "art_method-inl.h"
23#include "class_linker-inl.h"
24#include "dex_file-inl.h"
25#include "jvalue.h"
26#include "mirror/field.h"
27#include "mirror/method.h"
28#include "reflection.h"
29#include "thread.h"
30
31namespace art {
32
33struct DexFile::AnnotationValue {
34 JValue value_;
35 uint8_t type_;
36};
37
38namespace {
39mirror::Object* CreateAnnotationMember(Handle<mirror::Class> klass,
40 Handle<mirror::Class> annotation_class,
41 const uint8_t** annotation)
42 REQUIRES_SHARED(Locks::mutator_lock_);
43
44bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) {
45 if (expected == DexFile::kDexVisibilityRuntime) {
46 int32_t sdk_version = Runtime::Current()->GetTargetSdkVersion();
47 if (sdk_version > 0 && sdk_version <= 23) {
48 return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild;
49 }
50 }
51 return actual == expected;
52}
53
54const DexFile::AnnotationSetItem* FindAnnotationSetForField(ArtField* field)
55 REQUIRES_SHARED(Locks::mutator_lock_) {
56 const DexFile* dex_file = field->GetDexFile();
57 mirror::Class* klass = field->GetDeclaringClass();
58 const DexFile::AnnotationsDirectoryItem* annotations_dir =
59 dex_file->GetAnnotationsDirectory(*klass->GetClassDef());
60 if (annotations_dir == nullptr) {
61 return nullptr;
62 }
63 const DexFile::FieldAnnotationsItem* field_annotations =
64 dex_file->GetFieldAnnotations(annotations_dir);
65 if (field_annotations == nullptr) {
66 return nullptr;
67 }
68 uint32_t field_index = field->GetDexFieldIndex();
69 uint32_t field_count = annotations_dir->fields_size_;
70 for (uint32_t i = 0; i < field_count; ++i) {
71 if (field_annotations[i].field_idx_ == field_index) {
72 return dex_file->GetFieldAnnotationSetItem(field_annotations[i]);
73 }
74 }
75 return nullptr;
76}
77
78const DexFile::AnnotationItem* SearchAnnotationSet(const DexFile& dex_file,
79 const DexFile::AnnotationSetItem* annotation_set,
80 const char* descriptor,
81 uint32_t visibility)
82 REQUIRES_SHARED(Locks::mutator_lock_) {
83 const DexFile::AnnotationItem* result = nullptr;
84 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
85 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
86 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
87 continue;
88 }
89 const uint8_t* annotation = annotation_item->annotation_;
90 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
91
92 if (strcmp(descriptor, dex_file.StringByTypeIdx(type_index)) == 0) {
93 result = annotation_item;
94 break;
95 }
96 }
97 return result;
98}
99
100bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr)
101 REQUIRES_SHARED(Locks::mutator_lock_) {
102 const uint8_t* annotation = *annotation_ptr;
103 uint8_t header_byte = *(annotation++);
104 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
105 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
106 int32_t width = value_arg + 1;
107
108 switch (value_type) {
109 case DexFile::kDexAnnotationByte:
110 case DexFile::kDexAnnotationShort:
111 case DexFile::kDexAnnotationChar:
112 case DexFile::kDexAnnotationInt:
113 case DexFile::kDexAnnotationLong:
114 case DexFile::kDexAnnotationFloat:
115 case DexFile::kDexAnnotationDouble:
116 case DexFile::kDexAnnotationString:
117 case DexFile::kDexAnnotationType:
118 case DexFile::kDexAnnotationMethod:
119 case DexFile::kDexAnnotationField:
120 case DexFile::kDexAnnotationEnum:
121 break;
122 case DexFile::kDexAnnotationArray:
123 {
124 uint32_t size = DecodeUnsignedLeb128(&annotation);
125 while (size--) {
126 if (!SkipAnnotationValue(dex_file, &annotation)) {
127 return false;
128 }
129 }
130 width = 0;
131 break;
132 }
133 case DexFile::kDexAnnotationAnnotation:
134 {
135 DecodeUnsignedLeb128(&annotation); // unused type_index
136 uint32_t size = DecodeUnsignedLeb128(&annotation);
137 while (size--) {
138 DecodeUnsignedLeb128(&annotation); // unused element_name_index
139 if (!SkipAnnotationValue(dex_file, &annotation)) {
140 return false;
141 }
142 }
143 width = 0;
144 break;
145 }
146 case DexFile::kDexAnnotationBoolean:
147 case DexFile::kDexAnnotationNull:
148 width = 0;
149 break;
150 default:
151 LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type);
152 return false;
153 }
154
155 annotation += width;
156 *annotation_ptr = annotation;
157 return true;
158}
159
160const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file,
161 const uint8_t* annotation,
162 const char* name)
163 REQUIRES_SHARED(Locks::mutator_lock_) {
164 DecodeUnsignedLeb128(&annotation); // unused type_index
165 uint32_t size = DecodeUnsignedLeb128(&annotation);
166
167 while (size != 0) {
168 uint32_t element_name_index = DecodeUnsignedLeb128(&annotation);
169 const char* element_name = dex_file.GetStringData(dex_file.GetStringId(element_name_index));
170 if (strcmp(name, element_name) == 0) {
171 return annotation;
172 }
173 SkipAnnotationValue(dex_file, &annotation);
174 size--;
175 }
176 return nullptr;
177}
178
179const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method)
180 REQUIRES_SHARED(Locks::mutator_lock_) {
181 const DexFile* dex_file = method->GetDexFile();
182 mirror::Class* klass = method->GetDeclaringClass();
183 const DexFile::AnnotationsDirectoryItem* annotations_dir =
184 dex_file->GetAnnotationsDirectory(*klass->GetClassDef());
185 if (annotations_dir == nullptr) {
186 return nullptr;
187 }
188 const DexFile::MethodAnnotationsItem* method_annotations =
189 dex_file->GetMethodAnnotations(annotations_dir);
190 if (method_annotations == nullptr) {
191 return nullptr;
192 }
193 uint32_t method_index = method->GetDexMethodIndex();
194 uint32_t method_count = annotations_dir->methods_size_;
195 for (uint32_t i = 0; i < method_count; ++i) {
196 if (method_annotations[i].method_idx_ == method_index) {
197 return dex_file->GetMethodAnnotationSetItem(method_annotations[i]);
198 }
199 }
200 return nullptr;
201}
202
203const DexFile::ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method)
204 REQUIRES_SHARED(Locks::mutator_lock_) {
205 const DexFile* dex_file = method->GetDexFile();
206 mirror::Class* klass = method->GetDeclaringClass();
207 const DexFile::AnnotationsDirectoryItem* annotations_dir =
208 dex_file->GetAnnotationsDirectory(*klass->GetClassDef());
209 if (annotations_dir == nullptr) {
210 return nullptr;
211 }
212 const DexFile::ParameterAnnotationsItem* parameter_annotations =
213 dex_file->GetParameterAnnotations(annotations_dir);
214 if (parameter_annotations == nullptr) {
215 return nullptr;
216 }
217 uint32_t method_index = method->GetDexMethodIndex();
218 uint32_t parameter_count = annotations_dir->parameters_size_;
219 for (uint32_t i = 0; i < parameter_count; ++i) {
220 if (parameter_annotations[i].method_idx_ == method_index) {
221 return &parameter_annotations[i];
222 }
223 }
224 return nullptr;
225}
226
227const DexFile::AnnotationSetItem* FindAnnotationSetForClass(Handle<mirror::Class> klass)
228 REQUIRES_SHARED(Locks::mutator_lock_) {
229 const DexFile& dex_file = klass->GetDexFile();
230 const DexFile::AnnotationsDirectoryItem* annotations_dir =
231 dex_file.GetAnnotationsDirectory(*klass->GetClassDef());
232 if (annotations_dir == nullptr) {
233 return nullptr;
234 }
235 return dex_file.GetClassAnnotationSet(annotations_dir);
236}
237
238mirror::Object* ProcessEncodedAnnotation(Handle<mirror::Class> klass, const uint8_t** annotation)
239 REQUIRES_SHARED(Locks::mutator_lock_) {
240 uint32_t type_index = DecodeUnsignedLeb128(annotation);
241 uint32_t size = DecodeUnsignedLeb128(annotation);
242
243 Thread* self = Thread::Current();
244 ScopedObjectAccessUnchecked soa(self);
245 StackHandleScope<2> hs(self);
246 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
247 Handle<mirror::Class> annotation_class(hs.NewHandle(
248 class_linker->ResolveType(klass->GetDexFile(), type_index, klass.Get())));
249 if (annotation_class.Get() == nullptr) {
250 LOG(INFO) << "Unable to resolve " << PrettyClass(klass.Get()) << " annotation class "
251 << type_index;
252 DCHECK(Thread::Current()->IsExceptionPending());
253 Thread::Current()->ClearException();
254 return nullptr;
255 }
256
257 mirror::Class* annotation_member_class =
Mathieu Chartier0795f232016-09-27 18:43:30 -0700258 soa.Decode<mirror::Class>(WellKnownClasses::libcore_reflect_AnnotationMember).Decode();
David Sehr9323e6e2016-09-13 08:58:35 -0700259 mirror::Class* annotation_member_array_class =
260 class_linker->FindArrayClass(self, &annotation_member_class);
261 if (annotation_member_array_class == nullptr) {
262 return nullptr;
263 }
264 mirror::ObjectArray<mirror::Object>* element_array = nullptr;
265 if (size > 0) {
266 element_array =
267 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size);
268 if (element_array == nullptr) {
269 LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)";
270 return nullptr;
271 }
272 }
273
274 Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array));
275 for (uint32_t i = 0; i < size; ++i) {
276 mirror::Object* new_member = CreateAnnotationMember(klass, annotation_class, annotation);
277 if (new_member == nullptr) {
278 return nullptr;
279 }
280 h_element_array->SetWithoutChecks<false>(i, new_member);
281 }
282
283 JValue result;
284 ArtMethod* create_annotation_method =
285 soa.DecodeMethod(WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation);
286 uint32_t args[2] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(annotation_class.Get())),
287 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_element_array.Get())) };
288 create_annotation_method->Invoke(self, args, sizeof(args), &result, "LLL");
289 if (self->IsExceptionPending()) {
290 LOG(INFO) << "Exception in AnnotationFactory.createAnnotation";
291 return nullptr;
292 }
293
294 return result.GetL();
295}
296
297bool ProcessAnnotationValue(Handle<mirror::Class> klass,
298 const uint8_t** annotation_ptr,
299 DexFile::AnnotationValue* annotation_value,
300 Handle<mirror::Class> array_class,
301 DexFile::AnnotationResultStyle result_style)
302 REQUIRES_SHARED(Locks::mutator_lock_) {
303 const DexFile& dex_file = klass->GetDexFile();
304 Thread* self = Thread::Current();
305 mirror::Object* element_object = nullptr;
306 bool set_object = false;
307 Primitive::Type primitive_type = Primitive::kPrimVoid;
308 const uint8_t* annotation = *annotation_ptr;
309 uint8_t header_byte = *(annotation++);
310 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
311 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
312 int32_t width = value_arg + 1;
313 annotation_value->type_ = value_type;
314
315 switch (value_type) {
316 case DexFile::kDexAnnotationByte:
317 annotation_value->value_.SetB(
318 static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg)));
319 primitive_type = Primitive::kPrimByte;
320 break;
321 case DexFile::kDexAnnotationShort:
322 annotation_value->value_.SetS(
323 static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg)));
324 primitive_type = Primitive::kPrimShort;
325 break;
326 case DexFile::kDexAnnotationChar:
327 annotation_value->value_.SetC(
328 static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false)));
329 primitive_type = Primitive::kPrimChar;
330 break;
331 case DexFile::kDexAnnotationInt:
332 annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg));
333 primitive_type = Primitive::kPrimInt;
334 break;
335 case DexFile::kDexAnnotationLong:
336 annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg));
337 primitive_type = Primitive::kPrimLong;
338 break;
339 case DexFile::kDexAnnotationFloat:
340 annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true));
341 primitive_type = Primitive::kPrimFloat;
342 break;
343 case DexFile::kDexAnnotationDouble:
344 annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true));
345 primitive_type = Primitive::kPrimDouble;
346 break;
347 case DexFile::kDexAnnotationBoolean:
348 annotation_value->value_.SetZ(value_arg != 0);
349 primitive_type = Primitive::kPrimBoolean;
350 width = 0;
351 break;
352 case DexFile::kDexAnnotationString: {
353 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
354 if (result_style == DexFile::kAllRaw) {
355 annotation_value->value_.SetI(index);
356 } else {
357 StackHandleScope<1> hs(self);
358 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
359 element_object = Runtime::Current()->GetClassLinker()->ResolveString(
360 klass->GetDexFile(), index, dex_cache);
361 set_object = true;
362 if (element_object == nullptr) {
363 return false;
364 }
365 }
366 break;
367 }
368 case DexFile::kDexAnnotationType: {
369 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
370 if (result_style == DexFile::kAllRaw) {
371 annotation_value->value_.SetI(index);
372 } else {
373 element_object = Runtime::Current()->GetClassLinker()->ResolveType(
374 klass->GetDexFile(), index, klass.Get());
375 set_object = true;
376 if (element_object == nullptr) {
377 CHECK(self->IsExceptionPending());
378 if (result_style == DexFile::kAllObjects) {
379 const char* msg = dex_file.StringByTypeIdx(index);
380 self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg);
381 element_object = self->GetException();
382 self->ClearException();
383 } else {
384 return false;
385 }
386 }
387 }
388 break;
389 }
390 case DexFile::kDexAnnotationMethod: {
391 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
392 if (result_style == DexFile::kAllRaw) {
393 annotation_value->value_.SetI(index);
394 } else {
395 StackHandleScope<2> hs(self);
396 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
397 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
398 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
399 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
400 klass->GetDexFile(), index, dex_cache, class_loader);
401 if (method == nullptr) {
402 return false;
403 }
404 PointerSize pointer_size = class_linker->GetImagePointerSize();
405 set_object = true;
406 DCHECK(!Runtime::Current()->IsActiveTransaction());
407 if (method->IsConstructor()) {
408 if (pointer_size == PointerSize::k64) {
409 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k64,
410 false>(self, method);
411 } else {
412 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k32,
413 false>(self, method);
414 }
415 } else {
416 if (pointer_size == PointerSize::k64) {
417 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k64,
418 false>(self, method);
419 } else {
420 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k32,
421 false>(self, method);
422 }
423 }
424 if (element_object == nullptr) {
425 return false;
426 }
427 }
428 break;
429 }
430 case DexFile::kDexAnnotationField: {
431 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
432 if (result_style == DexFile::kAllRaw) {
433 annotation_value->value_.SetI(index);
434 } else {
435 StackHandleScope<2> hs(self);
436 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
437 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
438 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(
439 klass->GetDexFile(), index, dex_cache, class_loader);
440 if (field == nullptr) {
441 return false;
442 }
443 set_object = true;
444 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
445 if (pointer_size == PointerSize::k64) {
446 element_object = mirror::Field::CreateFromArtField<PointerSize::k64>(self, field, true);
447 } else {
448 element_object = mirror::Field::CreateFromArtField<PointerSize::k32>(self, field, true);
449 }
450 if (element_object == nullptr) {
451 return false;
452 }
453 }
454 break;
455 }
456 case DexFile::kDexAnnotationEnum: {
457 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
458 if (result_style == DexFile::kAllRaw) {
459 annotation_value->value_.SetI(index);
460 } else {
461 StackHandleScope<3> hs(self);
462 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
463 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
464 ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField(
465 klass->GetDexFile(), index, dex_cache, class_loader, true);
466 if (enum_field == nullptr) {
467 return false;
468 } else {
469 Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass()));
470 Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true);
471 element_object = enum_field->GetObject(field_class.Get());
472 set_object = true;
473 }
474 }
475 break;
476 }
477 case DexFile::kDexAnnotationArray:
478 if (result_style == DexFile::kAllRaw || array_class.Get() == nullptr) {
479 return false;
480 } else {
481 ScopedObjectAccessUnchecked soa(self);
482 StackHandleScope<2> hs(self);
483 uint32_t size = DecodeUnsignedLeb128(&annotation);
484 Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType()));
485 Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc<true>(
486 self, array_class.Get(), size, array_class->GetComponentSizeShift(),
487 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
488 if (new_array.Get() == nullptr) {
489 LOG(ERROR) << "Annotation element array allocation failed with size " << size;
490 return false;
491 }
492 DexFile::AnnotationValue new_annotation_value;
493 for (uint32_t i = 0; i < size; ++i) {
494 if (!ProcessAnnotationValue(klass, &annotation, &new_annotation_value,
495 component_type, DexFile::kPrimitivesOrObjects)) {
496 return false;
497 }
498 if (!component_type->IsPrimitive()) {
499 mirror::Object* obj = new_annotation_value.value_.GetL();
500 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<false>(i, obj);
501 } else {
502 switch (new_annotation_value.type_) {
503 case DexFile::kDexAnnotationByte:
504 new_array->AsByteArray()->SetWithoutChecks<false>(
505 i, new_annotation_value.value_.GetB());
506 break;
507 case DexFile::kDexAnnotationShort:
508 new_array->AsShortArray()->SetWithoutChecks<false>(
509 i, new_annotation_value.value_.GetS());
510 break;
511 case DexFile::kDexAnnotationChar:
512 new_array->AsCharArray()->SetWithoutChecks<false>(
513 i, new_annotation_value.value_.GetC());
514 break;
515 case DexFile::kDexAnnotationInt:
516 new_array->AsIntArray()->SetWithoutChecks<false>(
517 i, new_annotation_value.value_.GetI());
518 break;
519 case DexFile::kDexAnnotationLong:
520 new_array->AsLongArray()->SetWithoutChecks<false>(
521 i, new_annotation_value.value_.GetJ());
522 break;
523 case DexFile::kDexAnnotationFloat:
524 new_array->AsFloatArray()->SetWithoutChecks<false>(
525 i, new_annotation_value.value_.GetF());
526 break;
527 case DexFile::kDexAnnotationDouble:
528 new_array->AsDoubleArray()->SetWithoutChecks<false>(
529 i, new_annotation_value.value_.GetD());
530 break;
531 case DexFile::kDexAnnotationBoolean:
532 new_array->AsBooleanArray()->SetWithoutChecks<false>(
533 i, new_annotation_value.value_.GetZ());
534 break;
535 default:
536 LOG(FATAL) << "Found invalid annotation value type while building annotation array";
537 return false;
538 }
539 }
540 }
541 element_object = new_array.Get();
542 set_object = true;
543 width = 0;
544 }
545 break;
546 case DexFile::kDexAnnotationAnnotation:
547 if (result_style == DexFile::kAllRaw) {
548 return false;
549 }
550 element_object = ProcessEncodedAnnotation(klass, &annotation);
551 if (element_object == nullptr) {
552 return false;
553 }
554 set_object = true;
555 width = 0;
556 break;
557 case DexFile::kDexAnnotationNull:
558 if (result_style == DexFile::kAllRaw) {
559 annotation_value->value_.SetI(0);
560 } else {
561 CHECK(element_object == nullptr);
562 set_object = true;
563 }
564 width = 0;
565 break;
566 default:
567 LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type);
568 return false;
569 }
570
571 annotation += width;
572 *annotation_ptr = annotation;
573
574 if (result_style == DexFile::kAllObjects && primitive_type != Primitive::kPrimVoid) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700575 element_object = BoxPrimitive(primitive_type, annotation_value->value_).Decode();
David Sehr9323e6e2016-09-13 08:58:35 -0700576 set_object = true;
577 }
578
579 if (set_object) {
580 annotation_value->value_.SetL(element_object);
581 }
582
583 return true;
584}
585
586mirror::Object* CreateAnnotationMember(Handle<mirror::Class> klass,
587 Handle<mirror::Class> annotation_class,
588 const uint8_t** annotation) {
589 const DexFile& dex_file = klass->GetDexFile();
590 Thread* self = Thread::Current();
591 ScopedObjectAccessUnchecked soa(self);
592 StackHandleScope<5> hs(self);
593 uint32_t element_name_index = DecodeUnsignedLeb128(annotation);
594 const char* name = dex_file.StringDataByIdx(element_name_index);
595 Handle<mirror::String> string_name(
596 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name)));
597
598 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
599 ArtMethod* annotation_method =
600 annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size);
601 if (annotation_method == nullptr) {
602 return nullptr;
603 }
604 Handle<mirror::Class> method_return(hs.NewHandle(
605 annotation_method->GetReturnType(true /* resolve */, pointer_size)));
606
607 DexFile::AnnotationValue annotation_value;
608 if (!ProcessAnnotationValue(klass, annotation, &annotation_value, method_return,
609 DexFile::kAllObjects)) {
610 return nullptr;
611 }
612 Handle<mirror::Object> value_object(hs.NewHandle(annotation_value.value_.GetL()));
613
614 mirror::Class* annotation_member_class =
615 WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember);
616 Handle<mirror::Object> new_member(hs.NewHandle(annotation_member_class->AllocObject(self)));
617 mirror::Method* method_obj_ptr;
618 DCHECK(!Runtime::Current()->IsActiveTransaction());
619 if (pointer_size == PointerSize::k64) {
620 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k64, false>(
621 self, annotation_method);
622 } else {
623 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k32, false>(
624 self, annotation_method);
625 }
626 Handle<mirror::Method> method_object(hs.NewHandle(method_obj_ptr));
627
628 if (new_member.Get() == nullptr || string_name.Get() == nullptr ||
629 method_object.Get() == nullptr || method_return.Get() == nullptr) {
630 LOG(ERROR) << StringPrintf("Failed creating annotation element (m=%p n=%p a=%p r=%p",
631 new_member.Get(), string_name.Get(), method_object.Get(), method_return.Get());
632 return nullptr;
633 }
634
635 JValue result;
636 ArtMethod* annotation_member_init =
637 soa.DecodeMethod(WellKnownClasses::libcore_reflect_AnnotationMember_init);
638 uint32_t args[5] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(new_member.Get())),
639 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(string_name.Get())),
640 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(value_object.Get())),
641 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_return.Get())),
642 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_object.Get()))
643 };
644 annotation_member_init->Invoke(self, args, sizeof(args), &result, "VLLLL");
645 if (self->IsExceptionPending()) {
646 LOG(INFO) << "Exception in AnnotationMember.<init>";
647 return nullptr;
648 }
649
650 return new_member.Get();
651}
652
653const DexFile::AnnotationItem* GetAnnotationItemFromAnnotationSet(
654 Handle<mirror::Class> klass,
655 const DexFile::AnnotationSetItem* annotation_set,
656 uint32_t visibility,
657 Handle<mirror::Class> annotation_class)
658 REQUIRES_SHARED(Locks::mutator_lock_) {
659 const DexFile& dex_file = klass->GetDexFile();
660 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
661 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
662 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
663 continue;
664 }
665 const uint8_t* annotation = annotation_item->annotation_;
666 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
667 mirror::Class* resolved_class = Runtime::Current()->GetClassLinker()->ResolveType(
668 klass->GetDexFile(), type_index, klass.Get());
669 if (resolved_class == nullptr) {
670 std::string temp;
671 LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d",
672 klass->GetDescriptor(&temp), type_index);
673 CHECK(Thread::Current()->IsExceptionPending());
674 Thread::Current()->ClearException();
675 continue;
676 }
677 if (resolved_class == annotation_class.Get()) {
678 return annotation_item;
679 }
680 }
681
682 return nullptr;
683}
684
685mirror::Object* GetAnnotationObjectFromAnnotationSet(
686 Handle<mirror::Class> klass,
687 const DexFile::AnnotationSetItem* annotation_set,
688 uint32_t visibility,
689 Handle<mirror::Class> annotation_class)
690 REQUIRES_SHARED(Locks::mutator_lock_) {
691 const DexFile::AnnotationItem* annotation_item =
692 GetAnnotationItemFromAnnotationSet(klass, annotation_set, visibility, annotation_class);
693 if (annotation_item == nullptr) {
694 return nullptr;
695 }
696 const uint8_t* annotation = annotation_item->annotation_;
697 return ProcessEncodedAnnotation(klass, &annotation);
698}
699
700mirror::Object* GetAnnotationValue(Handle<mirror::Class> klass,
701 const DexFile::AnnotationItem* annotation_item,
702 const char* annotation_name,
703 Handle<mirror::Class> array_class,
704 uint32_t expected_type)
705 REQUIRES_SHARED(Locks::mutator_lock_) {
706 const DexFile& dex_file = klass->GetDexFile();
707 const uint8_t* annotation =
708 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name);
709 if (annotation == nullptr) {
710 return nullptr;
711 }
712 DexFile::AnnotationValue annotation_value;
713 if (!ProcessAnnotationValue(klass, &annotation, &annotation_value, array_class,
714 DexFile::kAllObjects)) {
715 return nullptr;
716 }
717 if (annotation_value.type_ != expected_type) {
718 return nullptr;
719 }
720 return annotation_value.value_.GetL();
721}
722
723mirror::ObjectArray<mirror::String>* GetSignatureValue(Handle<mirror::Class> klass,
724 const DexFile::AnnotationSetItem* annotation_set)
725 REQUIRES_SHARED(Locks::mutator_lock_) {
726 const DexFile& dex_file = klass->GetDexFile();
727 StackHandleScope<1> hs(Thread::Current());
728 const DexFile::AnnotationItem* annotation_item =
729 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;",
730 DexFile::kDexVisibilitySystem);
731 if (annotation_item == nullptr) {
732 return nullptr;
733 }
734 mirror::Class* string_class = mirror::String::GetJavaLangString();
735 Handle<mirror::Class> string_array_class(hs.NewHandle(
736 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
737 if (string_array_class.Get() == nullptr) {
738 return nullptr;
739 }
740 mirror::Object* obj =
741 GetAnnotationValue(klass, annotation_item, "value", string_array_class,
742 DexFile::kDexAnnotationArray);
743 if (obj == nullptr) {
744 return nullptr;
745 }
746 return obj->AsObjectArray<mirror::String>();
747}
748
749mirror::ObjectArray<mirror::Class>* GetThrowsValue(Handle<mirror::Class> klass,
750 const DexFile::AnnotationSetItem* annotation_set)
751 REQUIRES_SHARED(Locks::mutator_lock_) {
752 const DexFile& dex_file = klass->GetDexFile();
753 StackHandleScope<1> hs(Thread::Current());
754 const DexFile::AnnotationItem* annotation_item =
755 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;",
756 DexFile::kDexVisibilitySystem);
757 if (annotation_item == nullptr) {
758 return nullptr;
759 }
760 mirror::Class* class_class = mirror::Class::GetJavaLangClass();
761 Handle<mirror::Class> class_array_class(hs.NewHandle(
762 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &class_class)));
763 if (class_array_class.Get() == nullptr) {
764 return nullptr;
765 }
766 mirror::Object* obj =
767 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
768 DexFile::kDexAnnotationArray);
769 if (obj == nullptr) {
770 return nullptr;
771 }
772 return obj->AsObjectArray<mirror::Class>();
773}
774
775mirror::ObjectArray<mirror::Object>* ProcessAnnotationSet(
776 Handle<mirror::Class> klass,
777 const DexFile::AnnotationSetItem* annotation_set,
778 uint32_t visibility)
779 REQUIRES_SHARED(Locks::mutator_lock_) {
780 const DexFile& dex_file = klass->GetDexFile();
781 Thread* self = Thread::Current();
782 ScopedObjectAccessUnchecked soa(self);
783 StackHandleScope<2> hs(self);
784 Handle<mirror::Class> annotation_array_class(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700785 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array)));
David Sehr9323e6e2016-09-13 08:58:35 -0700786 if (annotation_set == nullptr) {
787 return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0);
788 }
789
790 uint32_t size = annotation_set->size_;
791 Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle(
792 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size)));
793 if (result.Get() == nullptr) {
794 return nullptr;
795 }
796
797 uint32_t dest_index = 0;
798 for (uint32_t i = 0; i < size; ++i) {
799 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
800 // Note that we do not use IsVisibilityCompatible here because older code
801 // was correct for this case.
802 if (annotation_item->visibility_ != visibility) {
803 continue;
804 }
805 const uint8_t* annotation = annotation_item->annotation_;
806 mirror::Object* annotation_obj = ProcessEncodedAnnotation(klass, &annotation);
807 if (annotation_obj != nullptr) {
808 result->SetWithoutChecks<false>(dest_index, annotation_obj);
809 ++dest_index;
810 } else if (self->IsExceptionPending()) {
811 return nullptr;
812 }
813 }
814
815 if (dest_index == size) {
816 return result.Get();
817 }
818
819 mirror::ObjectArray<mirror::Object>* trimmed_result =
820 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index);
821 if (trimmed_result == nullptr) {
822 return nullptr;
823 }
824
825 for (uint32_t i = 0; i < dest_index; ++i) {
826 mirror::Object* obj = result->GetWithoutChecks(i);
827 trimmed_result->SetWithoutChecks<false>(i, obj);
828 }
829
830 return trimmed_result;
831}
832
833mirror::ObjectArray<mirror::Object>* ProcessAnnotationSetRefList(
834 Handle<mirror::Class> klass,
835 const DexFile::AnnotationSetRefList* set_ref_list,
836 uint32_t size)
837 REQUIRES_SHARED(Locks::mutator_lock_) {
838 const DexFile& dex_file = klass->GetDexFile();
839 Thread* self = Thread::Current();
840 ScopedObjectAccessUnchecked soa(self);
841 StackHandleScope<1> hs(self);
842 mirror::Class* annotation_array_class =
Mathieu Chartier0795f232016-09-27 18:43:30 -0700843 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array).Decode();
David Sehr9323e6e2016-09-13 08:58:35 -0700844 mirror::Class* annotation_array_array_class =
845 Runtime::Current()->GetClassLinker()->FindArrayClass(self, &annotation_array_class);
846 if (annotation_array_array_class == nullptr) {
847 return nullptr;
848 }
849 Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle(
850 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size)));
851 if (annotation_array_array.Get() == nullptr) {
852 LOG(ERROR) << "Annotation set ref array allocation failed";
853 return nullptr;
854 }
855 for (uint32_t index = 0; index < size; ++index) {
856 const DexFile::AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index];
857 const DexFile::AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item);
858 mirror::Object* annotation_set = ProcessAnnotationSet(klass, set_item,
859 DexFile::kDexVisibilityRuntime);
860 if (annotation_set == nullptr) {
861 return nullptr;
862 }
863 annotation_array_array->SetWithoutChecks<false>(index, annotation_set);
864 }
865 return annotation_array_array.Get();
866}
867} // namespace
868
869namespace annotations {
870
871mirror::Object* GetAnnotationForField(ArtField* field, Handle<mirror::Class> annotation_class) {
872 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
873 if (annotation_set == nullptr) {
874 return nullptr;
875 }
876 StackHandleScope<1> hs(Thread::Current());
877 Handle<mirror::Class> field_class(hs.NewHandle(field->GetDeclaringClass()));
878 return GetAnnotationObjectFromAnnotationSet(field_class, annotation_set,
879 DexFile::kDexVisibilityRuntime, annotation_class);
880}
881
882mirror::ObjectArray<mirror::Object>* GetAnnotationsForField(ArtField* field) {
883 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
884 StackHandleScope<1> hs(Thread::Current());
885 Handle<mirror::Class> field_class(hs.NewHandle(field->GetDeclaringClass()));
886 return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime);
887}
888
889mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForField(ArtField* field) {
890 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
891 if (annotation_set == nullptr) {
892 return nullptr;
893 }
894 StackHandleScope<1> hs(Thread::Current());
895 Handle<mirror::Class> field_class(hs.NewHandle(field->GetDeclaringClass()));
896 return GetSignatureValue(field_class, annotation_set);
897}
898
899bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) {
900 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
901 if (annotation_set == nullptr) {
902 return false;
903 }
904 StackHandleScope<1> hs(Thread::Current());
905 Handle<mirror::Class> field_class(hs.NewHandle(field->GetDeclaringClass()));
906 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
907 field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
908 return annotation_item != nullptr;
909}
910
911mirror::Object* GetAnnotationDefaultValue(ArtMethod* method) {
912 const DexFile* dex_file = method->GetDexFile();
913 mirror::Class* klass = method->GetDeclaringClass();
914 const DexFile::AnnotationsDirectoryItem* annotations_dir =
915 dex_file->GetAnnotationsDirectory(*klass->GetClassDef());
916 if (annotations_dir == nullptr) {
917 return nullptr;
918 }
919 const DexFile::AnnotationSetItem* annotation_set =
920 dex_file->GetClassAnnotationSet(annotations_dir);
921 if (annotation_set == nullptr) {
922 return nullptr;
923 }
924 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set,
925 "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem);
926 if (annotation_item == nullptr) {
927 return nullptr;
928 }
929 const uint8_t* annotation =
930 SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value");
931 if (annotation == nullptr) {
932 return nullptr;
933 }
934 uint8_t header_byte = *(annotation++);
935 if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) {
936 return nullptr;
937 }
938 annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName());
939 if (annotation == nullptr) {
940 return nullptr;
941 }
942 DexFile::AnnotationValue annotation_value;
943 StackHandleScope<2> hs(Thread::Current());
944 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
945 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
946 Handle<mirror::Class> return_type(hs.NewHandle(
947 method->GetReturnType(true /* resolve */, pointer_size)));
948 if (!ProcessAnnotationValue(h_klass, &annotation, &annotation_value, return_type,
949 DexFile::kAllObjects)) {
950 return nullptr;
951 }
952 return annotation_value.value_.GetL();
953}
954
955mirror::Object* GetAnnotationForMethod(ArtMethod* method, Handle<mirror::Class> annotation_class) {
956 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
957 if (annotation_set == nullptr) {
958 return nullptr;
959 }
960 StackHandleScope<1> hs(Thread::Current());
961 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
962 return GetAnnotationObjectFromAnnotationSet(method_class, annotation_set,
963 DexFile::kDexVisibilityRuntime, annotation_class);
964}
965
966mirror::ObjectArray<mirror::Object>* GetAnnotationsForMethod(ArtMethod* method) {
967 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
968 StackHandleScope<1> hs(Thread::Current());
969 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
970 return ProcessAnnotationSet(method_class, annotation_set, DexFile::kDexVisibilityRuntime);
971}
972
973mirror::ObjectArray<mirror::Class>* GetExceptionTypesForMethod(ArtMethod* method) {
974 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
975 if (annotation_set == nullptr) {
976 return nullptr;
977 }
978 StackHandleScope<1> hs(Thread::Current());
979 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
980 return GetThrowsValue(method_class, annotation_set);
981}
982
983mirror::ObjectArray<mirror::Object>* GetParameterAnnotations(ArtMethod* method) {
984 const DexFile* dex_file = method->GetDexFile();
985 const DexFile::ParameterAnnotationsItem* parameter_annotations =
986 FindAnnotationsItemForMethod(method);
987 if (parameter_annotations == nullptr) {
988 return nullptr;
989 }
990 const DexFile::AnnotationSetRefList* set_ref_list =
991 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
992 if (set_ref_list == nullptr) {
993 return nullptr;
994 }
995 uint32_t size = set_ref_list->size_;
996 StackHandleScope<1> hs(Thread::Current());
997 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
998 return ProcessAnnotationSetRefList(method_class, set_ref_list, size);
999}
1000
1001mirror::Object* GetAnnotationForMethodParameter(ArtMethod* method,
1002 uint32_t parameter_idx,
1003 Handle<mirror::Class> annotation_class) {
1004 const DexFile* dex_file = method->GetDexFile();
1005 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1006 FindAnnotationsItemForMethod(method);
1007 if (parameter_annotations == nullptr) {
1008 return nullptr;
1009 }
1010 const DexFile::AnnotationSetRefList* set_ref_list =
1011 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1012 if (set_ref_list == nullptr) {
1013 return nullptr;
1014 }
1015 if (parameter_idx >= set_ref_list->size_) {
1016 return nullptr;
1017 }
1018 const DexFile::AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx];
1019 const DexFile::AnnotationSetItem* annotation_set =
1020 dex_file->GetSetRefItemItem(annotation_set_ref);
1021
1022 StackHandleScope<1> hs(Thread::Current());
1023 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
1024 return GetAnnotationObjectFromAnnotationSet(method_class,
1025 annotation_set,
1026 DexFile::kDexVisibilityRuntime,
1027 annotation_class);
1028}
1029
Neil Fuller79a21e72016-09-09 14:24:51 +01001030bool GetParametersMetadataForMethod(ArtMethod* method,
1031 MutableHandle<mirror::ObjectArray<mirror::String>>* names,
1032 MutableHandle<mirror::IntArray>* access_flags) {
1033 const DexFile::AnnotationSetItem::AnnotationSetItem* annotation_set =
1034 FindAnnotationSetForMethod(method);
1035 if (annotation_set == nullptr) {
1036 return false;
1037 }
1038
1039 const DexFile* dex_file = method->GetDexFile();
1040 const DexFile::AnnotationItem* annotation_item =
1041 SearchAnnotationSet(*dex_file,
1042 annotation_set,
1043 "Ldalvik/annotation/MethodParameters;",
1044 DexFile::kDexVisibilitySystem);
1045 if (annotation_item == nullptr) {
1046 return false;
1047 }
1048
1049 StackHandleScope<5> hs(Thread::Current());
1050
1051 // Extract the parameters' names String[].
1052 mirror::Class* string_class = mirror::String::GetJavaLangString();
1053 Handle<mirror::Class> string_array_class(hs.NewHandle(
1054 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
1055 if (UNLIKELY(string_array_class.Get() == nullptr)) {
1056 return false;
1057 }
1058
1059 Handle<mirror::Class> klass = hs.NewHandle(method->GetDeclaringClass());
1060 Handle<mirror::Object> names_obj =
1061 hs.NewHandle(GetAnnotationValue(klass,
1062 annotation_item,
1063 "names",
1064 string_array_class,
1065 DexFile::kDexAnnotationArray));
1066 if (names_obj.Get() == nullptr) {
1067 return false;
1068 }
1069
1070 // Extract the parameters' access flags int[].
1071 Handle<mirror::Class> int_array_class(hs.NewHandle(mirror::IntArray::GetArrayClass()));
1072 if (UNLIKELY(int_array_class.Get() == nullptr)) {
1073 return false;
1074 }
1075 Handle<mirror::Object> access_flags_obj =
1076 hs.NewHandle(GetAnnotationValue(klass,
1077 annotation_item,
1078 "accessFlags",
1079 int_array_class,
1080 DexFile::kDexAnnotationArray));
1081 if (access_flags_obj.Get() == nullptr) {
1082 return false;
1083 }
1084
1085 names->Assign(names_obj.Get()->AsObjectArray<mirror::String>());
1086 access_flags->Assign(access_flags_obj.Get()->AsIntArray());
1087 return true;
1088}
1089
David Sehr9323e6e2016-09-13 08:58:35 -07001090mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForMethod(ArtMethod* method) {
1091 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1092 if (annotation_set == nullptr) {
1093 return nullptr;
1094 }
1095 StackHandleScope<1> hs(Thread::Current());
1096 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
1097 return GetSignatureValue(method_class, annotation_set);
1098}
1099
1100bool IsMethodAnnotationPresent(ArtMethod* method, Handle<mirror::Class> annotation_class,
1101 uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) {
1102 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1103 if (annotation_set == nullptr) {
1104 return false;
1105 }
1106 StackHandleScope<1> hs(Thread::Current());
1107 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
1108 const DexFile::AnnotationItem* annotation_item =
1109 GetAnnotationItemFromAnnotationSet(method_class, annotation_set, visibility,
1110 annotation_class);
1111 return annotation_item != nullptr;
1112}
1113
1114mirror::Object* GetAnnotationForClass(Handle<mirror::Class> klass,
1115 Handle<mirror::Class> annotation_class) {
1116 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1117 if (annotation_set == nullptr) {
1118 return nullptr;
1119 }
1120 return GetAnnotationObjectFromAnnotationSet(klass, annotation_set, DexFile::kDexVisibilityRuntime,
1121 annotation_class);
1122}
1123
1124mirror::ObjectArray<mirror::Object>* GetAnnotationsForClass(Handle<mirror::Class> klass) {
1125 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1126 return ProcessAnnotationSet(klass, annotation_set, DexFile::kDexVisibilityRuntime);
1127}
1128
1129mirror::ObjectArray<mirror::Class>* GetDeclaredClasses(Handle<mirror::Class> klass) {
1130 const DexFile& dex_file = klass->GetDexFile();
1131 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1132 if (annotation_set == nullptr) {
1133 return nullptr;
1134 }
1135 const DexFile::AnnotationItem* annotation_item =
1136 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/MemberClasses;",
1137 DexFile::kDexVisibilitySystem);
1138 if (annotation_item == nullptr) {
1139 return nullptr;
1140 }
1141 StackHandleScope<1> hs(Thread::Current());
1142 mirror::Class* class_class = mirror::Class::GetJavaLangClass();
1143 Handle<mirror::Class> class_array_class(hs.NewHandle(
1144 Runtime::Current()->GetClassLinker()->FindArrayClass(hs.Self(), &class_class)));
1145 if (class_array_class.Get() == nullptr) {
1146 return nullptr;
1147 }
1148 mirror::Object* obj =
1149 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
1150 DexFile::kDexAnnotationArray);
1151 if (obj == nullptr) {
1152 return nullptr;
1153 }
1154 return obj->AsObjectArray<mirror::Class>();
1155}
1156
1157mirror::Class* GetDeclaringClass(Handle<mirror::Class> klass) {
1158 const DexFile& dex_file = klass->GetDexFile();
1159 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1160 if (annotation_set == nullptr) {
1161 return nullptr;
1162 }
1163 const DexFile::AnnotationItem* annotation_item =
1164 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/EnclosingClass;",
1165 DexFile::kDexVisibilitySystem);
1166 if (annotation_item == nullptr) {
1167 return nullptr;
1168 }
1169 mirror::Object* obj = GetAnnotationValue(klass, annotation_item, "value",
1170 ScopedNullHandle<mirror::Class>(),
1171 DexFile::kDexAnnotationType);
1172 if (obj == nullptr) {
1173 return nullptr;
1174 }
1175 return obj->AsClass();
1176}
1177
1178mirror::Class* GetEnclosingClass(Handle<mirror::Class> klass) {
1179 const DexFile& dex_file = klass->GetDexFile();
1180 mirror::Class* declaring_class = GetDeclaringClass(klass);
1181 if (declaring_class != nullptr) {
1182 return declaring_class;
1183 }
1184 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1185 if (annotation_set == nullptr) {
1186 return nullptr;
1187 }
1188 const DexFile::AnnotationItem* annotation_item =
1189 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/EnclosingMethod;",
1190 DexFile::kDexVisibilitySystem);
1191 if (annotation_item == nullptr) {
1192 return nullptr;
1193 }
1194 const uint8_t* annotation =
1195 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, "value");
1196 if (annotation == nullptr) {
1197 return nullptr;
1198 }
1199 DexFile::AnnotationValue annotation_value;
1200 if (!ProcessAnnotationValue(klass, &annotation, &annotation_value,
1201 ScopedNullHandle<mirror::Class>(), DexFile::kAllRaw)) {
1202 return nullptr;
1203 }
1204 if (annotation_value.type_ != DexFile::kDexAnnotationMethod) {
1205 return nullptr;
1206 }
1207 StackHandleScope<2> hs(Thread::Current());
1208 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
1209 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
1210 ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType(
1211 klass->GetDexFile(), annotation_value.value_.GetI(), dex_cache, class_loader);
1212 if (method == nullptr) {
1213 return nullptr;
1214 }
1215 return method->GetDeclaringClass();
1216}
1217
1218mirror::Object* GetEnclosingMethod(Handle<mirror::Class> klass) {
1219 const DexFile& dex_file = klass->GetDexFile();
1220 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1221 if (annotation_set == nullptr) {
1222 return nullptr;
1223 }
1224 const DexFile::AnnotationItem* annotation_item =
1225 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/EnclosingMethod;",
1226 DexFile::kDexVisibilitySystem);
1227 if (annotation_item == nullptr) {
1228 return nullptr;
1229 }
1230 return GetAnnotationValue(klass, annotation_item, "value", ScopedNullHandle<mirror::Class>(),
1231 DexFile::kDexAnnotationMethod);
1232}
1233
1234bool GetInnerClass(Handle<mirror::Class> klass, mirror::String** name) {
1235 const DexFile& dex_file = klass->GetDexFile();
1236 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1237 if (annotation_set == nullptr) {
1238 return false;
1239 }
1240 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
1241 dex_file, annotation_set, "Ldalvik/annotation/InnerClass;", DexFile::kDexVisibilitySystem);
1242 if (annotation_item == nullptr) {
1243 return false;
1244 }
1245 const uint8_t* annotation =
1246 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, "name");
1247 if (annotation == nullptr) {
1248 return false;
1249 }
1250 DexFile::AnnotationValue annotation_value;
1251 if (!ProcessAnnotationValue(klass, &annotation, &annotation_value,
1252 ScopedNullHandle<mirror::Class>(),
1253 DexFile::kAllObjects)) {
1254 return false;
1255 }
1256 if (annotation_value.type_ != DexFile::kDexAnnotationNull &&
1257 annotation_value.type_ != DexFile::kDexAnnotationString) {
1258 return false;
1259 }
1260 *name = down_cast<mirror::String*>(annotation_value.value_.GetL());
1261 return true;
1262}
1263
1264bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) {
1265 const DexFile& dex_file = klass->GetDexFile();
1266 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1267 if (annotation_set == nullptr) {
1268 return false;
1269 }
1270 const DexFile::AnnotationItem* annotation_item =
1271 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/InnerClass;",
1272 DexFile::kDexVisibilitySystem);
1273 if (annotation_item == nullptr) {
1274 return false;
1275 }
1276 const uint8_t* annotation =
1277 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, "accessFlags");
1278 if (annotation == nullptr) {
1279 return false;
1280 }
1281 DexFile::AnnotationValue annotation_value;
1282 if (!ProcessAnnotationValue(klass, &annotation, &annotation_value,
1283 ScopedNullHandle<mirror::Class>(), DexFile::kAllRaw)) {
1284 return false;
1285 }
1286 if (annotation_value.type_ != DexFile::kDexAnnotationInt) {
1287 return false;
1288 }
1289 *flags = annotation_value.value_.GetI();
1290 return true;
1291}
1292
1293mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForClass(Handle<mirror::Class> klass) {
1294 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1295 if (annotation_set == nullptr) {
1296 return nullptr;
1297 }
1298 return GetSignatureValue(klass, annotation_set);
1299}
1300
1301bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) {
1302 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1303 if (annotation_set == nullptr) {
1304 return false;
1305 }
1306 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1307 klass, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1308 return annotation_item != nullptr;
1309}
1310
1311int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) {
1312 // For native method, lineno should be -2 to indicate it is native. Note that
1313 // "line number == -2" is how libcore tells from StackTraceElement.
1314 if (method->GetCodeItemOffset() == 0) {
1315 return -2;
1316 }
1317
1318 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
1319 DCHECK(code_item != nullptr) << PrettyMethod(method) << " " << dex_file->GetLocation();
1320
1321 // A method with no line number info should return -1
1322 DexFile::LineNumFromPcContext context(rel_pc, -1);
1323 dex_file->DecodeDebugPositionInfo(code_item, DexFile::LineNumForPcCb, &context);
1324 return context.line_num_;
1325}
1326
1327template<bool kTransactionActive>
1328void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const {
1329 DCHECK(dex_cache_ != nullptr);
1330 DCHECK(class_loader_ != nullptr);
1331 switch (type_) {
1332 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z);
1333 break;
1334 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1335 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1336 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1337 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1338 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1339 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1340 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1341 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break;
1342 case kString: {
1343 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, *dex_cache_);
1344 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1345 break;
1346 }
1347 case kType: {
1348 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, *dex_cache_,
1349 *class_loader_);
1350 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1351 break;
1352 }
1353 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1354 }
1355}
1356template
1357void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const;
1358template
1359void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const;
1360
1361} // namespace annotations
1362
1363} // namespace art