1/*
2 * Copyright (C) 2015 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 "method.h"
18
19#include "art_method.h"
20#include "gc_root-inl.h"
21#include "mirror/class-inl.h"
22#include "mirror/object-inl.h"
23
24namespace art {
25namespace mirror {
26
27GcRoot<Class> Method::static_class_;
28GcRoot<Class> Method::array_class_;
29GcRoot<Class> Constructor::static_class_;
30GcRoot<Class> Constructor::array_class_;
31
32void Method::SetClass(Class* klass) {
33  CHECK(static_class_.IsNull()) << static_class_.Read() << " " << klass;
34  CHECK(klass != nullptr);
35  static_class_ = GcRoot<Class>(klass);
36}
37
38void Method::ResetClass() {
39  CHECK(!static_class_.IsNull());
40  static_class_ = GcRoot<Class>(nullptr);
41}
42
43void Method::SetArrayClass(Class* klass) {
44  CHECK(array_class_.IsNull()) << array_class_.Read() << " " << klass;
45  CHECK(klass != nullptr);
46  array_class_ = GcRoot<Class>(klass);
47}
48
49void Method::ResetArrayClass() {
50  CHECK(!array_class_.IsNull());
51  array_class_ = GcRoot<Class>(nullptr);
52}
53
54Method* Method::CreateFromArtMethod(Thread* self, ArtMethod* method) {
55  DCHECK(!method->IsConstructor()) << PrettyMethod(method);
56  auto* ret = down_cast<Method*>(StaticClass()->AllocObject(self));
57  if (LIKELY(ret != nullptr)) {
58    static_cast<AbstractMethod*>(ret)->CreateFromArtMethod(method);
59  }
60  return ret;
61}
62
63void Method::VisitRoots(RootVisitor* visitor) {
64  static_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
65  array_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
66}
67
68void Constructor::SetClass(Class* klass) {
69  CHECK(static_class_.IsNull()) << static_class_.Read() << " " << klass;
70  CHECK(klass != nullptr);
71  static_class_ = GcRoot<Class>(klass);
72}
73
74void Constructor::ResetClass() {
75  CHECK(!static_class_.IsNull());
76  static_class_ = GcRoot<Class>(nullptr);
77}
78
79void Constructor::SetArrayClass(Class* klass) {
80  CHECK(array_class_.IsNull()) << array_class_.Read() << " " << klass;
81  CHECK(klass != nullptr);
82  array_class_ = GcRoot<Class>(klass);
83}
84
85void Constructor::ResetArrayClass() {
86  CHECK(!array_class_.IsNull());
87  array_class_ = GcRoot<Class>(nullptr);
88}
89
90void Constructor::VisitRoots(RootVisitor* visitor) {
91  static_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
92  array_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
93}
94
95Constructor* Constructor::CreateFromArtMethod(Thread* self, ArtMethod* method) {
96  DCHECK(method->IsConstructor()) << PrettyMethod(method);
97  auto* ret = down_cast<Constructor*>(StaticClass()->AllocObject(self));
98  if (LIKELY(ret != nullptr)) {
99    static_cast<AbstractMethod*>(ret)->CreateFromArtMethod(method);
100  }
101  return ret;
102}
103
104}  // namespace mirror
105}  // namespace art
106