1daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier/*
2daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier * Copyright (C) 2015 The Android Open Source Project
3daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier *
4daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier * Licensed under the Apache License, Version 2.0 (the "License");
5daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier * you may not use this file except in compliance with the License.
6daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier * You may obtain a copy of the License at
7daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier *
8daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier *      http://www.apache.org/licenses/LICENSE-2.0
9daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier *
10daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier * Unless required by applicable law or agreed to in writing, software
11daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier * distributed under the License is distributed on an "AS IS" BASIS,
12daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier * See the License for the specific language governing permissions and
14daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier * limitations under the License.
15daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier */
16daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier
17daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier#ifndef ART_RUNTIME_MIRROR_FIELD_INL_H_
18daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier#define ART_RUNTIME_MIRROR_FIELD_INL_H_
19daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier
20daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier#include "field.h"
21daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier
22daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier#include "art_field-inl.h"
233481ba2c4e4f3aa80d8c6d50a9f85dacb56b508bVladimir Marko#include "mirror/dex_cache-inl.h"
24daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier#include "runtime-inl.h"
25daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier
26daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartiernamespace art {
27daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier
28daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartiernamespace mirror {
29daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier
30daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartiertemplate <bool kTransactionActive>
31c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartierinline mirror::Field* Field::CreateFromArtField(Thread* self, ArtField* field,
32daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier                                                bool force_resolve) {
332debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi  StackHandleScope<2> hs(self);
34daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier  // Try to resolve type before allocating since this is a thread suspension point.
352debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi  Handle<mirror::Class> type = hs.NewHandle(field->GetType<true>());
36daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier
372debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi  if (type.Get() == nullptr) {
38daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier    if (force_resolve) {
39daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier      if (kIsDebugBuild) {
40daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier        self->AssertPendingException();
41daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier      }
42daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier      return nullptr;
43daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier    } else {
44daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier      // Can't resolve, clear the exception if it isn't OOME and continue with a null type.
45daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier      mirror::Throwable* exception = self->GetException();
46daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier      if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
47daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier        return nullptr;
48daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier      }
49daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier      self->ClearException();
50daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier    }
51daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier  }
52daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier  auto ret = hs.NewHandle(static_cast<Field*>(StaticClass()->AllocObject(self)));
53e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  if (UNLIKELY(ret.Get() == nullptr)) {
54e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    self->AssertPendingOOMException();
55daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier    return nullptr;
56daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier  }
57e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  const auto pointer_size = kTransactionActive ?
58e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      Runtime::Current()->GetClassLinker()->GetImagePointerSize() : sizeof(void*);
59daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier  auto dex_field_index = field->GetDexFieldIndex();
603a0909248e04b22c3981cbf617bc2502ed5b6380Nicolas Geoffray  auto* resolved_field = field->GetDexCache()->GetResolvedField(dex_field_index, pointer_size);
613a0909248e04b22c3981cbf617bc2502ed5b6380Nicolas Geoffray  if (field->GetDeclaringClass()->IsProxyClass()) {
622debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi    DCHECK(field->IsStatic());
632debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi    DCHECK_LT(dex_field_index, 2U);
642debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi    // The two static fields (interfaces, throws) of all proxy classes
652debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi    // share the same dex file indices 0 and 1. So, we can't resolve
662debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi    // them in the dex cache.
67daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier  } else {
682debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi    if (resolved_field != nullptr) {
692debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi      DCHECK_EQ(resolved_field, field);
702debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi    } else {
712debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi      // We rely on the field being resolved so that we can back to the ArtField
722debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi      // (i.e. FromReflectedMethod).
73e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      field->GetDexCache()->SetResolvedField(dex_field_index, field, pointer_size);
742debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi    }
75daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier  }
762debd8032a9e44910bb7e39ca5d142a4fc237c05Hiroshi Yamauchi  ret->SetType<kTransactionActive>(type.Get());
77daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier  ret->SetDeclaringClass<kTransactionActive>(field->GetDeclaringClass());
78daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier  ret->SetAccessFlags<kTransactionActive>(field->GetAccessFlags());
79daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier  ret->SetDexFieldIndex<kTransactionActive>(dex_field_index);
80daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier  ret->SetOffset<kTransactionActive>(field->GetOffset().Int32Value());
81daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier  return ret.Get();
82daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier}
83daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier
84daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier}  // namespace mirror
85daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier}  // namespace art
86daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier
87daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier#endif  // ART_RUNTIME_MIRROR_FIELD_INL_H_
88