Module: Tapioca::Runtime::Reflection
- Extended by:
- T::Sig, Reflection
- Includes:
- AttachedClassOf
- Included in:
- Dsl::Compiler, Dsl::Compiler, Gem::Listeners::DynamicMixins, Gem::Listeners::ForeignConstants, Gem::Listeners::Methods, Gem::Listeners::Mixins, Gem::Listeners::RemoveEmptyPayloadScopes, Gem::Listeners::SorbetHelpers, Gem::Listeners::SorbetSignatures, Gem::Listeners::SorbetTypeVariables, Gem::Listeners::Subconstants, Gem::Pipeline, DynamicMixinCompiler, Reflection, Trackers::ConstantDefinition, Static::SymbolLoader
- Defined in:
- lib/tapioca/runtime/reflection.rb
Constant Summary collapse
- CLASS_METHOD =
: UnboundMethod
Kernel.instance_method(:class)
- CONSTANTS_METHOD =
: UnboundMethod
Module.instance_method(:constants)
- NAME_METHOD =
: UnboundMethod
Module.instance_method(:name)
- SINGLETON_CLASS_METHOD =
: UnboundMethod
Object.instance_method(:singleton_class)
- ANCESTORS_METHOD =
: UnboundMethod
Module.instance_method(:ancestors)
- SUPERCLASS_METHOD =
: UnboundMethod
Class.instance_method(:superclass)
- OBJECT_ID_METHOD =
: UnboundMethod
BasicObject.instance_method(:__id__)
- EQUAL_METHOD =
: UnboundMethod
BasicObject.instance_method(:equal?)
- PUBLIC_INSTANCE_METHODS_METHOD =
: UnboundMethod
Module.instance_method(:public_instance_methods)
- PROTECTED_INSTANCE_METHODS_METHOD =
: UnboundMethod
Module.instance_method(:protected_instance_methods)
- PRIVATE_INSTANCE_METHODS_METHOD =
: UnboundMethod
Module.instance_method(:private_instance_methods)
- METHOD_METHOD =
: UnboundMethod
Kernel.instance_method(:method)
- UNDEFINED_CONSTANT =
: Module
Module.new.freeze
- REQUIRED_FROM_LABELS =
: Array
["<top (required)>", "<main>", "<compiled>"].freeze
Instance Method Summary collapse
-
#abstract_type_of(constant) ⇒ Object
: (Module constant) -> untyped.
-
#ancestors_of(constant) ⇒ Object
: (Module constant) -> Array.
-
#are_equal?(object, other) ⇒ Boolean
: (BasicObject object, BasicObject other) -> bool.
-
#class_of(object) ⇒ Object
: (BasicObject object) -> Class.
-
#constant_defined?(constant) ⇒ Boolean
: (BasicObject constant) -> bool.
-
#constantize(symbol, inherit: false, namespace: Object) ⇒ Object
: (String symbol, ?inherit: bool, ?namespace: Module) -> BasicObject.
-
#constants_of(constant) ⇒ Object
: (Module constant) -> Array.
-
#descendants_of(klass) ⇒ Object
Returns an array with all classes that are < than the supplied class.
-
#file_candidates_for(constant) ⇒ Object
: (Module constant) -> Set.
-
#final_module?(constant) ⇒ Boolean
: (Module constant) -> bool.
-
#inherited_ancestors_of(constant) ⇒ Object
: (Module constant) -> Array.
-
#method_of(constant, method) ⇒ Object
: (Module constant, Symbol method) -> Method.
-
#name_of(constant) ⇒ Object
: (Module constant) -> String?.
-
#name_of_type(type) ⇒ Object
: (T::Types::Base type) -> String.
-
#object_id_of(object) ⇒ Object
: (BasicObject object) -> Integer.
-
#private_instance_methods_of(constant) ⇒ Object
: (Module constant) -> Array.
-
#protected_instance_methods_of(constant) ⇒ Object
: (Module constant) -> Array.
-
#public_instance_methods_of(constant) ⇒ Object
: (Module constant) -> Array.
-
#qualified_name_of(constant) ⇒ Object
: (Module constant) -> String?.
-
#resolve_loc(locations) ⇒ Object
Examines the call stack to identify the closest location where a “require” is performed by searching for the label “<top (required)>” or “block in <class:…>” in the case of an ActiveSupport.on_load hook.
-
#sealed_module?(constant) ⇒ Boolean
: (Module constant) -> bool.
-
#signature_of(method) ⇒ Object
: ((UnboundMethod | Method) method) -> untyped.
-
#signature_of!(method) ⇒ Object
: ((UnboundMethod | Method) method) -> untyped.
-
#singleton_class_of(constant) ⇒ Object
: (Module constant) -> Class.
- #superclass_of(constant) ⇒ Object
Methods included from AttachedClassOf
Instance Method Details
#abstract_type_of(constant) ⇒ Object
: (Module constant) -> untyped
199 200 201 202 |
# File 'lib/tapioca/runtime/reflection.rb', line 199 def abstract_type_of(constant) T::Private::Abstract::Data.get(constant, :abstract_type) || T::Private::Abstract::Data.get(singleton_class_of(constant), :abstract_type) end |
#ancestors_of(constant) ⇒ Object
: (Module constant) -> Array
73 74 75 |
# File 'lib/tapioca/runtime/reflection.rb', line 73 def ancestors_of(constant) ANCESTORS_METHOD.bind_call(constant) end |
#are_equal?(object, other) ⇒ Boolean
: (BasicObject object, BasicObject other) -> bool
88 89 90 |
# File 'lib/tapioca/runtime/reflection.rb', line 88 def are_equal?(object, other) EQUAL_METHOD.bind_call(object, other) end |
#class_of(object) ⇒ Object
: (BasicObject object) -> Class
52 53 54 |
# File 'lib/tapioca/runtime/reflection.rb', line 52 def class_of(object) CLASS_METHOD.bind_call(object) end |
#constant_defined?(constant) ⇒ Boolean
: (BasicObject constant) -> bool
39 40 41 |
# File 'lib/tapioca/runtime/reflection.rb', line 39 def constant_defined?(constant) !UNDEFINED_CONSTANT.eql?(constant) end |
#constantize(symbol, inherit: false, namespace: Object) ⇒ Object
: (String symbol, ?inherit: bool, ?namespace: Module) -> BasicObject
45 46 47 48 49 |
# File 'lib/tapioca/runtime/reflection.rb', line 45 def constantize(symbol, inherit: false, namespace: Object) namespace.const_get(symbol, inherit) rescue NameError, LoadError, RuntimeError, ArgumentError, TypeError UNDEFINED_CONSTANT end |
#constants_of(constant) ⇒ Object
: (Module constant) -> Array
57 58 59 |
# File 'lib/tapioca/runtime/reflection.rb', line 57 def constants_of(constant) CONSTANTS_METHOD.bind_call(constant, false) end |
#descendants_of(klass) ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/tapioca/runtime/reflection.rb', line 164 def descendants_of(klass) result = ObjectSpace.each_object(klass.singleton_class).reject do |k| k.singleton_class? || k == klass end T.unsafe(result) end |
#file_candidates_for(constant) ⇒ Object
: (Module constant) -> Set
192 193 194 195 196 |
# File 'lib/tapioca/runtime/reflection.rb', line 192 def file_candidates_for(constant) relevant_methods_for(constant).filter_map do |method| method.source_location&.first end.to_set end |
#final_module?(constant) ⇒ Boolean
: (Module constant) -> bool
205 206 207 |
# File 'lib/tapioca/runtime/reflection.rb', line 205 def final_module?(constant) T::Private::Final.final_module?(constant) end |
#inherited_ancestors_of(constant) ⇒ Object
: (Module constant) -> Array
108 109 110 111 112 113 114 |
# File 'lib/tapioca/runtime/reflection.rb', line 108 def inherited_ancestors_of(constant) if Class === constant ancestors_of(superclass_of(constant) || Object) else Module.new.ancestors end end |
#method_of(constant, method) ⇒ Object
: (Module constant, Symbol method) -> Method
146 147 148 |
# File 'lib/tapioca/runtime/reflection.rb', line 146 def method_of(constant, method) METHOD_METHOD.bind_call(constant, method) end |
#name_of(constant) ⇒ Object
: (Module constant) -> String?
62 63 64 65 |
# File 'lib/tapioca/runtime/reflection.rb', line 62 def name_of(constant) name = NAME_METHOD.bind_call(constant) name&.start_with?("#<") ? nil : name end |
#name_of_type(type) ⇒ Object
: (T::Types::Base type) -> String
141 142 143 |
# File 'lib/tapioca/runtime/reflection.rb', line 141 def name_of_type(type) type.to_s end |
#object_id_of(object) ⇒ Object
: (BasicObject object) -> Integer
83 84 85 |
# File 'lib/tapioca/runtime/reflection.rb', line 83 def object_id_of(object) OBJECT_ID_METHOD.bind_call(object) end |
#private_instance_methods_of(constant) ⇒ Object
: (Module constant) -> Array
103 104 105 |
# File 'lib/tapioca/runtime/reflection.rb', line 103 def private_instance_methods_of(constant) PRIVATE_INSTANCE_METHODS_METHOD.bind_call(constant) end |
#protected_instance_methods_of(constant) ⇒ Object
: (Module constant) -> Array
98 99 100 |
# File 'lib/tapioca/runtime/reflection.rb', line 98 def protected_instance_methods_of(constant) PROTECTED_INSTANCE_METHODS_METHOD.bind_call(constant) end |
#public_instance_methods_of(constant) ⇒ Object
: (Module constant) -> Array
93 94 95 |
# File 'lib/tapioca/runtime/reflection.rb', line 93 def public_instance_methods_of(constant) PUBLIC_INSTANCE_METHODS_METHOD.bind_call(constant) end |
#qualified_name_of(constant) ⇒ Object
: (Module constant) -> String?
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/tapioca/runtime/reflection.rb', line 117 def qualified_name_of(constant) name = name_of(constant) return if name.nil? if name.start_with?("::") name else "::#{name}" end end |
#resolve_loc(locations) ⇒ Object
Examines the call stack to identify the closest location where a “require” is performed by searching for the label “<top (required)>” or “block in <class:…>” in the case of an ActiveSupport.on_load hook. If none is found, it returns the location labeled “<main>”, which is the original call site. : (Array? locations) -> String
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/tapioca/runtime/reflection.rb', line 177 def resolve_loc(locations) return "" unless locations resolved_loc = locations.find do |loc| label = loc.label next unless label REQUIRED_FROM_LABELS.include?(label) || label.start_with?("block in <class:") end return "" unless resolved_loc resolved_loc.absolute_path || "" end |
#sealed_module?(constant) ⇒ Boolean
: (Module constant) -> bool
210 211 212 |
# File 'lib/tapioca/runtime/reflection.rb', line 210 def sealed_module?(constant) T::Private::Sealed.sealed_module?(constant) end |
#signature_of(method) ⇒ Object
: ((UnboundMethod | Method) method) -> untyped
134 135 136 137 138 |
# File 'lib/tapioca/runtime/reflection.rb', line 134 def signature_of(method) signature_of!(method) rescue LoadError, StandardError nil end |
#signature_of!(method) ⇒ Object
: ((UnboundMethod | Method) method) -> untyped
129 130 131 |
# File 'lib/tapioca/runtime/reflection.rb', line 129 def signature_of!(method) T::Utils.signature_for_method(method) end |
#singleton_class_of(constant) ⇒ Object
: (Module constant) -> Class
68 69 70 |
# File 'lib/tapioca/runtime/reflection.rb', line 68 def singleton_class_of(constant) SINGLETON_CLASS_METHOD.bind_call(constant) end |
#superclass_of(constant) ⇒ Object
78 79 80 |
# File 'lib/tapioca/runtime/reflection.rb', line 78 def superclass_of(constant) SUPERCLASS_METHOD.bind_call(constant) end |