Compare commits
49 Commits
record-bui
...
record-bui
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8dbec027e4 | ||
|
|
ef09d68b78 | ||
|
|
0a608f3b20 | ||
|
|
3494d01b2b | ||
|
|
584bdf4a25 | ||
|
|
9268c295fe | ||
|
|
0d2fe2ffe7 | ||
|
|
71e22042e9 | ||
|
|
3c8e305002 | ||
|
|
76f93039b0 | ||
|
|
5f82af97ae | ||
|
|
896aab18aa | ||
|
|
75010d0add | ||
|
|
26a6141bef | ||
|
|
cdd9009fa8 | ||
|
|
0624a6caca | ||
|
|
238ef872bf | ||
|
|
ef2a3692d1 | ||
|
|
cb9ceb0529 | ||
|
|
bd8fbeb045 | ||
|
|
5b10284d4e | ||
|
|
49a19ca8f8 | ||
|
|
f9fcd1e540 | ||
|
|
eebb2b2bfd | ||
|
|
67f219bd67 | ||
|
|
d9f27bc5a6 | ||
|
|
f7b6f47bbf | ||
|
|
7e90e64988 | ||
|
|
cbe3b7cef4 | ||
|
|
6c15bdd825 | ||
|
|
53aaf6b7e9 | ||
|
|
09ed5905e3 | ||
|
|
41f59e704f | ||
|
|
d0c56e6fdf | ||
|
|
2f3477a1ca | ||
|
|
091e9493b6 | ||
|
|
a0c03e88cc | ||
|
|
d21953c39a | ||
|
|
d9d98e765e | ||
|
|
5320c8dfa4 | ||
|
|
5d7bea565f | ||
|
|
b7c4e22066 | ||
|
|
19803592a2 | ||
|
|
9e25c6fa47 | ||
|
|
c16e3bb14b | ||
|
|
57e5d43b07 | ||
|
|
feecc29eea | ||
|
|
428d3c0378 | ||
|
|
61d355b9fc |
@@ -19,6 +19,6 @@ jobs:
|
||||
- name: Set up JDK
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 16.0.0-ea
|
||||
java-version: 16
|
||||
- name: Build with Maven
|
||||
run: mvn -B package --file pom.xml
|
||||
24
.github/workflows/maven_java17.yml
vendored
Normal file
24
.github/workflows/maven_java17.yml
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
# This workflow will build a Java project with Maven
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||
|
||||
name: Maven Build - Java 17
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up JDK
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 17
|
||||
- name: Build with Maven
|
||||
run: mvn -B package --file pom.xml
|
||||
11
README.md
11
README.md
@@ -21,7 +21,7 @@ _Details:_
|
||||
- [Record From Interface Details](#RecordInterface-Example)
|
||||
- [Generation Via Includes](#generation-via-includes)
|
||||
- [Usage](#usage)
|
||||
- [Customizing](customizing.md)
|
||||
- [Customizing](customizing.md) (e.g. add immutable collections, etc.)
|
||||
- [Java 15 Versions](#java-15-versions)
|
||||
|
||||
## RecordBuilder Example
|
||||
@@ -86,6 +86,8 @@ _Hat tip to [Benji Weber](https://benjiweber.co.uk/blog/2020/09/19/fun-with-java
|
||||
|
||||
## Builder Class Definition
|
||||
|
||||
(Note: you can see a builder class built using `@RecordBuilderFull` here: [SingleItemsBuilder.java](https://gist.github.com/Randgalt/8aa487a847ea2acdd76d702f7cf17d6a))
|
||||
|
||||
The full builder class is defined as:
|
||||
|
||||
```java
|
||||
@@ -164,8 +166,8 @@ public class NameAndAgeBuilder {
|
||||
* 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(new AbstractMap.SimpleEntry<>("name", record.name()),
|
||||
new AbstractMap.SimpleEntry<>("age", record.age()));
|
||||
return Stream.of(Map.entry("name", record.name()),
|
||||
Map.entry("age", record.age()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -286,6 +288,9 @@ public void Placeholder {
|
||||
}
|
||||
```
|
||||
|
||||
`@RecordBuilder.Include` also supports a `packages` attribute that includes all records
|
||||
in the listed packages.
|
||||
|
||||
The target package for generation is the same as the package that contains the "Include"
|
||||
annotation. Use `packagePattern` to change this (see Javadoc for details).
|
||||
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
[◀︎ RecordBuilder](README.md) • Customizing RecordBuilder
|
||||
|
||||
# RecordBuilderFull
|
||||
|
||||
Note: `@RecordBuilderFull` has most optional features enabled. It's an alternate
|
||||
form of `@RecordBuilder` that uses the [templating mechanism](#create-a-custom-annotation) to
|
||||
enable optional features.
|
||||
|
||||
# Customizing RecordBuilder
|
||||
|
||||
RecordBuilder can be customized in a number of ways. The types of customizations will change over time. See
|
||||
[@RecordBuilder.Options](record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilder.java)
|
||||
for the current set of customizations and their default values.
|
||||
for the current set of customizations and their default values. For example, the `useImmutableCollections` option
|
||||
adds special handling for record components of type `java.util.List`, `java.util.Set`, `java.util.Map` and `java.util.Collection`. When the record is built, any components of these types are passed through an added shim method that uses the corresponding immutable collection (e.g. `List.copyOf(o)`) or an empty immutable collection if the component is `null`.
|
||||
|
||||
You can:
|
||||
|
||||
@@ -83,3 +90,6 @@ public @interface MyCoRecordBuilder {
|
||||
|
||||
Now, you can use `@MyCoRecordBuilder` instead of `@RecordBuilder` and the record
|
||||
will be built with options as specified.
|
||||
|
||||
Note: the template mechanism also supports `@RecordInterface` templates via the `asRecordInterface` attribute.
|
||||
When it is set a `@RecordInterface` template is created instead.
|
||||
|
||||
4
pom.xml
4
pom.xml
@@ -5,7 +5,7 @@
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>26</version>
|
||||
<version>28</version>
|
||||
|
||||
<modules>
|
||||
<module>record-builder-core</module>
|
||||
@@ -80,7 +80,7 @@
|
||||
<url>https://github.com/randgalt/record-builder</url>
|
||||
<connection>scm:git:https://github.com/randgalt/record-builder.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:randgalt/record-builder.git</developerConnection>
|
||||
<tag>record-builder-26</tag>
|
||||
<tag>record-builder-28</tag>
|
||||
</scm>
|
||||
|
||||
<issueManagement>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<parent>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder</artifactId>
|
||||
<version>26</version>
|
||||
<version>28</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -25,12 +25,32 @@ public @interface RecordBuilder {
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Inherited
|
||||
@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
|
||||
* is the literal package name however two replacement values can be used. '@'
|
||||
* is replaced with the package of the Include annotation. '*' is replaced with
|
||||
* is replaced with the package of the {@code Include} annotation. '*' is replaced with
|
||||
* the package of the included class.
|
||||
*
|
||||
* @return package pattern
|
||||
@@ -144,12 +164,27 @@ public @interface RecordBuilder {
|
||||
* (e.g. {@code List.copyOf(o)}) or an empty immutable collection if the component is {@code null}.
|
||||
*/
|
||||
boolean useImmutableCollections() default false;
|
||||
|
||||
/**
|
||||
* When enabled, collection types ({@code List}, {@code Set} and {@code Map}) are handled specially.
|
||||
* The setters for these types now create an internal collection and items are added to that
|
||||
* collection. Additionally, "adder" methods prefixed with {@link #singleItemBuilderPrefix()} are created
|
||||
* to add single items to these collections.
|
||||
*/
|
||||
boolean addSingleItemCollectionBuilders() default false;
|
||||
|
||||
/**
|
||||
* The prefix for adder methods when {@link #addSingleItemCollectionBuilders()} is enabled
|
||||
*/
|
||||
String singleItemBuilderPrefix() default "add";
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Retention(RetentionPolicy.CLASS)
|
||||
@Target(ElementType.ANNOTATION_TYPE)
|
||||
@Inherited
|
||||
@interface Template {
|
||||
RecordBuilder.Options options();
|
||||
|
||||
boolean asRecordInterface() default false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* 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
|
||||
))
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Target(ElementType.TYPE)
|
||||
@Inherited
|
||||
/**
|
||||
* An alternate form of {@code @RecordBuilder} that has most
|
||||
* optional features turned on
|
||||
*/
|
||||
public @interface RecordBuilderFull {
|
||||
}
|
||||
@@ -15,11 +15,7 @@
|
||||
*/
|
||||
package io.soabase.recordbuilder.core;
|
||||
|
||||
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;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Target(ElementType.TYPE)
|
||||
@@ -31,14 +27,31 @@ public @interface RecordInterface {
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Inherited
|
||||
@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;
|
||||
|
||||
/**
|
||||
* Pattern used to generate the package for the generated class. The value
|
||||
* is the literal package name however two replacement values can be used. '@'
|
||||
* is replaced with the package of the Include annotation. '*' is replaced with
|
||||
* is replaced with the package of the {@code Include} annotation. '*' is replaced with
|
||||
* the package of the included class.
|
||||
*
|
||||
* @return package pattern
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<parent>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder</artifactId>
|
||||
<version>26</version>
|
||||
<version>28</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package io.soabase.recordbuilder.processor;
|
||||
|
||||
import com.squareup.javapoet.*;
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
import java.util.*;
|
||||
@@ -23,7 +24,8 @@ import java.util.*;
|
||||
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.generatedRecordBuilderAnnotation;
|
||||
|
||||
class CollectionBuilderUtils {
|
||||
private final boolean enabled;
|
||||
private final boolean useImmutableCollections;
|
||||
private final boolean addSingleItemCollectionBuilders;
|
||||
private final String listShimName;
|
||||
private final String mapShimName;
|
||||
private final String setShimName;
|
||||
@@ -47,8 +49,9 @@ class CollectionBuilderUtils {
|
||||
private static final ParameterizedTypeName parameterizedSetType = ParameterizedTypeName.get(ClassName.get(Set.class), tType);
|
||||
private static final ParameterizedTypeName parameterizedCollectionType = ParameterizedTypeName.get(ClassName.get(Collection.class), tType);
|
||||
|
||||
CollectionBuilderUtils(List<RecordClassType> recordComponents, boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
CollectionBuilderUtils(List<RecordClassType> recordComponents, RecordBuilder.Options metaData) {
|
||||
useImmutableCollections = metaData.useImmutableCollections();
|
||||
addSingleItemCollectionBuilders = metaData.addSingleItemCollectionBuilders();
|
||||
|
||||
listShimName = adjustShimName(recordComponents, "__list", 0);
|
||||
mapShimName = adjustShimName(recordComponents, "__map", 0);
|
||||
@@ -56,15 +59,77 @@ class CollectionBuilderUtils {
|
||||
collectionShimName = adjustShimName(recordComponents, "__collection", 0);
|
||||
}
|
||||
|
||||
enum SingleItemsMetaDataMode {
|
||||
STANDARD,
|
||||
STANDARD_FOR_SETTER,
|
||||
EXCLUDE_WILDCARD_TYPES
|
||||
}
|
||||
|
||||
record SingleItemsMetaData(Class<?> singleItemCollectionClass, List<TypeName> typeArguments, TypeName wildType) {}
|
||||
|
||||
Optional<SingleItemsMetaData> singleItemsMetaData(RecordClassType component, SingleItemsMetaDataMode mode) {
|
||||
if (addSingleItemCollectionBuilders && (component.typeName() instanceof ParameterizedTypeName parameterizedTypeName)) {
|
||||
Class<?> collectionClass = null;
|
||||
ClassName wildcardClass = null;
|
||||
int typeArgumentQty = 0;
|
||||
if (isList(component)) {
|
||||
collectionClass = ArrayList.class;
|
||||
wildcardClass = ClassName.get(Collection.class);
|
||||
typeArgumentQty = 1;
|
||||
} else if (isSet(component)) {
|
||||
collectionClass = HashSet.class;
|
||||
wildcardClass = ClassName.get(Collection.class);
|
||||
typeArgumentQty = 1;
|
||||
} else if (isMap(component)) {
|
||||
collectionClass = HashMap.class;
|
||||
wildcardClass = (ClassName) component.rawTypeName();
|
||||
typeArgumentQty = 2;
|
||||
}
|
||||
var hasWildcardTypeArguments = hasWildcardTypeArguments(parameterizedTypeName, typeArgumentQty);
|
||||
if (collectionClass != null) {
|
||||
return switch (mode) {
|
||||
case STANDARD -> singleItemsMetaDataWithWildType(parameterizedTypeName, collectionClass, wildcardClass, typeArgumentQty);
|
||||
|
||||
case STANDARD_FOR_SETTER -> {
|
||||
if (hasWildcardTypeArguments) {
|
||||
yield Optional.of(new SingleItemsMetaData(collectionClass, parameterizedTypeName.typeArguments, component.typeName()));
|
||||
}
|
||||
yield singleItemsMetaDataWithWildType(parameterizedTypeName, collectionClass, wildcardClass, typeArgumentQty);
|
||||
}
|
||||
|
||||
case EXCLUDE_WILDCARD_TYPES -> {
|
||||
if (hasWildcardTypeArguments) {
|
||||
yield Optional.empty();
|
||||
}
|
||||
yield singleItemsMetaDataWithWildType(parameterizedTypeName, collectionClass, wildcardClass, typeArgumentQty);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
boolean isList(RecordClassType component) {
|
||||
return component.rawTypeName().equals(listType);
|
||||
}
|
||||
|
||||
boolean isMap(RecordClassType component) {
|
||||
return component.rawTypeName().equals(mapType);
|
||||
}
|
||||
|
||||
boolean isSet(RecordClassType component) {
|
||||
return component.rawTypeName().equals(setType);
|
||||
}
|
||||
|
||||
void add(CodeBlock.Builder builder, RecordClassType component) {
|
||||
if (enabled) {
|
||||
if (component.rawTypeName().equals(listType)) {
|
||||
if (useImmutableCollections) {
|
||||
if (isList(component)) {
|
||||
needsListShim = true;
|
||||
builder.add("$L($L)", listShimName, component.name());
|
||||
} else if (component.rawTypeName().equals(mapType)) {
|
||||
} else if (isMap(component)) {
|
||||
needsMapShim = true;
|
||||
builder.add("$L($L)", mapShimName, component.name());
|
||||
} else if (component.rawTypeName().equals(setType)) {
|
||||
} else if (isSet(component)) {
|
||||
needsSetShim = true;
|
||||
builder.add("$L($L)", setShimName, component.name());
|
||||
} else if (component.rawTypeName().equals(collectionType)) {
|
||||
@@ -79,7 +144,7 @@ class CollectionBuilderUtils {
|
||||
}
|
||||
|
||||
void addShims(TypeSpec.Builder builder) {
|
||||
if (!enabled) {
|
||||
if (!useImmutableCollections) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -97,8 +162,28 @@ class CollectionBuilderUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private String adjustShimName(List<RecordClassType> recordComponents, String baseName, int index)
|
||||
{
|
||||
private Optional<SingleItemsMetaData> singleItemsMetaDataWithWildType(ParameterizedTypeName parameterizedTypeName, Class<?> collectionClass, ClassName wildcardClass, int typeArgumentQty) {
|
||||
TypeName wildType;
|
||||
if (typeArgumentQty == 1) {
|
||||
wildType = ParameterizedTypeName.get(wildcardClass, WildcardTypeName.subtypeOf(parameterizedTypeName.typeArguments.get(0)));
|
||||
} else { // if (typeArgumentQty == 2)
|
||||
wildType = ParameterizedTypeName.get(wildcardClass, WildcardTypeName.subtypeOf(parameterizedTypeName.typeArguments.get(0)), WildcardTypeName.subtypeOf(parameterizedTypeName.typeArguments.get(1)));
|
||||
}
|
||||
return Optional.of(new SingleItemsMetaData(collectionClass, parameterizedTypeName.typeArguments, wildType));
|
||||
}
|
||||
|
||||
private boolean hasWildcardTypeArguments(ParameterizedTypeName parameterizedTypeName, int argumentCount) {
|
||||
for (int i = 0; i < argumentCount; ++i) {
|
||||
if (parameterizedTypeName.typeArguments.size() > i) {
|
||||
if (parameterizedTypeName.typeArguments.get(i) instanceof WildcardTypeName) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String adjustShimName(List<RecordClassType> recordComponents, String baseName, int index) {
|
||||
var name = (index == 0) ? baseName : (baseName + index);
|
||||
if (recordComponents.stream().anyMatch(component -> component.name().equals(name))) {
|
||||
return adjustShimName(recordComponents, baseName, index + 1);
|
||||
@@ -122,7 +207,7 @@ class CollectionBuilderUtils {
|
||||
var code = CodeBlock.builder()
|
||||
.add("if (o instanceof Set) {\n")
|
||||
.indent()
|
||||
.addStatement("return (o != null) ? $T.copyOf(o) : $T.of()", setType, setType)
|
||||
.addStatement("return $T.copyOf(o)", setType)
|
||||
.unindent()
|
||||
.addStatement("}")
|
||||
.addStatement("return (o != null) ? $T.copyOf(o) : $T.of()", listType, listType)
|
||||
|
||||
@@ -52,12 +52,19 @@ public class ElementUtils {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<TypeMirror> getClassesAttribute(AnnotationValue attribute)
|
||||
public static List<TypeMirror> getAttributeTypeMirrorList(AnnotationValue attribute)
|
||||
{
|
||||
List<? extends AnnotationValue> values = (attribute != null) ? (List<? extends AnnotationValue>)attribute.getValue() : Collections.emptyList();
|
||||
return values.stream().map(v -> (TypeMirror)v.getValue()).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<String> getAttributeStringList(AnnotationValue attribute)
|
||||
{
|
||||
List<? extends AnnotationValue> values = (attribute != null) ? (List<? extends AnnotationValue>)attribute.getValue() : Collections.emptyList();
|
||||
return values.stream().map(v -> (String)v.getValue()).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static boolean getBooleanAttribute(AnnotationValue attribute)
|
||||
{
|
||||
Object value = (attribute != null) ? attribute.getValue() : null;
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.processor;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import javax.tools.Diagnostic;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class IncludeHelper {
|
||||
private final boolean isValid;
|
||||
private final List<TypeElement> classTypeElements;
|
||||
private final Map<? extends ExecutableElement, ? extends AnnotationValue> annotationValues;
|
||||
|
||||
IncludeHelper(ProcessingEnvironment processingEnv, Element element, AnnotationMirror annotationMirror, boolean packagesSupported) {
|
||||
annotationValues = processingEnv.getElementUtils().getElementValuesWithDefaults(annotationMirror);
|
||||
var value = ElementUtils.getAnnotationValue(annotationValues, "value");
|
||||
var classes = ElementUtils.getAnnotationValue(annotationValues, "classes");
|
||||
var packages = ElementUtils.getAnnotationValue(annotationValues, "packages");
|
||||
var isValid = true;
|
||||
var classTypeElements = new ArrayList<TypeElement>();
|
||||
if (value.isPresent() || classes.isPresent() || packages.isPresent()) {
|
||||
var valueList = value.map(ElementUtils::getAttributeTypeMirrorList).orElseGet(List::of);
|
||||
var classesList = classes.map(ElementUtils::getAttributeTypeMirrorList).orElseGet(List::of);
|
||||
var packagesList = packages.map(ElementUtils::getAttributeStringList).orElseGet(List::of);
|
||||
if (valueList.isEmpty() && classesList.isEmpty() && packagesList.isEmpty()) {
|
||||
if (packagesSupported) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "At least one of \"value\", \"classes\" or \"packages\" required", element);
|
||||
} else {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "At least one of \"value\" or \"classes\" required", element);
|
||||
}
|
||||
isValid = false;
|
||||
}
|
||||
isValid = processList(processingEnv, isValid, element, valueList, classTypeElements);
|
||||
isValid = processList(processingEnv, isValid, element, classesList, classTypeElements);
|
||||
packages.ifPresent(annotationValue -> processPackages(processingEnv, classTypeElements, packagesList));
|
||||
} else {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not read attribute for annotation", element);
|
||||
isValid = false;
|
||||
}
|
||||
this.isValid = isValid;
|
||||
this.classTypeElements = List.copyOf(classTypeElements);
|
||||
}
|
||||
|
||||
Map<? extends ExecutableElement, ? extends AnnotationValue> getAnnotationValues() {
|
||||
return annotationValues;
|
||||
}
|
||||
|
||||
boolean isValid() {
|
||||
return isValid;
|
||||
}
|
||||
|
||||
List<TypeElement> getClassTypeElements() {
|
||||
return classTypeElements;
|
||||
}
|
||||
|
||||
private boolean processList(ProcessingEnvironment processingEnv, boolean isValid, Element element, List<TypeMirror> list, ArrayList<TypeElement> classTypeElements) {
|
||||
for (var typeMirror : list) {
|
||||
TypeElement typeElement = (TypeElement) processingEnv.getTypeUtils().asElement(typeMirror);
|
||||
if (typeElement == null) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not get element for: " + typeMirror, element);
|
||||
isValid = false;
|
||||
} else {
|
||||
classTypeElements.add(typeElement);
|
||||
}
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
||||
private void processPackages(ProcessingEnvironment processingEnv, List<TypeElement> classTypeElements, List<String> packagesList) {
|
||||
for (var packageName : packagesList) {
|
||||
var packageElement = processingEnv.getElementUtils().getPackageElement(packageName);
|
||||
for (var child : packageElement.getEnclosedElements()) {
|
||||
if (child.getKind() == ElementKind.RECORD) {
|
||||
classTypeElements.add((TypeElement) child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.soabase.recordbuilder.processor.CollectionBuilderUtils.SingleItemsMetaDataMode.*;
|
||||
import static io.soabase.recordbuilder.processor.ElementUtils.getBuilderName;
|
||||
import static io.soabase.recordbuilder.processor.ElementUtils.getWithMethodName;
|
||||
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.generatedRecordBuilderAnnotation;
|
||||
@@ -55,7 +56,7 @@ class InternalRecordBuilderProcessor {
|
||||
InternalRecordBuilderProcessor(ProcessingEnvironment processingEnv, TypeElement record, RecordBuilder.Options metaData, Optional<String> packageNameOpt) {
|
||||
this.processingEnv = processingEnv;
|
||||
var recordActualPackage = ElementUtils.getPackageName(record);
|
||||
this.metaData = getMetaData(record, metaData);
|
||||
this.metaData = metaData;
|
||||
recordClassType = ElementUtils.getClassType(record, record.getTypeParameters());
|
||||
packageName = packageNameOpt.orElse(recordActualPackage);
|
||||
builderClassType = ElementUtils.getClassType(packageName, getBuilderName(record, metaData, recordClassType, metaData.suffix()), record.getTypeParameters());
|
||||
@@ -63,7 +64,7 @@ class InternalRecordBuilderProcessor {
|
||||
recordComponents = buildRecordComponents(record);
|
||||
uniqueVarName = getUniqueVarName();
|
||||
notNullPattern = Pattern.compile(metaData.interpretNotNullsPattern());
|
||||
collectionBuilderUtils = new CollectionBuilderUtils(recordComponents, this.metaData.useImmutableCollections());
|
||||
collectionBuilderUtils = new CollectionBuilderUtils(recordComponents, this.metaData);
|
||||
|
||||
builder = TypeSpec.classBuilder(builderClassType.name())
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
@@ -86,6 +87,8 @@ class InternalRecordBuilderProcessor {
|
||||
add1Field(component);
|
||||
add1SetterMethod(component);
|
||||
add1GetterMethod(component);
|
||||
var collectionMetaData = collectionBuilderUtils.singleItemsMetaData(component, EXCLUDE_WILDCARD_TYPES);
|
||||
collectionMetaData.ifPresent(meta -> add1CollectionBuilders(meta, component));
|
||||
});
|
||||
collectionBuilderUtils.addShims(builder);
|
||||
builderType = builder.build();
|
||||
@@ -109,8 +112,7 @@ class InternalRecordBuilderProcessor {
|
||||
builder.addModifiers(Modifier.PUBLIC); // builders are top level classes - can only be public or package-private
|
||||
}
|
||||
// is package-private
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
builder.addModifiers(Modifier.PUBLIC);
|
||||
}
|
||||
}
|
||||
@@ -128,11 +130,6 @@ class InternalRecordBuilderProcessor {
|
||||
.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() {
|
||||
/*
|
||||
Adds a nested interface that adds withers similar to:
|
||||
@@ -239,6 +236,7 @@ class InternalRecordBuilderProcessor {
|
||||
codeBlockBuilder.add(";$]");
|
||||
|
||||
var methodName = getWithMethodName(component, metaData.withClassMethodPrefix());
|
||||
var singleItemsMetaData = collectionBuilderUtils.singleItemsMetaData(component, STANDARD_FOR_SETTER);
|
||||
var parameterSpecBuilder = ParameterSpec.builder(component.typeName(), component.name());
|
||||
addConstructorAnnotations(component, parameterSpecBuilder);
|
||||
var methodSpec = MethodSpec.methodBuilder(methodName)
|
||||
@@ -341,8 +339,9 @@ class InternalRecordBuilderProcessor {
|
||||
.addAnnotation(generatedRecordBuilderAnnotation);
|
||||
recordComponents.forEach(component -> {
|
||||
constructorBuilder.addParameter(component.typeName(), component.name());
|
||||
var codeBuilder = CodeBlock.builder().add("this.$L = $L", component.name(), component.name());
|
||||
constructorBuilder.addStatement(codeBuilder.build());
|
||||
var collectionMetaData = collectionBuilderUtils.singleItemsMetaData(component, STANDARD);
|
||||
collectionMetaData.ifPresentOrElse(meta -> constructorBuilder.addStatement("this.$L = new $T<>($L)", component.name(), meta.singleItemCollectionClass(), component.name()),
|
||||
() -> constructorBuilder.addStatement("this.$L = $L", component.name(), component.name()));
|
||||
});
|
||||
builder.addMethod(constructorBuilder.build());
|
||||
}
|
||||
@@ -537,10 +536,8 @@ class InternalRecordBuilderProcessor {
|
||||
Adds a static method that converts a record instance into a stream of its component parts
|
||||
|
||||
public static Stream<Map.Entry<String, Object>> stream(MyRecord record) {
|
||||
return Stream.of(
|
||||
new AbstractMap.SimpleEntry<>("p1", record.p1()),
|
||||
new AbstractMap.SimpleEntry<>("p2", record.p2())
|
||||
);
|
||||
return Stream.of(Map.entry("p1", record.p1()),
|
||||
Map.entry("p2", record.p2()));
|
||||
}
|
||||
*/
|
||||
var codeBuilder = CodeBlock.builder().add("return $T.of(", Stream.class);
|
||||
@@ -549,7 +546,7 @@ class InternalRecordBuilderProcessor {
|
||||
codeBuilder.add(",\n ");
|
||||
}
|
||||
var name = recordComponents.get(index).name();
|
||||
codeBuilder.add("new $T<>($S, record.$L())", AbstractMap.SimpleEntry.class, name, name);
|
||||
codeBuilder.add("$T.entry($S, record.$L())", Map.class, name, name);
|
||||
});
|
||||
codeBuilder.add(")");
|
||||
var mapEntryTypeVariables = ParameterizedTypeName.get(Map.Entry.class, String.class, Object.class);
|
||||
@@ -638,6 +635,129 @@ 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) {
|
||||
/*
|
||||
For a single record component, add a getter similar to:
|
||||
@@ -665,19 +785,27 @@ class InternalRecordBuilderProcessor {
|
||||
return this;
|
||||
}
|
||||
*/
|
||||
var parameterSpecBuilder = ParameterSpec.builder(component.typeName(), component.name());
|
||||
addConstructorAnnotations(component, parameterSpecBuilder);
|
||||
|
||||
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)
|
||||
.addAnnotation(generatedRecordBuilderAnnotation)
|
||||
.addParameter(parameterSpecBuilder.build())
|
||||
.returns(builderClassType.typeName())
|
||||
.addStatement("this.$L = $L", component.name(), component.name())
|
||||
.addStatement("return this")
|
||||
.build();
|
||||
builder.addMethod(methodSpec);
|
||||
.returns(builderClassType.typeName());
|
||||
|
||||
var collectionMetaData = collectionBuilderUtils.singleItemsMetaData(component, STANDARD_FOR_SETTER);
|
||||
var parameterSpecBuilder = collectionMetaData.map(meta -> {
|
||||
CodeBlock.Builder codeSpec = CodeBlock.builder();
|
||||
codeSpec.addStatement("this.$L = ($L != null) ? new $T<>($L) : null", component.name(), component.name(), meta.singleItemCollectionClass(), component.name());
|
||||
methodSpec.addJavadoc("Re-create the internally allocated {@code $L} for {@code $L} by copying the argument\n", meta.singleItemCollectionClass().getSimpleName(), component.name())
|
||||
.addCode(codeSpec.build());
|
||||
return ParameterSpec.builder(meta.wildType(), component.name());
|
||||
}).orElseGet(() -> {
|
||||
methodSpec.addJavadoc("Set a new value for the {@code $L} record component in the builder\n", component.name())
|
||||
.addStatement("this.$L = $L", component.name(), component.name());
|
||||
return ParameterSpec.builder(component.typeName(), component.name());
|
||||
});
|
||||
addConstructorAnnotations(component, parameterSpecBuilder);
|
||||
methodSpec.addStatement("return this").addParameter(parameterSpecBuilder.build());
|
||||
builder.addMethod(methodSpec.build());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,13 +15,10 @@
|
||||
*/
|
||||
package io.soabase.recordbuilder.processor;
|
||||
|
||||
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 com.squareup.javapoet.*;
|
||||
import io.soabase.recordbuilder.core.IgnoreDefaultMethod;
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
@@ -29,13 +26,7 @@ import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.type.TypeKind;
|
||||
import javax.tools.Diagnostic;
|
||||
import java.util.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.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -56,9 +47,10 @@ class InternalRecordInterfaceProcessor {
|
||||
|
||||
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) {
|
||||
InternalRecordInterfaceProcessor(ProcessingEnvironment processingEnv, TypeElement iface, boolean addRecordBuilder, RecordBuilder.Options metaData, Optional<String> packageNameOpt, boolean fromTemplate) {
|
||||
this.processingEnv = processingEnv;
|
||||
packageName = packageNameOpt.orElseGet(() -> ElementUtils.getPackageName(iface));
|
||||
recordComponents = getRecordComponents(iface);
|
||||
@@ -81,6 +73,14 @@ class InternalRecordInterfaceProcessor {
|
||||
ClassType builderClassType = ElementUtils.getClassType(packageName, getBuilderName(iface, metaData, recordClassType, metaData.suffix()) + "." + metaData.withClassName(), iface.getTypeParameters());
|
||||
builder.addAnnotation(RecordBuilder.class);
|
||||
builder.addSuperinterface(builderClassType.typeName());
|
||||
if (fromTemplate) {
|
||||
builder.addAnnotation(AnnotationSpec.get(metaData));
|
||||
} else {
|
||||
var options = iface.getAnnotation(RecordBuilder.Options.class);
|
||||
if (options != null) {
|
||||
builder.addAnnotation(AnnotationSpec.get(options));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
alternateMethods = buildAlternateMethods(recordComponents);
|
||||
@@ -88,8 +88,7 @@ class InternalRecordInterfaceProcessor {
|
||||
recordType = builder.build();
|
||||
}
|
||||
|
||||
boolean isValid()
|
||||
{
|
||||
boolean isValid() {
|
||||
return !recordComponents.isEmpty();
|
||||
}
|
||||
|
||||
@@ -105,8 +104,7 @@ class InternalRecordInterfaceProcessor {
|
||||
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
|
||||
// The class will look something like this:
|
||||
/*
|
||||
@@ -139,8 +137,7 @@ class InternalRecordInterfaceProcessor {
|
||||
return fixedRecord.toString();
|
||||
}
|
||||
|
||||
private MethodSpec generateArgumentList()
|
||||
{
|
||||
private MethodSpec generateArgumentList() {
|
||||
MethodSpec.Builder builder = MethodSpec.methodBuilder(FAKE_METHOD_NAME);
|
||||
recordComponents.forEach(component -> {
|
||||
String name = component.alternateName.orElseGet(() -> component.element.getSimpleName().toString());
|
||||
@@ -153,18 +150,18 @@ class InternalRecordInterfaceProcessor {
|
||||
|
||||
private List<String> buildAlternateMethods(List<Component> recordComponents) {
|
||||
return recordComponents.stream()
|
||||
.filter(component -> component.alternateName.isPresent())
|
||||
.map(component -> {
|
||||
var method = MethodSpec.methodBuilder(component.element.getSimpleName().toString())
|
||||
.addAnnotation(Override.class)
|
||||
.addAnnotation(generatedRecordInterfaceAnnotation)
|
||||
.returns(ClassName.get(component.element.getReturnType()))
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addCode("return $L();", component.alternateName.get())
|
||||
.build();
|
||||
return method.toString();
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
.filter(component -> component.alternateName.isPresent())
|
||||
.map(component -> {
|
||||
var method = MethodSpec.methodBuilder(component.element.getSimpleName().toString())
|
||||
.addAnnotation(Override.class)
|
||||
.addAnnotation(generatedRecordInterfaceAnnotation)
|
||||
.returns(ClassName.get(component.element.getReturnType()))
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addCode("return $L();", component.alternateName.get())
|
||||
.build();
|
||||
return method.toString();
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<Component> getRecordComponents(TypeElement iface) {
|
||||
@@ -180,8 +177,8 @@ class InternalRecordInterfaceProcessor {
|
||||
}
|
||||
return components;
|
||||
}
|
||||
private static class IllegalInterface extends RuntimeException
|
||||
{
|
||||
|
||||
private static class IllegalInterface extends RuntimeException {
|
||||
public IllegalInterface(String message) {
|
||||
super(message);
|
||||
}
|
||||
@@ -219,14 +216,13 @@ class InternalRecordInterfaceProcessor {
|
||||
});
|
||||
}
|
||||
|
||||
private Optional<String> stripBeanPrefix(String name)
|
||||
{
|
||||
private Optional<String> stripBeanPrefix(String name) {
|
||||
return javaBeanPrefixes.stream()
|
||||
.filter(prefix -> name.startsWith(prefix) && (name.length() > prefix.length()))
|
||||
.findFirst()
|
||||
.map(prefix -> {
|
||||
var stripped = name.substring(prefix.length());
|
||||
return Character.toLowerCase(stripped.charAt(0)) + stripped.substring(1);
|
||||
});
|
||||
.filter(prefix -> name.startsWith(prefix) && (name.length() > prefix.length()))
|
||||
.findFirst()
|
||||
.map(prefix -> {
|
||||
var stripped = name.substring(prefix.length());
|
||||
return Character.toLowerCase(stripped.charAt(0)) + stripped.substring(1);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,10 +29,8 @@ import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.PackageElement;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import javax.tools.Diagnostic;
|
||||
import javax.tools.JavaFileObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Optional;
|
||||
@@ -40,8 +38,7 @@ import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class RecordBuilderProcessor
|
||||
extends AbstractProcessor
|
||||
{
|
||||
extends AbstractProcessor {
|
||||
private static final String RECORD_BUILDER = RecordBuilder.class.getName();
|
||||
private static final String RECORD_BUILDER_INCLUDE = RecordBuilder.Include.class.getName().replace('$', '.');
|
||||
private static final String RECORD_INTERFACE = RecordInterface.class.getName();
|
||||
@@ -51,21 +48,18 @@ public class RecordBuilderProcessor
|
||||
static final AnnotationSpec generatedRecordInterfaceAnnotation = AnnotationSpec.builder(Generated.class).addMember("value", "$S", RecordInterface.class.getName()).build();
|
||||
|
||||
@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)));
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getSupportedAnnotationTypes()
|
||||
{
|
||||
public Set<String> getSupportedAnnotationTypes() {
|
||||
return Set.of("*");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourceVersion getSupportedSourceVersion()
|
||||
{
|
||||
public SourceVersion getSupportedSourceVersion() {
|
||||
// we don't directly return RELEASE_14 as that may
|
||||
// not exist in prior releases
|
||||
// if we're running on an older release, returning latest()
|
||||
@@ -73,59 +67,52 @@ public class RecordBuilderProcessor
|
||||
return SourceVersion.latest();
|
||||
}
|
||||
|
||||
private void process(TypeElement annotation, Element element)
|
||||
{
|
||||
private void process(TypeElement annotation, Element element) {
|
||||
String annotationClass = annotation.getQualifiedName().toString();
|
||||
if (annotationClass.equals(RECORD_BUILDER)) {
|
||||
var metaData = RecordBuilderOptions.build(processingEnv.getOptions());
|
||||
processRecordBuilder((TypeElement) element, metaData, Optional.empty());
|
||||
}
|
||||
else if (annotationClass.equals(RECORD_INTERFACE)) {
|
||||
var metaData = RecordBuilderOptions.build(processingEnv.getOptions());
|
||||
processRecordInterface((TypeElement) element, element.getAnnotation(RecordInterface.class).addRecordBuilder(), metaData, Optional.empty());
|
||||
}
|
||||
else if (annotationClass.equals(RECORD_BUILDER_INCLUDE) || annotationClass.equals(RECORD_INTERFACE_INCLUDE)) {
|
||||
var typeElement = (TypeElement) element;
|
||||
processRecordBuilder(typeElement, getMetaData(typeElement), Optional.empty());
|
||||
} else if (annotationClass.equals(RECORD_INTERFACE)) {
|
||||
var typeElement = (TypeElement) element;
|
||||
processRecordInterface(typeElement, element.getAnnotation(RecordInterface.class).addRecordBuilder(), getMetaData(typeElement), Optional.empty(), false);
|
||||
} else if (annotationClass.equals(RECORD_BUILDER_INCLUDE) || annotationClass.equals(RECORD_INTERFACE_INCLUDE)) {
|
||||
var metaData = RecordBuilderOptions.build(processingEnv.getOptions());
|
||||
processIncludes(element, metaData, annotationClass);
|
||||
} else {
|
||||
var recordBuilderTemplate = annotation.getAnnotation(RecordBuilder.Template.class);
|
||||
if (recordBuilderTemplate != null) {
|
||||
processRecordBuilder((TypeElement) element, recordBuilderTemplate.options(), Optional.empty());
|
||||
if (recordBuilderTemplate.asRecordInterface()) {
|
||||
processRecordInterface((TypeElement) element, true, recordBuilderTemplate.options(), Optional.empty(), true);
|
||||
} else {
|
||||
processRecordBuilder((TypeElement) element, recordBuilderTemplate.options(), Optional.empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processIncludes(Element element, RecordBuilder.Options metaData, String annotationClass)
|
||||
{
|
||||
private RecordBuilder.Options getMetaData(TypeElement typeElement) {
|
||||
var recordSpecificMetaData = typeElement.getAnnotation(RecordBuilder.Options.class);
|
||||
return (recordSpecificMetaData != null) ? recordSpecificMetaData : RecordBuilderOptions.build(processingEnv.getOptions());
|
||||
}
|
||||
|
||||
private void processIncludes(Element element, RecordBuilder.Options metaData, String annotationClass) {
|
||||
var isRecordBuilderInclude = annotationClass.equals(RECORD_BUILDER_INCLUDE);
|
||||
var annotationMirrorOpt = ElementUtils.findAnnotationMirror(processingEnv, element, annotationClass);
|
||||
if (annotationMirrorOpt.isEmpty()) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not get annotation mirror for: " + annotationClass, element);
|
||||
}
|
||||
else {
|
||||
var values = processingEnv.getElementUtils().getElementValuesWithDefaults(annotationMirrorOpt.get());
|
||||
var classes = ElementUtils.getAnnotationValue(values, "value");
|
||||
if (classes.isEmpty()) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not get annotation value for: " + annotationClass, element);
|
||||
}
|
||||
else {
|
||||
var packagePattern = ElementUtils.getStringAttribute(ElementUtils.getAnnotationValue(values, "packagePattern").orElse(null), "*");
|
||||
var classesMirrors = ElementUtils.getClassesAttribute(classes.get());
|
||||
for (TypeMirror mirror : classesMirrors) {
|
||||
TypeElement typeElement = (TypeElement) processingEnv.getTypeUtils().asElement(mirror);
|
||||
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));
|
||||
}
|
||||
} else {
|
||||
var includeHelper = new IncludeHelper(processingEnv, element, annotationMirrorOpt.get(), isRecordBuilderInclude);
|
||||
if (includeHelper.isValid()) {
|
||||
var packagePattern = ElementUtils.getStringAttribute(ElementUtils.getAnnotationValue(includeHelper.getAnnotationValues(), "packagePattern").orElse(null), "*");
|
||||
for (var typeElement : includeHelper.getClassTypeElements()) {
|
||||
var packageName = buildPackageName(packagePattern, element, typeElement);
|
||||
if (packageName != null) {
|
||||
if (isRecordBuilderInclude) {
|
||||
processRecordBuilder(typeElement, metaData, Optional.of(packageName));
|
||||
} else {
|
||||
var addRecordBuilderOpt = ElementUtils.getAnnotationValue(includeHelper.getAnnotationValues(), "addRecordBuilder");
|
||||
var addRecordBuilder = addRecordBuilderOpt.map(ElementUtils::getBooleanAttribute).orElse(true);
|
||||
processRecordInterface(typeElement, addRecordBuilder, metaData, Optional.of(packageName), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -133,8 +120,7 @@ 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);
|
||||
if (includedClassPackage == null) {
|
||||
return null;
|
||||
@@ -146,8 +132,7 @@ public class RecordBuilderProcessor
|
||||
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) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Element has not package", actualElement);
|
||||
return null;
|
||||
@@ -158,21 +143,19 @@ public class RecordBuilderProcessor
|
||||
return findPackageElement(actualElement, includedClass.getEnclosingElement());
|
||||
}
|
||||
|
||||
private void processRecordInterface(TypeElement element, boolean addRecordBuilder, RecordBuilder.Options metaData, Optional<String> packageName)
|
||||
{
|
||||
private void processRecordInterface(TypeElement element, boolean addRecordBuilder, RecordBuilder.Options metaData, Optional<String> packageName, boolean fromTemplate) {
|
||||
if (!element.getKind().isInterface()) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "RecordInterface only valid for interfaces.", element);
|
||||
return;
|
||||
}
|
||||
var internalProcessor = new InternalRecordInterfaceProcessor(processingEnv, element, addRecordBuilder, metaData, packageName);
|
||||
var internalProcessor = new InternalRecordInterfaceProcessor(processingEnv, element, addRecordBuilder, metaData, packageName, fromTemplate);
|
||||
if (!internalProcessor.isValid()) {
|
||||
return;
|
||||
}
|
||||
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,
|
||||
// as the ElementKind.RECORD enum doesn't exist on JRE releases
|
||||
// older than Java 14, and we don't want to throw unexpected
|
||||
@@ -185,8 +168,7 @@ public class RecordBuilderProcessor
|
||||
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
|
||||
JavaFile javaFile = javaFileBuilder(packageName, builderType, metaData);
|
||||
Filer filer = processingEnv.getFiler();
|
||||
@@ -196,14 +178,12 @@ public class RecordBuilderProcessor
|
||||
try (Writer writer = sourceFile.openWriter()) {
|
||||
javaFile.writeTo(writer);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
} catch (IOException 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);
|
||||
|
||||
String classSourceCode = javaFile.toString();
|
||||
@@ -217,14 +197,12 @@ public class RecordBuilderProcessor
|
||||
try (Writer writer = sourceFile.openWriter()) {
|
||||
writer.write(recordSourceCode);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
} catch (IOException 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 comment = metaData.fileComment();
|
||||
if ((comment != null) && !comment.isEmpty()) {
|
||||
@@ -233,8 +211,7 @@ public class RecordBuilderProcessor
|
||||
return javaFileBuilder.build();
|
||||
}
|
||||
|
||||
private void handleWriteError(TypeElement element, IOException e)
|
||||
{
|
||||
private void handleWriteError(TypeElement element, IOException e) {
|
||||
String message = "Could not create source file";
|
||||
if (e.getMessage() != null) {
|
||||
message = message + ": " + e.getMessage();
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<parent>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder</artifactId>
|
||||
<version>26</version>
|
||||
<version>28</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
import io.soabase.recordbuilder.core.RecordInterface;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@RecordInterface
|
||||
@RecordBuilder.Options(useImmutableCollections = true)
|
||||
public interface CollectionInterface<T, X extends Point> {
|
||||
List<T> l();
|
||||
|
||||
Set<T> s();
|
||||
|
||||
Map<T, X> m();
|
||||
|
||||
Collection<X> c();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilderFull;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RecordBuilderFull
|
||||
public record FullRecord(@NotNull List<Number> numbers, @NotNull Map<Number, FullRecord> fullRecords) {
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
@MyInterfaceTemplate
|
||||
public interface InterfaceTemplateTest {
|
||||
String name();
|
||||
|
||||
int age();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
@RecordBuilder.Template(options = @RecordBuilder.Options(
|
||||
fileComment = "This is a test",
|
||||
withClassName = "Com"),
|
||||
asRecordInterface = true
|
||||
)
|
||||
public @interface MyInterfaceTemplate {
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* 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
|
||||
)
|
||||
public record SingleItems<T>(List<String> strings, Set<List<T>> sets, Map<Instant, T> map, Collection<T> collection) implements SingleItemsBuilder.With<T> {
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@RecordBuilder
|
||||
@RecordBuilder.Options(
|
||||
addSingleItemCollectionBuilders = true,
|
||||
useImmutableCollections = true
|
||||
)
|
||||
public record WildcardSingleItems<T>(List<? extends String> strings, Set<? extends List<? extends T>> sets, Map<? extends Instant, ? extends T> map, Collection<? extends T> collection) implements WildcardSingleItemsBuilder.With<T> {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
@RecordBuilder.Include(
|
||||
packages = "io.soabase.recordbuilder.test.includes.pack",
|
||||
classes = JustATest.class
|
||||
)
|
||||
public class IncludeFactory {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes;
|
||||
|
||||
public record JustATest(int i) {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes.pack;
|
||||
|
||||
public interface AlsoIgnoreMe {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes.pack;
|
||||
|
||||
public class IgnoreMe {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes.pack;
|
||||
|
||||
public record PackRecord1(String name) {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes.pack;
|
||||
|
||||
public record PackRecord2(String name, int age) {
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test.includes.pack;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public record PackRecord3(Instant time, int age) {
|
||||
}
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
@@ -23,6 +24,15 @@ import static io.soabase.recordbuilder.test.foo.PointBuilder.Point;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class TestCollections {
|
||||
@Test
|
||||
void testRecordBuilderOptionsCopied() {
|
||||
try {
|
||||
assertNotNull(CollectionInterfaceRecordBuilder.class.getDeclaredMethod("__list", List.class));
|
||||
} catch (NoSuchMethodException e) {
|
||||
Assertions.fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCollectionRecordDefaultValues() {
|
||||
var defaultValues = CollectionRecordBuilder.builder().build();
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
class TestRecordBuilderFull {
|
||||
@Test
|
||||
void testNonNull() {
|
||||
Assertions.assertThrows(NullPointerException.class, () -> FullRecordBuilder.builder().build());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testImmutable() {
|
||||
var record = FullRecordBuilder.builder()
|
||||
.fullRecords(new HashMap<>())
|
||||
.numbers(new ArrayList<>())
|
||||
.build();
|
||||
Assertions.assertThrows(UnsupportedOperationException.class, () -> record.fullRecords().put(1, record));
|
||||
Assertions.assertThrows(UnsupportedOperationException.class, () -> record.numbers().add(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Copyright 2019 Jordan Zimmerman
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.soabase.recordbuilder.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class TestSingleItems {
|
||||
@Test
|
||||
public void testInternalCollections()
|
||||
{
|
||||
var now = Instant.now();
|
||||
var item = SingleItemsBuilder.<String>builder()
|
||||
.add1Map(now, "now")
|
||||
.add1Map(Instant.MIN, "before")
|
||||
.add1Sets(Arrays.asList("1", "2"))
|
||||
.add1Sets(List.of("3"))
|
||||
.add1Strings("a")
|
||||
.add1Strings("b")
|
||||
.add1Strings("c")
|
||||
.build();
|
||||
Assertions.assertEquals(item.map(), Map.of(now, "now", Instant.MIN, "before"));
|
||||
Assertions.assertEquals(item.sets(), Set.of(List.of("1", "2"), List.of("3")));
|
||||
Assertions.assertEquals(item.strings(), List.of("a", "b", "c"));
|
||||
|
||||
var copy = item.with()
|
||||
.add1Strings("new")
|
||||
.add1Map(Instant.MAX, "after")
|
||||
.add1Sets(List.of("10", "20", "30"))
|
||||
.build();
|
||||
Assertions.assertNotEquals(item, copy);
|
||||
Assertions.assertEquals(copy.map(), Map.of(now, "now", Instant.MIN, "before", Instant.MAX, "after"));
|
||||
Assertions.assertEquals(copy.sets(), Set.of(List.of("1", "2"), List.of("3"), List.of("10", "20", "30")));
|
||||
Assertions.assertEquals(copy.strings(), List.of("a", "b", "c", "new"));
|
||||
|
||||
var stringsToAdd = Arrays.asList("x", "y", "z");
|
||||
var listToAdd = Arrays.asList(List.of("aa", "bb"), List.of("cc"));
|
||||
var mapToAdd = Map.of(now.plusMillis(1), "now+1", now.plusMillis(2), "now+2");
|
||||
var streamed = SingleItemsBuilder.builder(item)
|
||||
.add1Strings(stringsToAdd.stream())
|
||||
.add1Sets(listToAdd.stream())
|
||||
.add1Map(mapToAdd.entrySet().stream())
|
||||
.build();
|
||||
Assertions.assertEquals(streamed.map(), Map.of(now, "now", Instant.MIN, "before", now.plusMillis(1), "now+1", now.plusMillis(2), "now+2"));
|
||||
Assertions.assertEquals(streamed.sets(), Set.of(List.of("1", "2"), List.of("3"), List.of("aa", "bb"), List.of("cc")));
|
||||
Assertions.assertEquals(streamed.strings(), Arrays.asList("a", "b", "c", "x", "y", "z"));
|
||||
|
||||
var nulls = SingleItemsBuilder.builder(item)
|
||||
.strings(null)
|
||||
.sets(null)
|
||||
.map(null)
|
||||
.build();
|
||||
Assertions.assertEquals(nulls.map(), Map.of());
|
||||
Assertions.assertEquals(nulls.sets(), Set.of());
|
||||
Assertions.assertEquals(nulls.strings(), List.of());
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
<parent>
|
||||
<groupId>io.soabase.record-builder</groupId>
|
||||
<artifactId>record-builder</artifactId>
|
||||
<version>26</version>
|
||||
<version>28</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user