Compare commits
34 Commits
record-bui
...
record-bui
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
821718ac3f | ||
|
|
8ac34529e7 | ||
|
|
c701a09039 | ||
|
|
328bb7d660 | ||
|
|
41eddd46a3 | ||
|
|
b0f4bfbe88 | ||
|
|
09168b6104 | ||
|
|
d4e24993c9 | ||
|
|
8304cb1f83 | ||
|
|
7215ad3241 | ||
|
|
f7d65c7619 | ||
|
|
29ebe52914 | ||
|
|
d9c143aa8b | ||
|
|
0647b66bcd | ||
|
|
08a4471d15 | ||
|
|
a1acfb2863 | ||
|
|
82bc1c1625 | ||
|
|
2d029a2786 | ||
|
|
2625b3d849 | ||
|
|
cc5e189f94 | ||
|
|
9125ba0660 | ||
|
|
664809fc69 | ||
|
|
9d011b82aa | ||
|
|
35125f550d | ||
|
|
cb2bd68697 | ||
|
|
f40cfd48ee | ||
|
|
d9f2adc2f9 | ||
|
|
99832d50ae | ||
|
|
1203109108 | ||
|
|
7e4675f7c0 | ||
|
|
8ab9d8bdca | ||
|
|
24edc5e70c | ||
|
|
3832cb3881 | ||
|
|
802dd1f880 |
@@ -19,6 +19,6 @@ jobs:
|
|||||||
- name: Set up JDK
|
- name: Set up JDK
|
||||||
uses: actions/setup-java@v1
|
uses: actions/setup-java@v1
|
||||||
with:
|
with:
|
||||||
java-version: 16
|
java-version: 16.0.0-ea
|
||||||
- name: Build with Maven
|
- name: Build with Maven
|
||||||
run: mvn -B package --file pom.xml
|
run: mvn -B package --file pom.xml
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
# This workflow will build a Java project with Maven
|
# 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
|
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||||
|
|
||||||
name: Maven Build - Java 17
|
name: Maven Build - Java 15
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@@ -19,6 +19,10 @@ jobs:
|
|||||||
- name: Set up JDK
|
- name: Set up JDK
|
||||||
uses: actions/setup-java@v1
|
uses: actions/setup-java@v1
|
||||||
with:
|
with:
|
||||||
java-version: 17
|
java-version: 15
|
||||||
|
- name: Create Maven Directory
|
||||||
|
run: mkdir -p .mvn/
|
||||||
|
- name: Create Maven JVM file
|
||||||
|
run: echo "--enable-preview" > .mvn/jvm.config
|
||||||
- name: Build with Maven
|
- name: Build with Maven
|
||||||
run: mvn -B package --file pom.xml
|
run: mvn -P java15 -B package --file pom.xml
|
||||||
362
README.md
362
README.md
@@ -21,7 +21,8 @@ _Details:_
|
|||||||
- [Record From Interface Details](#RecordInterface-Example)
|
- [Record From Interface Details](#RecordInterface-Example)
|
||||||
- [Generation Via Includes](#generation-via-includes)
|
- [Generation Via Includes](#generation-via-includes)
|
||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
- [Customizing](customizing.md) (e.g. add immutable collections, etc.)
|
- [Customizing](customizing.md)
|
||||||
|
- [Java 15 Versions](#java-15-versions)
|
||||||
|
|
||||||
## RecordBuilder Example
|
## RecordBuilder Example
|
||||||
|
|
||||||
@@ -79,178 +80,155 @@ NameAndAge r5 = r4.with(b -> {
|
|||||||
b.name("whatever"));
|
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._
|
_Hat tip to [Benji Weber](https://benjiweber.co.uk/blog/2020/09/19/fun-with-java-records/) for the Withers idea._
|
||||||
|
|
||||||
## Builder Class Definition
|
## 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:
|
The full builder class is defined as:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class NameAndAgeBuilder {
|
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) {
|
private NameAndAgeBuilder(String name, int age) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.age = age;
|
this.age = age;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Static constructor/builder. Can be used instead of new NameAndAge(...)
|
* Static constructor/builder. Can be used instead of new NameAndAge(...)
|
||||||
*/
|
*/
|
||||||
public static NameAndAge NameAndAge(String name, int age) {
|
public static NameAndAge NameAndAge(String name, int age) {
|
||||||
return new NameAndAge(name, age);
|
return new NameAndAge(name, age);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a new builder with all fields set to default Java values
|
* Return a new builder with all fields set to default Java values
|
||||||
*/
|
*/
|
||||||
public static NameAndAgeBuilder builder() {
|
public static NameAndAgeBuilder builder() {
|
||||||
return new NameAndAgeBuilder();
|
return new NameAndAgeBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a new builder with all fields set to the values taken from the given record instance
|
* Return a new builder with all fields set to the values taken from the given record instance
|
||||||
*/
|
*/
|
||||||
public static NameAndAgeBuilder builder(NameAndAge from) {
|
public static NameAndAgeBuilder builder(NameAndAge from) {
|
||||||
return new NameAndAgeBuilder(from.name(), from.age());
|
return new NameAndAgeBuilder(from.name(), from.age());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a "with"er for an existing record instance
|
* Return a new record instance with all fields set to the current values in this builder
|
||||||
*/
|
*/
|
||||||
public static NameAndAgeBuilder.With from(NameAndAge from) {
|
public NameAndAge build() {
|
||||||
return new NameAndAgeBuilder.With() {
|
return new NameAndAge(name, age);
|
||||||
@Override
|
}
|
||||||
public String name() {
|
|
||||||
return from.name();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public int age() {
|
* Set a new value for the {@code name} record component in the builder
|
||||||
return from.age();
|
*/
|
||||||
}
|
public NameAndAgeBuilder name(String name) {
|
||||||
};
|
this.name = name;
|
||||||
}
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
* Return the current value for the {@code name} record component in the builder
|
||||||
*/
|
*/
|
||||||
String name();
|
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
|
* Return the current value for the {@code age} record component in the builder
|
||||||
*/
|
*/
|
||||||
int age();
|
public int age() {
|
||||||
|
return age;
|
||||||
/**
|
|
||||||
* Return a new record builder using the current values
|
|
||||||
*/
|
|
||||||
default NameAndAgeBuilder with() {
|
|
||||||
return new NameAndAgeBuilder(name(), age());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a new record built from the builder passed to the given consumer
|
* Return a stream of the record components as map entries keyed with the component name and the value as the component value
|
||||||
*/
|
*/
|
||||||
default NameAndAge with(Consumer<NameAndAgeBuilder> consumer) {
|
public static Stream<Map.Entry<String, Object>> stream(NameAndAge record) {
|
||||||
NameAndAgeBuilder builder = with();
|
return Stream.of(new AbstractMap.SimpleEntry<>("name", record.name()),
|
||||||
consumer.accept(builder);
|
new AbstractMap.SimpleEntry<>("age", record.age()));
|
||||||
return builder.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Return a new instance of {@code NameAndAge} with a new value for {@code name}
|
public String toString() {
|
||||||
*/
|
return "NameAndAgeBuilder[name=" + name + ", age=" + age + "]";
|
||||||
default NameAndAge withName(String name) {
|
|
||||||
return new NameAndAge(name, age());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Return a new instance of {@code NameAndAge} with a new value for {@code age}
|
public int hashCode() {
|
||||||
*/
|
return Objects.hash(name, age);
|
||||||
default NameAndAge withAge(int age) {
|
}
|
||||||
return new NameAndAge(name(), age);
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
return (this == o) || ((o instanceof NameAndAgeBuilder b)
|
||||||
|
&& Objects.equals(name, b.name)
|
||||||
|
&& (age == b.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 record built from the builder passed to the given consumer
|
||||||
|
*/
|
||||||
|
default NameAndAge with(Consumer<NameAndAgeBuilder> consumer) {
|
||||||
|
NameAndAgeBuilder builder = with();
|
||||||
|
consumer.accept(builder);
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a new instance of {@code NameAndAge} with a new value for {@code name}
|
||||||
|
*/
|
||||||
|
default NameAndAge withName(String name) {
|
||||||
|
return new NameAndAge(name, age());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a new instance of {@code NameAndAge} with a new value for {@code age}
|
||||||
|
*/
|
||||||
|
default NameAndAge withAge(int age) {
|
||||||
|
return new NameAndAge(name(), age);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -308,9 +286,6 @@ 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"
|
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).
|
annotation. Use `packagePattern` to change this (see Javadoc for details).
|
||||||
|
|
||||||
@@ -318,15 +293,41 @@ annotation. Use `packagePattern` to change this (see Javadoc for details).
|
|||||||
|
|
||||||
### Maven
|
### Maven
|
||||||
|
|
||||||
Add a dependency that contains the discoverable annotation processor:
|
1) Add the dependency that contains the `@RecordBuilder` annotation.
|
||||||
|
|
||||||
```xml
|
```xml
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.soabase.record-builder</groupId>
|
<groupId>io.soabase.record-builder</groupId>
|
||||||
<artifactId>record-builder-processor</artifactId>
|
<artifactId>record-builder-core</artifactId>
|
||||||
<version>${record.builder.version}</version>
|
<version>set-version-here</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
2) Enable the annotation processing for the Maven Compiler Plugin:
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<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>
|
||||||
|
|
||||||
|
|
||||||
|
... any other options here ...
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
```
|
```
|
||||||
|
|
||||||
### Gradle
|
### Gradle
|
||||||
@@ -349,3 +350,76 @@ Depending on your IDE you are likely to need to enable Annotation Processing in
|
|||||||
RecordBuilder can be customized to your needs and you can even create your
|
RecordBuilder can be customized to your needs and you can even create your
|
||||||
own custom RecordBuilder annotations. See [Customizing RecordBuilder](customizing.md)
|
own custom RecordBuilder annotations. See [Customizing RecordBuilder](customizing.md)
|
||||||
for details.
|
for details.
|
||||||
|
|
||||||
|
## Java 15 Versions
|
||||||
|
|
||||||
|
Artifacts compiled wth Java 15 are available. These versions have `-java15` appended.
|
||||||
|
|
||||||
|
Note: records are a preview feature only in Java 15. You'll need take a number of steps in order to try RecordBuilder:
|
||||||
|
|
||||||
|
- Install and make active Java 15 or later
|
||||||
|
- Make sure your development tool is using Java 15 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 15 and Maven. If you get internal Javac errors I suggest rebuilding with `mvn clean package` and/or `mvn clean install`.
|
||||||
|
|
||||||
|
You will need to enable preview in your build tools:
|
||||||
|
|
||||||
|
### Maven
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.soabase.record-builder</groupId>
|
||||||
|
<artifactId>record-builder-core</artifactId>
|
||||||
|
<version>record-builder-version-java15</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>maven-compiler-version</version>
|
||||||
|
<configuration>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<annotationProcessorPath>
|
||||||
|
<groupId>io.soabase.record-builder</groupId>
|
||||||
|
<artifactId>record-builder-processor</artifactId>
|
||||||
|
<version>record-builder-version-java15</version>
|
||||||
|
</annotationProcessorPath>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
<annotationProcessors>
|
||||||
|
<annotationProcessor>io.soabase.recordbuilder.processor.RecordBuilderProcessor</annotationProcessor>
|
||||||
|
</annotationProcessors>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- "release" and "enable-preview" are required while records are preview features -->
|
||||||
|
<release>15</release>
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>--enable-preview</arg>
|
||||||
|
</compilerArgs>
|
||||||
|
|
||||||
|
... any other options here ...
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
```
|
||||||
|
|
||||||
|
Create a file in your project's root named `.mvn/jvm.config`. The file should have 1 line with the value: `--enable-preview`. (see: https://stackoverflow.com/questions/58023240)
|
||||||
|
|
||||||
|
### Gradle
|
||||||
|
|
||||||
|
```groovy
|
||||||
|
dependencies {
|
||||||
|
annotationProcessor 'io.soabase.record-builder:record-builder-processor:$record-builder-version-java15'
|
||||||
|
compileOnly 'io.soabase.record-builder:record-builder-core:$record-builder-version-java15'
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.withType(JavaCompile) {
|
||||||
|
options.fork = true
|
||||||
|
options.forkOptions.jvmArgs += '--enable-preview'
|
||||||
|
options.compilerArgs += '--enable-preview'
|
||||||
|
}
|
||||||
|
tasks.withType(Test) {
|
||||||
|
jvmArgs += "--enable-preview"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,17 +1,10 @@
|
|||||||
[◀︎ RecordBuilder](README.md) • Customizing RecordBuilder
|
[◀︎ 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
|
# Customizing RecordBuilder
|
||||||
|
|
||||||
RecordBuilder can be customized in a number of ways. The types of customizations will change over time. See
|
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)
|
[@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
|
for the current set of customizations and their default values.
|
||||||
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:
|
You can:
|
||||||
|
|
||||||
@@ -90,6 +83,3 @@ public @interface MyCoRecordBuilder {
|
|||||||
|
|
||||||
Now, you can use `@MyCoRecordBuilder` instead of `@RecordBuilder` and the record
|
Now, you can use `@MyCoRecordBuilder` instead of `@RecordBuilder` and the record
|
||||||
will be built with options as specified.
|
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.
|
|
||||||
|
|||||||
22
java15.sh
Executable file
22
java15.sh
Executable file
@@ -0,0 +1,22 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
jenv local 15
|
||||||
|
javahome
|
||||||
|
mkdir -p .mvn/
|
||||||
|
echo "--enable-preview" > .mvn/jvm.config
|
||||||
21
java16.sh
Executable file
21
java16.sh
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
jenv local 16
|
||||||
|
javahome
|
||||||
|
rm -fr .mvn
|
||||||
33
pom.xml
33
pom.xml
@@ -5,7 +5,7 @@
|
|||||||
<groupId>io.soabase.record-builder</groupId>
|
<groupId>io.soabase.record-builder</groupId>
|
||||||
<artifactId>record-builder</artifactId>
|
<artifactId>record-builder</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>30</version>
|
<version>25-java15</version>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>record-builder-core</module>
|
<module>record-builder-core</module>
|
||||||
@@ -19,6 +19,8 @@
|
|||||||
<project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>
|
<project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
|
||||||
|
<enable-preview />
|
||||||
|
|
||||||
<jdk-version>16</jdk-version>
|
<jdk-version>16</jdk-version>
|
||||||
|
|
||||||
<maven-compiler-plugin-version>3.8.1</maven-compiler-plugin-version>
|
<maven-compiler-plugin-version>3.8.1</maven-compiler-plugin-version>
|
||||||
@@ -31,6 +33,7 @@
|
|||||||
<maven-clean-plugin-version>3.1.0</maven-clean-plugin-version>
|
<maven-clean-plugin-version>3.1.0</maven-clean-plugin-version>
|
||||||
<maven-shade-plugin-version>3.2.1</maven-shade-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-release-plugin-version>2.5.3</maven-release-plugin-version>
|
||||||
|
<maven-surefire-plugin-version>3.0.0-M5</maven-surefire-plugin-version>
|
||||||
<maven-jar-plugin-version>3.2.0</maven-jar-plugin-version>
|
<maven-jar-plugin-version>3.2.0</maven-jar-plugin-version>
|
||||||
|
|
||||||
<license-file-path>src/etc/header.txt</license-file-path>
|
<license-file-path>src/etc/header.txt</license-file-path>
|
||||||
@@ -77,7 +80,7 @@
|
|||||||
<url>https://github.com/randgalt/record-builder</url>
|
<url>https://github.com/randgalt/record-builder</url>
|
||||||
<connection>scm:git:https://github.com/randgalt/record-builder.git</connection>
|
<connection>scm:git:https://github.com/randgalt/record-builder.git</connection>
|
||||||
<developerConnection>scm:git:git@github.com:randgalt/record-builder.git</developerConnection>
|
<developerConnection>scm:git:git@github.com:randgalt/record-builder.git</developerConnection>
|
||||||
<tag>record-builder-30</tag>
|
<tag>record-builder-25-java15</tag>
|
||||||
</scm>
|
</scm>
|
||||||
|
|
||||||
<issueManagement>
|
<issueManagement>
|
||||||
@@ -112,12 +115,6 @@
|
|||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>io.soabase.record-builder</groupId>
|
|
||||||
<artifactId>record-builder-processor</artifactId>
|
|
||||||
<version>${project.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.soabase.record-builder</groupId>
|
<groupId>io.soabase.record-builder</groupId>
|
||||||
<artifactId>record-builder-validator</artifactId>
|
<artifactId>record-builder-validator</artifactId>
|
||||||
@@ -159,6 +156,9 @@
|
|||||||
<version>${maven-compiler-plugin-version}</version>
|
<version>${maven-compiler-plugin-version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<release>${jdk-version}</release>
|
<release>${jdk-version}</release>
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>${enable-preview}</arg>
|
||||||
|
</compilerArgs>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
@@ -308,6 +308,15 @@
|
|||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${maven-surefire-plugin-version}</version>
|
||||||
|
<configuration>
|
||||||
|
<argLine>${enable-preview}</argLine>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
@@ -379,5 +388,13 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</profile>
|
</profile>
|
||||||
|
|
||||||
|
<profile>
|
||||||
|
<id>java15</id>
|
||||||
|
<properties>
|
||||||
|
<jdk-version>15</jdk-version>
|
||||||
|
<enable-preview>--enable-preview</enable-preview>
|
||||||
|
</properties>
|
||||||
|
</profile>
|
||||||
</profiles>
|
</profiles>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>io.soabase.record-builder</groupId>
|
<groupId>io.soabase.record-builder</groupId>
|
||||||
<artifactId>record-builder</artifactId>
|
<artifactId>record-builder</artifactId>
|
||||||
<version>30</version>
|
<version>25-java15</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -25,32 +25,12 @@ public @interface RecordBuilder {
|
|||||||
@Retention(RetentionPolicy.SOURCE)
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
@Inherited
|
@Inherited
|
||||||
@interface Include {
|
@interface Include {
|
||||||
/**
|
Class<?>[] value();
|
||||||
* @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
|
* 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 the literal package name however two replacement values can be used. '@'
|
||||||
* is replaced with the package of the {@code Include} annotation. '*' is replaced with
|
* is replaced with the package of the Include annotation. '*' is replaced with
|
||||||
* the package of the included class.
|
* the package of the included class.
|
||||||
*
|
*
|
||||||
* @return package pattern
|
* @return package pattern
|
||||||
@@ -89,11 +69,6 @@ public @interface RecordBuilder {
|
|||||||
*/
|
*/
|
||||||
String buildMethodName() default "build";
|
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
|
* The name to use for the method that returns the record components as a stream
|
||||||
*/
|
*/
|
||||||
@@ -169,32 +144,12 @@ public @interface RecordBuilder {
|
|||||||
* (e.g. {@code List.copyOf(o)}) or an empty immutable collection if the component is {@code null}.
|
* (e.g. {@code List.copyOf(o)}) or an empty immutable collection if the component is {@code null}.
|
||||||
*/
|
*/
|
||||||
boolean useImmutableCollections() default false;
|
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)
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
@Target(ElementType.ANNOTATION_TYPE)
|
@Target(ElementType.ANNOTATION_TYPE)
|
||||||
@Inherited
|
@Inherited
|
||||||
@interface Template {
|
@interface Template {
|
||||||
RecordBuilder.Options options();
|
RecordBuilder.Options options();
|
||||||
|
|
||||||
boolean asRecordInterface() default false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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 {
|
|
||||||
}
|
|
||||||
@@ -15,7 +15,11 @@
|
|||||||
*/
|
*/
|
||||||
package io.soabase.recordbuilder.core;
|
package io.soabase.recordbuilder.core;
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Inherited;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
@Retention(RetentionPolicy.SOURCE)
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
@@ -27,31 +31,14 @@ public @interface RecordInterface {
|
|||||||
@Retention(RetentionPolicy.SOURCE)
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
@Inherited
|
@Inherited
|
||||||
@interface Include {
|
@interface Include {
|
||||||
/**
|
Class<?>[] value();
|
||||||
* @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;
|
boolean addRecordBuilder() default true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pattern used to generate the package for the generated class. The value
|
* 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 the literal package name however two replacement values can be used. '@'
|
||||||
* is replaced with the package of the {@code Include} annotation. '*' is replaced with
|
* is replaced with the package of the Include annotation. '*' is replaced with
|
||||||
* the package of the included class.
|
* the package of the included class.
|
||||||
*
|
*
|
||||||
* @return package pattern
|
* @return package pattern
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>io.soabase.record-builder</groupId>
|
<groupId>io.soabase.record-builder</groupId>
|
||||||
<artifactId>record-builder</artifactId>
|
<artifactId>record-builder</artifactId>
|
||||||
<version>30</version>
|
<version>25-java15</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
package io.soabase.recordbuilder.processor;
|
package io.soabase.recordbuilder.processor;
|
||||||
|
|
||||||
import com.squareup.javapoet.*;
|
import com.squareup.javapoet.*;
|
||||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
|
||||||
|
|
||||||
import javax.lang.model.element.Modifier;
|
import javax.lang.model.element.Modifier;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -24,8 +23,7 @@ import java.util.*;
|
|||||||
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.generatedRecordBuilderAnnotation;
|
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.generatedRecordBuilderAnnotation;
|
||||||
|
|
||||||
class CollectionBuilderUtils {
|
class CollectionBuilderUtils {
|
||||||
private final boolean useImmutableCollections;
|
private final boolean enabled;
|
||||||
private final boolean addSingleItemCollectionBuilders;
|
|
||||||
private final String listShimName;
|
private final String listShimName;
|
||||||
private final String mapShimName;
|
private final String mapShimName;
|
||||||
private final String setShimName;
|
private final String setShimName;
|
||||||
@@ -49,9 +47,8 @@ class CollectionBuilderUtils {
|
|||||||
private static final ParameterizedTypeName parameterizedSetType = ParameterizedTypeName.get(ClassName.get(Set.class), tType);
|
private static final ParameterizedTypeName parameterizedSetType = ParameterizedTypeName.get(ClassName.get(Set.class), tType);
|
||||||
private static final ParameterizedTypeName parameterizedCollectionType = ParameterizedTypeName.get(ClassName.get(Collection.class), tType);
|
private static final ParameterizedTypeName parameterizedCollectionType = ParameterizedTypeName.get(ClassName.get(Collection.class), tType);
|
||||||
|
|
||||||
CollectionBuilderUtils(List<RecordClassType> recordComponents, RecordBuilder.Options metaData) {
|
CollectionBuilderUtils(List<RecordClassType> recordComponents, boolean enabled) {
|
||||||
useImmutableCollections = metaData.useImmutableCollections();
|
this.enabled = enabled;
|
||||||
addSingleItemCollectionBuilders = metaData.addSingleItemCollectionBuilders();
|
|
||||||
|
|
||||||
listShimName = adjustShimName(recordComponents, "__list", 0);
|
listShimName = adjustShimName(recordComponents, "__list", 0);
|
||||||
mapShimName = adjustShimName(recordComponents, "__map", 0);
|
mapShimName = adjustShimName(recordComponents, "__map", 0);
|
||||||
@@ -59,77 +56,15 @@ class CollectionBuilderUtils {
|
|||||||
collectionShimName = adjustShimName(recordComponents, "__collection", 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) {
|
void add(CodeBlock.Builder builder, RecordClassType component) {
|
||||||
if (useImmutableCollections) {
|
if (enabled) {
|
||||||
if (isList(component)) {
|
if (component.rawTypeName().equals(listType)) {
|
||||||
needsListShim = true;
|
needsListShim = true;
|
||||||
builder.add("$L($L)", listShimName, component.name());
|
builder.add("$L($L)", listShimName, component.name());
|
||||||
} else if (isMap(component)) {
|
} else if (component.rawTypeName().equals(mapType)) {
|
||||||
needsMapShim = true;
|
needsMapShim = true;
|
||||||
builder.add("$L($L)", mapShimName, component.name());
|
builder.add("$L($L)", mapShimName, component.name());
|
||||||
} else if (isSet(component)) {
|
} else if (component.rawTypeName().equals(setType)) {
|
||||||
needsSetShim = true;
|
needsSetShim = true;
|
||||||
builder.add("$L($L)", setShimName, component.name());
|
builder.add("$L($L)", setShimName, component.name());
|
||||||
} else if (component.rawTypeName().equals(collectionType)) {
|
} else if (component.rawTypeName().equals(collectionType)) {
|
||||||
@@ -144,7 +79,7 @@ class CollectionBuilderUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void addShims(TypeSpec.Builder builder) {
|
void addShims(TypeSpec.Builder builder) {
|
||||||
if (!useImmutableCollections) {
|
if (!enabled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,28 +97,8 @@ class CollectionBuilderUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<SingleItemsMetaData> singleItemsMetaDataWithWildType(ParameterizedTypeName parameterizedTypeName, Class<?> collectionClass, ClassName wildcardClass, int typeArgumentQty) {
|
private String adjustShimName(List<RecordClassType> recordComponents, String baseName, int index)
|
||||||
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);
|
var name = (index == 0) ? baseName : (baseName + index);
|
||||||
if (recordComponents.stream().anyMatch(component -> component.name().equals(name))) {
|
if (recordComponents.stream().anyMatch(component -> component.name().equals(name))) {
|
||||||
return adjustShimName(recordComponents, baseName, index + 1);
|
return adjustShimName(recordComponents, baseName, index + 1);
|
||||||
@@ -207,7 +122,7 @@ class CollectionBuilderUtils {
|
|||||||
var code = CodeBlock.builder()
|
var code = CodeBlock.builder()
|
||||||
.add("if (o instanceof Set) {\n")
|
.add("if (o instanceof Set) {\n")
|
||||||
.indent()
|
.indent()
|
||||||
.addStatement("return $T.copyOf(o)", setType)
|
.addStatement("return (o != null) ? $T.copyOf(o) : $T.of()", setType, setType)
|
||||||
.unindent()
|
.unindent()
|
||||||
.addStatement("}")
|
.addStatement("}")
|
||||||
.addStatement("return (o != null) ? $T.copyOf(o) : $T.of()", listType, listType)
|
.addStatement("return (o != null) ? $T.copyOf(o) : $T.of()", listType, listType)
|
||||||
|
|||||||
@@ -52,19 +52,12 @@ public class ElementUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static List<TypeMirror> getAttributeTypeMirrorList(AnnotationValue attribute)
|
public static List<TypeMirror> getClassesAttribute(AnnotationValue attribute)
|
||||||
{
|
{
|
||||||
List<? extends AnnotationValue> values = (attribute != null) ? (List<? extends AnnotationValue>)attribute.getValue() : Collections.emptyList();
|
List<? extends AnnotationValue> values = (attribute != null) ? (List<? extends AnnotationValue>)attribute.getValue() : Collections.emptyList();
|
||||||
return values.stream().map(v -> (TypeMirror)v.getValue()).collect(Collectors.toList());
|
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)
|
public static boolean getBooleanAttribute(AnnotationValue attribute)
|
||||||
{
|
{
|
||||||
Object value = (attribute != null) ? attribute.getValue() : null;
|
Object value = (attribute != null) ? attribute.getValue() : null;
|
||||||
|
|||||||
@@ -1,96 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -27,7 +27,6 @@ import java.util.stream.Collectors;
|
|||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
import java.util.stream.Stream;
|
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.getBuilderName;
|
||||||
import static io.soabase.recordbuilder.processor.ElementUtils.getWithMethodName;
|
import static io.soabase.recordbuilder.processor.ElementUtils.getWithMethodName;
|
||||||
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.generatedRecordBuilderAnnotation;
|
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.generatedRecordBuilderAnnotation;
|
||||||
@@ -51,13 +50,12 @@ class InternalRecordBuilderProcessor {
|
|||||||
private static final TypeName optionalLongType = TypeName.get(OptionalLong.class);
|
private static final TypeName optionalLongType = TypeName.get(OptionalLong.class);
|
||||||
private static final TypeName optionalDoubleType = TypeName.get(OptionalDouble.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 TypeName validatorTypeName = ClassName.get("io.soabase.recordbuilder.validator", "RecordBuilderValidator");
|
||||||
private static final TypeVariableName rType = TypeVariableName.get("R");
|
|
||||||
private final ProcessingEnvironment processingEnv;
|
private final ProcessingEnvironment processingEnv;
|
||||||
|
|
||||||
InternalRecordBuilderProcessor(ProcessingEnvironment processingEnv, TypeElement record, RecordBuilder.Options metaData, Optional<String> packageNameOpt) {
|
InternalRecordBuilderProcessor(ProcessingEnvironment processingEnv, TypeElement record, RecordBuilder.Options metaData, Optional<String> packageNameOpt) {
|
||||||
this.processingEnv = processingEnv;
|
this.processingEnv = processingEnv;
|
||||||
var recordActualPackage = ElementUtils.getPackageName(record);
|
var recordActualPackage = ElementUtils.getPackageName(record);
|
||||||
this.metaData = metaData;
|
this.metaData = getMetaData(record, metaData);
|
||||||
recordClassType = ElementUtils.getClassType(record, record.getTypeParameters());
|
recordClassType = ElementUtils.getClassType(record, record.getTypeParameters());
|
||||||
packageName = packageNameOpt.orElse(recordActualPackage);
|
packageName = packageNameOpt.orElse(recordActualPackage);
|
||||||
builderClassType = ElementUtils.getClassType(packageName, getBuilderName(record, metaData, recordClassType, metaData.suffix()), record.getTypeParameters());
|
builderClassType = ElementUtils.getClassType(packageName, getBuilderName(record, metaData, recordClassType, metaData.suffix()), record.getTypeParameters());
|
||||||
@@ -65,7 +63,7 @@ class InternalRecordBuilderProcessor {
|
|||||||
recordComponents = buildRecordComponents(record);
|
recordComponents = buildRecordComponents(record);
|
||||||
uniqueVarName = getUniqueVarName();
|
uniqueVarName = getUniqueVarName();
|
||||||
notNullPattern = Pattern.compile(metaData.interpretNotNullsPattern());
|
notNullPattern = Pattern.compile(metaData.interpretNotNullsPattern());
|
||||||
collectionBuilderUtils = new CollectionBuilderUtils(recordComponents, this.metaData);
|
collectionBuilderUtils = new CollectionBuilderUtils(recordComponents, this.metaData.useImmutableCollections());
|
||||||
|
|
||||||
builder = TypeSpec.classBuilder(builderClassType.name())
|
builder = TypeSpec.classBuilder(builderClassType.name())
|
||||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||||
@@ -79,7 +77,6 @@ class InternalRecordBuilderProcessor {
|
|||||||
}
|
}
|
||||||
addStaticDefaultBuilderMethod();
|
addStaticDefaultBuilderMethod();
|
||||||
addStaticCopyBuilderMethod();
|
addStaticCopyBuilderMethod();
|
||||||
addStaticFromWithMethod();
|
|
||||||
addStaticComponentsMethod();
|
addStaticComponentsMethod();
|
||||||
addBuildMethod();
|
addBuildMethod();
|
||||||
addToStringMethod();
|
addToStringMethod();
|
||||||
@@ -89,8 +86,6 @@ class InternalRecordBuilderProcessor {
|
|||||||
add1Field(component);
|
add1Field(component);
|
||||||
add1SetterMethod(component);
|
add1SetterMethod(component);
|
||||||
add1GetterMethod(component);
|
add1GetterMethod(component);
|
||||||
var collectionMetaData = collectionBuilderUtils.singleItemsMetaData(component, EXCLUDE_WILDCARD_TYPES);
|
|
||||||
collectionMetaData.ifPresent(meta -> add1CollectionBuilders(meta, component));
|
|
||||||
});
|
});
|
||||||
collectionBuilderUtils.addShims(builder);
|
collectionBuilderUtils.addShims(builder);
|
||||||
builderType = builder.build();
|
builderType = builder.build();
|
||||||
@@ -114,7 +109,8 @@ class InternalRecordBuilderProcessor {
|
|||||||
builder.addModifiers(Modifier.PUBLIC); // builders are top level classes - can only be public or package-private
|
builder.addModifiers(Modifier.PUBLIC); // builders are top level classes - can only be public or package-private
|
||||||
}
|
}
|
||||||
// is package-private
|
// is package-private
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
builder.addModifiers(Modifier.PUBLIC);
|
builder.addModifiers(Modifier.PUBLIC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,6 +128,11 @@ class InternalRecordBuilderProcessor {
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private RecordBuilder.Options getMetaData(TypeElement record, RecordBuilder.Options metaData) {
|
||||||
|
var recordSpecificMetaData = record.getAnnotation(RecordBuilder.Options.class);
|
||||||
|
return (recordSpecificMetaData != null) ? recordSpecificMetaData : metaData;
|
||||||
|
}
|
||||||
|
|
||||||
private void addWithNestedClass() {
|
private void addWithNestedClass() {
|
||||||
/*
|
/*
|
||||||
Adds a nested interface that adds withers similar to:
|
Adds a nested interface that adds withers similar to:
|
||||||
@@ -147,16 +148,10 @@ class InternalRecordBuilderProcessor {
|
|||||||
.addJavadoc("Add withers to {@code $L}\n", recordClassType.name())
|
.addJavadoc("Add withers to {@code $L}\n", recordClassType.name())
|
||||||
.addModifiers(Modifier.PUBLIC)
|
.addModifiers(Modifier.PUBLIC)
|
||||||
.addTypeVariables(typeVariables);
|
.addTypeVariables(typeVariables);
|
||||||
recordComponents.forEach(component -> addNestedGetterMethod(classBuilder, component));
|
recordComponents.forEach(component -> addWithGetterMethod(classBuilder, component));
|
||||||
addWithBuilderMethod(classBuilder);
|
addWithBuilderMethod(classBuilder);
|
||||||
addWithSuppliedBuilderMethod(classBuilder);
|
addWithSuppliedBuilderMethod(classBuilder);
|
||||||
IntStream.range(0, recordComponents.size()).forEach(index -> add1WithMethod(classBuilder, recordComponents.get(index), index));
|
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());
|
builder.addType(classBuilder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,9 +341,8 @@ class InternalRecordBuilderProcessor {
|
|||||||
.addAnnotation(generatedRecordBuilderAnnotation);
|
.addAnnotation(generatedRecordBuilderAnnotation);
|
||||||
recordComponents.forEach(component -> {
|
recordComponents.forEach(component -> {
|
||||||
constructorBuilder.addParameter(component.typeName(), component.name());
|
constructorBuilder.addParameter(component.typeName(), component.name());
|
||||||
var collectionMetaData = collectionBuilderUtils.singleItemsMetaData(component, STANDARD);
|
var codeBuilder = CodeBlock.builder().add("this.$L = $L", component.name(), component.name());
|
||||||
collectionMetaData.ifPresentOrElse(meta -> constructorBuilder.addStatement("this.$L = new $T<>($L)", component.name(), meta.singleItemCollectionClass(), component.name()),
|
constructorBuilder.addStatement(codeBuilder.build());
|
||||||
() -> constructorBuilder.addStatement("this.$L = $L", component.name(), component.name()));
|
|
||||||
});
|
});
|
||||||
builder.addMethod(constructorBuilder.build());
|
builder.addMethod(constructorBuilder.build());
|
||||||
}
|
}
|
||||||
@@ -490,67 +484,6 @@ class InternalRecordBuilderProcessor {
|
|||||||
return codeBuilder.build();
|
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() {
|
private void addStaticCopyBuilderMethod() {
|
||||||
/*
|
/*
|
||||||
Adds a copy builder method that pre-fills the builder with existing values similar to:
|
Adds a copy builder method that pre-fills the builder with existing values similar to:
|
||||||
@@ -604,8 +537,10 @@ class InternalRecordBuilderProcessor {
|
|||||||
Adds a static method that converts a record instance into a stream of its component parts
|
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) {
|
public static Stream<Map.Entry<String, Object>> stream(MyRecord record) {
|
||||||
return Stream.of(new AbstractMap.SimpleImmutableEntry<>("p1", record.p1()),
|
return Stream.of(
|
||||||
new AbstractMap.SimpleImmutableEntry<>("p2", record.p2()));
|
new AbstractMap.SimpleEntry<>("p1", record.p1()),
|
||||||
|
new AbstractMap.SimpleEntry<>("p2", record.p2())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
var codeBuilder = CodeBlock.builder().add("return $T.of(", Stream.class);
|
var codeBuilder = CodeBlock.builder().add("return $T.of(", Stream.class);
|
||||||
@@ -614,7 +549,7 @@ class InternalRecordBuilderProcessor {
|
|||||||
codeBuilder.add(",\n ");
|
codeBuilder.add(",\n ");
|
||||||
}
|
}
|
||||||
var name = recordComponents.get(index).name();
|
var name = recordComponents.get(index).name();
|
||||||
codeBuilder.add("new $T<>($S, record.$L())", AbstractMap.SimpleImmutableEntry.class, name, name);
|
codeBuilder.add("new $T<>($S, record.$L())", AbstractMap.SimpleEntry.class, name, name);
|
||||||
});
|
});
|
||||||
codeBuilder.add(")");
|
codeBuilder.add(")");
|
||||||
var mapEntryTypeVariables = ParameterizedTypeName.get(Map.Entry.class, String.class, Object.class);
|
var mapEntryTypeVariables = ParameterizedTypeName.get(Map.Entry.class, String.class, Object.class);
|
||||||
@@ -664,7 +599,7 @@ class InternalRecordBuilderProcessor {
|
|||||||
return (component.typeName() instanceof ParameterizedTypeName parameterizedTypeName) && parameterizedTypeName.rawType.equals(optionalType);
|
return (component.typeName() instanceof ParameterizedTypeName parameterizedTypeName) && parameterizedTypeName.rawType.equals(optionalType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addNestedGetterMethod(TypeSpec.Builder classBuilder, RecordClassType component) {
|
private void addWithGetterMethod(TypeSpec.Builder classBuilder, RecordClassType component) {
|
||||||
/*
|
/*
|
||||||
For a single record component, add a getter similar to:
|
For a single record component, add a getter similar to:
|
||||||
|
|
||||||
@@ -703,129 +638,6 @@ class InternalRecordBuilderProcessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
private void add1GetterMethod(RecordClassType component) {
|
||||||
/*
|
/*
|
||||||
For a single record component, add a getter similar to:
|
For a single record component, add a getter similar to:
|
||||||
@@ -853,92 +665,19 @@ class InternalRecordBuilderProcessor {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
var parameterSpecBuilder = ParameterSpec.builder(component.typeName(), component.name());
|
||||||
|
addConstructorAnnotations(component, parameterSpecBuilder);
|
||||||
|
|
||||||
var methodSpec = MethodSpec.methodBuilder(component.name())
|
var methodSpec = MethodSpec.methodBuilder(component.name())
|
||||||
|
.addJavadoc("Set a new value for the {@code $L} record component in the builder\n", component.name())
|
||||||
.addModifiers(Modifier.PUBLIC)
|
.addModifiers(Modifier.PUBLIC)
|
||||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||||
.returns(builderClassType.typeName());
|
.addParameter(parameterSpecBuilder.build())
|
||||||
|
.returns(builderClassType.typeName())
|
||||||
var collectionMetaData = collectionBuilderUtils.singleItemsMetaData(component, STANDARD_FOR_SETTER);
|
.addStatement("this.$L = $L", component.name(), component.name())
|
||||||
var parameterSpecBuilder = collectionMetaData.map(meta -> {
|
.addStatement("return this")
|
||||||
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();
|
.build();
|
||||||
|
builder.addMethod(methodSpec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,10 +15,13 @@
|
|||||||
*/
|
*/
|
||||||
package io.soabase.recordbuilder.processor;
|
package io.soabase.recordbuilder.processor;
|
||||||
|
|
||||||
import com.squareup.javapoet.*;
|
import com.squareup.javapoet.ClassName;
|
||||||
|
import com.squareup.javapoet.MethodSpec;
|
||||||
|
import com.squareup.javapoet.ParameterSpec;
|
||||||
|
import com.squareup.javapoet.TypeSpec;
|
||||||
|
import com.squareup.javapoet.TypeVariableName;
|
||||||
import io.soabase.recordbuilder.core.IgnoreDefaultMethod;
|
import io.soabase.recordbuilder.core.IgnoreDefaultMethod;
|
||||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||||
|
|
||||||
import javax.annotation.processing.ProcessingEnvironment;
|
import javax.annotation.processing.ProcessingEnvironment;
|
||||||
import javax.lang.model.element.ElementKind;
|
import javax.lang.model.element.ElementKind;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
@@ -26,7 +29,13 @@ import javax.lang.model.element.Modifier;
|
|||||||
import javax.lang.model.element.TypeElement;
|
import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.type.TypeKind;
|
import javax.lang.model.type.TypeKind;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -47,10 +56,9 @@ class InternalRecordInterfaceProcessor {
|
|||||||
|
|
||||||
private static final Set<String> javaBeanPrefixes = Set.of("get", "is");
|
private static final Set<String> javaBeanPrefixes = Set.of("get", "is");
|
||||||
|
|
||||||
private record Component(ExecutableElement element, Optional<String> alternateName) {
|
private record Component(ExecutableElement element, Optional<String> alternateName){}
|
||||||
}
|
|
||||||
|
|
||||||
InternalRecordInterfaceProcessor(ProcessingEnvironment processingEnv, TypeElement iface, boolean addRecordBuilder, RecordBuilder.Options metaData, Optional<String> packageNameOpt, boolean fromTemplate) {
|
InternalRecordInterfaceProcessor(ProcessingEnvironment processingEnv, TypeElement iface, boolean addRecordBuilder, RecordBuilder.Options metaData, Optional<String> packageNameOpt) {
|
||||||
this.processingEnv = processingEnv;
|
this.processingEnv = processingEnv;
|
||||||
packageName = packageNameOpt.orElseGet(() -> ElementUtils.getPackageName(iface));
|
packageName = packageNameOpt.orElseGet(() -> ElementUtils.getPackageName(iface));
|
||||||
recordComponents = getRecordComponents(iface);
|
recordComponents = getRecordComponents(iface);
|
||||||
@@ -73,14 +81,6 @@ class InternalRecordInterfaceProcessor {
|
|||||||
ClassType builderClassType = ElementUtils.getClassType(packageName, getBuilderName(iface, metaData, recordClassType, metaData.suffix()) + "." + metaData.withClassName(), iface.getTypeParameters());
|
ClassType builderClassType = ElementUtils.getClassType(packageName, getBuilderName(iface, metaData, recordClassType, metaData.suffix()) + "." + metaData.withClassName(), iface.getTypeParameters());
|
||||||
builder.addAnnotation(RecordBuilder.class);
|
builder.addAnnotation(RecordBuilder.class);
|
||||||
builder.addSuperinterface(builderClassType.typeName());
|
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);
|
alternateMethods = buildAlternateMethods(recordComponents);
|
||||||
@@ -88,7 +88,8 @@ class InternalRecordInterfaceProcessor {
|
|||||||
recordType = builder.build();
|
recordType = builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isValid() {
|
boolean isValid()
|
||||||
|
{
|
||||||
return !recordComponents.isEmpty();
|
return !recordComponents.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +105,8 @@ class InternalRecordInterfaceProcessor {
|
|||||||
return recordClassType;
|
return recordClassType;
|
||||||
}
|
}
|
||||||
|
|
||||||
String toRecord(String classSource) {
|
String toRecord(String classSource)
|
||||||
|
{
|
||||||
// javapoet does yet support records - so a class was created and we can reshape it
|
// javapoet does yet support records - so a class was created and we can reshape it
|
||||||
// The class will look something like this:
|
// The class will look something like this:
|
||||||
/*
|
/*
|
||||||
@@ -137,7 +139,8 @@ class InternalRecordInterfaceProcessor {
|
|||||||
return fixedRecord.toString();
|
return fixedRecord.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private MethodSpec generateArgumentList() {
|
private MethodSpec generateArgumentList()
|
||||||
|
{
|
||||||
MethodSpec.Builder builder = MethodSpec.methodBuilder(FAKE_METHOD_NAME);
|
MethodSpec.Builder builder = MethodSpec.methodBuilder(FAKE_METHOD_NAME);
|
||||||
recordComponents.forEach(component -> {
|
recordComponents.forEach(component -> {
|
||||||
String name = component.alternateName.orElseGet(() -> component.element.getSimpleName().toString());
|
String name = component.alternateName.orElseGet(() -> component.element.getSimpleName().toString());
|
||||||
@@ -150,18 +153,18 @@ class InternalRecordInterfaceProcessor {
|
|||||||
|
|
||||||
private List<String> buildAlternateMethods(List<Component> recordComponents) {
|
private List<String> buildAlternateMethods(List<Component> recordComponents) {
|
||||||
return recordComponents.stream()
|
return recordComponents.stream()
|
||||||
.filter(component -> component.alternateName.isPresent())
|
.filter(component -> component.alternateName.isPresent())
|
||||||
.map(component -> {
|
.map(component -> {
|
||||||
var method = MethodSpec.methodBuilder(component.element.getSimpleName().toString())
|
var method = MethodSpec.methodBuilder(component.element.getSimpleName().toString())
|
||||||
.addAnnotation(Override.class)
|
.addAnnotation(Override.class)
|
||||||
.addAnnotation(generatedRecordInterfaceAnnotation)
|
.addAnnotation(generatedRecordInterfaceAnnotation)
|
||||||
.returns(ClassName.get(component.element.getReturnType()))
|
.returns(ClassName.get(component.element.getReturnType()))
|
||||||
.addModifiers(Modifier.PUBLIC)
|
.addModifiers(Modifier.PUBLIC)
|
||||||
.addCode("return $L();", component.alternateName.get())
|
.addCode("return $L();", component.alternateName.get())
|
||||||
.build();
|
.build();
|
||||||
return method.toString();
|
return method.toString();
|
||||||
})
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Component> getRecordComponents(TypeElement iface) {
|
private List<Component> getRecordComponents(TypeElement iface) {
|
||||||
@@ -177,8 +180,8 @@ class InternalRecordInterfaceProcessor {
|
|||||||
}
|
}
|
||||||
return components;
|
return components;
|
||||||
}
|
}
|
||||||
|
private static class IllegalInterface extends RuntimeException
|
||||||
private static class IllegalInterface extends RuntimeException {
|
{
|
||||||
public IllegalInterface(String message) {
|
public IllegalInterface(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
@@ -216,13 +219,14 @@ class InternalRecordInterfaceProcessor {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<String> stripBeanPrefix(String name) {
|
private Optional<String> stripBeanPrefix(String name)
|
||||||
|
{
|
||||||
return javaBeanPrefixes.stream()
|
return javaBeanPrefixes.stream()
|
||||||
.filter(prefix -> name.startsWith(prefix) && (name.length() > prefix.length()))
|
.filter(prefix -> name.startsWith(prefix) && (name.length() > prefix.length()))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.map(prefix -> {
|
.map(prefix -> {
|
||||||
var stripped = name.substring(prefix.length());
|
var stripped = name.substring(prefix.length());
|
||||||
return Character.toLowerCase(stripped.charAt(0)) + stripped.substring(1);
|
return Character.toLowerCase(stripped.charAt(0)) + stripped.substring(1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,8 +29,10 @@ import javax.lang.model.SourceVersion;
|
|||||||
import javax.lang.model.element.Element;
|
import javax.lang.model.element.Element;
|
||||||
import javax.lang.model.element.PackageElement;
|
import javax.lang.model.element.PackageElement;
|
||||||
import javax.lang.model.element.TypeElement;
|
import javax.lang.model.element.TypeElement;
|
||||||
|
import javax.lang.model.type.TypeMirror;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
import javax.tools.JavaFileObject;
|
import javax.tools.JavaFileObject;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -38,7 +40,8 @@ import java.util.Set;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class RecordBuilderProcessor
|
public class RecordBuilderProcessor
|
||||||
extends AbstractProcessor {
|
extends AbstractProcessor
|
||||||
|
{
|
||||||
private static final String RECORD_BUILDER = RecordBuilder.class.getName();
|
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_BUILDER_INCLUDE = RecordBuilder.Include.class.getName().replace('$', '.');
|
||||||
private static final String RECORD_INTERFACE = RecordInterface.class.getName();
|
private static final String RECORD_INTERFACE = RecordInterface.class.getName();
|
||||||
@@ -48,18 +51,21 @@ public class RecordBuilderProcessor
|
|||||||
static final AnnotationSpec generatedRecordInterfaceAnnotation = AnnotationSpec.builder(Generated.class).addMember("value", "$S", RecordInterface.class.getName()).build();
|
static final AnnotationSpec generatedRecordInterfaceAnnotation = AnnotationSpec.builder(Generated.class).addMember("value", "$S", RecordInterface.class.getName()).build();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
|
||||||
|
{
|
||||||
annotations.forEach(annotation -> roundEnv.getElementsAnnotatedWith(annotation).forEach(element -> process(annotation, element)));
|
annotations.forEach(annotation -> roundEnv.getElementsAnnotatedWith(annotation).forEach(element -> process(annotation, element)));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> getSupportedAnnotationTypes() {
|
public Set<String> getSupportedAnnotationTypes()
|
||||||
|
{
|
||||||
return Set.of("*");
|
return Set.of("*");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SourceVersion getSupportedSourceVersion() {
|
public SourceVersion getSupportedSourceVersion()
|
||||||
|
{
|
||||||
// we don't directly return RELEASE_14 as that may
|
// we don't directly return RELEASE_14 as that may
|
||||||
// not exist in prior releases
|
// not exist in prior releases
|
||||||
// if we're running on an older release, returning latest()
|
// if we're running on an older release, returning latest()
|
||||||
@@ -67,51 +73,59 @@ public class RecordBuilderProcessor
|
|||||||
return SourceVersion.latest();
|
return SourceVersion.latest();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void process(TypeElement annotation, Element element) {
|
private void process(TypeElement annotation, Element element)
|
||||||
|
{
|
||||||
String annotationClass = annotation.getQualifiedName().toString();
|
String annotationClass = annotation.getQualifiedName().toString();
|
||||||
if (annotationClass.equals(RECORD_BUILDER)) {
|
if (annotationClass.equals(RECORD_BUILDER)) {
|
||||||
var typeElement = (TypeElement) element;
|
var metaData = RecordBuilderOptions.build(processingEnv.getOptions());
|
||||||
processRecordBuilder(typeElement, getMetaData(typeElement), Optional.empty());
|
processRecordBuilder((TypeElement) element, metaData, Optional.empty());
|
||||||
} else if (annotationClass.equals(RECORD_INTERFACE)) {
|
}
|
||||||
var typeElement = (TypeElement) element;
|
else if (annotationClass.equals(RECORD_INTERFACE)) {
|
||||||
processRecordInterface(typeElement, element.getAnnotation(RecordInterface.class).addRecordBuilder(), getMetaData(typeElement), Optional.empty(), false);
|
var metaData = RecordBuilderOptions.build(processingEnv.getOptions());
|
||||||
} else if (annotationClass.equals(RECORD_BUILDER_INCLUDE) || annotationClass.equals(RECORD_INTERFACE_INCLUDE)) {
|
processRecordInterface((TypeElement) element, element.getAnnotation(RecordInterface.class).addRecordBuilder(), metaData, Optional.empty());
|
||||||
processIncludes(element, getMetaData(element), annotationClass);
|
}
|
||||||
|
else if (annotationClass.equals(RECORD_BUILDER_INCLUDE) || annotationClass.equals(RECORD_INTERFACE_INCLUDE)) {
|
||||||
|
var metaData = RecordBuilderOptions.build(processingEnv.getOptions());
|
||||||
|
processIncludes(element, metaData, annotationClass);
|
||||||
} else {
|
} else {
|
||||||
var recordBuilderTemplate = annotation.getAnnotation(RecordBuilder.Template.class);
|
var recordBuilderTemplate = annotation.getAnnotation(RecordBuilder.Template.class);
|
||||||
if (recordBuilderTemplate != null) {
|
if (recordBuilderTemplate != null) {
|
||||||
if (recordBuilderTemplate.asRecordInterface()) {
|
processRecordBuilder((TypeElement) element, recordBuilderTemplate.options(), Optional.empty());
|
||||||
processRecordInterface((TypeElement) element, true, recordBuilderTemplate.options(), Optional.empty(), true);
|
|
||||||
} else {
|
|
||||||
processRecordBuilder((TypeElement) element, recordBuilderTemplate.options(), Optional.empty());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private RecordBuilder.Options getMetaData(Element element) {
|
private void processIncludes(Element element, RecordBuilder.Options metaData, String annotationClass)
|
||||||
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);
|
var annotationMirrorOpt = ElementUtils.findAnnotationMirror(processingEnv, element, annotationClass);
|
||||||
if (annotationMirrorOpt.isEmpty()) {
|
if (annotationMirrorOpt.isEmpty()) {
|
||||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not get annotation mirror for: " + annotationClass, element);
|
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not get annotation mirror for: " + annotationClass, element);
|
||||||
} else {
|
}
|
||||||
var includeHelper = new IncludeHelper(processingEnv, element, annotationMirrorOpt.get(), isRecordBuilderInclude);
|
else {
|
||||||
if (includeHelper.isValid()) {
|
var values = processingEnv.getElementUtils().getElementValuesWithDefaults(annotationMirrorOpt.get());
|
||||||
var packagePattern = ElementUtils.getStringAttribute(ElementUtils.getAnnotationValue(includeHelper.getAnnotationValues(), "packagePattern").orElse(null), "*");
|
var classes = ElementUtils.getAnnotationValue(values, "value");
|
||||||
for (var typeElement : includeHelper.getClassTypeElements()) {
|
if (classes.isEmpty()) {
|
||||||
var packageName = buildPackageName(packagePattern, element, typeElement);
|
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not get annotation value for: " + annotationClass, element);
|
||||||
if (packageName != null) {
|
}
|
||||||
if (isRecordBuilderInclude) {
|
else {
|
||||||
processRecordBuilder(typeElement, metaData, Optional.of(packageName));
|
var packagePattern = ElementUtils.getStringAttribute(ElementUtils.getAnnotationValue(values, "packagePattern").orElse(null), "*");
|
||||||
} else {
|
var classesMirrors = ElementUtils.getClassesAttribute(classes.get());
|
||||||
var addRecordBuilderOpt = ElementUtils.getAnnotationValue(includeHelper.getAnnotationValues(), "addRecordBuilder");
|
for (TypeMirror mirror : classesMirrors) {
|
||||||
var addRecordBuilder = addRecordBuilderOpt.map(ElementUtils::getBooleanAttribute).orElse(true);
|
TypeElement typeElement = (TypeElement) processingEnv.getTypeUtils().asElement(mirror);
|
||||||
processRecordInterface(typeElement, addRecordBuilder, metaData, Optional.of(packageName), false);
|
if (typeElement == null) {
|
||||||
|
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not get element for: " + mirror, element);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var packageName = buildPackageName(packagePattern, element, typeElement);
|
||||||
|
if (packageName != null) {
|
||||||
|
if (annotationClass.equals(RECORD_INTERFACE_INCLUDE)) {
|
||||||
|
var addRecordBuilderOpt = ElementUtils.getAnnotationValue(values, "addRecordBuilder");
|
||||||
|
var addRecordBuilder = addRecordBuilderOpt.map(ElementUtils::getBooleanAttribute).orElse(true);
|
||||||
|
processRecordInterface(typeElement, addRecordBuilder, metaData, Optional.of(packageName));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
processRecordBuilder(typeElement, metaData, Optional.of(packageName));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,7 +133,8 @@ public class RecordBuilderProcessor
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String buildPackageName(String packagePattern, Element builderElement, TypeElement includedClass) {
|
private String buildPackageName(String packagePattern, Element builderElement, TypeElement includedClass)
|
||||||
|
{
|
||||||
PackageElement includedClassPackage = findPackageElement(includedClass, includedClass);
|
PackageElement includedClassPackage = findPackageElement(includedClass, includedClass);
|
||||||
if (includedClassPackage == null) {
|
if (includedClassPackage == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -131,7 +146,8 @@ public class RecordBuilderProcessor
|
|||||||
return replaced.replace("@", ((PackageElement) builderElement.getEnclosingElement()).getQualifiedName().toString());
|
return replaced.replace("@", ((PackageElement) builderElement.getEnclosingElement()).getQualifiedName().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private PackageElement findPackageElement(Element actualElement, Element includedClass) {
|
private PackageElement findPackageElement(Element actualElement, Element includedClass)
|
||||||
|
{
|
||||||
if (includedClass == null) {
|
if (includedClass == null) {
|
||||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Element has not package", actualElement);
|
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Element has not package", actualElement);
|
||||||
return null;
|
return null;
|
||||||
@@ -142,19 +158,21 @@ public class RecordBuilderProcessor
|
|||||||
return findPackageElement(actualElement, includedClass.getEnclosingElement());
|
return findPackageElement(actualElement, includedClass.getEnclosingElement());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processRecordInterface(TypeElement element, boolean addRecordBuilder, RecordBuilder.Options metaData, Optional<String> packageName, boolean fromTemplate) {
|
private void processRecordInterface(TypeElement element, boolean addRecordBuilder, RecordBuilder.Options metaData, Optional<String> packageName)
|
||||||
|
{
|
||||||
if (!element.getKind().isInterface()) {
|
if (!element.getKind().isInterface()) {
|
||||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "RecordInterface only valid for interfaces.", element);
|
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "RecordInterface only valid for interfaces.", element);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var internalProcessor = new InternalRecordInterfaceProcessor(processingEnv, element, addRecordBuilder, metaData, packageName, fromTemplate);
|
var internalProcessor = new InternalRecordInterfaceProcessor(processingEnv, element, addRecordBuilder, metaData, packageName);
|
||||||
if (!internalProcessor.isValid()) {
|
if (!internalProcessor.isValid()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
writeRecordInterfaceJavaFile(element, internalProcessor.packageName(), internalProcessor.recordClassType(), internalProcessor.recordType(), metaData, internalProcessor::toRecord);
|
writeRecordInterfaceJavaFile(element, internalProcessor.packageName(), internalProcessor.recordClassType(), internalProcessor.recordType(), metaData, internalProcessor::toRecord);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processRecordBuilder(TypeElement record, RecordBuilder.Options metaData, Optional<String> packageName) {
|
private void processRecordBuilder(TypeElement record, RecordBuilder.Options metaData, Optional<String> packageName)
|
||||||
|
{
|
||||||
// we use string based name comparison for the element kind,
|
// we use string based name comparison for the element kind,
|
||||||
// as the ElementKind.RECORD enum doesn't exist on JRE releases
|
// as the ElementKind.RECORD enum doesn't exist on JRE releases
|
||||||
// older than Java 14, and we don't want to throw unexpected
|
// older than Java 14, and we don't want to throw unexpected
|
||||||
@@ -167,7 +185,8 @@ public class RecordBuilderProcessor
|
|||||||
writeRecordBuilderJavaFile(record, internalProcessor.packageName(), internalProcessor.builderClassType(), internalProcessor.builderType(), metaData);
|
writeRecordBuilderJavaFile(record, internalProcessor.packageName(), internalProcessor.builderClassType(), internalProcessor.builderType(), metaData);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeRecordBuilderJavaFile(TypeElement record, String packageName, ClassType builderClassType, TypeSpec builderType, RecordBuilder.Options metaData) {
|
private void writeRecordBuilderJavaFile(TypeElement record, String packageName, ClassType builderClassType, TypeSpec builderType, RecordBuilder.Options metaData)
|
||||||
|
{
|
||||||
// produces the Java file
|
// produces the Java file
|
||||||
JavaFile javaFile = javaFileBuilder(packageName, builderType, metaData);
|
JavaFile javaFile = javaFileBuilder(packageName, builderType, metaData);
|
||||||
Filer filer = processingEnv.getFiler();
|
Filer filer = processingEnv.getFiler();
|
||||||
@@ -177,12 +196,14 @@ public class RecordBuilderProcessor
|
|||||||
try (Writer writer = sourceFile.openWriter()) {
|
try (Writer writer = sourceFile.openWriter()) {
|
||||||
javaFile.writeTo(writer);
|
javaFile.writeTo(writer);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
}
|
||||||
|
catch (IOException e) {
|
||||||
handleWriteError(record, e);
|
handleWriteError(record, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeRecordInterfaceJavaFile(TypeElement element, String packageName, ClassType classType, TypeSpec type, RecordBuilder.Options metaData, Function<String, String> toRecordProc) {
|
private void writeRecordInterfaceJavaFile(TypeElement element, String packageName, ClassType classType, TypeSpec type, RecordBuilder.Options metaData, Function<String, String> toRecordProc)
|
||||||
|
{
|
||||||
JavaFile javaFile = javaFileBuilder(packageName, type, metaData);
|
JavaFile javaFile = javaFileBuilder(packageName, type, metaData);
|
||||||
|
|
||||||
String classSourceCode = javaFile.toString();
|
String classSourceCode = javaFile.toString();
|
||||||
@@ -196,12 +217,14 @@ public class RecordBuilderProcessor
|
|||||||
try (Writer writer = sourceFile.openWriter()) {
|
try (Writer writer = sourceFile.openWriter()) {
|
||||||
writer.write(recordSourceCode);
|
writer.write(recordSourceCode);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
}
|
||||||
|
catch (IOException e) {
|
||||||
handleWriteError(element, e);
|
handleWriteError(element, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private JavaFile javaFileBuilder(String packageName, TypeSpec type, RecordBuilder.Options metaData) {
|
private JavaFile javaFileBuilder(String packageName, TypeSpec type, RecordBuilder.Options metaData)
|
||||||
|
{
|
||||||
var javaFileBuilder = JavaFile.builder(packageName, type).skipJavaLangImports(true).indent(metaData.fileIndent());
|
var javaFileBuilder = JavaFile.builder(packageName, type).skipJavaLangImports(true).indent(metaData.fileIndent());
|
||||||
var comment = metaData.fileComment();
|
var comment = metaData.fileComment();
|
||||||
if ((comment != null) && !comment.isEmpty()) {
|
if ((comment != null) && !comment.isEmpty()) {
|
||||||
@@ -210,7 +233,8 @@ public class RecordBuilderProcessor
|
|||||||
return javaFileBuilder.build();
|
return javaFileBuilder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleWriteError(TypeElement element, IOException e) {
|
private void handleWriteError(TypeElement element, IOException e)
|
||||||
|
{
|
||||||
String message = "Could not create source file";
|
String message = "Could not create source file";
|
||||||
if (e.getMessage() != null) {
|
if (e.getMessage() != null) {
|
||||||
message = message + ": " + e.getMessage();
|
message = message + ": " + e.getMessage();
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>io.soabase.record-builder</groupId>
|
<groupId>io.soabase.record-builder</groupId>
|
||||||
<artifactId>record-builder</artifactId>
|
<artifactId>record-builder</artifactId>
|
||||||
<version>30</version>
|
<version>25-java15</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.soabase.record-builder</groupId>
|
<groupId>io.soabase.record-builder</groupId>
|
||||||
<artifactId>record-builder-processor</artifactId>
|
<artifactId>record-builder-core</artifactId>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
@@ -51,6 +51,31 @@
|
|||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<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>
|
||||||
|
<release>${jdk-version}</release>
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>${enable-preview}</arg>
|
||||||
|
</compilerArgs>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-deploy-plugin</artifactId>
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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();
|
|
||||||
}
|
|
||||||
@@ -17,20 +17,12 @@ package io.soabase.recordbuilder.test;
|
|||||||
|
|
||||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||||
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@RecordBuilder
|
@RecordBuilder
|
||||||
@RecordBuilder.Options(useImmutableCollections = true, addFunctionalMethodsToWith = true)
|
@RecordBuilder.Options(useImmutableCollections = true)
|
||||||
public record CollectionRecord<T, X extends Point>(List<T> l, Set<T> s, Map<T, X> m,
|
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> {
|
||||||
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) -> {
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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) {
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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){}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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();
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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 {
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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> {
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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> {
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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 {
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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) {
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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 {
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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 {
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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) {
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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) {
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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) {
|
|
||||||
}
|
|
||||||
@@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package io.soabase.recordbuilder.test;
|
package io.soabase.recordbuilder.test;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -24,15 +23,6 @@ import static io.soabase.recordbuilder.test.foo.PointBuilder.Point;
|
|||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class TestCollections {
|
class TestCollections {
|
||||||
@Test
|
|
||||||
void testRecordBuilderOptionsCopied() {
|
|
||||||
try {
|
|
||||||
assertNotNull(CollectionInterfaceRecordBuilder.class.getDeclaredMethod("__list", List.class));
|
|
||||||
} catch (NoSuchMethodException e) {
|
|
||||||
Assertions.fail(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCollectionRecordDefaultValues() {
|
void testCollectionRecordDefaultValues() {
|
||||||
var defaultValues = CollectionRecordBuilder.builder().build();
|
var defaultValues = CollectionRecordBuilder.builder().build();
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -15,9 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package io.soabase.recordbuilder.test;
|
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.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@@ -51,28 +48,4 @@ public class TestRecordInterface
|
|||||||
Assertions.assertEquals(generic.i(), 101);
|
Assertions.assertEquals(generic.i(), 101);
|
||||||
Assertions.assertEquals(generic.s(), now);
|
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)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,76 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -21,17 +21,6 @@ import org.junit.jupiter.api.Test;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
class TestWithers {
|
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
|
@Test
|
||||||
void testWithers() {
|
void testWithers() {
|
||||||
var r1 = new SimpleGenericRecord<>(10, List.of("1", "2", "3"));
|
var r1 = new SimpleGenericRecord<>(10, List.of("1", "2", "3"));
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>io.soabase.record-builder</groupId>
|
<groupId>io.soabase.record-builder</groupId>
|
||||||
<artifactId>record-builder</artifactId>
|
<artifactId>record-builder</artifactId>
|
||||||
<version>30</version>
|
<version>25-java15</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user