Compare commits
174 Commits
record-bui
...
record-bui
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1dd00b2c65 | ||
|
|
3b34b5dee3 | ||
|
|
7248bad2bd | ||
|
|
7e494d8753 | ||
|
|
3954499d4b | ||
|
|
13959dee2a | ||
|
|
af759c0570 | ||
|
|
9a7d73e78c | ||
|
|
3b8c3ff9e3 | ||
|
|
9943667af1 | ||
|
|
b0c8f10711 | ||
|
|
0d3c2f37c1 | ||
|
|
eabcb2f179 | ||
|
|
5fef81191d | ||
|
|
8dbec027e4 | ||
|
|
ef09d68b78 | ||
|
|
0a608f3b20 | ||
|
|
3494d01b2b | ||
|
|
584bdf4a25 | ||
|
|
9268c295fe | ||
|
|
0d2fe2ffe7 | ||
|
|
71e22042e9 | ||
|
|
3c8e305002 | ||
|
|
76f93039b0 | ||
|
|
5f82af97ae | ||
|
|
896aab18aa | ||
|
|
75010d0add | ||
|
|
26a6141bef | ||
|
|
cdd9009fa8 | ||
|
|
0624a6caca | ||
|
|
238ef872bf | ||
|
|
ef2a3692d1 | ||
|
|
cb9ceb0529 | ||
|
|
bd8fbeb045 | ||
|
|
5b10284d4e | ||
|
|
49a19ca8f8 | ||
|
|
f9fcd1e540 | ||
|
|
eebb2b2bfd | ||
|
|
67f219bd67 | ||
|
|
d9f27bc5a6 | ||
|
|
f7b6f47bbf | ||
|
|
7e90e64988 | ||
|
|
cbe3b7cef4 | ||
|
|
6c15bdd825 | ||
|
|
53aaf6b7e9 | ||
|
|
09ed5905e3 | ||
|
|
41f59e704f | ||
|
|
d0c56e6fdf | ||
|
|
2f3477a1ca | ||
|
|
091e9493b6 | ||
|
|
a0c03e88cc | ||
|
|
d21953c39a | ||
|
|
d9d98e765e | ||
|
|
5320c8dfa4 | ||
|
|
5d7bea565f | ||
|
|
b7c4e22066 | ||
|
|
19803592a2 | ||
|
|
9e25c6fa47 | ||
|
|
c16e3bb14b | ||
|
|
57e5d43b07 | ||
|
|
feecc29eea | ||
|
|
428d3c0378 | ||
|
|
61d355b9fc | ||
|
|
2beafc4803 | ||
|
|
82b3925618 | ||
|
|
ba90e6cdca | ||
|
|
07e52035ee | ||
|
|
07fc606147 | ||
|
|
677813e875 | ||
|
|
7d877963fb | ||
|
|
96bb6ef9f3 | ||
|
|
6f3046f507 | ||
|
|
7564643556 | ||
|
|
dfd76fc58b | ||
|
|
04e9135591 | ||
|
|
b512a6e968 | ||
|
|
5a1cd35320 | ||
|
|
a615e3abb6 | ||
|
|
3f8bb47cbf | ||
|
|
5a8e72f0e9 | ||
|
|
44ad4531b6 | ||
|
|
7e78d32780 | ||
|
|
0dc4aa7657 | ||
|
|
b6d9a6202f | ||
|
|
b21368f32f | ||
|
|
501da86afd | ||
|
|
13d867e6e6 | ||
|
|
a1206fa57f | ||
|
|
b89722ebfe | ||
|
|
75163f53ed | ||
|
|
9855e7b504 | ||
|
|
aa3bdedf28 | ||
|
|
6d5e15baa1 | ||
|
|
9c8e3626ba | ||
|
|
24b85e7ad5 | ||
|
|
861e2e745a | ||
|
|
1fc7c9a4b3 | ||
|
|
570514e077 | ||
|
|
9d8b9e65bc | ||
|
|
93d6204b76 | ||
|
|
15e5bfccc6 | ||
|
|
67c54244c5 | ||
|
|
870ac4a9d9 | ||
|
|
9ee8b5912a | ||
|
|
6d9bcf27da | ||
|
|
44db5fdf17 | ||
|
|
90a65235a9 | ||
|
|
7e8ddbd700 | ||
|
|
3a534fbea9 | ||
|
|
6d7ebe2545 | ||
|
|
f1e47391c8 | ||
|
|
c999d0ba06 | ||
|
|
e0243c8b1c | ||
|
|
65bbbaea05 | ||
|
|
5ae03a2c66 | ||
|
|
437e314799 | ||
|
|
a870beee21 | ||
|
|
5b879743ef | ||
|
|
c7bdafb0b9 | ||
|
|
d67c62ed3b | ||
|
|
39cf2b0353 | ||
|
|
6813b88f8d | ||
|
|
54662d69c7 | ||
|
|
7811ff8823 | ||
|
|
4c5076690d | ||
|
|
39a800f10e | ||
|
|
610081b27e | ||
|
|
1eb91d612e | ||
|
|
7c84f26972 | ||
|
|
82cc4f4cad | ||
|
|
f16e1b1d0e | ||
|
|
c92bf78ec5 | ||
|
|
4c4baa015f | ||
|
|
81b7b93a5b | ||
|
|
c39983e342 | ||
|
|
400caa2943 | ||
|
|
a2edd7299f | ||
|
|
6661c2ae0e | ||
|
|
74c8480b43 | ||
|
|
8dbdb43391 | ||
|
|
44064d656e | ||
|
|
791eb02faf | ||
|
|
aa3aefa39c | ||
|
|
de6946030f | ||
|
|
091c663520 | ||
|
|
6a45a2cbd9 | ||
|
|
c47e290363 | ||
|
|
d7abf4c60d | ||
|
|
6fbb0d0330 | ||
|
|
e235fc078f | ||
|
|
cf8f277018 | ||
|
|
e8e74cce1a | ||
|
|
3b425d4dce | ||
|
|
395f0879ac | ||
|
|
8969a17053 | ||
|
|
5ef0662d99 | ||
|
|
c6de55d5ad | ||
|
|
d19a0c2dc5 | ||
|
|
feb334e6ea | ||
|
|
f812e173e3 | ||
|
|
d50303ca09 | ||
|
|
13319e643b | ||
|
|
83802125aa | ||
|
|
b1e343f733 | ||
|
|
75fce6bb84 | ||
|
|
c676436f5e | ||
|
|
b861d0f4b1 | ||
|
|
933098b07d | ||
|
|
8560666ec6 | ||
|
|
85070cc106 | ||
|
|
7ac840ae2a | ||
|
|
19017e6693 | ||
|
|
6d36b86c68 | ||
|
|
417ce0e139 |
3
.github/FUNDING.yml
vendored
Normal file
3
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
github: Randgalt
|
||||
24
.github/workflows/maven_java16.yml
vendored
Normal file
24
.github/workflows/maven_java16.yml
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
# This workflow will build a Java project with Maven
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||
|
||||
name: Maven Build - Java 16
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up JDK
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 16
|
||||
- name: Build with Maven
|
||||
run: mvn -B package --file pom.xml
|
||||
24
.github/workflows/maven_java17.yml
vendored
Normal file
24
.github/workflows/maven_java17.yml
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
# This workflow will build a Java project with Maven
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||
|
||||
name: Maven Build - Java 17
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up JDK
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 17
|
||||
- name: Build with Maven
|
||||
run: mvn -B package --file pom.xml
|
||||
376
README.md
376
README.md
@@ -1,10 +1,29 @@
|
||||
# RecordBuilder - Early Access
|
||||
[](https://github.com/Randgalt/record-builder/actions)
|
||||
[](https://search.maven.org/search?q=g:io.soabase.record-builder%20a:record-builder)
|
||||
|
||||
# RecordBuilder
|
||||
|
||||
## What is RecordBuilder
|
||||
|
||||
Java 14 is introducing [Records](https://cr.openjdk.java.net/~briangoetz/amber/datum.html) as a preview feature. Since Java 9, features in Java are being released in stages. While the Java 14 version of records is fantastic, it's currently missing an important feature for data classes: a builder. This project is an annotation processor that creates companion builder classes for Java records.
|
||||
Java 16 introduces [Records](https://openjdk.java.net/jeps/395). While this version of records is fantastic,
|
||||
it's currently missing some important features normally found in data classes: a builder
|
||||
and "with"ers. This project is an annotation processor that creates:
|
||||
|
||||
- a companion builder class for Java records
|
||||
- an interface that adds "with" copy methods
|
||||
- an annotation that generates a Java record from an Interface template
|
||||
|
||||
## Example
|
||||
_Details:_
|
||||
|
||||
- [RecordBuilder Details](#RecordBuilder-Example)
|
||||
- [Wither Details](#Wither-Example)
|
||||
- [RecordBuilder Full Definition](#Builder-Class-Definition)
|
||||
- [Record From Interface Details](#RecordInterface-Example)
|
||||
- [Generation Via Includes](#generation-via-includes)
|
||||
- [Usage](#usage)
|
||||
- [Customizing](customizing.md) (e.g. add immutable collections, etc.)
|
||||
|
||||
## RecordBuilder Example
|
||||
|
||||
```java
|
||||
@RecordBuilder
|
||||
@@ -15,141 +34,318 @@ This will generate a builder class that can be used ala:
|
||||
|
||||
```java
|
||||
// build from components
|
||||
var n1 = NameAndAgeBuilder.builder().name(aName).age(anAge).build();
|
||||
NameAndAge n1 = NameAndAgeBuilder.builder().name(aName).age(anAge).build();
|
||||
|
||||
// generate a copy with a changed value
|
||||
var n2 = NameAndAgeBuilder.builder(n1).age(newAge).build(); // name is the same as the name in n1
|
||||
NameAndAge n2 = NameAndAgeBuilder.builder(n1).age(newAge).build(); // name is the same as the name in n1
|
||||
|
||||
// pass to other methods to set components
|
||||
var builder = new NameAndAgeBuilder();
|
||||
setName(builder);
|
||||
setAge(builder);
|
||||
var n3 = builder.build();
|
||||
NameAndAge n3 = builder.build();
|
||||
|
||||
// use the generated static constructor/builder
|
||||
import static NameAndAgeBuilder.NameAndAge;
|
||||
...
|
||||
var n4 = NameAndAge("hey", 42);
|
||||
```
|
||||
|
||||
## Wither Example
|
||||
|
||||
```java
|
||||
@RecordBuilder
|
||||
public record NameAndAge(String name, int age) implements NameAndAgeBuilder.With {}
|
||||
```
|
||||
|
||||
In addition to creating a builder, your record is enhanced by "wither" methods ala:
|
||||
|
||||
```java
|
||||
NameAndAge r1 = new NameAndAge("foo", 123);
|
||||
NameAndAge r2 = r1.withName("bar");
|
||||
NameAndAge r3 = r2.withAge(456);
|
||||
|
||||
// access the builder as well
|
||||
NameAndAge r4 = r3.with().age(101).name("baz").build();
|
||||
|
||||
// alternate method of accessing the builder (note: no need to call "build()")
|
||||
NameAndAge r5 = r4.with(b -> b.age(200).name("whatever"));
|
||||
|
||||
// perform some logic in addition to changing values
|
||||
NameAndAge r5 = r4.with(b -> {
|
||||
if (b.age() > 13) {
|
||||
b.name("Teen " + b.name());
|
||||
} else {
|
||||
b.name("whatever"));
|
||||
}
|
||||
});
|
||||
|
||||
// or, if you cannot add the "With" interface to your record...
|
||||
NameAndAge r6 = NameAndAgeBuilder.from(r5).with(b -> b.age(200).name("whatever"));
|
||||
NameAndAge r7 = NameAndAgeBuilder.from(r5).withName("boop");
|
||||
```
|
||||
|
||||
_Hat tip to [Benji Weber](https://benjiweber.co.uk/blog/2020/09/19/fun-with-java-records/) for the Withers idea._
|
||||
|
||||
## Builder Class Definition
|
||||
|
||||
(Note: you can see a builder class built using `@RecordBuilderFull` here: [SingleItemsBuilder.java](https://gist.github.com/Randgalt/8aa487a847ea2acdd76d702f7cf17d6a))
|
||||
|
||||
The full builder class is defined as:
|
||||
|
||||
```java
|
||||
public class NameAndAgeBuilder {
|
||||
private String name;
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
private int age;
|
||||
|
||||
private NameAndAgeBuilder() {
|
||||
}
|
||||
private NameAndAgeBuilder() {
|
||||
}
|
||||
|
||||
private NameAndAgeBuilder(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
private NameAndAgeBuilder(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static constructor/builder. Can be used instead of new NameAndAge(...)
|
||||
*/
|
||||
public static NameAndAge NameAndAge(String name, int age) {
|
||||
return new NameAndAge(name, age);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new builder with all fields set to default Java values
|
||||
*/
|
||||
public static NameAndAgeBuilder builder() {
|
||||
return new NameAndAgeBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new builder with all fields set to the values taken from the given record instance
|
||||
*/
|
||||
public static NameAndAgeBuilder builder(NameAndAge from) {
|
||||
return new NameAndAgeBuilder(from.name(), from.age());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a "with"er for an existing record instance
|
||||
*/
|
||||
public static NameAndAgeBuilder.With from(NameAndAge from) {
|
||||
return new NameAndAgeBuilder.With() {
|
||||
@Override
|
||||
public String name() {
|
||||
return from.name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int age() {
|
||||
return from.age();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a stream of the record components as map entries keyed with the component name and the value as the component value
|
||||
*/
|
||||
public static Stream<Map.Entry<String, Object>> stream(NameAndAge record) {
|
||||
return Stream.of(Map.entry("name", record.name()),
|
||||
Map.entry("age", record.age()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new record instance with all fields set to the current values in this builder
|
||||
*/
|
||||
public NameAndAge build() {
|
||||
return new NameAndAge(name, age);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NameAndAgeBuilder[name=" + name + ", age=" + age + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, age);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return (this == o) || ((o instanceof NameAndAgeBuilder r)
|
||||
&& Objects.equals(name, r.name)
|
||||
&& (age == r.age));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a new value for the {@code name} record component in the builder
|
||||
*/
|
||||
public NameAndAgeBuilder name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current value for the {@code name} record component in the builder
|
||||
*/
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a new value for the {@code age} record component in the builder
|
||||
*/
|
||||
public NameAndAgeBuilder age(int age) {
|
||||
this.age = age;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current value for the {@code age} record component in the builder
|
||||
*/
|
||||
public int age() {
|
||||
return age;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add withers to {@code NameAndAge}
|
||||
*/
|
||||
public interface With {
|
||||
/**
|
||||
* Return the current value for the {@code name} record component in the builder
|
||||
*/
|
||||
String name();
|
||||
|
||||
/**
|
||||
* Return the current value for the {@code age} record component in the builder
|
||||
*/
|
||||
int age();
|
||||
|
||||
/**
|
||||
* Return a new record builder using the current values
|
||||
*/
|
||||
default NameAndAgeBuilder with() {
|
||||
return new NameAndAgeBuilder(name(), age());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new builder with all fields set to default Java values
|
||||
* Return a new record built from the builder passed to the given consumer
|
||||
*/
|
||||
public static NameAndAgeBuilder builder() {
|
||||
return new NameAndAgeBuilder();
|
||||
default NameAndAge with(Consumer<NameAndAgeBuilder> consumer) {
|
||||
NameAndAgeBuilder builder = with();
|
||||
consumer.accept(builder);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new builder with all fields set to the values taken from the given record instance
|
||||
* Return a new instance of {@code NameAndAge} with a new value for {@code name}
|
||||
*/
|
||||
public static NameAndAgeBuilder builder(NameAndAge from) {
|
||||
return new NameAndAgeBuilder(from.name(), from.age());
|
||||
default NameAndAge withName(String name) {
|
||||
return new NameAndAge(name, age());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new record instance with all fields set to the current values in this builder
|
||||
* Return a new instance of {@code NameAndAge} with a new value for {@code age}
|
||||
*/
|
||||
public NameAndAge build() {
|
||||
return new NameAndAge(name, age);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a new value for this record component in the builder
|
||||
*/
|
||||
public NameAndAgeBuilder name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a new value for this record component in the builder
|
||||
*/
|
||||
public NameAndAgeBuilder age(int age) {
|
||||
this.age = age;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NameAndAgeBuilder[name=" + name + ", age=" + age + "]";
|
||||
default NameAndAge withAge(int age) {
|
||||
return new NameAndAge(name(), age);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## RecordInterface Example
|
||||
|
||||
```java
|
||||
@RecordInterface
|
||||
public interface NameAndAge {
|
||||
String name();
|
||||
int age();
|
||||
}
|
||||
```
|
||||
|
||||
This will generate a record ala:
|
||||
|
||||
```java
|
||||
@RecordBuilder
|
||||
public record NameAndAgeRecord(String name, int age) implements
|
||||
NameAndAge, NameAndAgeRecordBuilder.With {}
|
||||
```
|
||||
|
||||
Note that the generated record is annotated with `@RecordBuilder` so a record
|
||||
builder is generated for the new record as well.
|
||||
|
||||
Notes:
|
||||
|
||||
- Non static methods in the interface...
|
||||
- ...cannot have arguments
|
||||
- ...must return a value
|
||||
- ...cannot have type parameters
|
||||
- Methods with default implementations are used in the generation unless they are annotated with `@IgnoreDefaultMethod`
|
||||
- If you do not want a record builder generated, annotate your interface as `@RecordInterface(addRecordBuilder = false)`
|
||||
- If your interface is a JavaBean (e.g. `getThing()`, `isThing()`) the "get" and "is" prefixes are
|
||||
stripped and forwarding methods are added.
|
||||
|
||||
## Generation Via Includes
|
||||
|
||||
An alternate method of generation is to use the Include variants of the annotations. These variants
|
||||
act on lists of specified classes. This allows the source classes to be pristine or even come from
|
||||
libraries where you are not able to annotate the source.
|
||||
|
||||
E.g.
|
||||
|
||||
```java
|
||||
import some.library.code.ImportedRecord
|
||||
import some.library.code.ImportedInterface
|
||||
|
||||
@RecordBuilder.Include({
|
||||
ImportedRecord.class // generates a record builder for ImportedRecord
|
||||
})
|
||||
@RecordInterface.Include({
|
||||
ImportedInterface.class // generates a record interface for ImportedInterface
|
||||
})
|
||||
public void Placeholder {
|
||||
}
|
||||
```
|
||||
|
||||
`@RecordBuilder.Include` also supports a `packages` attribute that includes all records
|
||||
in the listed packages.
|
||||
|
||||
The target package for generation is the same as the package that contains the "Include"
|
||||
annotation. Use `packagePattern` to change this (see Javadoc for details).
|
||||
|
||||
## Usage
|
||||
|
||||
### Maven
|
||||
|
||||
1\. Add the dependency that contains the `@RecordBuilder` annotation.
|
||||
Add a dependency that contains the discoverable annotation processor:
|
||||
|
||||
```
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder-core</artifactId>
|
||||
<version>set-version-here</version>
|
||||
<artifactId>record-builder-processor</artifactId>
|
||||
<version>${record.builder.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
```
|
||||
|
||||
2\. Enable the annotation processing for the Maven Compiler Plugin:
|
||||
### Gradle
|
||||
|
||||
```
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>set-version-here</version>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<annotationProcessorPath>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder-processor</artifactId>
|
||||
<version>set-version-here</version>
|
||||
</annotationProcessorPath>
|
||||
</annotationProcessorPaths>
|
||||
<annotationProcessors>
|
||||
<annotationProcessor>io.soabase.recordbuilder.processor.RecordBuilderProcessor</annotationProcessor>
|
||||
</annotationProcessors>
|
||||
Add the following to your build.gradle file:
|
||||
|
||||
|
||||
<!-- "release" and "enable-preview" are required while records are preview features -->
|
||||
<release>14</release>
|
||||
<compilerArgs>
|
||||
<arg>--enable-preview</arg>
|
||||
</compilerArgs>
|
||||
|
||||
... any other options here ...
|
||||
</configuration>
|
||||
</plugin>
|
||||
```groovy
|
||||
dependencies {
|
||||
annotationProcessor 'io.soabase.record-builder:record-builder-processor:$version-goes-here'
|
||||
compileOnly 'io.soabase.record-builder:record-builder-core:$version-goes-here'
|
||||
}
|
||||
```
|
||||
|
||||
### IDE
|
||||
|
||||
Depending on your IDE you are likely to need to enable Annotation Processing in your IDE settings.
|
||||
|
||||
## Enable Preview
|
||||
## Customizing
|
||||
|
||||
Note: records are a preview feature only. You'll need take a number of steps in order to try RecordBuilder:
|
||||
|
||||
- Install and make active Java 14 or later
|
||||
- Make sure your development tool is using Java 14 or later and is configured to enable preview features (for Maven I've documented how to do this here: [https://stackoverflow.com/a/59363152/2048051](https://stackoverflow.com/a/59363152/2048051))
|
||||
- Bear in mind that this is not yet meant for production and there are numerous bugs in the tools and JDKs.
|
||||
|
||||
Note: I've seen some very odd compilation bugs with the current Java 14 and Maven. If you get internal Javac errors I suggest rebuilding with `mvn clean package` and/or `mvn clean install`.
|
||||
|
||||
## TODOs
|
||||
|
||||
- Document how to integrate with Gradle
|
||||
- Keep up with changes
|
||||
- Testing
|
||||
- Etc.
|
||||
RecordBuilder can be customized to your needs and you can even create your
|
||||
own custom RecordBuilder annotations. See [Customizing RecordBuilder](customizing.md)
|
||||
for details.
|
||||
|
||||
95
customizing.md
Normal file
95
customizing.md
Normal file
@@ -0,0 +1,95 @@
|
||||
[◀︎ RecordBuilder](README.md) • Customizing RecordBuilder
|
||||
|
||||
# RecordBuilderFull
|
||||
|
||||
Note: `@RecordBuilderFull` has most optional features enabled. It's an alternate
|
||||
form of `@RecordBuilder` that uses the [templating mechanism](#create-a-custom-annotation) to
|
||||
enable optional features.
|
||||
|
||||
# Customizing RecordBuilder
|
||||
|
||||
RecordBuilder can be customized in a number of ways. The types of customizations will change over time. See
|
||||
[@RecordBuilder.Options](record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilder.java)
|
||||
for the current set of customizations and their default values. For example, the `useImmutableCollections` option
|
||||
adds special handling for record components of type `java.util.List`, `java.util.Set`, `java.util.Map` and `java.util.Collection`. When the record is built, any components of these types are passed through an added shim method that uses the corresponding immutable collection (e.g. `List.copyOf(o)`) or an empty immutable collection if the component is `null`.
|
||||
|
||||
You can:
|
||||
|
||||
- [Customize an entire build](#customize-an-entire-build) - all uses of `@RecordBuilder` in your project
|
||||
- [Customize a single record](#customize-a-single-record) annotated with `@RecordBuilder`
|
||||
- [Create a custom annotation](#create-a-custom-annotation) that specifies your options and use that instead of `@RecordBuilder`
|
||||
|
||||
## Customize an entire build
|
||||
|
||||
To customize an entire build, use javac's annotation processor options via `-A` on the command line.
|
||||
The options available are the same as the attributes in [@RecordBuilder.Options](record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilder.java).
|
||||
i.e. to disable "prefixing enclosing class names", compile with:
|
||||
|
||||
```shell
|
||||
javac -AprefixEnclosingClassNames=false ...
|
||||
```
|
||||
|
||||
_Note: use a separate `-A` for each option._
|
||||
|
||||
#### Maven
|
||||
|
||||
If you are using Maven, specify the options in the compiler plugin:
|
||||
|
||||
```xml
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin-version}</version>
|
||||
<configuration>
|
||||
<compilerArgs>
|
||||
<arg>-AprefixEnclosingClassNames=false</arg>
|
||||
<arg>-AfileComment="something different"</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
```
|
||||
|
||||
#### Gradle
|
||||
|
||||
For Gradle, specify the options:
|
||||
|
||||
```groovy
|
||||
compilerArgs.addAll(['-AprefixEnclosingClassNames=false', '-AfileComment="something different"'])
|
||||
```
|
||||
|
||||
## Customize a single record
|
||||
|
||||
To customize a single record, add `@RecordBuilder.Options` in addition to
|
||||
`@RecordBuilder`.
|
||||
|
||||
E.g.
|
||||
|
||||
```java
|
||||
@RecordBuilder.Options(withClassName = "Wither")
|
||||
@RecordBuilder
|
||||
public record MyRecord(String s){}
|
||||
```
|
||||
|
||||
## Create a custom annotation
|
||||
|
||||
Using `@RecordBuilder.Template` you can create your own RecordBuilder annotation
|
||||
that uses the set of options you want. E.g. to create a custom annotation that
|
||||
uses an alternate file comment and an alternate With classname:
|
||||
|
||||
```java
|
||||
@RecordBuilder.Template(options = @RecordBuilder.Options(
|
||||
fileComment = "MyCo license",
|
||||
withClassName = "Wither"
|
||||
))
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Target(ElementType.TYPE)
|
||||
@Inherited
|
||||
public @interface MyCoRecordBuilder {
|
||||
}
|
||||
```
|
||||
|
||||
Now, you can use `@MyCoRecordBuilder` instead of `@RecordBuilder` and the record
|
||||
will be built with options as specified.
|
||||
|
||||
Note: the template mechanism also supports `@RecordInterface` templates via the `asRecordInterface` attribute.
|
||||
When it is set a `@RecordInterface` template is created instead.
|
||||
68
pom.xml
68
pom.xml
@@ -5,12 +5,13 @@
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>1.2.ea</version>
|
||||
<version>31</version>
|
||||
|
||||
<modules>
|
||||
<module>record-builder-core</module>
|
||||
<module>record-builder-processor</module>
|
||||
<module>record-builder-test</module>
|
||||
<module>record-builder-validator</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
@@ -18,7 +19,7 @@
|
||||
<project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
|
||||
<jdk-version>14</jdk-version>
|
||||
<jdk-version>16</jdk-version>
|
||||
|
||||
<maven-compiler-plugin-version>3.8.1</maven-compiler-plugin-version>
|
||||
<maven-source-plugin-version>3.2.0</maven-source-plugin-version>
|
||||
@@ -30,10 +31,16 @@
|
||||
<maven-clean-plugin-version>3.1.0</maven-clean-plugin-version>
|
||||
<maven-shade-plugin-version>3.2.1</maven-shade-plugin-version>
|
||||
<maven-release-plugin-version>2.5.3</maven-release-plugin-version>
|
||||
<maven-jar-plugin-version>3.2.0</maven-jar-plugin-version>
|
||||
|
||||
<javapoet-version>1.11.0</javapoet-version>
|
||||
<license-file-path>src/etc/header.txt</license-file-path>
|
||||
|
||||
<javapoet-version>1.12.1</javapoet-version>
|
||||
<junit-jupiter-version>5.5.2</junit-jupiter-version>
|
||||
<asm-version>7.2</asm-version>
|
||||
<validation-api-version>2.0.1.Final</validation-api-version>
|
||||
<hibernate-validator-version>6.0.20.Final</hibernate-validator-version>
|
||||
<javax-el-version>3.0.1-b09</javax-el-version>
|
||||
</properties>
|
||||
|
||||
<name>Record Builder</name>
|
||||
@@ -70,7 +77,7 @@
|
||||
<url>https://github.com/randgalt/record-builder</url>
|
||||
<connection>scm:git:https://github.com/randgalt/record-builder.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:randgalt/record-builder.git</developerConnection>
|
||||
<tag>record-builder-1.2.ea</tag>
|
||||
<tag>record-builder-31</tag>
|
||||
</scm>
|
||||
|
||||
<issueManagement>
|
||||
@@ -105,11 +112,41 @@
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder-processor</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder-validator</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter-version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${validation-api-version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator-version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.el</artifactId>
|
||||
<version>${javax-el-version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
@@ -122,9 +159,6 @@
|
||||
<version>${maven-compiler-plugin-version}</version>
|
||||
<configuration>
|
||||
<release>${jdk-version}</release>
|
||||
<compilerArgs>
|
||||
<arg>--enable-preview</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
@@ -156,7 +190,7 @@
|
||||
<artifactId>maven-license-plugin</artifactId>
|
||||
<version>${maven-license-plugin-version}</version>
|
||||
<configuration>
|
||||
<header>src/etc/header.txt</header>
|
||||
<header>${license-file-path}</header>
|
||||
<excludes>
|
||||
<exclude>**/*.apt</exclude>
|
||||
<exclude>**/*.md</exclude>
|
||||
@@ -179,6 +213,11 @@
|
||||
<exclude>**/io/soabase/com/google/**</exclude>
|
||||
<exclude>**/com/company/**</exclude>
|
||||
<exclude>**/META-INF/services/**</exclude>
|
||||
<exclude>**/jvm.config</exclude>
|
||||
<exclude>**/.java-version</exclude>
|
||||
<exclude>**/.travis.yml</exclude>
|
||||
<exclude>**/gradlew</exclude>
|
||||
<exclude>**/.github/**</exclude>
|
||||
</excludes>
|
||||
<strictCheck>true</strictCheck>
|
||||
</configuration>
|
||||
@@ -268,6 +307,18 @@
|
||||
<tagNameFormat>record-builder-@{project.version}</tagNameFormat>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>${maven-jar-plugin-version}</version>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<version>${maven-gpg-plugin-version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
|
||||
@@ -311,7 +362,6 @@
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<version>${maven-gpg-plugin-version}</version>
|
||||
<configuration>
|
||||
<passphrase>${gpg.passphrase}</passphrase>
|
||||
<useAgent>true</useAgent>
|
||||
|
||||
@@ -3,9 +3,29 @@
|
||||
<parent>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder</artifactId>
|
||||
<version>1.2.ea</version>
|
||||
<version>31</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>record-builder-core</artifactId>
|
||||
|
||||
<properties>
|
||||
<license-file-path>${project.parent.basedir}/src/etc/header.txt</license-file-path>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestEntries>
|
||||
<Automatic-Module-Name>io.soabase.recordbuilder.core</Automatic-Module-Name>
|
||||
</manifestEntries>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.core;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface IgnoreDefaultMethod {
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2016 Jordan Zimmerman
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,12 +15,186 @@
|
||||
*/
|
||||
package io.soabase.recordbuilder.core;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Target(ElementType.TYPE)
|
||||
@Inherited
|
||||
public @interface RecordBuilder {
|
||||
@Target({ElementType.TYPE, ElementType.PACKAGE})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Inherited
|
||||
@interface Include {
|
||||
/**
|
||||
* @return list of classes to include
|
||||
*/
|
||||
Class<?>[] value() default {};
|
||||
|
||||
/**
|
||||
* Synonym for {@code value()}. When using the other attributes it maybe more clear to
|
||||
* use {@code classes()} instead of {@code value()}. Note: both attributes are applied
|
||||
* (i.e. a union of classes from both attributes).
|
||||
*
|
||||
* @return list of classes
|
||||
*/
|
||||
Class<?>[] classes() default {};
|
||||
|
||||
/**
|
||||
* Optional list of package names. All records in the packages will get processed as
|
||||
* if they were listed as classes to include.
|
||||
*
|
||||
* @return list of package names
|
||||
*/
|
||||
String[] packages() default {};
|
||||
|
||||
/**
|
||||
* Pattern used to generate the package for the generated class. The value
|
||||
* is the literal package name however two replacement values can be used. '@'
|
||||
* is replaced with the package of the {@code Include} annotation. '*' is replaced with
|
||||
* the package of the included class.
|
||||
*
|
||||
* @return package pattern
|
||||
*/
|
||||
String packagePattern() default "@";
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Target(ElementType.TYPE)
|
||||
@Inherited
|
||||
@interface Options {
|
||||
/**
|
||||
* The builder class name will be the name of the record (prefixed with any enclosing class) plus this suffix. E.g.
|
||||
* if the record name is "Foo", the builder will be named "FooBuilder".
|
||||
*/
|
||||
String suffix() default "Builder";
|
||||
|
||||
/**
|
||||
* Used by {@code RecordInterface}. The generated record will have the same name as the annotated interface
|
||||
* plus this suffix. E.g. if the interface name is "Foo", the record will be named "FooRecord".
|
||||
*/
|
||||
String interfaceSuffix() default "Record";
|
||||
|
||||
/**
|
||||
* The name to use for the copy builder
|
||||
*/
|
||||
String copyMethodName() default "builder";
|
||||
|
||||
/**
|
||||
* The name to use for the builder
|
||||
*/
|
||||
String builderMethodName() default "builder";
|
||||
|
||||
/**
|
||||
* The name to use for the build method
|
||||
*/
|
||||
String buildMethodName() default "build";
|
||||
|
||||
/**
|
||||
* The name to use for the from-to-wither method
|
||||
*/
|
||||
String fromMethodName() default "from";
|
||||
|
||||
/**
|
||||
* The name to use for the method that returns the record components as a stream
|
||||
*/
|
||||
String componentsMethodName() default "stream";
|
||||
|
||||
/**
|
||||
* The name to use for the nested With class
|
||||
*/
|
||||
String withClassName() default "With";
|
||||
|
||||
/**
|
||||
* The prefix to use for the methods in the With class
|
||||
*/
|
||||
String withClassMethodPrefix() default "with";
|
||||
|
||||
/**
|
||||
* Return the comment to place at the top of generated files. Return null or an empty string for no comment.
|
||||
*/
|
||||
String fileComment() default "Auto generated by io.soabase.recordbuilder.core.RecordBuilder: https://github.com/Randgalt/record-builder";
|
||||
|
||||
/**
|
||||
* Return the file indent to use
|
||||
*/
|
||||
String fileIndent() default " ";
|
||||
|
||||
/**
|
||||
* If the record is declared inside of another class, the outer class's name will
|
||||
* be prefixed to the builder name if this returns true.
|
||||
*/
|
||||
boolean prefixEnclosingClassNames() default true;
|
||||
|
||||
/**
|
||||
* If true, any annotations (if applicable) on record components are copied
|
||||
* to the builder methods
|
||||
*
|
||||
* @return true/false
|
||||
*/
|
||||
boolean inheritComponentAnnotations() default true;
|
||||
|
||||
/**
|
||||
* Set the default value of {@code Optional} record components to
|
||||
* {@code Optional.empty()}
|
||||
*/
|
||||
boolean emptyDefaultForOptional() default true;
|
||||
|
||||
/**
|
||||
* Add not-null checks for record components annotated with any annotation named either "NotNull",
|
||||
* "NoNull", or "NonNull" (see {@link #interpretNotNullsPattern()} for the actual regex matching pattern).
|
||||
*/
|
||||
boolean interpretNotNulls() default false;
|
||||
|
||||
/**
|
||||
* If {@link #interpretNotNulls()} is true, this is the regex pattern used to determine if an annotation name
|
||||
* means "not null"
|
||||
*/
|
||||
String interpretNotNullsPattern() default "(?i)((notnull)|(nonnull)|(nonull))";
|
||||
|
||||
/**
|
||||
* <p>Pass built records through the Java Validation API if it's available in the classpath.</p>
|
||||
*
|
||||
* <p>IMPORTANT:
|
||||
* if this option is enabled you must include the {@code record-builder-validator} dependency in addition
|
||||
* to {@code record-builder-core}. {@code record-builder-validator} is implemented completely via reflection and
|
||||
* does not require other dependencies. Alternatively, you can define your own class with the package {@code package io.soabase.recordbuilder.validator;}
|
||||
* named {@code RecordBuilderValidator} which has a public static method: {@code public static <T> T validate(T o)}.</p>
|
||||
*/
|
||||
boolean useValidationApi() default false;
|
||||
|
||||
/**
|
||||
* Adds special handling for record components of type {@link java.util.List}, {@link java.util.Set},
|
||||
* {@link java.util.Map} and {@link java.util.Collection}. When the record is built, any components
|
||||
* of these types are passed through an added shim method that uses the corresponding immutable collection
|
||||
* (e.g. {@code List.copyOf(o)}) or an empty immutable collection if the component is {@code null}.
|
||||
*/
|
||||
boolean useImmutableCollections() default false;
|
||||
|
||||
/**
|
||||
* When enabled, collection types ({@code List}, {@code Set} and {@code Map}) are handled specially.
|
||||
* The setters for these types now create an internal collection and items are added to that
|
||||
* collection. Additionally, "adder" methods prefixed with {@link #singleItemBuilderPrefix()} are created
|
||||
* to add single items to these collections.
|
||||
*/
|
||||
boolean addSingleItemCollectionBuilders() default false;
|
||||
|
||||
/**
|
||||
* The prefix for adder methods when {@link #addSingleItemCollectionBuilders()} is enabled
|
||||
*/
|
||||
String singleItemBuilderPrefix() default "add";
|
||||
|
||||
/**
|
||||
* When enabled, adds functional methods to the nested "With" class (such as {@code map()} and {@code accept()}).
|
||||
*/
|
||||
boolean addFunctionalMethodsToWith() default false;
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.CLASS)
|
||||
@Target(ElementType.ANNOTATION_TYPE)
|
||||
@Inherited
|
||||
@interface Template {
|
||||
RecordBuilder.Options options();
|
||||
|
||||
boolean asRecordInterface() default false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.core;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@RecordBuilder.Template(options = @RecordBuilder.Options(
|
||||
interpretNotNulls = true,
|
||||
useImmutableCollections = true,
|
||||
addSingleItemCollectionBuilders = true,
|
||||
addFunctionalMethodsToWith = true
|
||||
))
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Target(ElementType.TYPE)
|
||||
@Inherited
|
||||
/**
|
||||
* An alternate form of {@code @RecordBuilder} that has most
|
||||
* optional features turned on
|
||||
*/
|
||||
public @interface RecordBuilderFull {
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
/**
|
||||
* Copyright 2016 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.core;
|
||||
|
||||
public interface RecordBuilderMetaData {
|
||||
/**
|
||||
* If you want to use your own meta data instance:
|
||||
* <ul>
|
||||
* <li>create a class that implements {@code RecordBuilderMetaData}</li>
|
||||
* <li>When compiling, make sure that compiled class is in the processor path</li>
|
||||
* <li>Add a "metaDataClass" compiler option with the class name. E.g. {@code javac ... -AmetaDataClass=foo.bar.MyMetaData}</li>
|
||||
* </ul>
|
||||
*/
|
||||
String JAVAC_OPTION_NAME = "metaDataClass";
|
||||
|
||||
/**
|
||||
* The default meta data instance
|
||||
*/
|
||||
RecordBuilderMetaData DEFAULT = new RecordBuilderMetaData() {};
|
||||
|
||||
/**
|
||||
* The builder class name will be the name of the record (prefixed with any enclosing class) plus this suffix. E.g.
|
||||
* if the record name is "Foo", the builder will be named "FooBuilder".
|
||||
*
|
||||
* @return suffix
|
||||
*/
|
||||
default String suffix() {
|
||||
return "Builder";
|
||||
}
|
||||
|
||||
/**
|
||||
* The name to use for the copy builder
|
||||
*
|
||||
* @return copy builder name
|
||||
*/
|
||||
default String copyMethodName() {
|
||||
return builderMethodName();
|
||||
}
|
||||
|
||||
/**
|
||||
* The name to use for the builder
|
||||
*
|
||||
* @return builder name
|
||||
*/
|
||||
default String builderMethodName() {
|
||||
return "builder";
|
||||
}
|
||||
|
||||
/**
|
||||
* The name to use for the build method
|
||||
*
|
||||
* @return build method
|
||||
*/
|
||||
default String buildMethodName() {
|
||||
return "build";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the comment to place at the top of generated files. Return null or an empty string for no comment.
|
||||
*
|
||||
* @return comment or empty
|
||||
*/
|
||||
default String fileComment() {
|
||||
return "Auto generated by RecordBuilder: https://github.com/Randgalt/record-builder";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file indent to use
|
||||
*
|
||||
* @return file index
|
||||
*/
|
||||
default String fileIndent() {
|
||||
return " ";
|
||||
}
|
||||
|
||||
/**
|
||||
* If the record is declared inside of another class, the outer class's name will
|
||||
* be prefixed to the builder name if this returns true.
|
||||
*
|
||||
* @return true/false
|
||||
*/
|
||||
default boolean prefixEnclosingClassNames() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.core;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Target(ElementType.TYPE)
|
||||
@Inherited
|
||||
public @interface RecordInterface {
|
||||
boolean addRecordBuilder() default true;
|
||||
|
||||
@Target({ElementType.TYPE, ElementType.PACKAGE})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Inherited
|
||||
@interface Include {
|
||||
/**
|
||||
* @return list of classes to include
|
||||
*/
|
||||
Class<?>[] value() default {};
|
||||
|
||||
/**
|
||||
* Synonym for {@code value()}. When using the other attributes it maybe more clear to
|
||||
* use {@code classes()} instead of {@code value()}. Note: both attributes are applied
|
||||
* (i.e. a union of classes from both attributes).
|
||||
*
|
||||
* @return list of classes
|
||||
*/
|
||||
Class<?>[] classes() default {};
|
||||
|
||||
/**
|
||||
* If true the generated record is annotated with {@code @RecordBuilder}
|
||||
*
|
||||
* @return true/false
|
||||
*/
|
||||
boolean addRecordBuilder() default true;
|
||||
|
||||
/**
|
||||
* Pattern used to generate the package for the generated class. The value
|
||||
* is the literal package name however two replacement values can be used. '@'
|
||||
* is replaced with the package of the {@code Include} annotation. '*' is replaced with
|
||||
* the package of the included class.
|
||||
*
|
||||
* @return package pattern
|
||||
*/
|
||||
String packagePattern() default "@";
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,16 @@
|
||||
<parent>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder</artifactId>
|
||||
<version>1.2.ea</version>
|
||||
<version>31</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>record-builder-processor</artifactId>
|
||||
|
||||
<properties>
|
||||
<license-file-path>${project.parent.basedir}/src/etc/header.txt</license-file-path>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.squareup</groupId>
|
||||
@@ -24,30 +28,12 @@
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<proc>none</proc>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!--plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<configuration>
|
||||
<artifactSet>
|
||||
<includes>
|
||||
<include>com.squareup:javapoet</include>
|
||||
<include>io.soabase.record-builder:record-builder-processor</include>
|
||||
</includes>
|
||||
</artifactSet>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>com.squareup.javapoet</pattern>
|
||||
<shadedPattern>io.soabase.com.squareup.javapoet</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
</configuration>
|
||||
</plugin-->
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2016 Jordan Zimmerman
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.processor;
|
||||
|
||||
import com.squareup.javapoet.*;
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
import java.util.*;
|
||||
|
||||
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.generatedRecordBuilderAnnotation;
|
||||
|
||||
class CollectionBuilderUtils {
|
||||
private final boolean useImmutableCollections;
|
||||
private final boolean addSingleItemCollectionBuilders;
|
||||
private final String listShimName;
|
||||
private final String mapShimName;
|
||||
private final String setShimName;
|
||||
private final String collectionShimName;
|
||||
|
||||
private boolean needsListShim;
|
||||
private boolean needsMapShim;
|
||||
private boolean needsSetShim;
|
||||
private boolean needsCollectionShim;
|
||||
|
||||
private static final TypeName listType = TypeName.get(List.class);
|
||||
private static final TypeName mapType = TypeName.get(Map.class);
|
||||
private static final TypeName setType = TypeName.get(Set.class);
|
||||
private static final TypeName collectionType = TypeName.get(Collection.class);
|
||||
|
||||
private static final TypeVariableName tType = TypeVariableName.get("T");
|
||||
private static final TypeVariableName kType = TypeVariableName.get("K");
|
||||
private static final TypeVariableName vType = TypeVariableName.get("V");
|
||||
private static final ParameterizedTypeName parameterizedListType = ParameterizedTypeName.get(ClassName.get(List.class), tType);
|
||||
private static final ParameterizedTypeName parameterizedMapType = ParameterizedTypeName.get(ClassName.get(Map.class), kType, vType);
|
||||
private static final ParameterizedTypeName parameterizedSetType = ParameterizedTypeName.get(ClassName.get(Set.class), tType);
|
||||
private static final ParameterizedTypeName parameterizedCollectionType = ParameterizedTypeName.get(ClassName.get(Collection.class), tType);
|
||||
|
||||
CollectionBuilderUtils(List<RecordClassType> recordComponents, RecordBuilder.Options metaData) {
|
||||
useImmutableCollections = metaData.useImmutableCollections();
|
||||
addSingleItemCollectionBuilders = metaData.addSingleItemCollectionBuilders();
|
||||
|
||||
listShimName = adjustShimName(recordComponents, "__list", 0);
|
||||
mapShimName = adjustShimName(recordComponents, "__map", 0);
|
||||
setShimName = adjustShimName(recordComponents, "__set", 0);
|
||||
collectionShimName = adjustShimName(recordComponents, "__collection", 0);
|
||||
}
|
||||
|
||||
enum SingleItemsMetaDataMode {
|
||||
STANDARD,
|
||||
STANDARD_FOR_SETTER,
|
||||
EXCLUDE_WILDCARD_TYPES
|
||||
}
|
||||
|
||||
record SingleItemsMetaData(Class<?> singleItemCollectionClass, List<TypeName> typeArguments, TypeName wildType) {}
|
||||
|
||||
Optional<SingleItemsMetaData> singleItemsMetaData(RecordClassType component, SingleItemsMetaDataMode mode) {
|
||||
if (addSingleItemCollectionBuilders && (component.typeName() instanceof ParameterizedTypeName parameterizedTypeName)) {
|
||||
Class<?> collectionClass = null;
|
||||
ClassName wildcardClass = null;
|
||||
int typeArgumentQty = 0;
|
||||
if (isList(component)) {
|
||||
collectionClass = ArrayList.class;
|
||||
wildcardClass = ClassName.get(Collection.class);
|
||||
typeArgumentQty = 1;
|
||||
} else if (isSet(component)) {
|
||||
collectionClass = HashSet.class;
|
||||
wildcardClass = ClassName.get(Collection.class);
|
||||
typeArgumentQty = 1;
|
||||
} else if (isMap(component)) {
|
||||
collectionClass = HashMap.class;
|
||||
wildcardClass = (ClassName) component.rawTypeName();
|
||||
typeArgumentQty = 2;
|
||||
}
|
||||
var hasWildcardTypeArguments = hasWildcardTypeArguments(parameterizedTypeName, typeArgumentQty);
|
||||
if (collectionClass != null) {
|
||||
return switch (mode) {
|
||||
case STANDARD -> singleItemsMetaDataWithWildType(parameterizedTypeName, collectionClass, wildcardClass, typeArgumentQty);
|
||||
|
||||
case STANDARD_FOR_SETTER -> {
|
||||
if (hasWildcardTypeArguments) {
|
||||
yield Optional.of(new SingleItemsMetaData(collectionClass, parameterizedTypeName.typeArguments, component.typeName()));
|
||||
}
|
||||
yield singleItemsMetaDataWithWildType(parameterizedTypeName, collectionClass, wildcardClass, typeArgumentQty);
|
||||
}
|
||||
|
||||
case EXCLUDE_WILDCARD_TYPES -> {
|
||||
if (hasWildcardTypeArguments) {
|
||||
yield Optional.empty();
|
||||
}
|
||||
yield singleItemsMetaDataWithWildType(parameterizedTypeName, collectionClass, wildcardClass, typeArgumentQty);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
boolean isList(RecordClassType component) {
|
||||
return component.rawTypeName().equals(listType);
|
||||
}
|
||||
|
||||
boolean isMap(RecordClassType component) {
|
||||
return component.rawTypeName().equals(mapType);
|
||||
}
|
||||
|
||||
boolean isSet(RecordClassType component) {
|
||||
return component.rawTypeName().equals(setType);
|
||||
}
|
||||
|
||||
void add(CodeBlock.Builder builder, RecordClassType component) {
|
||||
if (useImmutableCollections) {
|
||||
if (isList(component)) {
|
||||
needsListShim = true;
|
||||
builder.add("$L($L)", listShimName, component.name());
|
||||
} else if (isMap(component)) {
|
||||
needsMapShim = true;
|
||||
builder.add("$L($L)", mapShimName, component.name());
|
||||
} else if (isSet(component)) {
|
||||
needsSetShim = true;
|
||||
builder.add("$L($L)", setShimName, component.name());
|
||||
} else if (component.rawTypeName().equals(collectionType)) {
|
||||
needsCollectionShim = true;
|
||||
builder.add("$L($L)", collectionShimName, component.name());
|
||||
} else {
|
||||
builder.add("$L", component.name());
|
||||
}
|
||||
} else {
|
||||
builder.add("$L", component.name());
|
||||
}
|
||||
}
|
||||
|
||||
void addShims(TypeSpec.Builder builder) {
|
||||
if (!useImmutableCollections) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (needsListShim) {
|
||||
builder.addMethod(buildMethod(listShimName, listType, parameterizedListType, tType));
|
||||
}
|
||||
if (needsSetShim) {
|
||||
builder.addMethod(buildMethod(setShimName, setType, parameterizedSetType, tType));
|
||||
}
|
||||
if (needsMapShim) {
|
||||
builder.addMethod(buildMethod(mapShimName, mapType, parameterizedMapType, kType, vType));
|
||||
}
|
||||
if (needsCollectionShim) {
|
||||
builder.addMethod(buildCollectionsMethod());
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<SingleItemsMetaData> singleItemsMetaDataWithWildType(ParameterizedTypeName parameterizedTypeName, Class<?> collectionClass, ClassName wildcardClass, int typeArgumentQty) {
|
||||
TypeName wildType;
|
||||
if (typeArgumentQty == 1) {
|
||||
wildType = ParameterizedTypeName.get(wildcardClass, WildcardTypeName.subtypeOf(parameterizedTypeName.typeArguments.get(0)));
|
||||
} else { // if (typeArgumentQty == 2)
|
||||
wildType = ParameterizedTypeName.get(wildcardClass, WildcardTypeName.subtypeOf(parameterizedTypeName.typeArguments.get(0)), WildcardTypeName.subtypeOf(parameterizedTypeName.typeArguments.get(1)));
|
||||
}
|
||||
return Optional.of(new SingleItemsMetaData(collectionClass, parameterizedTypeName.typeArguments, wildType));
|
||||
}
|
||||
|
||||
private boolean hasWildcardTypeArguments(ParameterizedTypeName parameterizedTypeName, int argumentCount) {
|
||||
for (int i = 0; i < argumentCount; ++i) {
|
||||
if (parameterizedTypeName.typeArguments.size() > i) {
|
||||
if (parameterizedTypeName.typeArguments.get(i) instanceof WildcardTypeName) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String adjustShimName(List<RecordClassType> recordComponents, String baseName, int index) {
|
||||
var name = (index == 0) ? baseName : (baseName + index);
|
||||
if (recordComponents.stream().anyMatch(component -> component.name().equals(name))) {
|
||||
return adjustShimName(recordComponents, baseName, index + 1);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
private MethodSpec buildMethod(String name, TypeName mainType, ParameterizedTypeName parameterizedType, TypeVariableName... typeVariables) {
|
||||
var code = CodeBlock.of("return (o != null) ? $T.copyOf(o) : $T.of()", mainType, mainType);
|
||||
return MethodSpec.methodBuilder(name)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
.addTypeVariables(Arrays.asList(typeVariables))
|
||||
.returns(parameterizedType)
|
||||
.addParameter(parameterizedType, "o")
|
||||
.addStatement(code)
|
||||
.build();
|
||||
}
|
||||
|
||||
private MethodSpec buildCollectionsMethod() {
|
||||
var code = CodeBlock.builder()
|
||||
.add("if (o instanceof Set) {\n")
|
||||
.indent()
|
||||
.addStatement("return $T.copyOf(o)", setType)
|
||||
.unindent()
|
||||
.addStatement("}")
|
||||
.addStatement("return (o != null) ? $T.copyOf(o) : $T.of()", listType, listType)
|
||||
.build();
|
||||
return MethodSpec.methodBuilder(collectionShimName)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
.addTypeVariable(tType)
|
||||
.returns(parameterizedCollectionType)
|
||||
.addParameter(parameterizedCollectionType, "o")
|
||||
.addCode(code)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2016 Jordan Zimmerman
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,14 +19,72 @@ import com.squareup.javapoet.ClassName;
|
||||
import com.squareup.javapoet.ParameterizedTypeName;
|
||||
import com.squareup.javapoet.TypeName;
|
||||
import com.squareup.javapoet.TypeVariableName;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.lang.model.element.AnnotationMirror;
|
||||
import javax.lang.model.element.AnnotationValue;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.element.RecordComponentElement;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.TypeParameterElement;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ElementUtils {
|
||||
public static Optional<? extends AnnotationMirror> findAnnotationMirror(ProcessingEnvironment processingEnv, Element element, String annotationClass) {
|
||||
return processingEnv.getElementUtils().getAllAnnotationMirrors(element).stream()
|
||||
.filter(e -> e.getAnnotationType().toString().equals(annotationClass))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public static Optional<? extends AnnotationValue> getAnnotationValue(Map<? extends ExecutableElement, ? extends AnnotationValue> values, String name) {
|
||||
return values.entrySet()
|
||||
.stream()
|
||||
.filter(e -> e.getKey().getSimpleName().toString().equals(name))
|
||||
.map(Map.Entry::getValue)
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<TypeMirror> getAttributeTypeMirrorList(AnnotationValue attribute)
|
||||
{
|
||||
List<? extends AnnotationValue> values = (attribute != null) ? (List<? extends AnnotationValue>)attribute.getValue() : Collections.emptyList();
|
||||
return values.stream().map(v -> (TypeMirror)v.getValue()).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<String> getAttributeStringList(AnnotationValue attribute)
|
||||
{
|
||||
List<? extends AnnotationValue> values = (attribute != null) ? (List<? extends AnnotationValue>)attribute.getValue() : Collections.emptyList();
|
||||
return values.stream().map(v -> (String)v.getValue()).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static boolean getBooleanAttribute(AnnotationValue attribute)
|
||||
{
|
||||
Object value = (attribute != null) ? attribute.getValue() : null;
|
||||
if ( value != null )
|
||||
{
|
||||
return Boolean.parseBoolean(String.valueOf(value));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String getStringAttribute(AnnotationValue attribute, String defaultValue)
|
||||
{
|
||||
Object value = (attribute != null) ? attribute.getValue() : null;
|
||||
if ( value != null )
|
||||
{
|
||||
return String.valueOf(value);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public static String getPackageName(TypeElement typeElement) {
|
||||
while (typeElement.getNestingKind().isNested()) {
|
||||
Element enclosingElement = typeElement.getEnclosingElement();
|
||||
@@ -36,8 +94,9 @@ public class ElementUtils {
|
||||
break;
|
||||
}
|
||||
}
|
||||
String name = typeElement.getEnclosingElement().toString();
|
||||
return !name.equals("unnamed package") ? name : "";
|
||||
String name = typeElement.getQualifiedName().toString();
|
||||
int index = name.lastIndexOf(".");
|
||||
return (index > -1) ? name.substring(0, index) : "";
|
||||
}
|
||||
|
||||
public static ClassType getClassType(String packageName, String simpleName, List<? extends TypeParameterElement> typeParameters) {
|
||||
@@ -56,8 +115,49 @@ public class ElementUtils {
|
||||
return new ClassType(ParameterizedTypeName.get(builderClassName, typeNames), builderClassName.simpleName());
|
||||
}
|
||||
|
||||
public static ClassType getClassType(RecordComponentElement recordComponent) {
|
||||
return new ClassType(TypeName.get(recordComponent.asType()), recordComponent.getSimpleName().toString());
|
||||
public static RecordClassType getRecordClassType(ProcessingEnvironment processingEnv, RecordComponentElement recordComponent, List<? extends AnnotationMirror> accessorAnnotations, List<? extends AnnotationMirror> canonicalConstructorAnnotations) {
|
||||
var typeName = TypeName.get(recordComponent.asType());
|
||||
var rawTypeName = TypeName.get(processingEnv.getTypeUtils().erasure(recordComponent.asType()));
|
||||
return new RecordClassType(typeName, rawTypeName, recordComponent.getSimpleName().toString(), accessorAnnotations, canonicalConstructorAnnotations);
|
||||
}
|
||||
|
||||
public static String getWithMethodName(ClassType component, String prefix) {
|
||||
var name = component.name();
|
||||
if (name.length() == 1) {
|
||||
return prefix + name.toUpperCase();
|
||||
}
|
||||
return prefix + Character.toUpperCase(name.charAt(0)) + name.substring(1);
|
||||
}
|
||||
|
||||
public static String getBuilderName(TypeElement element, RecordBuilder.Options metaData, ClassType classType, String suffix) {
|
||||
// generate the class name
|
||||
var baseName = classType.name() + suffix;
|
||||
return metaData.prefixEnclosingClassNames() ? (getBuilderNamePrefix(element.getEnclosingElement()) + baseName) : baseName;
|
||||
}
|
||||
|
||||
public static Optional<? extends Element> findCanonicalConstructor(TypeElement record) {
|
||||
if ( record.getKind() != ElementKind.RECORD ) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
// based on https://github.com/openjdk/jdk/pull/3556/files#diff-a6270f4b50989abe733607c69038b2036306d13f77276af005d023b7fc57f1a2R2368
|
||||
var componentList = record.getRecordComponents().stream().map(e -> e.asType().toString()).collect(Collectors.toList());
|
||||
return record.getEnclosedElements().stream()
|
||||
.filter(element -> element.getKind() == ElementKind.CONSTRUCTOR)
|
||||
.filter(element -> {
|
||||
var parameters = ((ExecutableElement)element).getParameters();
|
||||
var parametersList = parameters.stream().map(e -> e.asType().toString()).collect(Collectors.toList());
|
||||
return componentList.equals(parametersList);
|
||||
})
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private static String getBuilderNamePrefix(Element element) {
|
||||
// prefix enclosing class names if nested in a class
|
||||
if (element instanceof TypeElement) {
|
||||
return getBuilderNamePrefix(element.getEnclosingElement()) + element.getSimpleName().toString();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private ElementUtils() {
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.processor;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import javax.tools.Diagnostic;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class IncludeHelper {
|
||||
private final boolean isValid;
|
||||
private final List<TypeElement> classTypeElements;
|
||||
private final Map<? extends ExecutableElement, ? extends AnnotationValue> annotationValues;
|
||||
|
||||
IncludeHelper(ProcessingEnvironment processingEnv, Element element, AnnotationMirror annotationMirror, boolean packagesSupported) {
|
||||
annotationValues = processingEnv.getElementUtils().getElementValuesWithDefaults(annotationMirror);
|
||||
var value = ElementUtils.getAnnotationValue(annotationValues, "value");
|
||||
var classes = ElementUtils.getAnnotationValue(annotationValues, "classes");
|
||||
var packages = ElementUtils.getAnnotationValue(annotationValues, "packages");
|
||||
var isValid = true;
|
||||
var classTypeElements = new ArrayList<TypeElement>();
|
||||
if (value.isPresent() || classes.isPresent() || packages.isPresent()) {
|
||||
var valueList = value.map(ElementUtils::getAttributeTypeMirrorList).orElseGet(List::of);
|
||||
var classesList = classes.map(ElementUtils::getAttributeTypeMirrorList).orElseGet(List::of);
|
||||
var packagesList = packages.map(ElementUtils::getAttributeStringList).orElseGet(List::of);
|
||||
if (valueList.isEmpty() && classesList.isEmpty() && packagesList.isEmpty()) {
|
||||
if (packagesSupported) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "At least one of \"value\", \"classes\" or \"packages\" required", element);
|
||||
} else {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "At least one of \"value\" or \"classes\" required", element);
|
||||
}
|
||||
isValid = false;
|
||||
}
|
||||
isValid = processList(processingEnv, isValid, element, valueList, classTypeElements);
|
||||
isValid = processList(processingEnv, isValid, element, classesList, classTypeElements);
|
||||
packages.ifPresent(annotationValue -> processPackages(processingEnv, classTypeElements, packagesList));
|
||||
} else {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not read attribute for annotation", element);
|
||||
isValid = false;
|
||||
}
|
||||
this.isValid = isValid;
|
||||
this.classTypeElements = List.copyOf(classTypeElements);
|
||||
}
|
||||
|
||||
Map<? extends ExecutableElement, ? extends AnnotationValue> getAnnotationValues() {
|
||||
return annotationValues;
|
||||
}
|
||||
|
||||
boolean isValid() {
|
||||
return isValid;
|
||||
}
|
||||
|
||||
List<TypeElement> getClassTypeElements() {
|
||||
return classTypeElements;
|
||||
}
|
||||
|
||||
private boolean processList(ProcessingEnvironment processingEnv, boolean isValid, Element element, List<TypeMirror> list, ArrayList<TypeElement> classTypeElements) {
|
||||
for (var typeMirror : list) {
|
||||
TypeElement typeElement = (TypeElement) processingEnv.getTypeUtils().asElement(typeMirror);
|
||||
if (typeElement == null) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not get element for: " + typeMirror, element);
|
||||
isValid = false;
|
||||
} else {
|
||||
classTypeElements.add(typeElement);
|
||||
}
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
||||
private void processPackages(ProcessingEnvironment processingEnv, List<TypeElement> classTypeElements, List<String> packagesList) {
|
||||
for (var packageName : packagesList) {
|
||||
var packageElement = processingEnv.getElementUtils().getPackageElement(packageName);
|
||||
for (var child : packageElement.getEnclosedElements()) {
|
||||
if (child.getKind() == ElementKind.RECORD) {
|
||||
classTypeElements.add((TypeElement) child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,944 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.processor;
|
||||
|
||||
import com.squareup.javapoet.*;
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.lang.model.element.*;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.soabase.recordbuilder.processor.CollectionBuilderUtils.SingleItemsMetaDataMode.*;
|
||||
import static io.soabase.recordbuilder.processor.ElementUtils.getBuilderName;
|
||||
import static io.soabase.recordbuilder.processor.ElementUtils.getWithMethodName;
|
||||
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.generatedRecordBuilderAnnotation;
|
||||
|
||||
class InternalRecordBuilderProcessor {
|
||||
private final RecordBuilder.Options metaData;
|
||||
private final ClassType recordClassType;
|
||||
private final String packageName;
|
||||
private final ClassType builderClassType;
|
||||
private final List<TypeVariableName> typeVariables;
|
||||
private final List<RecordClassType> recordComponents;
|
||||
private final TypeSpec builderType;
|
||||
private final TypeSpec.Builder builder;
|
||||
private final String uniqueVarName;
|
||||
private final Pattern notNullPattern;
|
||||
private final CollectionBuilderUtils collectionBuilderUtils;
|
||||
|
||||
private static final TypeName overrideType = TypeName.get(Override.class);
|
||||
private static final TypeName optionalType = TypeName.get(Optional.class);
|
||||
private static final TypeName optionalIntType = TypeName.get(OptionalInt.class);
|
||||
private static final TypeName optionalLongType = TypeName.get(OptionalLong.class);
|
||||
private static final TypeName optionalDoubleType = TypeName.get(OptionalDouble.class);
|
||||
private static final TypeName validatorTypeName = ClassName.get("io.soabase.recordbuilder.validator", "RecordBuilderValidator");
|
||||
private static final TypeVariableName rType = TypeVariableName.get("R");
|
||||
private final ProcessingEnvironment processingEnv;
|
||||
|
||||
InternalRecordBuilderProcessor(ProcessingEnvironment processingEnv, TypeElement record, RecordBuilder.Options metaData, Optional<String> packageNameOpt) {
|
||||
this.processingEnv = processingEnv;
|
||||
var recordActualPackage = ElementUtils.getPackageName(record);
|
||||
this.metaData = metaData;
|
||||
recordClassType = ElementUtils.getClassType(record, record.getTypeParameters());
|
||||
packageName = packageNameOpt.orElse(recordActualPackage);
|
||||
builderClassType = ElementUtils.getClassType(packageName, getBuilderName(record, metaData, recordClassType, metaData.suffix()), record.getTypeParameters());
|
||||
typeVariables = record.getTypeParameters().stream().map(TypeVariableName::get).collect(Collectors.toList());
|
||||
recordComponents = buildRecordComponents(record);
|
||||
uniqueVarName = getUniqueVarName();
|
||||
notNullPattern = Pattern.compile(metaData.interpretNotNullsPattern());
|
||||
collectionBuilderUtils = new CollectionBuilderUtils(recordComponents, this.metaData);
|
||||
|
||||
builder = TypeSpec.classBuilder(builderClassType.name())
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addTypeVariables(typeVariables);
|
||||
addVisibility(recordActualPackage.equals(packageName), record.getModifiers());
|
||||
addWithNestedClass();
|
||||
addDefaultConstructor();
|
||||
addStaticBuilder();
|
||||
if (recordComponents.size() > 0) {
|
||||
addAllArgsConstructor();
|
||||
}
|
||||
addStaticDefaultBuilderMethod();
|
||||
addStaticCopyBuilderMethod();
|
||||
addStaticFromWithMethod();
|
||||
addStaticComponentsMethod();
|
||||
addBuildMethod();
|
||||
addToStringMethod();
|
||||
addHashCodeMethod();
|
||||
addEqualsMethod();
|
||||
recordComponents.forEach(component -> {
|
||||
add1Field(component);
|
||||
add1SetterMethod(component);
|
||||
add1GetterMethod(component);
|
||||
var collectionMetaData = collectionBuilderUtils.singleItemsMetaData(component, EXCLUDE_WILDCARD_TYPES);
|
||||
collectionMetaData.ifPresent(meta -> add1CollectionBuilders(meta, component));
|
||||
});
|
||||
collectionBuilderUtils.addShims(builder);
|
||||
builderType = builder.build();
|
||||
}
|
||||
|
||||
String packageName() {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
ClassType builderClassType() {
|
||||
return builderClassType;
|
||||
}
|
||||
|
||||
TypeSpec builderType() {
|
||||
return builderType;
|
||||
}
|
||||
|
||||
private void addVisibility(boolean builderIsInRecordPackage, Set<Modifier> modifiers) {
|
||||
if (builderIsInRecordPackage) {
|
||||
if (modifiers.contains(Modifier.PUBLIC) || modifiers.contains(Modifier.PRIVATE) || modifiers.contains(Modifier.PROTECTED)) {
|
||||
builder.addModifiers(Modifier.PUBLIC); // builders are top level classes - can only be public or package-private
|
||||
}
|
||||
// is package-private
|
||||
} else {
|
||||
builder.addModifiers(Modifier.PUBLIC);
|
||||
}
|
||||
}
|
||||
|
||||
private List<RecordClassType> buildRecordComponents(TypeElement record) {
|
||||
var accessorAnnotations = record.getRecordComponents().stream().map(e -> e.getAccessor().getAnnotationMirrors()).collect(Collectors.toList());
|
||||
var canonicalConstructorAnnotations = ElementUtils.findCanonicalConstructor(record).map(constructor -> ((ExecutableElement) constructor).getParameters().stream().map(Element::getAnnotationMirrors).collect(Collectors.toList())).orElse(List.of());
|
||||
var recordComponents = record.getRecordComponents();
|
||||
return IntStream.range(0, recordComponents.size())
|
||||
.mapToObj(index -> {
|
||||
var thisAccessorAnnotations = (accessorAnnotations.size() > index) ? accessorAnnotations.get(index) : List.<AnnotationMirror>of();
|
||||
var thisCanonicalConstructorAnnotations = (canonicalConstructorAnnotations.size() > index) ? canonicalConstructorAnnotations.get(index) : List.<AnnotationMirror>of();
|
||||
return ElementUtils.getRecordClassType(processingEnv, recordComponents.get(index), thisAccessorAnnotations, thisCanonicalConstructorAnnotations);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private void addWithNestedClass() {
|
||||
/*
|
||||
Adds a nested interface that adds withers similar to:
|
||||
|
||||
public class MyRecordBuilder {
|
||||
public interface With {
|
||||
// with methods
|
||||
}
|
||||
}
|
||||
*/
|
||||
var classBuilder = TypeSpec.interfaceBuilder(metaData.withClassName())
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addJavadoc("Add withers to {@code $L}\n", recordClassType.name())
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addTypeVariables(typeVariables);
|
||||
recordComponents.forEach(component -> addNestedGetterMethod(classBuilder, component));
|
||||
addWithBuilderMethod(classBuilder);
|
||||
addWithSuppliedBuilderMethod(classBuilder);
|
||||
IntStream.range(0, recordComponents.size()).forEach(index -> add1WithMethod(classBuilder, recordComponents.get(index), index));
|
||||
if (metaData.addFunctionalMethodsToWith()) {
|
||||
classBuilder.addType(buildFunctionalInterface("Function", true))
|
||||
.addType(buildFunctionalInterface("Consumer", false))
|
||||
.addMethod(buildFunctionalHandler("Function", "map", true))
|
||||
.addMethod(buildFunctionalHandler("Consumer", "accept", false));
|
||||
}
|
||||
builder.addType(classBuilder.build());
|
||||
}
|
||||
|
||||
private void addWithSuppliedBuilderMethod(TypeSpec.Builder classBuilder) {
|
||||
/*
|
||||
Adds a method that returns a pre-filled copy builder similar to:
|
||||
|
||||
default MyRecord with(Consumer<MyRecordBuilder> consumer) {
|
||||
MyRecordBuilder builder = with();
|
||||
consumer.accept(builder);
|
||||
return builder.build();
|
||||
}
|
||||
*/
|
||||
var codeBlockBuilder = CodeBlock.builder()
|
||||
.add("$T builder = with();\n", builderClassType.typeName())
|
||||
.add("consumer.accept(builder);\n")
|
||||
.add("return builder.build();\n");
|
||||
var consumerType = ParameterizedTypeName.get(ClassName.get(Consumer.class), builderClassType.typeName());
|
||||
var parameter = ParameterSpec.builder(consumerType, "consumer").build();
|
||||
var methodSpec = MethodSpec.methodBuilder(metaData.withClassMethodPrefix())
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addJavadoc("Return a new record built from the builder passed to the given consumer")
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
|
||||
.addParameter(parameter)
|
||||
.returns(recordClassType.typeName())
|
||||
.addCode(codeBlockBuilder.build())
|
||||
.build();
|
||||
classBuilder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void addWithBuilderMethod(TypeSpec.Builder classBuilder) {
|
||||
/*
|
||||
Adds a method that returns a pre-filled copy builder similar to:
|
||||
|
||||
default MyRecordBuilder with() {
|
||||
return MyRecordBuilder.builder(r);
|
||||
}
|
||||
*/
|
||||
var codeBlockBuilder = CodeBlock.builder()
|
||||
.add("return new $L(", builderClassType.name());
|
||||
addComponentCallsAsArguments(-1, codeBlockBuilder);
|
||||
codeBlockBuilder.add(");");
|
||||
var methodSpec = MethodSpec.methodBuilder(metaData.withClassMethodPrefix())
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addJavadoc("Return a new record builder using the current values")
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
|
||||
.returns(builderClassType.typeName())
|
||||
.addCode(codeBlockBuilder.build())
|
||||
.build();
|
||||
classBuilder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private String getUniqueVarName() {
|
||||
return getUniqueVarName("");
|
||||
}
|
||||
|
||||
private String getUniqueVarName(String prefix) {
|
||||
var name = prefix + "r";
|
||||
var alreadyExists = recordComponents.stream()
|
||||
.map(ClassType::name)
|
||||
.anyMatch(n -> n.equals(name));
|
||||
return alreadyExists ? getUniqueVarName(prefix + "_") : name;
|
||||
}
|
||||
|
||||
private void add1WithMethod(TypeSpec.Builder classBuilder, RecordClassType component, int index) {
|
||||
/*
|
||||
Adds a with method for the component similar to:
|
||||
|
||||
default MyRecord withName(String name) {
|
||||
return new MyRecord(name, r.age());
|
||||
}
|
||||
*/
|
||||
var codeBlockBuilder = CodeBlock.builder();
|
||||
addNullCheckCodeBlock(codeBlockBuilder, index);
|
||||
codeBlockBuilder.add("$[return ");
|
||||
if (metaData.useValidationApi()) {
|
||||
codeBlockBuilder.add("$T.validate(", validatorTypeName);
|
||||
}
|
||||
codeBlockBuilder.add("new $T(", recordClassType.typeName());
|
||||
addComponentCallsAsArguments(index, codeBlockBuilder);
|
||||
codeBlockBuilder.add(")");
|
||||
if (metaData.useValidationApi()) {
|
||||
codeBlockBuilder.add(")");
|
||||
}
|
||||
codeBlockBuilder.add(";$]");
|
||||
|
||||
var methodName = getWithMethodName(component, metaData.withClassMethodPrefix());
|
||||
var parameterSpecBuilder = ParameterSpec.builder(component.typeName(), component.name());
|
||||
addConstructorAnnotations(component, parameterSpecBuilder);
|
||||
var methodSpec = MethodSpec.methodBuilder(methodName)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addJavadoc("Return a new instance of {@code $L} with a new value for {@code $L}\n", recordClassType.name(), component.name())
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
|
||||
.addParameter(parameterSpecBuilder.build())
|
||||
.addCode(codeBlockBuilder.build())
|
||||
.returns(recordClassType.typeName())
|
||||
.build();
|
||||
classBuilder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void addComponentCallsAsArguments(int index, CodeBlock.Builder codeBlockBuilder) {
|
||||
IntStream.range(0, recordComponents.size()).forEach(parameterIndex -> {
|
||||
if (parameterIndex > 0) {
|
||||
codeBlockBuilder.add(", ");
|
||||
}
|
||||
RecordClassType parameterComponent = recordComponents.get(parameterIndex);
|
||||
if (parameterIndex == index) {
|
||||
collectionBuilderUtils.add(codeBlockBuilder, parameterComponent);
|
||||
} else {
|
||||
codeBlockBuilder.add("$L()", parameterComponent.name());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void addDefaultConstructor() {
|
||||
/*
|
||||
Adds a default constructor similar to:
|
||||
|
||||
private MyRecordBuilder() {
|
||||
}
|
||||
*/
|
||||
var constructor = MethodSpec.constructorBuilder()
|
||||
.addModifiers(Modifier.PRIVATE)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.build();
|
||||
builder.addMethod(constructor);
|
||||
}
|
||||
|
||||
private void addStaticBuilder() {
|
||||
/*
|
||||
Adds an static builder similar to:
|
||||
|
||||
public static MyRecord(int p1, T p2, ...) {
|
||||
return new MyRecord(p1, p2, ...);
|
||||
}
|
||||
*/
|
||||
CodeBlock codeBlock = buildCodeBlock();
|
||||
var builder = MethodSpec.methodBuilder(recordClassType.name())
|
||||
.addJavadoc("Static constructor/builder. Can be used instead of new $L(...)\n", recordClassType.name())
|
||||
.addTypeVariables(typeVariables)
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.returns(recordClassType.typeName())
|
||||
.addCode(codeBlock);
|
||||
recordComponents.forEach(component -> {
|
||||
var parameterSpecBuilder = ParameterSpec.builder(component.typeName(), component.name());
|
||||
addConstructorAnnotations(component, parameterSpecBuilder);
|
||||
builder.addParameter(parameterSpecBuilder.build());
|
||||
});
|
||||
this.builder.addMethod(builder.build());
|
||||
}
|
||||
|
||||
private void addNullCheckCodeBlock(CodeBlock.Builder builder) {
|
||||
if (metaData.interpretNotNulls()) {
|
||||
for (int i = 0; i < recordComponents.size(); ++i) {
|
||||
addNullCheckCodeBlock(builder, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addNullCheckCodeBlock(CodeBlock.Builder builder, int index) {
|
||||
if (metaData.interpretNotNulls()) {
|
||||
var component = recordComponents.get(index);
|
||||
if (!component.typeName().isPrimitive() && isNullAnnotated(component)) {
|
||||
builder.addStatement("$T.requireNonNull($L, $S)", Objects.class, component.name(), component.name() + " is required");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNullAnnotated(RecordClassType component) {
|
||||
return component.getCanonicalConstructorAnnotations().stream()
|
||||
.anyMatch(annotation -> notNullPattern.matcher(annotation.getAnnotationType().asElement().getSimpleName().toString()).matches());
|
||||
}
|
||||
|
||||
private void addAllArgsConstructor() {
|
||||
/*
|
||||
Adds an all-args constructor similar to:
|
||||
|
||||
private MyRecordBuilder(int p1, T p2, ...) {
|
||||
this.p1 = p1;
|
||||
this.p2 = p2;
|
||||
...
|
||||
}
|
||||
*/
|
||||
var constructorBuilder = MethodSpec.constructorBuilder()
|
||||
.addModifiers(Modifier.PRIVATE)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation);
|
||||
recordComponents.forEach(component -> {
|
||||
constructorBuilder.addParameter(component.typeName(), component.name());
|
||||
var collectionMetaData = collectionBuilderUtils.singleItemsMetaData(component, STANDARD);
|
||||
collectionMetaData.ifPresentOrElse(meta -> constructorBuilder.addStatement("this.$L = new $T<>($L)", component.name(), meta.singleItemCollectionClass(), component.name()),
|
||||
() -> constructorBuilder.addStatement("this.$L = $L", component.name(), component.name()));
|
||||
});
|
||||
builder.addMethod(constructorBuilder.build());
|
||||
}
|
||||
|
||||
private void addToStringMethod() {
|
||||
/*
|
||||
add a toString() method similar to:
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MyRecord[p1=blah, p2=blah]";
|
||||
}
|
||||
*/
|
||||
var codeBuilder = CodeBlock.builder().add("return \"$L[", builderClassType.name());
|
||||
IntStream.range(0, recordComponents.size()).forEach(index -> {
|
||||
if (index > 0) {
|
||||
codeBuilder.add(", ");
|
||||
}
|
||||
String name = recordComponents.get(index).name();
|
||||
codeBuilder.add("$L=\" + $L + \"", name, name);
|
||||
});
|
||||
codeBuilder.add("]\"");
|
||||
|
||||
var methodSpec = MethodSpec.methodBuilder("toString")
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addAnnotation(Override.class)
|
||||
.returns(String.class)
|
||||
.addStatement(codeBuilder.build())
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void addHashCodeMethod() {
|
||||
/*
|
||||
add a hashCode() method similar to:
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(p1, p2);
|
||||
}
|
||||
*/
|
||||
var codeBuilder = CodeBlock.builder().add("return $T.hash(", Objects.class);
|
||||
IntStream.range(0, recordComponents.size()).forEach(index -> {
|
||||
if (index > 0) {
|
||||
codeBuilder.add(", ");
|
||||
}
|
||||
codeBuilder.add("$L", recordComponents.get(index).name());
|
||||
});
|
||||
codeBuilder.add(")");
|
||||
|
||||
var methodSpec = MethodSpec.methodBuilder("hashCode")
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addAnnotation(Override.class)
|
||||
.returns(TypeName.INT)
|
||||
.addStatement(codeBuilder.build())
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void addEqualsMethod() {
|
||||
/*
|
||||
add an equals() method similar to:
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) || ((o instanceof MyRecordBuilder b)
|
||||
&& Objects.equals(p1, b.p1)
|
||||
&& Objects.equals(p2, b.p2));
|
||||
}
|
||||
*/
|
||||
var codeBuilder = CodeBlock.builder();
|
||||
codeBuilder.add("return (this == o) || (");
|
||||
codeBuilder.add("(o instanceof $L $L)", builderClassType.name(), uniqueVarName);
|
||||
recordComponents.forEach(recordComponent -> {
|
||||
String name = recordComponent.name();
|
||||
if (recordComponent.typeName().isPrimitive()) {
|
||||
codeBuilder.add("\n&& ($L == $L.$L)", name, uniqueVarName, name);
|
||||
} else {
|
||||
codeBuilder.add("\n&& $T.equals($L, $L.$L)", Objects.class, name, uniqueVarName, name);
|
||||
}
|
||||
});
|
||||
codeBuilder.add(")");
|
||||
|
||||
var methodSpec = MethodSpec.methodBuilder("equals")
|
||||
.addParameter(Object.class, "o")
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addAnnotation(Override.class)
|
||||
.returns(TypeName.BOOLEAN)
|
||||
.addStatement(codeBuilder.build())
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void addBuildMethod() {
|
||||
/*
|
||||
Adds the build method that generates the record similar to:
|
||||
|
||||
public MyRecord build() {
|
||||
return new MyRecord(p1, p2, ...);
|
||||
}
|
||||
*/
|
||||
CodeBlock codeBlock = buildCodeBlock();
|
||||
var methodSpec = MethodSpec.methodBuilder(metaData.buildMethodName())
|
||||
.addJavadoc("Return a new record instance with all fields set to the current values in this builder\n")
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.returns(recordClassType.typeName())
|
||||
.addCode(codeBlock)
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private CodeBlock buildCodeBlock() {
|
||||
/*
|
||||
Builds the code block for allocating the record from its parts
|
||||
*/
|
||||
|
||||
var codeBuilder = CodeBlock.builder();
|
||||
addNullCheckCodeBlock(codeBuilder);
|
||||
codeBuilder.add("$[return ");
|
||||
if (metaData.useValidationApi()) {
|
||||
codeBuilder.add("$T.validate(", validatorTypeName);
|
||||
}
|
||||
codeBuilder.add("new $T(", recordClassType.typeName());
|
||||
IntStream.range(0, recordComponents.size()).forEach(index -> {
|
||||
if (index > 0) {
|
||||
codeBuilder.add(", ");
|
||||
}
|
||||
collectionBuilderUtils.add(codeBuilder, recordComponents.get(index));
|
||||
});
|
||||
codeBuilder.add(")");
|
||||
if (metaData.useValidationApi()) {
|
||||
codeBuilder.add(")");
|
||||
}
|
||||
codeBuilder.add(";$]");
|
||||
return codeBuilder.build();
|
||||
}
|
||||
|
||||
private void addStaticFromWithMethod() {
|
||||
/*
|
||||
Adds static method that returns a "with"er view of an existing record.
|
||||
|
||||
public static With from(MyRecord from) {
|
||||
return new MyRecordBuilder.With() {
|
||||
@Override
|
||||
public String p1() {
|
||||
return from.p1();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String p2() {
|
||||
return from.p2();
|
||||
}
|
||||
};
|
||||
}
|
||||
*/
|
||||
var witherClassNameBuilder = CodeBlock.builder()
|
||||
.add("$L.$L", builderClassType.name(), metaData.withClassName());
|
||||
if (!typeVariables.isEmpty()) {
|
||||
witherClassNameBuilder.add("<");
|
||||
IntStream.range(0, typeVariables.size()).forEach(index -> {
|
||||
if (index > 0) {
|
||||
witherClassNameBuilder.add(", ");
|
||||
}
|
||||
witherClassNameBuilder.add(typeVariables.get(index).name);
|
||||
});
|
||||
witherClassNameBuilder.add(">");
|
||||
}
|
||||
var witherClassName = witherClassNameBuilder.build().toString();
|
||||
var codeBuilder = CodeBlock.builder()
|
||||
.add("return new $L", witherClassName)
|
||||
.add("() {\n").indent();
|
||||
IntStream.range(0, recordComponents.size()).forEach(index -> {
|
||||
var component = recordComponents.get(index);
|
||||
if (index > 0) {
|
||||
codeBuilder.add("\n");
|
||||
}
|
||||
codeBuilder.add("@Override\n")
|
||||
.add("public $T $L() {\n", component.typeName(), component.name())
|
||||
.indent()
|
||||
.addStatement("return from.$L()", component.name())
|
||||
.unindent()
|
||||
.add("}\n");
|
||||
});
|
||||
codeBuilder.unindent().addStatement("}");
|
||||
|
||||
var withType = ClassName.get("", witherClassName);
|
||||
var methodSpec = MethodSpec.methodBuilder("from")//metaData.copyMethodName())
|
||||
.addJavadoc("Return a \"with\"er for an existing record instance\n")
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addTypeVariables(typeVariables)
|
||||
.addParameter(recordClassType.typeName(), metaData.fromMethodName())
|
||||
.returns(withType)
|
||||
.addCode(codeBuilder.build())
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void addStaticCopyBuilderMethod() {
|
||||
/*
|
||||
Adds a copy builder method that pre-fills the builder with existing values similar to:
|
||||
|
||||
public static MyRecordBuilder builder(MyRecord from) {
|
||||
return new MyRecordBuilder(from.p1(), from.p2(), ...);
|
||||
}
|
||||
*/
|
||||
var codeBuilder = CodeBlock.builder().add("return new $T(", builderClassType.typeName());
|
||||
IntStream.range(0, recordComponents.size()).forEach(index -> {
|
||||
if (index > 0) {
|
||||
codeBuilder.add(", ");
|
||||
}
|
||||
codeBuilder.add("from.$L()", recordComponents.get(index).name());
|
||||
});
|
||||
codeBuilder.add(")");
|
||||
|
||||
var methodSpec = MethodSpec.methodBuilder(metaData.copyMethodName())
|
||||
.addJavadoc("Return a new builder with all fields set to the values taken from the given record instance\n")
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addTypeVariables(typeVariables)
|
||||
.addParameter(recordClassType.typeName(), "from")
|
||||
.returns(builderClassType.typeName())
|
||||
.addStatement(codeBuilder.build())
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void addStaticDefaultBuilderMethod() {
|
||||
/*
|
||||
Adds a the default builder method similar to:
|
||||
|
||||
public static MyRecordBuilder builder() {
|
||||
return new MyRecordBuilder();
|
||||
}
|
||||
*/
|
||||
var methodSpec = MethodSpec.methodBuilder(metaData.builderMethodName())
|
||||
.addJavadoc("Return a new builder with all fields set to default Java values\n")
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addTypeVariables(typeVariables)
|
||||
.returns(builderClassType.typeName())
|
||||
.addStatement("return new $T()", builderClassType.typeName())
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void addStaticComponentsMethod() {
|
||||
/*
|
||||
Adds a static method that converts a record instance into a stream of its component parts
|
||||
|
||||
public static Stream<Map.Entry<String, Object>> stream(MyRecord record) {
|
||||
return Stream.of(new AbstractMap.SimpleImmutableEntry<>("p1", record.p1()),
|
||||
new AbstractMap.SimpleImmutableEntry<>("p2", record.p2()));
|
||||
}
|
||||
*/
|
||||
var codeBuilder = CodeBlock.builder().add("return $T.of(", Stream.class);
|
||||
IntStream.range(0, recordComponents.size()).forEach(index -> {
|
||||
if (index > 0) {
|
||||
codeBuilder.add(",\n ");
|
||||
}
|
||||
var name = recordComponents.get(index).name();
|
||||
codeBuilder.add("new $T<>($S, record.$L())", AbstractMap.SimpleImmutableEntry.class, name, name);
|
||||
});
|
||||
codeBuilder.add(")");
|
||||
var mapEntryTypeVariables = ParameterizedTypeName.get(Map.Entry.class, String.class, Object.class);
|
||||
var mapEntryType = ParameterizedTypeName.get(ClassName.get(Stream.class), mapEntryTypeVariables);
|
||||
var methodSpec = MethodSpec.methodBuilder(metaData.componentsMethodName())
|
||||
.addJavadoc("Return a stream of the record components as map entries keyed with the component name and the value as the component value\n")
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
||||
.addParameter(recordClassType.typeName(), "record")
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addTypeVariables(typeVariables)
|
||||
.returns(mapEntryType)
|
||||
.addStatement(codeBuilder.build())
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void add1Field(ClassType component) {
|
||||
/*
|
||||
For a single record component, add a field similar to:
|
||||
|
||||
private T p;
|
||||
*/
|
||||
var fieldSpecBuilder = FieldSpec.builder(component.typeName(), component.name(), Modifier.PRIVATE);
|
||||
if (metaData.emptyDefaultForOptional()) {
|
||||
TypeName thisOptionalType = null;
|
||||
if (isOptional(component)) {
|
||||
thisOptionalType = optionalType;
|
||||
} else if (component.typeName().equals(optionalIntType)) {
|
||||
thisOptionalType = optionalIntType;
|
||||
} else if (component.typeName().equals(optionalLongType)) {
|
||||
thisOptionalType = optionalLongType;
|
||||
} else if (component.typeName().equals(optionalDoubleType)) {
|
||||
thisOptionalType = optionalDoubleType;
|
||||
}
|
||||
if (thisOptionalType != null) {
|
||||
var codeBlock = CodeBlock.builder().add("$T.empty()", thisOptionalType).build();
|
||||
fieldSpecBuilder.initializer(codeBlock);
|
||||
}
|
||||
}
|
||||
builder.addField(fieldSpecBuilder.build());
|
||||
}
|
||||
|
||||
private boolean isOptional(ClassType component) {
|
||||
if (component.typeName().equals(optionalType)) {
|
||||
return true;
|
||||
}
|
||||
return (component.typeName() instanceof ParameterizedTypeName parameterizedTypeName) && parameterizedTypeName.rawType.equals(optionalType);
|
||||
}
|
||||
|
||||
private void addNestedGetterMethod(TypeSpec.Builder classBuilder, RecordClassType component) {
|
||||
/*
|
||||
For a single record component, add a getter similar to:
|
||||
|
||||
T p();
|
||||
*/
|
||||
var methodSpecBuilder = MethodSpec.methodBuilder(component.name())
|
||||
.addJavadoc("Return the current value for the {@code $L} record component in the builder\n", component.name())
|
||||
.addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.returns(component.typeName());
|
||||
addAccessorAnnotations(component, methodSpecBuilder);
|
||||
classBuilder.addMethod(methodSpecBuilder.build());
|
||||
}
|
||||
|
||||
private boolean filterOutOverride(AnnotationSpec annotationSpec) {
|
||||
return !annotationSpec.type.equals(overrideType);
|
||||
}
|
||||
|
||||
private void addConstructorAnnotations(RecordClassType component, ParameterSpec.Builder parameterSpecBuilder) {
|
||||
if (metaData.inheritComponentAnnotations()) {
|
||||
component.getCanonicalConstructorAnnotations()
|
||||
.stream()
|
||||
.map(AnnotationSpec::get)
|
||||
.filter(this::filterOutOverride)
|
||||
.forEach(parameterSpecBuilder::addAnnotation);
|
||||
}
|
||||
}
|
||||
|
||||
private void addAccessorAnnotations(RecordClassType component, MethodSpec.Builder methodSpecBuilder) {
|
||||
if (metaData.inheritComponentAnnotations()) {
|
||||
component.getAccessorAnnotations()
|
||||
.stream()
|
||||
.map(AnnotationSpec::get)
|
||||
.filter(this::filterOutOverride)
|
||||
.forEach(methodSpecBuilder::addAnnotation);
|
||||
}
|
||||
}
|
||||
|
||||
private String capitalize(String s) {
|
||||
return (s.length() < 2) ? s.toUpperCase(Locale.ROOT) : (Character.toUpperCase(s.charAt(0)) + s.substring(1));
|
||||
}
|
||||
|
||||
private void add1CollectionBuilders(CollectionBuilderUtils.SingleItemsMetaData meta, RecordClassType component) {
|
||||
if (collectionBuilderUtils.isList(component) || collectionBuilderUtils.isSet(component)) {
|
||||
add1ListBuilder(meta, component);
|
||||
} else if (collectionBuilderUtils.isMap(component)) {
|
||||
add1MapBuilder(meta, component);
|
||||
}
|
||||
}
|
||||
|
||||
private void add1MapBuilder(CollectionBuilderUtils.SingleItemsMetaData meta, RecordClassType component) {
|
||||
/*
|
||||
For a single map record component, add a methods similar to:
|
||||
|
||||
public T addP(K key, V value) {
|
||||
if (this.p == null) {
|
||||
this.p = new HashMap<>();
|
||||
}
|
||||
this.p.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public T addP(Stream<? extends Map.Entry<K, V> i) {
|
||||
if (p == null) {
|
||||
p = new HashMap<>();
|
||||
}
|
||||
i.forEach(this.p::put);
|
||||
return this;
|
||||
}
|
||||
|
||||
public T addP(Iterable<? extends Map.Entry<K, V> i) {
|
||||
if (p == null) {
|
||||
p = new HashMap<>();
|
||||
}
|
||||
i.forEach(this.p::put);
|
||||
return this;
|
||||
}
|
||||
*/
|
||||
for (var i = 0; i < 3; ++i) {
|
||||
var codeBlockBuilder = CodeBlock.builder()
|
||||
.beginControlFlow("if (this.$L == null)", component.name())
|
||||
.addStatement("this.$L = new $T<>()", component.name(), HashMap.class)
|
||||
.endControlFlow();
|
||||
var methodSpecBuilder = MethodSpec.methodBuilder(metaData.singleItemBuilderPrefix() + capitalize(component.name()))
|
||||
.addJavadoc("Add to the internally allocated {@code HashMap} for {@code $L}\n", component.name())
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.returns(builderClassType.typeName());
|
||||
if (i == 0) {
|
||||
methodSpecBuilder.addParameter(meta.typeArguments().get(0), "key");
|
||||
methodSpecBuilder.addParameter(meta.typeArguments().get(1), "value");
|
||||
codeBlockBuilder.addStatement("this.$L.put(key, value)", component.name());
|
||||
} else {
|
||||
var parameterClass = ClassName.get((i == 1) ? Stream.class : Iterable.class);
|
||||
var entryType = ParameterizedTypeName.get(ClassName.get(Map.Entry.class), WildcardTypeName.subtypeOf(meta.typeArguments().get(0)), WildcardTypeName.subtypeOf(meta.typeArguments().get(1)));
|
||||
ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(parameterClass, WildcardTypeName.subtypeOf(entryType));
|
||||
methodSpecBuilder.addParameter(parameterizedTypeName, "i");
|
||||
codeBlockBuilder.addStatement("i.forEach(entry -> this.$L.put(entry.getKey(), entry.getValue()))", component.name());
|
||||
}
|
||||
codeBlockBuilder.addStatement("return this");
|
||||
methodSpecBuilder.addCode(codeBlockBuilder.build());
|
||||
builder.addMethod(methodSpecBuilder.build());
|
||||
}
|
||||
}
|
||||
|
||||
private void add1ListBuilder(CollectionBuilderUtils.SingleItemsMetaData meta, RecordClassType component) {
|
||||
/*
|
||||
For a single list or set record component, add methods similar to:
|
||||
|
||||
public T addP(I i) {
|
||||
if (this.p == null) {
|
||||
this.p = new ArrayList<>();
|
||||
}
|
||||
this.p.add(i);
|
||||
return this;
|
||||
}
|
||||
|
||||
public T addP(Stream<? extends I> i) {
|
||||
if (this.p == null) {
|
||||
this.p = new ArrayList<>();
|
||||
}
|
||||
this.p.addAll(i);
|
||||
return this;
|
||||
}
|
||||
|
||||
public T addP(Iterable<? extends I> i) {
|
||||
if (this.p == null) {
|
||||
this.p = new ArrayList<>();
|
||||
}
|
||||
this.p.addAll(i);
|
||||
return this;
|
||||
}
|
||||
*/
|
||||
for (var i = 0; i < 3; ++i) {
|
||||
var addClockBlock = CodeBlock.builder();
|
||||
TypeName parameter;
|
||||
if (i == 0) {
|
||||
addClockBlock.addStatement("this.$L.add(i)", component.name());
|
||||
parameter = meta.typeArguments().get(0);
|
||||
} else {
|
||||
addClockBlock.addStatement("i.forEach(this.$L::add)", component.name());
|
||||
var parameterClass = ClassName.get((i == 1) ? Stream.class : Iterable.class);
|
||||
parameter = ParameterizedTypeName.get(parameterClass, WildcardTypeName.subtypeOf(meta.typeArguments().get(0)));
|
||||
}
|
||||
var codeBlockBuilder = CodeBlock.builder()
|
||||
.beginControlFlow("if (this.$L == null)", component.name())
|
||||
.addStatement("this.$L = new $T<>()", component.name(), meta.singleItemCollectionClass())
|
||||
.endControlFlow()
|
||||
.add(addClockBlock.build())
|
||||
.addStatement("return this");
|
||||
var methodSpecBuilder = MethodSpec.methodBuilder(metaData.singleItemBuilderPrefix() + capitalize(component.name()))
|
||||
.addJavadoc("Add to the internally allocated {@code $L} for {@code $L}\n", meta.singleItemCollectionClass().getSimpleName(), component.name())
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.returns(builderClassType.typeName())
|
||||
.addParameter(parameter, "i")
|
||||
.addCode(codeBlockBuilder.build());
|
||||
builder.addMethod(methodSpecBuilder.build());
|
||||
}
|
||||
}
|
||||
|
||||
private void add1GetterMethod(RecordClassType component) {
|
||||
/*
|
||||
For a single record component, add a getter similar to:
|
||||
|
||||
public T p() {
|
||||
return p;
|
||||
}
|
||||
*/
|
||||
var methodSpecBuilder = MethodSpec.methodBuilder(component.name())
|
||||
.addJavadoc("Return the current value for the {@code $L} record component in the builder\n", component.name())
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.returns(component.typeName())
|
||||
.addStatement("return $L", component.name());
|
||||
addAccessorAnnotations(component, methodSpecBuilder);
|
||||
builder.addMethod(methodSpecBuilder.build());
|
||||
}
|
||||
|
||||
private void add1SetterMethod(RecordClassType component) {
|
||||
/*
|
||||
For a single record component, add a setter similar to:
|
||||
|
||||
public MyRecordBuilder p(T p) {
|
||||
this.p = p;
|
||||
return this;
|
||||
}
|
||||
*/
|
||||
|
||||
var methodSpec = MethodSpec.methodBuilder(component.name())
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.returns(builderClassType.typeName());
|
||||
|
||||
var collectionMetaData = collectionBuilderUtils.singleItemsMetaData(component, STANDARD_FOR_SETTER);
|
||||
var parameterSpecBuilder = collectionMetaData.map(meta -> {
|
||||
CodeBlock.Builder codeSpec = CodeBlock.builder();
|
||||
codeSpec.addStatement("this.$L = ($L != null) ? new $T<>($L) : null", component.name(), component.name(), meta.singleItemCollectionClass(), component.name());
|
||||
methodSpec.addJavadoc("Re-create the internally allocated {@code $L} for {@code $L} by copying the argument\n", meta.singleItemCollectionClass().getSimpleName(), component.name())
|
||||
.addCode(codeSpec.build());
|
||||
return ParameterSpec.builder(meta.wildType(), component.name());
|
||||
}).orElseGet(() -> {
|
||||
methodSpec.addJavadoc("Set a new value for the {@code $L} record component in the builder\n", component.name())
|
||||
.addStatement("this.$L = $L", component.name(), component.name());
|
||||
return ParameterSpec.builder(component.typeName(), component.name());
|
||||
});
|
||||
addConstructorAnnotations(component, parameterSpecBuilder);
|
||||
methodSpec.addStatement("return this").addParameter(parameterSpecBuilder.build());
|
||||
builder.addMethod(methodSpec.build());
|
||||
}
|
||||
|
||||
private List<TypeVariableName> typeVariablesWithReturn() {
|
||||
var variables = new ArrayList<TypeVariableName>();
|
||||
variables.add(rType);
|
||||
variables.addAll(typeVariables);
|
||||
return variables;
|
||||
}
|
||||
|
||||
private MethodSpec buildFunctionalHandler(String className, String methodName, boolean isMap) {
|
||||
/*
|
||||
Build a Functional handler ala:
|
||||
|
||||
default <R> R map(Function<R, T> proc) {
|
||||
return proc.apply(p());
|
||||
}
|
||||
*/
|
||||
var localTypeVariables = isMap ? typeVariablesWithReturn() : typeVariables;
|
||||
var typeName = localTypeVariables.isEmpty() ? ClassName.get("", className) : ParameterizedTypeName.get(ClassName.get("", className), localTypeVariables.toArray(TypeName[]::new));
|
||||
var methodBuilder = MethodSpec.methodBuilder(methodName)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
|
||||
.addParameter(typeName, "proc");
|
||||
var codeBlockBuilder = CodeBlock.builder();
|
||||
if (isMap) {
|
||||
methodBuilder.addJavadoc("Map record components into a new object");
|
||||
methodBuilder.addTypeVariable(rType);
|
||||
methodBuilder.returns(rType);
|
||||
codeBlockBuilder.add("return ");
|
||||
} else {
|
||||
methodBuilder.addJavadoc("Perform an operation on record components");
|
||||
}
|
||||
codeBlockBuilder.add("proc.apply(");
|
||||
addComponentCallsAsArguments(-1, codeBlockBuilder);
|
||||
codeBlockBuilder.add(");");
|
||||
methodBuilder.addCode(codeBlockBuilder.build());
|
||||
return methodBuilder.build();
|
||||
}
|
||||
|
||||
private TypeSpec buildFunctionalInterface(String className, boolean isMap) {
|
||||
/*
|
||||
Build a Functional interface ala:
|
||||
|
||||
@FunctionalInterface
|
||||
interface Function<R, T> {
|
||||
R apply(T a);
|
||||
}
|
||||
*/
|
||||
var localTypeVariables = isMap ? typeVariablesWithReturn() : typeVariables;
|
||||
var methodBuilder = MethodSpec.methodBuilder("apply").addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT);
|
||||
recordComponents.forEach(component -> {
|
||||
var parameterSpecBuilder = ParameterSpec.builder(component.typeName(), component.name());
|
||||
addConstructorAnnotations(component, parameterSpecBuilder);
|
||||
methodBuilder.addParameter(parameterSpecBuilder.build());
|
||||
});
|
||||
if (isMap) {
|
||||
methodBuilder.returns(rType);
|
||||
}
|
||||
return TypeSpec.interfaceBuilder(className)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addAnnotation(FunctionalInterface.class)
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
||||
.addTypeVariables(localTypeVariables)
|
||||
.addMethod(methodBuilder.build())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.processor;
|
||||
|
||||
import com.squareup.javapoet.*;
|
||||
import io.soabase.recordbuilder.core.IgnoreDefaultMethod;
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.type.TypeKind;
|
||||
import javax.tools.Diagnostic;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static io.soabase.recordbuilder.processor.ElementUtils.getBuilderName;
|
||||
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.generatedRecordInterfaceAnnotation;
|
||||
|
||||
class InternalRecordInterfaceProcessor {
|
||||
private final ProcessingEnvironment processingEnv;
|
||||
private final String packageName;
|
||||
private final TypeSpec recordType;
|
||||
private final List<Component> recordComponents;
|
||||
private final TypeElement iface;
|
||||
private final ClassType recordClassType;
|
||||
private final List<String> alternateMethods;
|
||||
|
||||
private static final String FAKE_METHOD_NAME = "__FAKE__";
|
||||
|
||||
private static final Set<String> javaBeanPrefixes = Set.of("get", "is");
|
||||
|
||||
private record Component(ExecutableElement element, Optional<String> alternateName) {
|
||||
}
|
||||
|
||||
InternalRecordInterfaceProcessor(ProcessingEnvironment processingEnv, TypeElement iface, boolean addRecordBuilder, RecordBuilder.Options metaData, Optional<String> packageNameOpt, boolean fromTemplate) {
|
||||
this.processingEnv = processingEnv;
|
||||
packageName = packageNameOpt.orElseGet(() -> ElementUtils.getPackageName(iface));
|
||||
recordComponents = getRecordComponents(iface);
|
||||
this.iface = iface;
|
||||
|
||||
ClassType ifaceClassType = ElementUtils.getClassType(iface, iface.getTypeParameters());
|
||||
recordClassType = ElementUtils.getClassType(packageName, getBuilderName(iface, metaData, ifaceClassType, metaData.interfaceSuffix()), iface.getTypeParameters());
|
||||
List<TypeVariableName> typeVariables = iface.getTypeParameters().stream().map(TypeVariableName::get).collect(Collectors.toList());
|
||||
|
||||
MethodSpec methodSpec = generateArgumentList();
|
||||
|
||||
TypeSpec.Builder builder = TypeSpec.classBuilder(recordClassType.name())
|
||||
.addSuperinterface(iface.asType())
|
||||
.addMethod(methodSpec)
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addAnnotation(generatedRecordInterfaceAnnotation)
|
||||
.addTypeVariables(typeVariables);
|
||||
|
||||
if (addRecordBuilder) {
|
||||
ClassType builderClassType = ElementUtils.getClassType(packageName, getBuilderName(iface, metaData, recordClassType, metaData.suffix()) + "." + metaData.withClassName(), iface.getTypeParameters());
|
||||
builder.addAnnotation(RecordBuilder.class);
|
||||
builder.addSuperinterface(builderClassType.typeName());
|
||||
if (fromTemplate) {
|
||||
builder.addAnnotation(AnnotationSpec.get(metaData));
|
||||
} else {
|
||||
var options = iface.getAnnotation(RecordBuilder.Options.class);
|
||||
if (options != null) {
|
||||
builder.addAnnotation(AnnotationSpec.get(options));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
alternateMethods = buildAlternateMethods(recordComponents);
|
||||
|
||||
recordType = builder.build();
|
||||
}
|
||||
|
||||
boolean isValid() {
|
||||
return !recordComponents.isEmpty();
|
||||
}
|
||||
|
||||
TypeSpec recordType() {
|
||||
return recordType;
|
||||
}
|
||||
|
||||
String packageName() {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
ClassType recordClassType() {
|
||||
return recordClassType;
|
||||
}
|
||||
|
||||
String toRecord(String classSource) {
|
||||
// javapoet does yet support records - so a class was created and we can reshape it
|
||||
// The class will look something like this:
|
||||
/*
|
||||
// Auto generated by io.soabase.recordbuilder.core.RecordBuilder: https://github.com/Randgalt/record-builder
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
import javax.annotation.processing.Generated;
|
||||
|
||||
@Generated("io.soabase.recordbuilder.core.RecordInterface")
|
||||
@RecordBuilder
|
||||
public class MyRecord implements MyInterface {
|
||||
void __FAKE__(String name, int age) {
|
||||
}
|
||||
}
|
||||
*/
|
||||
Pattern pattern = Pattern.compile("(.*)(implements.*)(\\{)(.*" + FAKE_METHOD_NAME + ")(\\(.*\\))(.*)", Pattern.MULTILINE | Pattern.DOTALL);
|
||||
Matcher matcher = pattern.matcher(classSource);
|
||||
if (!matcher.find() || matcher.groupCount() != 6) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Internal error generating record. Group count: " + matcher.groupCount(), iface);
|
||||
}
|
||||
|
||||
String declaration = matcher.group(1).trim().replace("class", "record");
|
||||
String implementsSection = matcher.group(2).trim();
|
||||
String argumentList = matcher.group(5).trim();
|
||||
|
||||
StringBuilder fixedRecord = new StringBuilder(declaration).append(argumentList).append(' ').append(implementsSection).append(" {");
|
||||
alternateMethods.forEach(method -> fixedRecord.append('\n').append(method));
|
||||
fixedRecord.append('}');
|
||||
return fixedRecord.toString();
|
||||
}
|
||||
|
||||
private MethodSpec generateArgumentList() {
|
||||
MethodSpec.Builder builder = MethodSpec.methodBuilder(FAKE_METHOD_NAME);
|
||||
recordComponents.forEach(component -> {
|
||||
String name = component.alternateName.orElseGet(() -> component.element.getSimpleName().toString());
|
||||
ParameterSpec parameterSpec = ParameterSpec.builder(ClassName.get(component.element.getReturnType()), name).build();
|
||||
builder.addTypeVariables(component.element.getTypeParameters().stream().map(TypeVariableName::get).collect(Collectors.toList()));
|
||||
builder.addParameter(parameterSpec);
|
||||
});
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private List<String> buildAlternateMethods(List<Component> recordComponents) {
|
||||
return recordComponents.stream()
|
||||
.filter(component -> component.alternateName.isPresent())
|
||||
.map(component -> {
|
||||
var method = MethodSpec.methodBuilder(component.element.getSimpleName().toString())
|
||||
.addAnnotation(Override.class)
|
||||
.addAnnotation(generatedRecordInterfaceAnnotation)
|
||||
.returns(ClassName.get(component.element.getReturnType()))
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addCode("return $L();", component.alternateName.get())
|
||||
.build();
|
||||
return method.toString();
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<Component> getRecordComponents(TypeElement iface) {
|
||||
List<Component> components = new ArrayList<>();
|
||||
try {
|
||||
getRecordComponents(iface, components, new HashSet<>(), new HashSet<>());
|
||||
if (components.isEmpty()) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Annotated interface has no component methods", iface);
|
||||
}
|
||||
} catch (IllegalInterface e) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage(), iface);
|
||||
components = Collections.emptyList();
|
||||
}
|
||||
return components;
|
||||
}
|
||||
|
||||
private static class IllegalInterface extends RuntimeException {
|
||||
public IllegalInterface(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void getRecordComponents(TypeElement iface, Collection<Component> components, Set<String> visitedSet, Set<String> usedNames) {
|
||||
if (!visitedSet.add(iface.getQualifiedName().toString())) {
|
||||
return;
|
||||
}
|
||||
|
||||
iface.getEnclosedElements().stream()
|
||||
.filter(element -> (element.getKind() == ElementKind.METHOD) && !(element.getModifiers().contains(Modifier.STATIC)))
|
||||
.map(element -> ((ExecutableElement) element))
|
||||
.filter(element -> {
|
||||
if (element.isDefault()) {
|
||||
return element.getAnnotation(IgnoreDefaultMethod.class) == null;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.peek(element -> {
|
||||
if (!element.getParameters().isEmpty() || element.getReturnType().getKind() == TypeKind.VOID) {
|
||||
throw new IllegalInterface(String.format("Non-static, non-default methods must take no arguments and must return a value. Bad method: %s.%s()", iface.getSimpleName(), element.getSimpleName()));
|
||||
}
|
||||
if (!element.getTypeParameters().isEmpty()) {
|
||||
throw new IllegalInterface(String.format("Interface methods cannot have type parameters. Bad method: %s.%s()", iface.getSimpleName(), element.getSimpleName()));
|
||||
}
|
||||
})
|
||||
.filter(element -> usedNames.add(element.getSimpleName().toString()))
|
||||
.map(element -> new Component(element, stripBeanPrefix(element.getSimpleName().toString())))
|
||||
.collect(Collectors.toCollection(() -> components));
|
||||
iface.getInterfaces().forEach(parentIface -> {
|
||||
TypeElement parentIfaceElement = (TypeElement) processingEnv.getTypeUtils().asElement(parentIface);
|
||||
getRecordComponents(parentIfaceElement, components, visitedSet, usedNames);
|
||||
});
|
||||
}
|
||||
|
||||
private Optional<String> stripBeanPrefix(String name) {
|
||||
return javaBeanPrefixes.stream()
|
||||
.filter(prefix -> name.startsWith(prefix) && (name.length() > prefix.length()))
|
||||
.findFirst()
|
||||
.map(prefix -> {
|
||||
var stripped = name.substring(prefix.length());
|
||||
return Character.toLowerCase(stripped.charAt(0)) + stripped.substring(1);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
/**
|
||||
* Copyright 2016 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.processor;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilderMetaData;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
class RecordBuilderMetaDataLoader {
|
||||
private final RecordBuilderMetaData metaData;
|
||||
|
||||
RecordBuilderMetaDataLoader(String metaDataClassName, Consumer<String> logger) {
|
||||
RecordBuilderMetaData localMetaData = null;
|
||||
if ((metaDataClassName != null) && !metaDataClassName.isEmpty()) {
|
||||
try {
|
||||
Class<?> clazz = Class.forName(metaDataClassName);
|
||||
localMetaData = (RecordBuilderMetaData) clazz.getDeclaredConstructor().newInstance();
|
||||
logger.accept("Found meta data: " + localMetaData.getClass());
|
||||
} catch (Exception e) {
|
||||
logger.accept("Could not load meta data: " + metaDataClassName + " - " + e.getMessage());
|
||||
}
|
||||
}
|
||||
metaData = (localMetaData != null) ? localMetaData : RecordBuilderMetaData.DEFAULT;
|
||||
}
|
||||
|
||||
RecordBuilderMetaData getMetaData() {
|
||||
return metaData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.processor;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
class RecordBuilderOptions {
|
||||
private static final Map<String, Object> defaultValues = buildDefaultValues();
|
||||
|
||||
static RecordBuilder.Options build(Map<String, String> options) {
|
||||
return (RecordBuilder.Options)Proxy.newProxyInstance(RecordBuilderOptions.class.getClassLoader(), new Class[]{RecordBuilder.Options.class}, (proxy, method, args) -> {
|
||||
var name = method.getName();
|
||||
var defaultValue = defaultValues.get(name);
|
||||
var option = options.get(name);
|
||||
if (option != null) {
|
||||
if (defaultValue instanceof String) {
|
||||
return option;
|
||||
}
|
||||
if (defaultValue instanceof Boolean) {
|
||||
return Boolean.parseBoolean(option);
|
||||
}
|
||||
if (defaultValue instanceof Integer) {
|
||||
return Integer.parseInt(option);
|
||||
}
|
||||
if (defaultValue instanceof Long) {
|
||||
return Long.parseLong(option);
|
||||
}
|
||||
if (defaultValue instanceof Double) {
|
||||
return Double.parseDouble(option);
|
||||
}
|
||||
throw new IllegalArgumentException("Unhandled option type: " + defaultValue.getClass());
|
||||
}
|
||||
return defaultValue;
|
||||
});
|
||||
}
|
||||
|
||||
private static Map<String, Object> buildDefaultValues() {
|
||||
var workMap = new HashMap<String, Object>();
|
||||
for ( Method method : RecordBuilder.Options.class.getDeclaredMethods()) {
|
||||
workMap.put(method.getName(), method.getDefaultValue());
|
||||
}
|
||||
workMap.put("toString", "Generated RecordBuilder.Options");
|
||||
return Map.copyOf(workMap);
|
||||
}
|
||||
|
||||
private RecordBuilderOptions() {
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2016 Jordan Zimmerman
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,268 +15,206 @@
|
||||
*/
|
||||
package io.soabase.recordbuilder.processor;
|
||||
|
||||
import com.squareup.javapoet.*;
|
||||
import io.soabase.recordbuilder.core.RecordBuilderMetaData;
|
||||
import com.squareup.javapoet.AnnotationSpec;
|
||||
import com.squareup.javapoet.JavaFile;
|
||||
import com.squareup.javapoet.TypeSpec;
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
import io.soabase.recordbuilder.core.RecordInterface;
|
||||
|
||||
import javax.annotation.processing.*;
|
||||
import javax.annotation.processing.AbstractProcessor;
|
||||
import javax.annotation.processing.Filer;
|
||||
import javax.annotation.processing.Generated;
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.element.PackageElement;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.tools.Diagnostic;
|
||||
import javax.tools.JavaFileObject;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class RecordBuilderProcessor
|
||||
extends AbstractProcessor {
|
||||
private static final String RECORD_BUILDER = RecordBuilder.class.getName();
|
||||
private static final String RECORD_BUILDER_INCLUDE = RecordBuilder.Include.class.getName().replace('$', '.');
|
||||
private static final String RECORD_INTERFACE = RecordInterface.class.getName();
|
||||
private static final String RECORD_INTERFACE_INCLUDE = RecordInterface.Include.class.getName().replace('$', '.');
|
||||
|
||||
static final AnnotationSpec generatedRecordBuilderAnnotation = AnnotationSpec.builder(Generated.class).addMember("value", "$S", RecordBuilder.class.getName()).build();
|
||||
static final AnnotationSpec generatedRecordInterfaceAnnotation = AnnotationSpec.builder(Generated.class).addMember("value", "$S", RecordInterface.class.getName()).build();
|
||||
|
||||
@SupportedAnnotationTypes("io.soabase.recordbuilder.core.RecordBuilder")
|
||||
@SupportedSourceVersion(SourceVersion.RELEASE_14)
|
||||
public class RecordBuilderProcessor extends AbstractProcessor {
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
annotations.forEach(annotation -> roundEnv.getElementsAnnotatedWith(annotation).forEach(this::process));
|
||||
return true;
|
||||
annotations.forEach(annotation -> roundEnv.getElementsAnnotatedWith(annotation).forEach(element -> process(annotation, element)));
|
||||
return false;
|
||||
}
|
||||
|
||||
private void process(Element element) {
|
||||
var messager = processingEnv.getMessager();
|
||||
if (element.getKind() != ElementKind.RECORD) {
|
||||
messager.printMessage(Diagnostic.Kind.ERROR, "RecordBuilder only valid for records.", element);
|
||||
@Override
|
||||
public Set<String> getSupportedAnnotationTypes() {
|
||||
return Set.of("*");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourceVersion getSupportedSourceVersion() {
|
||||
// we don't directly return RELEASE_14 as that may
|
||||
// not exist in prior releases
|
||||
// if we're running on an older release, returning latest()
|
||||
// is fine as we won't encounter any records anyway
|
||||
return SourceVersion.latest();
|
||||
}
|
||||
|
||||
private void process(TypeElement annotation, Element element) {
|
||||
String annotationClass = annotation.getQualifiedName().toString();
|
||||
if (annotationClass.equals(RECORD_BUILDER)) {
|
||||
var typeElement = (TypeElement) element;
|
||||
processRecordBuilder(typeElement, getMetaData(typeElement), Optional.empty());
|
||||
} else if (annotationClass.equals(RECORD_INTERFACE)) {
|
||||
var typeElement = (TypeElement) element;
|
||||
processRecordInterface(typeElement, element.getAnnotation(RecordInterface.class).addRecordBuilder(), getMetaData(typeElement), Optional.empty(), false);
|
||||
} else if (annotationClass.equals(RECORD_BUILDER_INCLUDE) || annotationClass.equals(RECORD_INTERFACE_INCLUDE)) {
|
||||
processIncludes(element, getMetaData(element), annotationClass);
|
||||
} else {
|
||||
var recordBuilderTemplate = annotation.getAnnotation(RecordBuilder.Template.class);
|
||||
if (recordBuilderTemplate != null) {
|
||||
if (recordBuilderTemplate.asRecordInterface()) {
|
||||
processRecordInterface((TypeElement) element, true, recordBuilderTemplate.options(), Optional.empty(), true);
|
||||
} else {
|
||||
processRecordBuilder((TypeElement) element, recordBuilderTemplate.options(), Optional.empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private RecordBuilder.Options getMetaData(Element element) {
|
||||
var recordSpecificMetaData = element.getAnnotation(RecordBuilder.Options.class);
|
||||
return (recordSpecificMetaData != null) ? recordSpecificMetaData : RecordBuilderOptions.build(processingEnv.getOptions());
|
||||
}
|
||||
|
||||
private void processIncludes(Element element, RecordBuilder.Options metaData, String annotationClass) {
|
||||
var isRecordBuilderInclude = annotationClass.equals(RECORD_BUILDER_INCLUDE);
|
||||
var annotationMirrorOpt = ElementUtils.findAnnotationMirror(processingEnv, element, annotationClass);
|
||||
if (annotationMirrorOpt.isEmpty()) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not get annotation mirror for: " + annotationClass, element);
|
||||
} else {
|
||||
var includeHelper = new IncludeHelper(processingEnv, element, annotationMirrorOpt.get(), isRecordBuilderInclude);
|
||||
if (includeHelper.isValid()) {
|
||||
var packagePattern = ElementUtils.getStringAttribute(ElementUtils.getAnnotationValue(includeHelper.getAnnotationValues(), "packagePattern").orElse(null), "*");
|
||||
for (var typeElement : includeHelper.getClassTypeElements()) {
|
||||
var packageName = buildPackageName(packagePattern, element, typeElement);
|
||||
if (packageName != null) {
|
||||
if (isRecordBuilderInclude) {
|
||||
processRecordBuilder(typeElement, metaData, Optional.of(packageName));
|
||||
} else {
|
||||
var addRecordBuilderOpt = ElementUtils.getAnnotationValue(includeHelper.getAnnotationValues(), "addRecordBuilder");
|
||||
var addRecordBuilder = addRecordBuilderOpt.map(ElementUtils::getBooleanAttribute).orElse(true);
|
||||
processRecordInterface(typeElement, addRecordBuilder, metaData, Optional.of(packageName), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String buildPackageName(String packagePattern, Element builderElement, TypeElement includedClass) {
|
||||
PackageElement includedClassPackage = findPackageElement(includedClass, includedClass);
|
||||
if (includedClassPackage == null) {
|
||||
return null;
|
||||
}
|
||||
String replaced = packagePattern.replace("*", includedClassPackage.getQualifiedName().toString());
|
||||
if (builderElement instanceof PackageElement) {
|
||||
return replaced.replace("@", ((PackageElement) builderElement).getQualifiedName().toString());
|
||||
}
|
||||
return replaced.replace("@", ((PackageElement) builderElement.getEnclosingElement()).getQualifiedName().toString());
|
||||
}
|
||||
|
||||
private PackageElement findPackageElement(Element actualElement, Element includedClass) {
|
||||
if (includedClass == null) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Element has not package", actualElement);
|
||||
return null;
|
||||
}
|
||||
if (includedClass.getEnclosingElement() instanceof PackageElement) {
|
||||
return (PackageElement) includedClass.getEnclosingElement();
|
||||
}
|
||||
return findPackageElement(actualElement, includedClass.getEnclosingElement());
|
||||
}
|
||||
|
||||
private void processRecordInterface(TypeElement element, boolean addRecordBuilder, RecordBuilder.Options metaData, Optional<String> packageName, boolean fromTemplate) {
|
||||
if (!element.getKind().isInterface()) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "RecordInterface only valid for interfaces.", element);
|
||||
return;
|
||||
}
|
||||
var metaData = new RecordBuilderMetaDataLoader(processingEnv.getOptions().get(RecordBuilderMetaData.JAVAC_OPTION_NAME), s -> messager.printMessage(Diagnostic.Kind.NOTE, s)).getMetaData();
|
||||
process((TypeElement) element, metaData);
|
||||
}
|
||||
|
||||
private void process(TypeElement record, RecordBuilderMetaData metaData) {
|
||||
var recordClassType = ElementUtils.getClassType(record, record.getTypeParameters());
|
||||
var packageName = ElementUtils.getPackageName(record);
|
||||
var builderClassType = ElementUtils.getClassType(packageName, getBuilderName(record, metaData, recordClassType), record.getTypeParameters());
|
||||
var typeVariables = record.getTypeParameters().stream().map(TypeVariableName::get).collect(Collectors.toList());
|
||||
var recordComponents = record.getRecordComponents().stream().map(ElementUtils::getClassType).collect(Collectors.toList());
|
||||
|
||||
var builder = TypeSpec.classBuilder(builderClassType.name()).addModifiers(Modifier.PUBLIC);
|
||||
builder.addTypeVariables(typeVariables);
|
||||
|
||||
addDefaultConstructor(builder);
|
||||
addAllArgsConstructor(builder, recordComponents);
|
||||
addStaticDefaultBuilderMethod(builder, builderClassType, typeVariables, metaData);
|
||||
addStaticCopyMethod(builder, builderClassType, recordClassType, recordComponents, typeVariables, metaData);
|
||||
addBuildMethod(builder, recordClassType, recordComponents, metaData);
|
||||
addToStringMethod(builder, builderClassType, recordComponents);
|
||||
recordComponents.forEach(component -> {
|
||||
add1Field(builder, component);
|
||||
add1SetterMethod(builder, component, builderClassType);
|
||||
});
|
||||
|
||||
writeJavaFile(record, packageName, builderClassType, builder, metaData);
|
||||
}
|
||||
|
||||
private String getBuilderName(TypeElement record, RecordBuilderMetaData metaData, ClassType recordClassType) {
|
||||
// generate the record builder class name
|
||||
var baseName = recordClassType.name() + metaData.suffix();
|
||||
return metaData.prefixEnclosingClassNames() ? (getBuilderNamePrefix(record.getEnclosingElement()) + baseName) : baseName;
|
||||
}
|
||||
|
||||
private String getBuilderNamePrefix(Element element) {
|
||||
// prefix enclosing class names if this record is nested in a class
|
||||
if (element instanceof TypeElement) {
|
||||
return getBuilderNamePrefix(element.getEnclosingElement()) + element.getSimpleName().toString();
|
||||
var internalProcessor = new InternalRecordInterfaceProcessor(processingEnv, element, addRecordBuilder, metaData, packageName, fromTemplate);
|
||||
if (!internalProcessor.isValid()) {
|
||||
return;
|
||||
}
|
||||
return "";
|
||||
writeRecordInterfaceJavaFile(element, internalProcessor.packageName(), internalProcessor.recordClassType(), internalProcessor.recordType(), metaData, internalProcessor::toRecord);
|
||||
}
|
||||
|
||||
private void addDefaultConstructor(TypeSpec.Builder builder) {
|
||||
/*
|
||||
Adds a default constructor similar to:
|
||||
|
||||
private MyRecordBuilder() {
|
||||
}
|
||||
*/
|
||||
var constructorBuilder = MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE);
|
||||
builder.addMethod(constructorBuilder.build());
|
||||
private void processRecordBuilder(TypeElement record, RecordBuilder.Options metaData, Optional<String> packageName) {
|
||||
// we use string based name comparison for the element kind,
|
||||
// as the ElementKind.RECORD enum doesn't exist on JRE releases
|
||||
// older than Java 14, and we don't want to throw unexpected
|
||||
// NoSuchFieldErrors
|
||||
if (!"RECORD".equals(record.getKind().name())) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "RecordBuilder only valid for records.", record);
|
||||
return;
|
||||
}
|
||||
var internalProcessor = new InternalRecordBuilderProcessor(processingEnv, record, metaData, packageName);
|
||||
writeRecordBuilderJavaFile(record, internalProcessor.packageName(), internalProcessor.builderClassType(), internalProcessor.builderType(), metaData);
|
||||
}
|
||||
|
||||
private void addAllArgsConstructor(TypeSpec.Builder builder, List<ClassType> recordComponents) {
|
||||
/*
|
||||
Adds an all-args constructor similar to:
|
||||
|
||||
private MyRecordBuilder(int p1, T p2, ...) {
|
||||
this.p1 = p1;
|
||||
this.p2 = p2;
|
||||
...
|
||||
}
|
||||
*/
|
||||
var constructorBuilder = MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE);
|
||||
recordComponents.forEach(component -> {
|
||||
constructorBuilder.addParameter(component.typeName(), component.name());
|
||||
var codeBuilder = CodeBlock.builder().add("this.$L = $L", component.name(), component.name());
|
||||
constructorBuilder.addStatement(codeBuilder.build());
|
||||
});
|
||||
builder.addMethod(constructorBuilder.build());
|
||||
}
|
||||
|
||||
private void addToStringMethod(TypeSpec.Builder builder, ClassType builderClassType, List<ClassType> recordComponents) {
|
||||
/*
|
||||
add a toString() method similar to:
|
||||
|
||||
public String toString() {
|
||||
return "MyRecord[p1=blah, p2=blah]";
|
||||
}
|
||||
*/
|
||||
var codeBuilder = CodeBlock.builder().add("return \"$L[", builderClassType.name());
|
||||
IntStream.range(0, recordComponents.size()).forEach(index -> {
|
||||
if (index > 0) {
|
||||
codeBuilder.add(", ");
|
||||
}
|
||||
String name = recordComponents.get(index).name();
|
||||
codeBuilder.add("$L=\" + $L + \"", name, name);
|
||||
});
|
||||
codeBuilder.add("]\"");
|
||||
|
||||
var methodSpec = MethodSpec.methodBuilder("toString")
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addAnnotation(Override.class)
|
||||
.returns(String.class)
|
||||
.addStatement(codeBuilder.build())
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void addBuildMethod(TypeSpec.Builder builder, ClassType recordClassType, List<ClassType> recordComponents, RecordBuilderMetaData metaData) {
|
||||
/*
|
||||
Adds the build method that generates the record similar to:
|
||||
|
||||
public MyRecord build() {
|
||||
return new MyRecord(p1, p2, ...);
|
||||
}
|
||||
*/
|
||||
var codeBuilder = CodeBlock.builder().add("return new $T(", recordClassType.typeName());
|
||||
IntStream.range(0, recordComponents.size()).forEach(index -> {
|
||||
if (index > 0) {
|
||||
codeBuilder.add(", ");
|
||||
}
|
||||
codeBuilder.add("$L", recordComponents.get(index).name());
|
||||
});
|
||||
codeBuilder.add(")");
|
||||
|
||||
var methodSpec = MethodSpec.methodBuilder(metaData.buildMethodName())
|
||||
.addJavadoc("Return a new record instance with all fields set to the current values in this builder\n")
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.returns(recordClassType.typeName())
|
||||
.addStatement(codeBuilder.build())
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void addStaticCopyMethod(TypeSpec.Builder builder, ClassType builderClassType, ClassType recordClassType, List<ClassType> recordComponents, List<TypeVariableName> typeVariables, RecordBuilderMetaData metaData) {
|
||||
/*
|
||||
Adds a copy builder method that pre-fills the builder with existing values similar to:
|
||||
|
||||
public static MyRecordBuilder builder(MyRecord from) {
|
||||
return new MyRecordBuilder(from.p1(), from.p2(), ...);
|
||||
}
|
||||
*/
|
||||
var codeBuilder = CodeBlock.builder().add("return new $T(", builderClassType.typeName());
|
||||
IntStream.range(0, recordComponents.size()).forEach(index -> {
|
||||
if (index > 0) {
|
||||
codeBuilder.add(", ");
|
||||
}
|
||||
codeBuilder.add("from.$L()", recordComponents.get(index).name());
|
||||
});
|
||||
codeBuilder.add(")");
|
||||
|
||||
var methodSpec = MethodSpec.methodBuilder(metaData.copyMethodName())
|
||||
.addJavadoc("Return a new builder with all fields set to the values taken from the given record instance\n")
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
||||
.addTypeVariables(typeVariables)
|
||||
.addParameter(recordClassType.typeName(), "from")
|
||||
.returns(builderClassType.typeName())
|
||||
.addStatement(codeBuilder.build())
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void addStaticDefaultBuilderMethod(TypeSpec.Builder builder, ClassType builderClassType, List<TypeVariableName> typeVariables, RecordBuilderMetaData metaData) {
|
||||
/*
|
||||
Adds a the default builder method similar to:
|
||||
|
||||
public static MyRecordBuilder builder() {
|
||||
return new MyRecordBuilder();
|
||||
}
|
||||
*/
|
||||
var methodSpec = MethodSpec.methodBuilder(metaData.builderMethodName())
|
||||
.addJavadoc("Return a new builder with all fields set to default Java values\n")
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
||||
.addTypeVariables(typeVariables)
|
||||
.returns(builderClassType.typeName())
|
||||
.addStatement("return new $T()", builderClassType.typeName())
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void add1Field(TypeSpec.Builder builder, ClassType component) {
|
||||
/*
|
||||
For a single record component, add a field similar to:
|
||||
|
||||
private T p;
|
||||
*/
|
||||
var fieldSpec = FieldSpec.builder(component.typeName(), component.name(), Modifier.PRIVATE).build();
|
||||
builder.addField(fieldSpec);
|
||||
}
|
||||
|
||||
private void add1SetterMethod(TypeSpec.Builder builder, ClassType component, ClassType builderClassType) {
|
||||
/*
|
||||
For a single record component, add a setter similar to:
|
||||
|
||||
public MyRecordBuilder p(T p) {
|
||||
this.p = p;
|
||||
return this;
|
||||
}
|
||||
*/
|
||||
var parameterSpec = ParameterSpec.builder(component.typeName(), component.name()).build();
|
||||
var methodSpec = MethodSpec.methodBuilder(component.name())
|
||||
.addJavadoc("Set a new value for this record component in the builder\n")
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addParameter(parameterSpec)
|
||||
.returns(builderClassType.typeName())
|
||||
.addStatement("this.$L = $L", component.name(), component.name())
|
||||
.addStatement("return this")
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
}
|
||||
|
||||
private void writeJavaFile(TypeElement record, String packageName, ClassType builderClassType, TypeSpec.Builder builder, RecordBuilderMetaData metaData) {
|
||||
private void writeRecordBuilderJavaFile(TypeElement record, String packageName, ClassType builderClassType, TypeSpec builderType, RecordBuilder.Options metaData) {
|
||||
// produces the Java file
|
||||
var javaFileBuilder = JavaFile.builder(packageName, builder.build())
|
||||
.indent(metaData.fileIndent());
|
||||
JavaFile javaFile = javaFileBuilder(packageName, builderType, metaData);
|
||||
Filer filer = processingEnv.getFiler();
|
||||
try {
|
||||
String fullyQualifiedName = packageName.isEmpty() ? builderClassType.name() : (packageName + "." + builderClassType.name());
|
||||
JavaFileObject sourceFile = filer.createSourceFile(fullyQualifiedName);
|
||||
try (Writer writer = sourceFile.openWriter()) {
|
||||
javaFile.writeTo(writer);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
handleWriteError(record, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeRecordInterfaceJavaFile(TypeElement element, String packageName, ClassType classType, TypeSpec type, RecordBuilder.Options metaData, Function<String, String> toRecordProc) {
|
||||
JavaFile javaFile = javaFileBuilder(packageName, type, metaData);
|
||||
|
||||
String classSourceCode = javaFile.toString();
|
||||
int generatedIndex = classSourceCode.indexOf("@Generated");
|
||||
String recordSourceCode = toRecordProc.apply(classSourceCode);
|
||||
|
||||
Filer filer = processingEnv.getFiler();
|
||||
try {
|
||||
String fullyQualifiedName = packageName.isEmpty() ? classType.name() : (packageName + "." + classType.name());
|
||||
JavaFileObject sourceFile = filer.createSourceFile(fullyQualifiedName);
|
||||
try (Writer writer = sourceFile.openWriter()) {
|
||||
writer.write(recordSourceCode);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
handleWriteError(element, e);
|
||||
}
|
||||
}
|
||||
|
||||
private JavaFile javaFileBuilder(String packageName, TypeSpec type, RecordBuilder.Options metaData) {
|
||||
var javaFileBuilder = JavaFile.builder(packageName, type).skipJavaLangImports(true).indent(metaData.fileIndent());
|
||||
var comment = metaData.fileComment();
|
||||
if ((comment != null) && !comment.isEmpty()) {
|
||||
javaFileBuilder.addFileComment(comment);
|
||||
}
|
||||
JavaFile javaFile = javaFileBuilder.build();
|
||||
Filer filer = processingEnv.getFiler();
|
||||
try
|
||||
{
|
||||
String fullyQualifiedName = packageName.isEmpty() ? builderClassType.name() : (packageName + "." + builderClassType.name());
|
||||
JavaFileObject sourceFile = filer.createSourceFile(fullyQualifiedName);
|
||||
try ( Writer writer = sourceFile.openWriter() )
|
||||
{
|
||||
javaFile.writeTo(writer);
|
||||
}
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
String message = "Could not create source file";
|
||||
if ( e.getMessage() != null )
|
||||
{
|
||||
message = message + ": " + e.getMessage();
|
||||
}
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, record);
|
||||
return javaFileBuilder.build();
|
||||
}
|
||||
|
||||
private void handleWriteError(TypeElement element, IOException e) {
|
||||
String message = "Could not create source file";
|
||||
if (e.getMessage() != null) {
|
||||
message = message + ": " + e.getMessage();
|
||||
}
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, element);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.processor;
|
||||
|
||||
import com.squareup.javapoet.TypeName;
|
||||
|
||||
import javax.lang.model.element.AnnotationMirror;
|
||||
import java.util.List;
|
||||
|
||||
public class RecordClassType extends ClassType {
|
||||
private final TypeName rawTypeName;
|
||||
private final List<? extends AnnotationMirror> accessorAnnotations;
|
||||
private final List<? extends AnnotationMirror> canonicalConstructorAnnotations;
|
||||
|
||||
public RecordClassType(TypeName typeName, TypeName rawTypeName, String name, List<? extends AnnotationMirror> accessorAnnotations, List<? extends AnnotationMirror> canonicalConstructorAnnotations) {
|
||||
super(typeName, name);
|
||||
this.rawTypeName = rawTypeName;
|
||||
this.accessorAnnotations = accessorAnnotations;
|
||||
this.canonicalConstructorAnnotations = canonicalConstructorAnnotations;
|
||||
}
|
||||
|
||||
public TypeName rawTypeName() {
|
||||
return rawTypeName;
|
||||
}
|
||||
|
||||
public List<? extends AnnotationMirror> getAccessorAnnotations() {
|
||||
return accessorAnnotations;
|
||||
}
|
||||
|
||||
public List<? extends AnnotationMirror> getCanonicalConstructorAnnotations() {
|
||||
return canonicalConstructorAnnotations;
|
||||
}
|
||||
}
|
||||
@@ -3,16 +3,43 @@
|
||||
<parent>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder</artifactId>
|
||||
<version>1.2.ea</version>
|
||||
<version>31</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>record-builder-test</artifactId>
|
||||
|
||||
<properties>
|
||||
<license-file-path>${project.parent.basedir}/src/etc/header.txt</license-file-path>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder-core</artifactId>
|
||||
<artifactId>record-builder-processor</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder-validator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.el</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@@ -24,22 +51,6 @@
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<annotationProcessorPath>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder-processor</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</annotationProcessorPath>
|
||||
</annotationProcessorPaths>
|
||||
<annotationProcessors>
|
||||
<annotationProcessor>io.soabase.recordbuilder.processor.RecordBuilderProcessor</annotationProcessor>
|
||||
</annotationProcessors>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Null;
|
||||
|
||||
@RecordBuilder
|
||||
public record Annotated(@NotNull @Null String hey, @Min(10) @Max(100) int i, double d) {
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordInterface;
|
||||
import java.time.Instant;
|
||||
|
||||
@RecordInterface
|
||||
public interface BeanStyle {
|
||||
String getName();
|
||||
|
||||
Instant getDate();
|
||||
|
||||
boolean isSomething();
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
import io.soabase.recordbuilder.core.RecordInterface;
|
||||
|
||||
@RecordInterface.Include({
|
||||
Thingy.class
|
||||
})
|
||||
@RecordBuilder.Include({
|
||||
Nested.NestedRecord.class
|
||||
})
|
||||
public class Builder {
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
import io.soabase.recordbuilder.core.RecordInterface;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@RecordInterface
|
||||
@RecordBuilder.Options(useImmutableCollections = true)
|
||||
public interface CollectionInterface<T, X extends Point> {
|
||||
List<T> l();
|
||||
|
||||
Set<T> s();
|
||||
|
||||
Map<T, X> m();
|
||||
|
||||
Collection<X> c();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@RecordBuilder
|
||||
@RecordBuilder.Options(useImmutableCollections = true, addFunctionalMethodsToWith = true)
|
||||
public record CollectionRecord<T, X extends Point>(List<T> l, Set<T> s, Map<T, X> m,
|
||||
Collection<X> c) implements CollectionRecordBuilder.With<T, X> {
|
||||
public static void main(String[] args) {
|
||||
var r = new CollectionRecord<>(List.of("hey"), Set.of("there"), Map.of("one", new Point(10, 20)), Set.of(new Point(30, 40)));
|
||||
Instant now = r.map((l1, s1, m1, c1) -> Instant.now());
|
||||
r.accept((l1, s1, m1, c1) -> {
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@RecordBuilder
|
||||
@RecordBuilder.Options(useImmutableCollections = true)
|
||||
public record CollectionRecordConflicts(List<String> __list, Set<String> __set, Map<String, String> __map, Collection<String> __collection) implements CollectionRecordConflictsBuilder.With {
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public interface Customer {
|
||||
String name();
|
||||
|
||||
String address();
|
||||
|
||||
Instant activeDate();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
@RecordBuilder
|
||||
public record Empty() implements EmptyBuilder.With {
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import javax.lang.model.type.ErrorType;
|
||||
import java.util.List;
|
||||
|
||||
@RecordBuilder
|
||||
public record ExceptionDetails(
|
||||
String internalMessage, String endUserMessage, String httpStatus,
|
||||
ErrorType errorType, List<String> jsonProblems, Throwable cause
|
||||
) {
|
||||
@Override
|
||||
public List<String> jsonProblems() {
|
||||
if (jsonProblems == null) {
|
||||
return List.of();
|
||||
}
|
||||
return jsonProblems;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilderFull;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RecordBuilderFull
|
||||
public record FullRecord(@NotNull List<Number> numbers, @NotNull Map<Number, FullRecord> fullRecords) {
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.IgnoreDefaultMethod;
|
||||
import io.soabase.recordbuilder.core.RecordInterface;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@RecordInterface
|
||||
public interface HasDefaults {
|
||||
Instant time();
|
||||
|
||||
default Instant tomorrow() {
|
||||
return Instant.now().plusMillis(TimeUnit.DAYS.toMillis(1));
|
||||
}
|
||||
|
||||
@IgnoreDefaultMethod
|
||||
default void complexMethod(String s1, String s2) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@RecordBuilder()
|
||||
@RecordBuilder.Options(inheritComponentAnnotations = false)
|
||||
public record IgnoreAnnotated(@NotNull String s) {
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
@RecordBuilder.Options(prefixEnclosingClassNames = false)
|
||||
@RecordBuilder.Include(IncludeWithOption.Hey.class)
|
||||
public class IncludeWithOption {
|
||||
public static record Hey(String s){}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
@MyInterfaceTemplate
|
||||
public interface InterfaceTemplateTest {
|
||||
String name();
|
||||
|
||||
int age();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RecordBuilder
|
||||
public record MutableCollectionRecord(List<String> l) implements MutableCollectionRecordBuilder.With {
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
@RecordBuilder.Template(options = @RecordBuilder.Options(
|
||||
fileComment = "This is a test",
|
||||
withClassName = "Com"),
|
||||
asRecordInterface = true
|
||||
)
|
||||
public @interface MyInterfaceTemplate {
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
@RecordBuilder.Template(options = @RecordBuilder.Options(
|
||||
fileComment = "This is a test",
|
||||
withClassName = "Com"
|
||||
))
|
||||
public @interface MyTemplate
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
public class Nested {
|
||||
record NestedRecord(int x, int y){}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordInterface;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
||||
@RecordInterface(addRecordBuilder = false)
|
||||
public interface NoBuilder {
|
||||
BigInteger big();
|
||||
|
||||
BigDecimal decimal();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
public record Pair<T, U>(T t, U u) {}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordInterface;
|
||||
|
||||
@RecordInterface
|
||||
public interface Person {
|
||||
String name();
|
||||
|
||||
int age();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
public record Point(int x, int y) {}
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
@RecordBuilder
|
||||
public record RecordWithAnR(int r, String b) {}
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalDouble;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.OptionalLong;
|
||||
|
||||
@RecordBuilder.Options(emptyDefaultForOptional = true)
|
||||
@RecordBuilder
|
||||
public record RecordWithOptional(Optional<String> value, Optional raw, OptionalInt i, OptionalLong l, OptionalDouble d) {}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@RecordBuilder.Options(interpretNotNulls = true)
|
||||
@RecordBuilder
|
||||
public record RequiredRecord(@NotNull String hey, @NotNull int i) implements RequiredRecordBuilder.With {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@RecordBuilder.Options(useValidationApi = true)
|
||||
@RecordBuilder
|
||||
public record RequiredRecord2(@NotNull String hey, @NotNull int i) implements RequiredRecord2Builder.With {
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2016 Jordan Zimmerman
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,5 +18,5 @@ package io.soabase.recordbuilder.test;
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
@RecordBuilder
|
||||
public record SimpleGenericRecord<T>(int i, T s) {
|
||||
public record SimpleGenericRecord<T>(int i, T s) implements SimpleGenericRecordBuilder.With<T> {
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2016 Jordan Zimmerman
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,5 +18,6 @@ package io.soabase.recordbuilder.test;
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
@RecordBuilder
|
||||
@RecordBuilder.Options(prefixEnclosingClassNames = false)
|
||||
public record SimpleRecord(int i, String s) {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@RecordBuilder
|
||||
@RecordBuilder.Options(
|
||||
addSingleItemCollectionBuilders = true,
|
||||
singleItemBuilderPrefix = "add1",
|
||||
useImmutableCollections = true,
|
||||
addFunctionalMethodsToWith = true
|
||||
)
|
||||
public record SingleItems<T>(List<String> strings, Set<List<T>> sets, Map<Instant, T> map, Collection<T> collection) implements SingleItemsBuilder.With<T> {
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordInterface;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@RecordInterface
|
||||
public interface SpecializedPerson<T, U> extends Person {
|
||||
List<T> features();
|
||||
|
||||
Map<Supplier<T>, Function<U, Function<T, U>>> complex();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@MyTemplate
|
||||
public record TemplateTest(String text, Instant date) implements TemplateTestBuilder.Com
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
public interface Thingy<T> {
|
||||
T getIt();
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2016 Jordan Zimmerman
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,13 +15,20 @@
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.test.SimpleRecordBuilder;
|
||||
|
||||
public class Usage {
|
||||
public static void main(String[] args) {
|
||||
var hey = SimpleRecordBuilder.builder().i(10).s("hey").build();
|
||||
System.out.println(hey);
|
||||
var hey2 = SimpleRecordBuilder.builder(hey).i(100).build();
|
||||
System.out.println(hey2);
|
||||
|
||||
var person = new PersonRecord("me", 42);
|
||||
outputPerson(person);
|
||||
var aged = PersonRecordBuilder.builder(person).age(100).build();
|
||||
outputPerson(aged);
|
||||
}
|
||||
|
||||
private static void outputPerson(Person p) {
|
||||
System.out.println(p.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@RecordBuilder
|
||||
@RecordBuilder.Options(
|
||||
addSingleItemCollectionBuilders = true,
|
||||
useImmutableCollections = true
|
||||
)
|
||||
public record WildcardSingleItems<T>(List<? extends String> strings, Set<? extends List<? extends T>> sets, Map<? extends Instant, ? extends T> map, Collection<? extends T> collection) implements WildcardSingleItemsBuilder.With<T> {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
@RecordBuilder.Include(
|
||||
packages = "io.soabase.recordbuilder.test.includes.pack",
|
||||
classes = JustATest.class
|
||||
)
|
||||
public class IncludeFactory {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes;
|
||||
|
||||
public record JustATest(int i) {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes.pack;
|
||||
|
||||
public interface AlsoIgnoreMe {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes.pack;
|
||||
|
||||
public class IgnoreMe {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes.pack;
|
||||
|
||||
public record PackRecord1(String name) {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes.pack;
|
||||
|
||||
public record PackRecord2(String name, int age) {
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes.pack;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public record PackRecord3(Instant time, int age) {
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
@RecordBuilder.Include(value = {Point.class, Pair.class}, packagePattern = "*.foo")
|
||||
@RecordInterface.Include(value = Customer.class, addRecordBuilder = false, packagePattern = "*.bar")
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
import io.soabase.recordbuilder.core.RecordInterface;
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.visibility;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
@RecordBuilder
|
||||
record PackagePrivateRecord(int i) {
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.visibility;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
class Wrapper {
|
||||
@RecordBuilder
|
||||
protected record ProtectedRecord(int i) {
|
||||
}
|
||||
|
||||
@RecordBuilder
|
||||
record PackagePrivateRecord(int i) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Null;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
|
||||
class TestAnnotated {
|
||||
@Test
|
||||
void testInheritComponentAnnotationsFalse() throws NoSuchMethodException {
|
||||
var method = IgnoreAnnotatedBuilder.class.getMethod("s");
|
||||
Assertions.assertNull(method.getAnnotation(NotNull.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStaticConstructor() throws NoSuchMethodException {
|
||||
var method = AnnotatedBuilder.class.getMethod("Annotated", String.class, Integer.TYPE, Double.TYPE);
|
||||
var parameters = method.getParameters();
|
||||
Assertions.assertEquals(3, parameters.length);
|
||||
assertHey(parameters[0]);
|
||||
assertI(parameters[1]);
|
||||
assertD(parameters[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetters() throws NoSuchMethodException {
|
||||
var method = AnnotatedBuilder.class.getMethod("hey", String.class);
|
||||
var parameters = method.getParameters();
|
||||
Assertions.assertEquals(1, parameters.length);
|
||||
assertHey(parameters[0]);
|
||||
|
||||
method = AnnotatedBuilder.class.getMethod("i", Integer.TYPE);
|
||||
parameters = method.getParameters();
|
||||
Assertions.assertEquals(1, parameters.length);
|
||||
assertI(parameters[0]);
|
||||
|
||||
method = AnnotatedBuilder.class.getMethod("d", Double.TYPE);
|
||||
parameters = method.getParameters();
|
||||
Assertions.assertEquals(1, parameters.length);
|
||||
assertD(parameters[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetters() throws NoSuchMethodException {
|
||||
var method = AnnotatedBuilder.class.getMethod("hey");
|
||||
assertHey(method);
|
||||
|
||||
method = AnnotatedBuilder.class.getMethod("i");
|
||||
assertI(method);
|
||||
|
||||
method = AnnotatedBuilder.class.getMethod("d");
|
||||
assertD(method);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWitherSetters() throws NoSuchMethodException {
|
||||
var method = AnnotatedBuilder.With.class.getMethod("withHey", String.class);
|
||||
var parameters = method.getParameters();
|
||||
Assertions.assertEquals(1, parameters.length);
|
||||
assertHey(parameters[0]);
|
||||
|
||||
method = AnnotatedBuilder.With.class.getMethod("withI", Integer.TYPE);
|
||||
parameters = method.getParameters();
|
||||
Assertions.assertEquals(1, parameters.length);
|
||||
assertI(parameters[0]);
|
||||
|
||||
method = AnnotatedBuilder.With.class.getMethod("withD", Double.TYPE);
|
||||
parameters = method.getParameters();
|
||||
Assertions.assertEquals(1, parameters.length);
|
||||
assertD(parameters[0]);
|
||||
}
|
||||
|
||||
private void assertD(AnnotatedElement d) {
|
||||
Assertions.assertEquals(0, d.getAnnotations().length);
|
||||
}
|
||||
|
||||
private void assertI(AnnotatedElement i) {
|
||||
Assertions.assertNotNull(i.getAnnotation(Min.class));
|
||||
Assertions.assertEquals(i.getAnnotation(Min.class).value(), 10);
|
||||
Assertions.assertNotNull(i.getAnnotation(Max.class));
|
||||
Assertions.assertEquals(i.getAnnotation(Max.class).value(), 100);
|
||||
}
|
||||
|
||||
private void assertHey(AnnotatedElement hey) {
|
||||
Assertions.assertNotNull(hey.getAnnotation(NotNull.class));
|
||||
Assertions.assertNotNull(hey.getAnnotation(Null.class));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static io.soabase.recordbuilder.test.foo.PointBuilder.Point;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class TestCollections {
|
||||
@Test
|
||||
void testRecordBuilderOptionsCopied() {
|
||||
try {
|
||||
assertNotNull(CollectionInterfaceRecordBuilder.class.getDeclaredMethod("__list", List.class));
|
||||
} catch (NoSuchMethodException e) {
|
||||
Assertions.fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCollectionRecordDefaultValues() {
|
||||
var defaultValues = CollectionRecordBuilder.builder().build();
|
||||
assertNotNull(defaultValues.l());
|
||||
assertNotNull(defaultValues.s());
|
||||
assertNotNull(defaultValues.m());
|
||||
assertNotNull(defaultValues.c());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCollectionRecordImmutable() {
|
||||
var list = new ArrayList<String>();
|
||||
list.add("one");
|
||||
var set = new HashSet<String>();
|
||||
set.add("one");
|
||||
var map = new HashMap<String, Point>();
|
||||
map.put("one", Point(10, 20));
|
||||
var collectionAsSet = new HashSet<Point>();
|
||||
collectionAsSet.add(Point(30, 40));
|
||||
var r = CollectionRecordBuilder.<String, Point>builder()
|
||||
.l(list)
|
||||
.s(set)
|
||||
.m(map)
|
||||
.c(collectionAsSet)
|
||||
.build();
|
||||
|
||||
assertValues(r, list, set, map, collectionAsSet);
|
||||
assertValueChanges(r, list, set, map, collectionAsSet);
|
||||
assertImmutable(r);
|
||||
|
||||
var collectionAsList = new ArrayList<Point>();
|
||||
var x = CollectionRecordBuilder.<String, Point>builder()
|
||||
.l(list)
|
||||
.s(set)
|
||||
.m(map)
|
||||
.c(collectionAsList)
|
||||
.build();
|
||||
assertTrue(x.c() instanceof List);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCollectionRecordImmutableWithers() {
|
||||
var r = CollectionRecordBuilder.<String, Point>builder().build();
|
||||
|
||||
var list = new ArrayList<String>();
|
||||
list.add("one");
|
||||
var set = new HashSet<String>();
|
||||
set.add("one");
|
||||
var map = new HashMap<String, Point>();
|
||||
map.put("one", Point(10, 20));
|
||||
var collectionAsSet = new HashSet<Point>();
|
||||
collectionAsSet.add(Point(30, 40));
|
||||
var x = r.withL(list).withS(set).withM(map).withC(collectionAsSet);
|
||||
|
||||
assertValues(x, list, set, map, collectionAsSet);
|
||||
assertValueChanges(x, list, set, map, collectionAsSet);
|
||||
assertImmutable(x);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMutableCollectionRecord() {
|
||||
var r = MutableCollectionRecordBuilder.builder().build();
|
||||
assertNull(r.l());
|
||||
|
||||
var list = new ArrayList<String>();
|
||||
list.add("one");
|
||||
r = MutableCollectionRecordBuilder.builder().l(list).build();
|
||||
|
||||
assertEquals(r.l(), list);
|
||||
list.add("two");
|
||||
assertEquals(r.l(), list);
|
||||
}
|
||||
|
||||
private void assertImmutable(CollectionRecord<String, Point> r) {
|
||||
assertThrows(UnsupportedOperationException.class, () -> r.l().add("hey"));
|
||||
assertThrows(UnsupportedOperationException.class, () -> r.s().add("hey"));
|
||||
assertThrows(UnsupportedOperationException.class, () -> r.m().put("hey", Point(1, 2)));
|
||||
assertThrows(UnsupportedOperationException.class, () -> r.c().add(Point(1, 2)));
|
||||
}
|
||||
|
||||
private void assertValueChanges(CollectionRecord<String, Point> r, ArrayList<String> list, HashSet<String> set, HashMap<String, Point> map, HashSet<Point> collectionAsSet) {
|
||||
list.add("two");
|
||||
set.add("two");
|
||||
map.put("two", Point(50, 60));
|
||||
collectionAsSet.add(Point(70, 80));
|
||||
|
||||
assertNotEquals(r.l(), list);
|
||||
assertNotEquals(r.s(), set);
|
||||
assertNotEquals(r.m(), map);
|
||||
assertNotEquals(r.c(), collectionAsSet);
|
||||
}
|
||||
|
||||
private void assertValues(CollectionRecord<String, Point> r, ArrayList<String> list, HashSet<String> set, HashMap<String, Point> map, HashSet<Point> collectionAsSet) {
|
||||
assertEquals(r.l(), list);
|
||||
assertEquals(r.s(), set);
|
||||
assertEquals(r.m(), map);
|
||||
assertEquals(r.c(), collectionAsSet);
|
||||
assertTrue(r.c() instanceof Set);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class TestIncludes {
|
||||
@Test
|
||||
void testOptionsOnInclude() {
|
||||
// assert it's not prefixed with the enclosing class name
|
||||
IncludeWithOption.Hey hey = io.soabase.recordbuilder.test.HeyBuilder.builder().s("this is s").build();
|
||||
Assertions.assertEquals("this is s", hey.s());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalDouble;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.OptionalLong;
|
||||
|
||||
class TestOptional {
|
||||
@Test
|
||||
void testDefaultEmpty() {
|
||||
var record = RecordWithOptionalBuilder.builder();
|
||||
Assertions.assertEquals(Optional.empty(), record.value());
|
||||
Assertions.assertEquals(Optional.empty(), record.raw());
|
||||
Assertions.assertEquals(OptionalInt.empty(), record.i());
|
||||
Assertions.assertEquals(OptionalLong.empty(), record.l());
|
||||
Assertions.assertEquals(OptionalDouble.empty(), record.d());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
class TestRecordBuilderFull {
|
||||
@Test
|
||||
void testNonNull() {
|
||||
Assertions.assertThrows(NullPointerException.class, () -> FullRecordBuilder.builder().build());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testImmutable() {
|
||||
var record = FullRecordBuilder.builder()
|
||||
.fullRecords(new HashMap<>())
|
||||
.numbers(new ArrayList<>())
|
||||
.build();
|
||||
Assertions.assertThrows(UnsupportedOperationException.class, () -> record.fullRecords().put(1, record));
|
||||
Assertions.assertThrows(UnsupportedOperationException.class, () -> record.numbers().add(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import java.util.AbstractMap.SimpleImmutableEntry;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import static io.soabase.recordbuilder.test.SimpleGenericRecordBuilder.SimpleGenericRecord;
|
||||
import static io.soabase.recordbuilder.test.SimpleRecordBuilder.SimpleRecord;
|
||||
|
||||
public class TestRecordInterface
|
||||
{
|
||||
@Test
|
||||
public void testHasDefaults()
|
||||
{
|
||||
var r1 = new HasDefaultsRecord(Instant.MIN, Instant.MAX);
|
||||
var r2 = r1.with(b -> b.tomorrow(Instant.MIN));
|
||||
Assertions.assertEquals(Instant.MIN, r1.time());
|
||||
Assertions.assertEquals(Instant.MAX, r1.tomorrow());
|
||||
Assertions.assertEquals(Instant.MIN, r2.time());
|
||||
Assertions.assertEquals(Instant.MIN, r2.tomorrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticConstructor()
|
||||
{
|
||||
var simple = SimpleRecord(10,"hey");
|
||||
Assertions.assertEquals(simple.i(), 10);
|
||||
Assertions.assertEquals(simple.s(), "hey");
|
||||
|
||||
var now = Instant.now();
|
||||
var generic = SimpleGenericRecord(101, now);
|
||||
Assertions.assertEquals(generic.i(), 101);
|
||||
Assertions.assertEquals(generic.s(), now);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuilderStreamWithValues()
|
||||
{
|
||||
var stream = SimpleRecordBuilder.stream(SimpleRecordBuilder.builder()
|
||||
.i(19)
|
||||
.s("value")
|
||||
.build())
|
||||
.toList();
|
||||
Assertions.assertEquals(stream, List.of(
|
||||
Map.entry("i", 19),
|
||||
Map.entry("s", "value")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuilderStreamWithNulls()
|
||||
{
|
||||
var stream = SimpleRecordBuilder.stream(SimpleRecordBuilder.builder()
|
||||
.build())
|
||||
.toList();
|
||||
Assertions.assertEquals(stream, List.of(
|
||||
new SimpleImmutableEntry<>("i", 0),
|
||||
new SimpleImmutableEntry<>("s", null)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class TestSingleItems {
|
||||
@Test
|
||||
public void testInternalCollections()
|
||||
{
|
||||
var now = Instant.now();
|
||||
var item = SingleItemsBuilder.<String>builder()
|
||||
.add1Map(now, "now")
|
||||
.add1Map(Instant.MIN, "before")
|
||||
.add1Sets(Arrays.asList("1", "2"))
|
||||
.add1Sets(List.of("3"))
|
||||
.add1Strings("a")
|
||||
.add1Strings("b")
|
||||
.add1Strings("c")
|
||||
.build();
|
||||
Assertions.assertEquals(item.map(), Map.of(now, "now", Instant.MIN, "before"));
|
||||
Assertions.assertEquals(item.sets(), Set.of(List.of("1", "2"), List.of("3")));
|
||||
Assertions.assertEquals(item.strings(), List.of("a", "b", "c"));
|
||||
|
||||
var copy = item.with()
|
||||
.add1Strings("new")
|
||||
.add1Map(Instant.MAX, "after")
|
||||
.add1Sets(List.of("10", "20", "30"))
|
||||
.build();
|
||||
Assertions.assertNotEquals(item, copy);
|
||||
Assertions.assertEquals(copy.map(), Map.of(now, "now", Instant.MIN, "before", Instant.MAX, "after"));
|
||||
Assertions.assertEquals(copy.sets(), Set.of(List.of("1", "2"), List.of("3"), List.of("10", "20", "30")));
|
||||
Assertions.assertEquals(copy.strings(), List.of("a", "b", "c", "new"));
|
||||
|
||||
var stringsToAdd = Arrays.asList("x", "y", "z");
|
||||
var listToAdd = Arrays.asList(List.of("aa", "bb"), List.of("cc"));
|
||||
var mapToAdd = Map.of(now.plusMillis(1), "now+1", now.plusMillis(2), "now+2");
|
||||
var streamed = SingleItemsBuilder.builder(item)
|
||||
.add1Strings(stringsToAdd.stream())
|
||||
.add1Sets(listToAdd.stream())
|
||||
.add1Map(mapToAdd.entrySet().stream())
|
||||
.build();
|
||||
Assertions.assertEquals(streamed.map(), Map.of(now, "now", Instant.MIN, "before", now.plusMillis(1), "now+1", now.plusMillis(2), "now+2"));
|
||||
Assertions.assertEquals(streamed.sets(), Set.of(List.of("1", "2"), List.of("3"), List.of("aa", "bb"), List.of("cc")));
|
||||
Assertions.assertEquals(streamed.strings(), Arrays.asList("a", "b", "c", "x", "y", "z"));
|
||||
|
||||
var nulls = SingleItemsBuilder.builder(item)
|
||||
.strings(null)
|
||||
.sets(null)
|
||||
.map(null)
|
||||
.build();
|
||||
Assertions.assertEquals(nulls.map(), Map.of());
|
||||
Assertions.assertEquals(nulls.sets(), Set.of());
|
||||
Assertions.assertEquals(nulls.strings(), List.of());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
class TestTemplate {
|
||||
@Test
|
||||
void testTemplate() {
|
||||
var t = TemplateTestBuilder.TemplateTest("one", Instant.MIN);
|
||||
var w = t.withText("other");
|
||||
Assertions.assertEquals("one", t.text());
|
||||
Assertions.assertEquals("other", w.text());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.validation.ValidationException;
|
||||
|
||||
class TestValidation {
|
||||
@Test
|
||||
void testNotNulls() {
|
||||
Assertions.assertThrows(NullPointerException.class, () -> RequiredRecordBuilder.builder().build());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testValidation() {
|
||||
Assertions.assertThrows(ValidationException.class, () -> RequiredRecord2Builder.builder().build());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNotNullsWithNewProperty() {
|
||||
var valid = RequiredRecordBuilder.builder().hey("hey").i(1).build();
|
||||
Assertions.assertThrows(NullPointerException.class, () -> valid.withHey(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testValidationWithNewProperty() {
|
||||
var valid = RequiredRecord2Builder.builder().hey("hey").i(1).build();
|
||||
Assertions.assertThrows(ValidationException.class, () -> valid.withHey(null));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class TestWithers {
|
||||
@Test
|
||||
void testFromWithers() {
|
||||
var r1 = new SimpleGenericRecord<>(10, List.of("1", "2", "3"));
|
||||
var r2 = SimpleGenericRecordBuilder.from(r1).withS(List.of("4", "5"));
|
||||
var r3 = SimpleGenericRecordBuilder.from(r1).with(b -> b.i(20).s(List.of("6", "7")));
|
||||
Assertions.assertEquals(List.of("1", "2", "3"), r1.s());
|
||||
Assertions.assertEquals(List.of("4", "5"), r2.s());
|
||||
Assertions.assertEquals(List.of("6", "7"), r3.s());
|
||||
Assertions.assertEquals(20, r3.i());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWithers() {
|
||||
var r1 = new SimpleGenericRecord<>(10, List.of("1", "2", "3"));
|
||||
var r2 = r1.withS(List.of("4", "5"));
|
||||
var r3 = r2.withI(20);
|
||||
Assertions.assertEquals(10, r1.i());
|
||||
Assertions.assertEquals(List.of("1", "2", "3"), r1.s());
|
||||
Assertions.assertEquals(10, r2.i());
|
||||
Assertions.assertEquals(List.of("4", "5"), r2.s());
|
||||
Assertions.assertEquals(20, r3.i());
|
||||
Assertions.assertEquals(List.of("4", "5"), r2.s());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWitherBuilder() {
|
||||
var r1 = new SimpleGenericRecord<>(10, "ten");
|
||||
var r2 = r1.with().i(20).s("twenty").build();
|
||||
var r3 = r2.with().s("changed");
|
||||
Assertions.assertEquals(10, r1.i());
|
||||
Assertions.assertEquals("ten", r1.s());
|
||||
Assertions.assertEquals(20, r2.i());
|
||||
Assertions.assertEquals("twenty", r2.s());
|
||||
Assertions.assertEquals(20, r3.i());
|
||||
Assertions.assertEquals("changed", r3.s());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWitherBuilderConsumer() {
|
||||
var r1 = new SimpleGenericRecord<>(10, "ten");
|
||||
var r2 = r1.with(r -> r.i(15));
|
||||
var r3 = r1.with(r -> r.s("twenty").i(20));
|
||||
Assertions.assertEquals(10, r1.i());
|
||||
Assertions.assertEquals("ten", r1.s());
|
||||
Assertions.assertEquals(15, r2.i());
|
||||
Assertions.assertEquals("ten", r2.s());
|
||||
Assertions.assertEquals(20, r3.i());
|
||||
Assertions.assertEquals("twenty", r3.s());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.visibility;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
class TestVisibility {
|
||||
@Test
|
||||
void testMatches() {
|
||||
Assertions.assertFalse(Modifier.isPublic(PackagePrivateRecordBuilder.class.getModifiers()));
|
||||
Assertions.assertFalse(Modifier.isPrivate(PackagePrivateRecordBuilder.class.getModifiers()));
|
||||
Assertions.assertFalse(Modifier.isProtected(PackagePrivateRecordBuilder.class.getModifiers()));
|
||||
|
||||
Assertions.assertTrue(Modifier.isPublic(WrapperProtectedRecordBuilder.class.getModifiers()));
|
||||
}
|
||||
}
|
||||
31
record-builder-validator/pom.xml
Normal file
31
record-builder-validator/pom.xml
Normal file
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder</artifactId>
|
||||
<version>31</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>record-builder-validator</artifactId>
|
||||
|
||||
<properties>
|
||||
<license-file-path>${project.parent.basedir}/src/etc/header.txt</license-file-path>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestEntries>
|
||||
<Automatic-Module-Name>io.soabase.recordbuilder.validator</Automatic-Module-Name>
|
||||
</manifestEntries>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.validator;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
// complete Java Validation via reflection to avoid dependencies
|
||||
public class RecordBuilderValidator {
|
||||
private static final Object validator;
|
||||
private static final Method validationMethod;
|
||||
private static final Constructor<?> constraintViolationExceptionCtor;
|
||||
private static final Class<?>[] emptyGroups = new Class<?>[0];
|
||||
|
||||
private static final boolean PRINT_ERROR_STACKTRACE = Boolean.getBoolean("record_builder_validator_errors");
|
||||
|
||||
static {
|
||||
Object localValidator = null;
|
||||
Method localValidationMethod = null;
|
||||
Constructor<?> localConstraintViolationExceptionCtor = null;
|
||||
try {
|
||||
var validationClass = Class.forName("javax.validation.Validation");
|
||||
var factoryClass = validationClass.getDeclaredMethod("buildDefaultValidatorFactory");
|
||||
var factory = factoryClass.invoke(null);
|
||||
var getValidatorMethod = factory.getClass().getMethod("getValidator");
|
||||
var constraintViolationExceptionClass = Class.forName("javax.validation.ConstraintViolationException");
|
||||
localValidator = getValidatorMethod.invoke(factory);
|
||||
localValidationMethod = localValidator.getClass().getMethod("validate", Object.class, Class[].class);
|
||||
localConstraintViolationExceptionCtor = constraintViolationExceptionClass.getConstructor(Set.class);
|
||||
} catch (Exception e) {
|
||||
if (PRINT_ERROR_STACKTRACE) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
validator = localValidator;
|
||||
validationMethod = localValidationMethod;
|
||||
constraintViolationExceptionCtor = localConstraintViolationExceptionCtor;
|
||||
}
|
||||
|
||||
public static <T> T validate(T o) {
|
||||
if ((validator != null) && (validationMethod != null)) {
|
||||
try {
|
||||
var violations = validationMethod.invoke(validator, o, emptyGroups);
|
||||
if (!((Collection<?>) violations).isEmpty()) {
|
||||
throw (RuntimeException) constraintViolationExceptionCtor.newInstance(violations);
|
||||
}
|
||||
} catch (IllegalAccessException | InstantiationException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
if (e.getCause() != null) {
|
||||
if (e.getCause() instanceof RuntimeException) {
|
||||
throw (RuntimeException) e.getCause();
|
||||
}
|
||||
throw new RuntimeException(e.getCause());
|
||||
}
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
private RecordBuilderValidator() {
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
Copyright 2016 Jordan Zimmerman
|
||||
Copyright 2019 Jordan Zimmerman
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
Reference in New Issue
Block a user