Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shoyu666/angen
annotion process & code gen demo
https://github.com/shoyu666/angen
Last synced: 25 days ago
JSON representation
annotion process & code gen demo
- Host: GitHub
- URL: https://github.com/shoyu666/angen
- Owner: shoyu666
- Created: 2019-06-01T11:18:11.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-04T22:00:05.000Z (over 3 years ago)
- Last Synced: 2023-11-08T08:57:26.563Z (almost 1 year ago)
- Language: Java
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Java Annotation Processor Demo
```java
@Builder
public interface Person {
@NotNull(message = "name should not be null")
String name();
@NotNull(message = "age should not be null")
int age();
@NotNull(message = "male should not be null")
boolean male();
List books();
}
```
生成```java
// This codes are generated automatically. Do not modify!
package com.xining.annotation;import com.xining.sample2.Book;
import java.lang.IllegalAccessException;
import java.lang.NoSuchFieldException;
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import javax.validation.constraints.NotNull;public class PersonBuilder implements InvocationHandler {
private HashMap fieldMap = new HashMap();@NotNull(
message = "name should not be null"
)
private String name;@NotNull(
message = "age should not be null"
)
private int age;@NotNull(
message = "male should not be null"
)
private boolean male;private List books;
private PersonBuilder() {
}public static PersonBuilder of() {
return new PersonBuilder();
}public static PersonBuilder of(Person t) {
return new PersonBuilder().name(t.name()).age(t.age()).male(t.male()).books(t.books());
}@Override
public Object invoke(Object proxy, Method method, Object[] args) throws NoSuchFieldException, IllegalAccessException {
String methodName = method.getName();
Object value = fieldMap.get(methodName);
if(value == null) {
Field field = this.getClass().getDeclaredField(methodName);
field.setAccessible(true);
fieldMap.put(methodName, value = field.get(this));
}
return value;
}PersonBuilder name(String name) {
this.name = name;
return this;
}PersonBuilder age(int age) {
this.age = age;
return this;
}PersonBuilder male(boolean male) {
this.male = male;
return this;
}PersonBuilder books(List books) {
this.books = books;
return this;
}Person build() {
ValidatorUtil.validate(this);
return ProxyUtil.proxy(this,Person.class);
}
}```