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
24 namespace art {
25 namespace mirror {
26
27 GcRoot<Class> Method::static_class_;
28 GcRoot<Class> Method::array_class_;
29 GcRoot<Class> Constructor::static_class_;
30 GcRoot<Class> Constructor::array_class_;
31
SetClass(Class * klass)32 void 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
ResetClass()38 void Method::ResetClass() {
39 CHECK(!static_class_.IsNull());
40 static_class_ = GcRoot<Class>(nullptr);
41 }
42
SetArrayClass(Class * klass)43 void 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
ResetArrayClass()49 void Method::ResetArrayClass() {
50 CHECK(!array_class_.IsNull());
51 array_class_ = GcRoot<Class>(nullptr);
52 }
53
CreateFromArtMethod(Thread * self,ArtMethod * method)54 Method* 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
VisitRoots(RootVisitor * visitor)63 void Method::VisitRoots(RootVisitor* visitor) {
64 static_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
65 array_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
66 }
67
SetClass(Class * klass)68 void 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
ResetClass()74 void Constructor::ResetClass() {
75 CHECK(!static_class_.IsNull());
76 static_class_ = GcRoot<Class>(nullptr);
77 }
78
SetArrayClass(Class * klass)79 void 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
ResetArrayClass()85 void Constructor::ResetArrayClass() {
86 CHECK(!array_class_.IsNull());
87 array_class_ = GcRoot<Class>(nullptr);
88 }
89
VisitRoots(RootVisitor * visitor)90 void Constructor::VisitRoots(RootVisitor* visitor) {
91 static_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
92 array_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
93 }
94
CreateFromArtMethod(Thread * self,ArtMethod * method)95 Constructor* 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