Skip to content
Arian Treffer edited this page Dec 22, 2013 · 4 revisions

Release

Download: central
Maven:

<dependency>
    <groupId>org.cthul</groupId>
    <artifactId>cthul-objects</artifactId>
    <version>1.1.0</version>
</dependency>

Signatures

Find the best match of an overloaded method for given arguments

Method m = Signatures.bestMethod(Foo.class, "methodName", "arg1", 2);

or for given argument types.

Method m = Signatures.bestMethod(Foo.class, "methodName", String.class, int.class);

The algorithm chooses the best method according to the Java Specification and supports null arguments, boxing, and varargs. If there is more than one best match, an exception is thrown. To avoid exceptions, all best matches can be collected at once.

Method[] m = Signatures.candidateMethods(Foo.class, "methodName", "arg1", 2);

By default, only public methods are considered. The following example shows how to search for all non-static methods:

Method[] methods = Signatures.collectMethods(Foo.class, "methodName", Signatures.ANY, Signatures.STATIC);
Method m = Signatures.bestMethod(methods, "arg1", 2);

A specialized invoke can be used to ensure that methods with varargs are called correctly.

Object result = Signatures.invoke(theFoo, m, "arg1", 2);

All functions are also available for constructors.

Types

Methods for dealing with type hierarchies

  • getSuperclasses(Class) collects all super classes and interfaces
  • commonSuperclasses(Class[]) collects all classes and interfaces that are extended/implemented by all given classes
  • lowestClasses(Class[]) returns all classes from the input that do not have a subclass in that list
  • lowestCommonSuperclasses(Class[]) returns the lowest common super classes of the input

Boxing

Boxing and unboxing of values and (deep) arrays.

Object[][] data = {{1L, 2.0}, {true, ' '}};
int[][] result = Boxing.as(data, int[][].class);
// result == {{1, 2}, {1, 32}}
Clone this wiki locally