If the module is 3rd-party and/or is already compiled, please refer to the answer provided by @Dogbert.
If the module is owned, the requested information might be collected on compilation stage using @on_definition
hook:
defmodule TestInfo do
def on_definition(_env, kind, name, args, guards, body) do
with {:ok, table} <- :dets.open_file(:test_info, type: :set) do
clauses =
case :dets.lookup(table, name) do
{:error, _reason} -> []
[] -> []
list when is_list(list) -> list[name]
end
:dets.insert(table, {name, [{kind, args, guards} | clauses]})
:dets.close(table)
end
end
end
defmodule Test do
@on_definition {TestInfo, :on_definition} # ⇐ THIS
def greet(name) when name == "foo" do
IO.puts("Hello, bar")
end
def greet(name), do: IO.puts("Hello, #{name}")
end
Now you have all the definitions stored in DETS:
{:ok, table} = :dets.open_file(:test_info, type: :set)
:dets.lookup(table, :greet)
#⇒ [
# greet: [
# {:def, [{:name, [line: 10], nil}], []},
# {:def, [{:name, [line: 6], nil}],
# [{:==, [line: 6], [{:name, [line: 6], nil}, "foo"]}]}]
# ]
:dets.close(table)
I used DETS to store the info because it’s being stored on compilation stage and the typical usage would be in runtime.