v6/site/posts/2016-07-28-introducing-mirr...

4.7 KiB

title = "Introducing Mirror"
tags = ["java"]
date = "2016-07-28 16:45:00 -0400"
old_permalink = ["/java/2016/07/28/introducing-mirror/", "/java/2016/introducing-mirror/"]
short_desc = "Allow me to introduce my latest project, Mirror. Mirror is a reflection library for Java designed to take advantage of the streams, lambdas, and optionals introduced in Java 8."

Allow me to introduce my latest project, Mirror. Mirror is a reflection library for Java designed to take advantage of the streams, lambdas, and optionals introduced in Java 8.

The source code is publicly available on GitHub under the MIT license and the JavaDocs are viewable here.

Installation

All version of Mirror are available on my Maven.

Maven

<repositories>
	<repository>
		<id>shadowfacts</id>
		<url>http://mvn.rx14.co.uk/shadowfacts/</url>
	</repository>
</repositories>

<dependency>
	<groupId>net.shadowfacts</groupId>
	<artifactId>Mirror</artifactId>
	<version>1.0.0</version>
</dependency>

Gradle

repositories {
	maven {
		name "shadowfacts"
		url "http://mvn.rx14.co.uk/shadowfacts/"
	}
}

dependencies {
	compile group: "net.shadowfacts", name: "Mirror", version: "1.0.0"
}

Usage

A couple of simple examples for getting started with Mirror.

For more complex examples of everything possible with Mirror, you can look at the unit tests.

General Overview

The Mirror.of methods are used to retrieve mirrors on which operations can be performed. The types of mirrors are:

The Mirror.ofAll methods are used to create mirror stream wrappers for a given stream/collection/array of reflection objects or mirrors.

These examples will use the following classes:

public class Test {
	public static String name = "Mirror";
	public static String author;

	public static String reverse(String str) {
		return new StringBuilder(str).reverse().toString();
	}
}

public class Test2 {
	public static String name = "Test 2";

	public static void doSomething() {
	}
}

Getting Fields

// get the field
Optional<MirrorField> optional = Mirror.of(Test.class).field("name");
// unwrap the optional
MirrorField field = optional.get();
// get the value of the field
// we pass null as the instance because the field is static
field.get(null); // "Mirror"

Setting Fields

// get the field
Optional<MirrorField> optional = Mirror.of(Test.class).field("author");
// unwrap the optional
MirrorField field = optional.get();
// set the value of the field
// we once again pass null as the instance because the field is static
field.set(null, "Shadowfacts");

Invoking Methods

// get the method using the name and the types of the arguments it accepts
Optional<MirrorMethod> optional = Mirror.of(Test.class).method("reverse", String.class);
// unwrap the optional
MirrorMethod method = optional.get();
// invoke the method
method.invoke(null, "Mirror"); // "rorriM";

Class Streams

Mirror.ofAllUnwrapped(Test.class, Test2.class) // create the stream of classes
	.unwrap() // map the MirrorClasses to their Java versions
	.toArray(); // [Test.class, Test2.class]

Field Streams

Mirror.ofAllUnwrapped(Test.class, Test2.class) // create the stream of classes
	.flatMapToFields() // flat map the classes to their fields
	.get(null) // get the value of the fields on null
	.toArray(); // ["Mirror", "Shadowfacts", "Tesst 2"]

Method Streams

Mirror.ofAllUnwrapped(Test.class, Test2.class)  // create the stream of classes
	.flatMapToMethods() // flat map the classes to their methods
	.filter(m -> Arrays.equals(m.parameterTypes(), new MirrorClass<?>[]{Mirror.of(String.class)})) // filter the methods by which accept only a String
	.invoke(null, "Shadowfacts") // invoke them all on nothing, passing in "Shadowfacts"
	.toArray(); // ["stcafwodahS"]