1 /*
2  * Copyright (C) 2022 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_ART_METHOD_ALLOC_INL_H_
18 #define ART_RUNTIME_ART_METHOD_ALLOC_INL_H_
19 
20 #include "art_method-inl.h"
21 
22 #include "handle.h"
23 #include "handle_scope.h"
24 #include "mirror/class-alloc-inl.h"
25 
26 namespace art HIDDEN {
27 
28 namespace detail {
29 
30 template <char Shorty>
31 struct HandleShortyTraits {
32   using Type = typename ShortyTraits<Shorty>::Type;
ExtractHandleShortyTraits33   static Type Extract(Type value) ALWAYS_INLINE { return value; }
34 };
35 
36 template <> struct HandleShortyTraits<'L'> {
37   using Type = Handle<mirror::Object>;
38   static typename ShortyTraits<'L'>::Type Extract(Type value)
39       REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
40     return value.Get();
41   }
42 };
43 
44 }  // namespace detail
45 
46 template <char... ArgType, typename HandleScopeType>
47 inline Handle<mirror::Object> ArtMethod::NewObject(
48     HandleScopeType& hs,
49     Thread* self,
50     typename detail::HandleShortyTraits<ArgType>::Type... args) {
51   DCHECK(!GetDeclaringClass()->IsInterface());
52   DCHECK(GetDeclaringClass()->IsInitialized());
53   DCHECK(IsConstructor());
54   DCHECK(!IsStatic());
55   MutableHandle<mirror::Object> new_object = hs.NewHandle(GetDeclaringClass()->AllocObject(self));
56   DCHECK_EQ(new_object == nullptr, self->IsExceptionPending());
57   if (LIKELY(new_object != nullptr)) {
58     InvokeInstance<'V', ArgType...>(
59         self, new_object.Get(), detail::HandleShortyTraits<ArgType>::Extract(args)...);
60     if (UNLIKELY(self->IsExceptionPending())) {
61       new_object.Assign(nullptr);
62     }
63   }
64   return new_object;
65 }
66 
67 template <char... ArgType>
68 inline ObjPtr<mirror::Object> ArtMethod::NewObject(
69     Thread* self, typename detail::HandleShortyTraits<ArgType>::Type... args) {
70   StackHandleScope<1u> hs(self);
71   return NewObject<ArgType...>(hs, self, args...).Get();
72 }
73 
74 }  // namespace art
75 
76 #endif  // ART_RUNTIME_ART_METHOD_ALLOC_INL_H_
77