Module: Tapioca::Runtime::Reflection
- Extended by:
- T::Sig, Reflection
- 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 =
T.let(Kernel.instance_method(:class), UnboundMethod)
- CONSTANTS_METHOD =
T.let(Module.instance_method(:constants), UnboundMethod)
- NAME_METHOD =
T.let(Module.instance_method(:name), UnboundMethod)
- SINGLETON_CLASS_METHOD =
T.let(Object.instance_method(:singleton_class), UnboundMethod)
- ANCESTORS_METHOD =
T.let(Module.instance_method(:ancestors), UnboundMethod)
- SUPERCLASS_METHOD =
T.let(Class.instance_method(:superclass), UnboundMethod)
- OBJECT_ID_METHOD =
T.let(BasicObject.instance_method(:__id__), UnboundMethod)
- EQUAL_METHOD =
T.let(BasicObject.instance_method(:equal?), UnboundMethod)
- PUBLIC_INSTANCE_METHODS_METHOD =
T.let(Module.instance_method(:public_instance_methods), UnboundMethod)
- PROTECTED_INSTANCE_METHODS_METHOD =
T.let(Module.instance_method(:protected_instance_methods), UnboundMethod)
- PRIVATE_INSTANCE_METHODS_METHOD =
T.let(Module.instance_method(:private_instance_methods), UnboundMethod)
- METHOD_METHOD =
T.let(Kernel.instance_method(:method), UnboundMethod)
- REQUIRED_FROM_LABELS =
T.let(["<top (required)>", "<main>"].freeze, T::Array[String])
Instance Method Summary collapse
- #ancestors_of(constant) ⇒ Object
- #are_equal?(object, other) ⇒ Boolean
- #class_of(object) ⇒ Object
- #constant_from_singleton_class(constant) ⇒ Object
- #constant_name_from_singleton_class(constant) ⇒ Object
- #constantize(symbol, inherit: false, namespace: Object) ⇒ Object
- #constants_of(constant) ⇒ Object
- #descendants_of(klass) ⇒ Object
- #inherited_ancestors_of(constant) ⇒ Object
- #method_of(constant, method) ⇒ Object
- #name_of(constant) ⇒ Object
- #name_of_type(type) ⇒ Object
- #object_id_of(object) ⇒ Object
- #private_instance_methods_of(constant) ⇒ Object
- #protected_instance_methods_of(constant) ⇒ Object
- #public_instance_methods_of(constant) ⇒ Object
- #qualified_name_of(constant) ⇒ Object
- #resolve_loc(locations) ⇒ Object
- #signature_of(method) ⇒ Object
- #singleton_class_of(constant) ⇒ Object
- #superclass_of(constant) ⇒ Object
Instance Method Details
#ancestors_of(constant) ⇒ Object
60 61 62 |
# File 'lib/tapioca/runtime/reflection.rb', line 60 def ancestors_of(constant) ANCESTORS_METHOD.bind_call(constant) end |
#are_equal?(object, other) ⇒ Boolean
75 76 77 |
# File 'lib/tapioca/runtime/reflection.rb', line 75 def are_equal?(object, other) EQUAL_METHOD.bind_call(object, other) end |
#class_of(object) ⇒ Object
39 40 41 |
# File 'lib/tapioca/runtime/reflection.rb', line 39 def class_of(object) CLASS_METHOD.bind_call(object) end |
#constant_from_singleton_class(constant) ⇒ Object
177 178 179 180 |
# File 'lib/tapioca/runtime/reflection.rb', line 177 def constant_from_singleton_class(constant) constant_name = constant_name_from_singleton_class(constant) constantize(constant_name) if constant_name end |
#constant_name_from_singleton_class(constant) ⇒ Object
172 173 174 |
# File 'lib/tapioca/runtime/reflection.rb', line 172 def constant_name_from_singleton_class(constant) constant.to_s.match("#<Class:(.+)>")&.captures&.first end |
#constantize(symbol, inherit: false, namespace: Object) ⇒ Object
32 33 34 35 36 |
# File 'lib/tapioca/runtime/reflection.rb', line 32 def constantize(symbol, inherit: false, namespace: Object) namespace.const_get(symbol, inherit) rescue NameError, LoadError, RuntimeError, ArgumentError, TypeError nil end |
#constants_of(constant) ⇒ Object
44 45 46 |
# File 'lib/tapioca/runtime/reflection.rb', line 44 def constants_of(constant) CONSTANTS_METHOD.bind_call(constant, false) end |
#descendants_of(klass) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/tapioca/runtime/reflection.rb', line 150 def descendants_of(klass) result = ObjectSpace.each_object(klass.singleton_class).reject do |k| T.cast(k, Module).singleton_class? || T.unsafe(k) == klass end T.unsafe(result) end |
#inherited_ancestors_of(constant) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/tapioca/runtime/reflection.rb', line 95 def inherited_ancestors_of(constant) if Class === constant ancestors_of(superclass_of(constant) || Object) else Module.ancestors end end |
#method_of(constant, method) ⇒ Object
128 129 130 |
# File 'lib/tapioca/runtime/reflection.rb', line 128 def method_of(constant, method) METHOD_METHOD.bind_call(constant, method) end |
#name_of(constant) ⇒ Object
49 50 51 52 |
# File 'lib/tapioca/runtime/reflection.rb', line 49 def name_of(constant) name = NAME_METHOD.bind_call(constant) name&.start_with?("#<") ? nil : name end |
#name_of_type(type) ⇒ Object
123 124 125 |
# File 'lib/tapioca/runtime/reflection.rb', line 123 def name_of_type(type) type.to_s.gsub(/\bAttachedClass\b/, "T.attached_class") end |
#object_id_of(object) ⇒ Object
70 71 72 |
# File 'lib/tapioca/runtime/reflection.rb', line 70 def object_id_of(object) OBJECT_ID_METHOD.bind_call(object) end |
#private_instance_methods_of(constant) ⇒ Object
90 91 92 |
# File 'lib/tapioca/runtime/reflection.rb', line 90 def private_instance_methods_of(constant) PRIVATE_INSTANCE_METHODS_METHOD.bind_call(constant) end |
#protected_instance_methods_of(constant) ⇒ Object
85 86 87 |
# File 'lib/tapioca/runtime/reflection.rb', line 85 def protected_instance_methods_of(constant) PROTECTED_INSTANCE_METHODS_METHOD.bind_call(constant) end |
#public_instance_methods_of(constant) ⇒ Object
80 81 82 |
# File 'lib/tapioca/runtime/reflection.rb', line 80 def public_instance_methods_of(constant) PUBLIC_INSTANCE_METHODS_METHOD.bind_call(constant) end |
#qualified_name_of(constant) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/tapioca/runtime/reflection.rb', line 104 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
162 163 164 165 166 167 168 169 |
# File 'lib/tapioca/runtime/reflection.rb', line 162 def resolve_loc(locations) return "" unless locations resolved_loc = locations.find { |loc| REQUIRED_FROM_LABELS.include?(loc.label) } return "" unless resolved_loc resolved_loc.absolute_path || "" end |
#signature_of(method) ⇒ Object
116 117 118 119 120 |
# File 'lib/tapioca/runtime/reflection.rb', line 116 def signature_of(method) T::Utils.signature_for_method(method) rescue LoadError, StandardError nil end |
#singleton_class_of(constant) ⇒ Object
55 56 57 |
# File 'lib/tapioca/runtime/reflection.rb', line 55 def singleton_class_of(constant) SINGLETON_CLASS_METHOD.bind_call(constant) end |
#superclass_of(constant) ⇒ Object
65 66 67 |
# File 'lib/tapioca/runtime/reflection.rb', line 65 def superclass_of(constant) SUPERCLASS_METHOD.bind_call(constant) end |