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)
- UNDEFINED_CONSTANT =
T.let(Module.new.freeze, Module)
- 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
- #attached_class_of(singleton_class) ⇒ Object
- #class_of(object) ⇒ Object
- #constant_defined?(constant) ⇒ Boolean
- #constantize(symbol, inherit: false, namespace: Object) ⇒ Object
- #constants_of(constant) ⇒ Object
- #descendants_of(klass) ⇒ Object
- #file_candidates_for(constant) ⇒ 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
66 67 68 |
# File 'lib/tapioca/runtime/reflection.rb', line 66 def ancestors_of(constant) ANCESTORS_METHOD.bind_call(constant) end |
#are_equal?(object, other) ⇒ Boolean
81 82 83 |
# File 'lib/tapioca/runtime/reflection.rb', line 81 def are_equal?(object, other) EQUAL_METHOD.bind_call(object, other) end |
#attached_class_of(singleton_class) ⇒ Object
178 179 180 181 182 183 184 185 |
# File 'lib/tapioca/runtime/reflection.rb', line 178 def attached_class_of(singleton_class) # https://stackoverflow.com/a/36622320/98634 result = ObjectSpace.each_object(singleton_class).find do |klass| singleton_class_of(T.cast(klass, Module)) == singleton_class end T.cast(result, Module) end |
#class_of(object) ⇒ Object
45 46 47 |
# File 'lib/tapioca/runtime/reflection.rb', line 45 def class_of(object) CLASS_METHOD.bind_call(object) end |
#constant_defined?(constant) ⇒ Boolean
27 28 29 |
# File 'lib/tapioca/runtime/reflection.rb', line 27 def constant_defined?(constant) !UNDEFINED_CONSTANT.eql?(constant) end |
#constantize(symbol, inherit: false, namespace: Object) ⇒ Object
38 39 40 41 42 |
# File 'lib/tapioca/runtime/reflection.rb', line 38 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
50 51 52 |
# File 'lib/tapioca/runtime/reflection.rb', line 50 def constants_of(constant) CONSTANTS_METHOD.bind_call(constant, false) end |
#descendants_of(klass) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/tapioca/runtime/reflection.rb', line 156 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 |
#file_candidates_for(constant) ⇒ Object
188 189 190 191 192 |
# File 'lib/tapioca/runtime/reflection.rb', line 188 def file_candidates_for(constant) relevant_methods_for(constant).filter_map do |method| method.source_location&.first end.to_set end |
#inherited_ancestors_of(constant) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/tapioca/runtime/reflection.rb', line 101 def inherited_ancestors_of(constant) if Class === constant ancestors_of(superclass_of(constant) || Object) else Module.ancestors end end |
#method_of(constant, method) ⇒ Object
134 135 136 |
# File 'lib/tapioca/runtime/reflection.rb', line 134 def method_of(constant, method) METHOD_METHOD.bind_call(constant, method) end |
#name_of(constant) ⇒ Object
55 56 57 58 |
# File 'lib/tapioca/runtime/reflection.rb', line 55 def name_of(constant) name = NAME_METHOD.bind_call(constant) name&.start_with?("#<") ? nil : name end |
#name_of_type(type) ⇒ Object
129 130 131 |
# File 'lib/tapioca/runtime/reflection.rb', line 129 def name_of_type(type) type.to_s.gsub(/\bAttachedClass\b/, "T.attached_class") end |
#object_id_of(object) ⇒ Object
76 77 78 |
# File 'lib/tapioca/runtime/reflection.rb', line 76 def object_id_of(object) OBJECT_ID_METHOD.bind_call(object) end |
#private_instance_methods_of(constant) ⇒ Object
96 97 98 |
# File 'lib/tapioca/runtime/reflection.rb', line 96 def private_instance_methods_of(constant) PRIVATE_INSTANCE_METHODS_METHOD.bind_call(constant) end |
#protected_instance_methods_of(constant) ⇒ Object
91 92 93 |
# File 'lib/tapioca/runtime/reflection.rb', line 91 def protected_instance_methods_of(constant) PROTECTED_INSTANCE_METHODS_METHOD.bind_call(constant) end |
#public_instance_methods_of(constant) ⇒ Object
86 87 88 |
# File 'lib/tapioca/runtime/reflection.rb', line 86 def public_instance_methods_of(constant) PUBLIC_INSTANCE_METHODS_METHOD.bind_call(constant) end |
#qualified_name_of(constant) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/tapioca/runtime/reflection.rb', line 110 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
168 169 170 171 172 173 174 175 |
# File 'lib/tapioca/runtime/reflection.rb', line 168 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
122 123 124 125 126 |
# File 'lib/tapioca/runtime/reflection.rb', line 122 def signature_of(method) T::Utils.signature_for_method(method) rescue LoadError, StandardError nil end |
#singleton_class_of(constant) ⇒ Object
61 62 63 |
# File 'lib/tapioca/runtime/reflection.rb', line 61 def singleton_class_of(constant) SINGLETON_CLASS_METHOD.bind_call(constant) end |
#superclass_of(constant) ⇒ Object
71 72 73 |
# File 'lib/tapioca/runtime/reflection.rb', line 71 def superclass_of(constant) SUPERCLASS_METHOD.bind_call(constant) end |