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
54 template <bool kTransactionActive>
CreateFromArtMethod(Thread * self,ArtMethod * method)55 Method* Method::CreateFromArtMethod(Thread* self, ArtMethod* method) {
56 DCHECK(!method->IsConstructor()) << PrettyMethod(method);
57 auto* ret = down_cast<Method*>(StaticClass()->AllocObject(self));
58 if (LIKELY(ret != nullptr)) {
59 static_cast<AbstractMethod*>(ret)->CreateFromArtMethod<kTransactionActive>(method);
60 }
61 return ret;
62 }
63
64 template Method* Method::CreateFromArtMethod<false>(Thread* self, ArtMethod* method);
65 template Method* Method::CreateFromArtMethod<true>(Thread* self, ArtMethod* method);
66
VisitRoots(RootVisitor * visitor)67 void Method::VisitRoots(RootVisitor* visitor) {
68 static_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
69 array_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
70 }
71
SetClass(Class * klass)72 void Constructor::SetClass(Class* klass) {
73 CHECK(static_class_.IsNull()) << static_class_.Read() << " " << klass;
74 CHECK(klass != nullptr);
75 static_class_ = GcRoot<Class>(klass);
76 }
77
ResetClass()78 void Constructor::ResetClass() {
79 CHECK(!static_class_.IsNull());
80 static_class_ = GcRoot<Class>(nullptr);
81 }
82
SetArrayClass(Class * klass)83 void Constructor::SetArrayClass(Class* klass) {
84 CHECK(array_class_.IsNull()) << array_class_.Read() << " " << klass;
85 CHECK(klass != nullptr);
86 array_class_ = GcRoot<Class>(klass);
87 }
88
ResetArrayClass()89 void Constructor::ResetArrayClass() {
90 CHECK(!array_class_.IsNull());
91 array_class_ = GcRoot<Class>(nullptr);
92 }
93
VisitRoots(RootVisitor * visitor)94 void Constructor::VisitRoots(RootVisitor* visitor) {
95 static_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
96 array_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
97 }
98
99 template <bool kTransactionActive>
CreateFromArtMethod(Thread * self,ArtMethod * method)100 Constructor* Constructor::CreateFromArtMethod(Thread* self, ArtMethod* method) {
101 DCHECK(method->IsConstructor()) << PrettyMethod(method);
102 auto* ret = down_cast<Constructor*>(StaticClass()->AllocObject(self));
103 if (LIKELY(ret != nullptr)) {
104 static_cast<AbstractMethod*>(ret)->CreateFromArtMethod<kTransactionActive>(method);
105 }
106 return ret;
107 }
108
109 template Constructor* Constructor::CreateFromArtMethod<false>(Thread* self, ArtMethod* method);
110 template Constructor* Constructor::CreateFromArtMethod<true>(Thread* self, ArtMethod* method);
111
112 } // namespace mirror
113 } // namespace art
114