Kuroko class generator.
Add @Kuroko
annotation to your bean class.
package com.example.domain;
import io.disc99.kuroko.annotation.Kuroko;
@Kuroko
public class User {
UserId id;
Name name;
Integer age;
public User(UserId id, Name name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
}
@Kuroko
public class UserId {
String val;
public UserId(String val) {
this.val = val;
}
}
@Kuroko
public class Name {
String first;
String last;
public Name(String first, String last) {
this.first = first;
this.last = last;
}
}
package com.example.domain;
import java.lang.reflect.Field;
/**
* {@link com.example.domain.User } meta class.
*
* <p>
* Don't modify this file!
* This file is automatically generated for annotation processor.
*/
public class _User {
private final User bean;
public _User(User bean) {
this.bean = bean;
}
public com.example.domain.UserId getId() {
try {
Field field = User.class.getDeclaredField("id");
field.setAccessible(true);
return (com.example.domain.UserId) field.get(bean);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public com.example.domain.UserId id() {
try {
Field field = User.class.getDeclaredField("id");
field.setAccessible(true);
return (com.example.domain.UserId) field.get(bean);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public com.example.domain._UserId get_id() {
return new com.example.domain._UserId(getId());
}
public com.example.domain._UserId _id() {
return new com.example.domain._UserId(getId());
}
public void setId(com.example.domain.UserId value) {
try {
Field field = User.class.getDeclaredField("id");
field.setAccessible(true);
field.set(bean, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void id(com.example.domain.UserId value) {
try {
Field field = User.class.getDeclaredField("id");
field.setAccessible(true);
field.set(bean, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public com.example.domain.Name getName() {
try {
Field field = User.class.getDeclaredField("name");
field.setAccessible(true);
return (com.example.domain.Name) field.get(bean);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public com.example.domain.Name name() {
try {
Field field = User.class.getDeclaredField("name");
field.setAccessible(true);
return (com.example.domain.Name) field.get(bean);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public com.example.domain._Name get_name() {
return new com.example.domain._Name(getName());
}
public com.example.domain._Name _name() {
return new com.example.domain._Name(getName());
}
public void setName(com.example.domain.Name value) {
try {
Field field = User.class.getDeclaredField("name");
field.setAccessible(true);
field.set(bean, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void name(com.example.domain.Name value) {
try {
Field field = User.class.getDeclaredField("name");
field.setAccessible(true);
field.set(bean, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public java.lang.Integer getAge() {
try {
Field field = User.class.getDeclaredField("age");
field.setAccessible(true);
return (java.lang.Integer) field.get(bean);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public java.lang.Integer age() {
try {
Field field = User.class.getDeclaredField("age");
field.setAccessible(true);
return (java.lang.Integer) field.get(bean);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void setAge(java.lang.Integer value) {
try {
Field field = User.class.getDeclaredField("age");
field.setAccessible(true);
field.set(bean, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void age(java.lang.Integer value) {
try {
Field field = User.class.getDeclaredField("age");
field.setAccessible(true);
field.set(bean, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
// and generated _UserId, _Name classes
// The domain class does not have an accessor.
// domainUser.getAge() -> compile error
User domainUser = new User(
new UserId("99"),
new Name("Tom", "Simpson"),
20
);
// Use kuroko class
_User user = new _User(domainUser);
// Kuroko class provides an accessor for accessing private fields in reflection.
user.getAge(); // 20
user.setAge(21);
user.getAge(); // 21
user.age(22);
user.age(); // 22
//If the acquisition target is Kuroko class, it can be obtained with "_" prefixed accessor
user.get_name().getFirst(); // Tom
user._name().last(); // Simpson
- Add Maven repository:
http://dl.bintray.com/disc99/maven
- Add dependency:
io.disc99.kuroko:annotation:${version}
,io.disc99.kuroko:processor:${version}
Configuration example for Gradle + IntelliJ IDEA:
// Add dependencies
repositories {
maven {
url 'http://dl.bintray.com/disc99/maven'
}
}
dependencies {
compile 'io.disc99.kuroko:annotation:0.1.1'
compileOnly 'io.disc99.kuroko:processor:0.1.1'
}
// IntelliJ IDEA settings(It is necessary to check `Enable annotation processing` from the IDE preference in addition)
apply plugin: 'idea'
idea {
module {
sourceDirs += file('build/classes/main/generated')
generatedSourceDirs += file('build/classes/main/generated')
}
}
- JDK 8 +
(The MIT License)
Copyright (c) 2014 @disc99