Skip to content
Anjunar/ DOCS

Methods

Methods are listed once per signature: the most specific override wins, bridges and synthetic methods are left out, and return types are resolved against the starting type.

Lookup

Public in the hierarchy, or anything on the class

findMethod matches public methods like Class.getMethod, inherited ones included. findDeclaredMethod finds any method of the class itself, private ones included. Parameters are given as erased classes.

scala
class User(val name: String) { def greeting(prefix: String): String = s"$prefix $name" private def secret(): String = "hidden" } val user = TypeResolver.resolve(classOf[User]) println(user.findMethod("greeting", classOf[String]).returnType.name) // String println(user.findMethod("secret")) // null println(user.findDeclaredMethod("secret").name) // secret
Overrides

One entry per signature

overrides lists the methods of supertypes with the same name and parameter types. methods leaves every one of them out, so iterating methods never sees a signature twice.

scala
trait Greeter { def greet(name: String): String } class Polite extends Greeter { def greet(name: String) = s"Good day, $name" } val greet = TypeResolver.resolve(classOf[Polite]).findMethod("greet", classOf[String]) println(greet.owner.name) // Polite println(greet.overrides.map(_.owner.name).mkString(", ")) // Greeter
Annotations from the interface

annotations of a method includes the declared annotations of everything it overrides. An annotation on the trait's method is found on the implementation.

Invocation

invoke

invoke makes the method accessible and calls it with the given arguments. Exceptions thrown by the method arrive wrapped in InvocationTargetException, as with plain reflection.

scala
val ada = new User("Ada") println(user.findMethod("greeting", classOf[String]).invoke(ada, "Hello")) // Hello Ada println(user.findDeclaredMethod("secret").invoke(ada)) // hidden
Scala

What methods a Scala class has

Accessors, setters and compiler-generated members are ordinary methods to the JVM and appear in methods.

val name
Method name().
var name
Methods name() and name_$eq(value).
case class
copy, productArity, productElement, equals, hashCode, toString and _1, _2 …
default arguments
Methods like greeting$default$1 for each default.