1 /*
2  * Copyright (C) 2011 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 #ifndef ART_RUNTIME_MIRROR_THROWABLE_H_
18 #define ART_RUNTIME_MIRROR_THROWABLE_H_
19 
20 #include "gc_root.h"
21 #include "object.h"
22 #include "object_callbacks.h"
23 #include "string.h"
24 
25 namespace art {
26 
27 struct ThrowableOffsets;
28 
29 namespace mirror {
30 
31 // C++ mirror of java.lang.Throwable
32 class MANAGED Throwable : public Object {
33  public:
34   void SetDetailMessage(ObjPtr<String> new_detail_message) REQUIRES_SHARED(Locks::mutator_lock_);
35 
GetDetailMessage()36   String* GetDetailMessage() REQUIRES_SHARED(Locks::mutator_lock_) {
37     return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_));
38   }
39 
40   std::string Dump() REQUIRES_SHARED(Locks::mutator_lock_);
41 
42   // This is a runtime version of initCause, you shouldn't use it if initCause may have been
43   // overridden. Also it asserts rather than throwing exceptions. Currently this is only used
44   // in cases like the verifier where the checks cannot fail and initCause isn't overridden.
45   void SetCause(ObjPtr<Throwable> cause) REQUIRES_SHARED(Locks::mutator_lock_);
46   void SetStackState(ObjPtr<Object> state) REQUIRES_SHARED(Locks::mutator_lock_);
47   bool IsCheckedException() REQUIRES_SHARED(Locks::mutator_lock_);
48 
GetJavaLangThrowable()49   static Class* GetJavaLangThrowable() REQUIRES_SHARED(Locks::mutator_lock_) {
50     DCHECK(!java_lang_Throwable_.IsNull());
51     return java_lang_Throwable_.Read();
52   }
53 
54   int32_t GetStackDepth() REQUIRES_SHARED(Locks::mutator_lock_);
55 
56   static void SetClass(ObjPtr<Class> java_lang_Throwable);
57   static void ResetClass();
58   static void VisitRoots(RootVisitor* visitor)
59       REQUIRES_SHARED(Locks::mutator_lock_);
60 
61  private:
GetStackState()62   Object* GetStackState() REQUIRES_SHARED(Locks::mutator_lock_) {
63     return GetFieldObjectVolatile<Object>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_));
64   }
GetStackTrace()65   Object* GetStackTrace() REQUIRES_SHARED(Locks::mutator_lock_) {
66     return GetFieldObjectVolatile<Object>(OFFSET_OF_OBJECT_MEMBER(Throwable, backtrace_));
67   }
68 
69   // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
70   HeapReference<Object> backtrace_;  // Note this is Java volatile:
71   HeapReference<Throwable> cause_;
72   HeapReference<String> detail_message_;
73   HeapReference<Object> stack_trace_;
74   HeapReference<Object> suppressed_exceptions_;
75 
76   static GcRoot<Class> java_lang_Throwable_;
77 
78   friend struct art::ThrowableOffsets;  // for verifying offset information
79   DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
80 };
81 
82 }  // namespace mirror
83 }  // namespace art
84 
85 #endif  // ART_RUNTIME_MIRROR_THROWABLE_H_
86