Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/evant/value-processor
Helper for creating annotation processors that create/read value objects.
https://github.com/evant/value-processor
Last synced: about 1 month ago
JSON representation
Helper for creating annotation processors that create/read value objects.
- Host: GitHub
- URL: https://github.com/evant/value-processor
- Owner: evant
- License: apache-2.0
- Created: 2018-02-18T23:36:12.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2020-06-14T22:56:01.000Z (over 4 years ago)
- Last Synced: 2023-07-26T23:25:05.158Z (over 1 year ago)
- Language: Kotlin
- Size: 75.2 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# value-processor
Helper for creating annotation processors that create/read value objects.## Download
### Gradle
```groovy
dependencies {
compile 'me.tatarka.value:value-processor:0.2'
}
```### Maven
```xml
me.tatarka.value
value-processor
0.2```
## Usage
First you create a `ValueCreator` with the processing environment.
```java
public class MyProcessor extends AbstractProcessor {private ValueCreator valueCreator;
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
valueCreator = new ValueCreator(processingEnv);
}
```Then create a `Value` from an `Element`. There are various methods based on what you have annotated. For example, the below creates it from the target class.
```java
@Override
public boolean process(Set extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement annotation : annotations) {
Set extends Element> elements = roundEnv.getElementsAnnotatedWith(annotation);
for (Element element : elements) {
try {
Value value = valueCreator.fromClass((TypeElement) element);
} catch (ElementException e) {
e.printMessage(messenger);
}
}
}
return false;
}
```You can then iterate through the properties to generate your code.
```java
// all properties
for (Property> property : value.getProperties()) {
...
}
// of a specific kind
for (Property.Getter getter : value.getProperties().getGetters()) {
...
}
```## Supported Classes
Note: Classes are expected to be treated as immutable. This meas you can only create instances with properties or read properties on and instance.### Fields
```java
public class Foo {
public String bar;
}
```
### Getters
```java
public class Foo {
public String getBar() { ... }
}
```
### Constructor Params
```java
public class Foo {
public Foo(String bar) { ... }
}
```
### Factory Method Params
```java
public static Foo createFoo(String bar) { ... }
```
### Builder
```java
public class FooBuilder {
public FooBuilder setBar(String bar) { ... }
public Foo build() { ... }
}
```
### Builder Constructor Params
```java
public class FooBuilder {
public FooBuilder(String bar) { ... }
public Foo build() { ... }
}
```
### Builder Factory Params
```java
public static FooBuilder builder(String bar) { ... }
```
### Kotlin Data Classes
```kotlin
data class Foo(val bar: String)
```