Compare commits

...

15 Commits

Author SHA1 Message Date
randgalt
af69217eb5 [maven-release-plugin] prepare release record-builder-1.6.ea 2020-02-06 08:14:59 -05:00
randgalt
9133d5f66d Updated the README 2020-02-06 08:12:39 -05:00
sipkab
456b6f0f62 Fix indentation in RecordBuilderProcessor 2020-02-05 08:50:31 +01:00
sipkab
0fc82139de Fix indentation in OptionBasedRecordBuilderMetaData as well 2020-02-05 08:49:02 +01:00
sipkab
0774278032 Fix indentation 2020-02-05 08:47:38 +01:00
sipkab
578c7ad532 Allow -A option based processor configuration 2020-02-05 08:46:21 +01:00
sipkab
6e10d3a3c0 Mitigate possible runtime Errors when running on older JVMs 2020-02-05 08:30:43 +01:00
randgalt
a9fa609911 updated copyright date in license 2019-12-27 18:50:08 -05:00
randgalt
43db1586ac [maven-release-plugin] prepare for next development iteration 2019-12-24 18:43:17 -05:00
randgalt
d21994143f [maven-release-plugin] prepare release record-builder-1.5.ea 2019-12-24 18:43:10 -05:00
randgalt
f49089d26e Merge branch 'master' of github.com:Randgalt/record-builder 2019-12-24 12:34:08 -05:00
randgalt
e9a6e79e79 Added a component stream method 2019-12-24 12:34:02 -05:00
Jordan Zimmerman
f091e094e8 Update README.md 2019-12-23 09:38:36 -05:00
Jordan Zimmerman
72c9332b89 Update README.md 2019-12-23 09:37:47 -05:00
randgalt
41f1e5fa7e [maven-release-plugin] prepare for next development iteration 2019-12-22 13:21:26 -05:00
17 changed files with 260 additions and 42 deletions

View File

@@ -67,23 +67,6 @@ public class NameAndAgeBuilder {
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 b)
&& Objects.equals(name, b.name)
&& (age == b.age));
}
/**
* Set a new value for the {@code name} record component in the builder
*/
@@ -113,6 +96,31 @@ public class NameAndAgeBuilder {
public int age() {
return age;
}
/**
* Return a stream of the record components as map entries keyed with the component name and the value as the component value
*/
public static Stream<Map.Entry<String, Object>> stream(NameAndAge record) {
return Stream.of(new AbstractMap.SimpleEntry<>("name", record.name()),
new AbstractMap.SimpleEntry<>("age", record.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 b)
&& Objects.equals(name, b.name)
&& (age == b.age));
}
}
```
@@ -176,6 +184,24 @@ Note: records are a preview feature only. You'll need take a number of steps in
Note: I've seen some very odd compilation bugs with the current Java 14 and Maven. If you get internal Javac errors I suggest rebuilding with `mvn clean package` and/or `mvn clean install`.
## Customizing
The names of the generated methods, etc. are determined by [RecordBuilderMetaData](https://github.com/Randgalt/record-builder/blob/master/record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilderMetaData.java). If you want to use your own meta data instance:
- Create a class that implements RecordBuilderMetaData
- When compiling, make sure that the compiled class is in the processor path
- Add a "metaDataClass" compiler option with the class name. E.g. `javac ... -AmetaDataClass=foo.bar.MyMetaData`
Alternatively, you can provide values for each individual meta data (or combinations):
- `javac ... -AcopyMethodName=foo`
- `javac ... -AbuilderMethodName=foo`
- `javac ... -AbuildMethodName=foo`
- `javac ... -AcomponentsMethodName=foo`
- `javac ... -AfileComment=foo`
- `javac ... -AfileIndent=foo`
- `javac ... -AprefixEnclosingClassNames=foo`
## TODOs
- Document how to integrate with Gradle

View File

@@ -5,7 +5,7 @@
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder</artifactId>
<packaging>pom</packaging>
<version>1.4.ea</version>
<version>1.6.ea</version>
<modules>
<module>record-builder-core</module>
@@ -70,7 +70,7 @@
<url>https://github.com/randgalt/record-builder</url>
<connection>scm:git:https://github.com/randgalt/record-builder.git</connection>
<developerConnection>scm:git:git@github.com:randgalt/record-builder.git</developerConnection>
<tag>record-builder-1.4.ea</tag>
<tag>record-builder-1.6.ea</tag>
</scm>
<issueManagement>

View File

@@ -3,7 +3,7 @@
<parent>
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder</artifactId>
<version>1.4.ea</version>
<version>1.6.ea</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2016 Jordan Zimmerman
* Copyright 2019 Jordan Zimmerman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2016 Jordan Zimmerman
* Copyright 2019 Jordan Zimmerman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -68,6 +68,15 @@ public interface RecordBuilderMetaData {
return "build";
}
/**
* The name to use for the method that returns the record components as a stream
*
* @return build method
*/
default String componentsMethodName() {
return "stream";
}
/**
* Return the comment to place at the top of generated files. Return null or an empty string for no comment.
*

View File

@@ -3,7 +3,7 @@
<parent>
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder</artifactId>
<version>1.4.ea</version>
<version>1.6.ea</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2016 Jordan Zimmerman
* Copyright 2019 Jordan Zimmerman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2016 Jordan Zimmerman
* Copyright 2019 Jordan Zimmerman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2016 Jordan Zimmerman
* Copyright 2019 Jordan Zimmerman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,10 +22,13 @@ import javax.annotation.processing.Generated;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
class InternalProcessor {
private static final AnnotationSpec generatedAnnotation = AnnotationSpec.builder(Generated.class).addMember("value", "$S", RecordBuilderProcessor.NAME).build();
@@ -55,6 +58,7 @@ class InternalProcessor {
addAllArgsConstructor();
addStaticDefaultBuilderMethod();
addStaticCopyBuilderMethod();
addStaticComponentsMethod();
addBuildMethod();
addToStringMethod();
addHashCodeMethod();
@@ -296,6 +300,40 @@ class InternalProcessor {
builder.addMethod(methodSpec);
}
private void addStaticComponentsMethod() {
/*
Adds a static method that converts a record instance into a stream of its component parts
public static Stream<Map.Entry<String, Object>> stream(MyRecord record) {
return Stream.of(
new AbstractMap.SimpleEntry<>("p1", record.p1()),
new AbstractMap.SimpleEntry<>("p2", record.p2())
);
}
*/
var codeBuilder = CodeBlock.builder().add("return $T.of(", Stream.class);
IntStream.range(0, recordComponents.size()).forEach(index -> {
if (index > 0) {
codeBuilder.add(",\n ");
}
var name = recordComponents.get(index).name();
codeBuilder.add("new $T<>($S, record.$L())", AbstractMap.SimpleEntry.class, name, name);
});
codeBuilder.add(")");
var mapEntryTypeVariables = ParameterizedTypeName.get(Map.Entry.class, String.class, Object.class);
var mapEntryType = ParameterizedTypeName.get(ClassName.get(Stream.class), mapEntryTypeVariables);
var methodSpec = MethodSpec.methodBuilder(metaData.componentsMethodName())
.addJavadoc("Return a stream of the record components as map entries keyed with the component name and the value as the component value\n")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameter(recordClassType.typeName(), "record")
.addAnnotation(generatedAnnotation)
.addTypeVariables(typeVariables)
.returns(mapEntryType)
.addStatement(codeBuilder.build())
.build();
builder.addMethod(methodSpec);
}
private void add1Field(ClassType component) {
/*
For a single record component, add a field similar to:

View File

@@ -0,0 +1,122 @@
/**
* 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 java.util.Map;
import io.soabase.recordbuilder.core.RecordBuilderMetaData;
public class OptionBasedRecordBuilderMetaData implements RecordBuilderMetaData {
/**
* @see #suffix()
*/
public static final String OPTION_SUFFIX = "suffix";
/**
* @see #copyMethodName()
*/
public static final String OPTION_COPY_METHOD_NAME = "copyMethodName";
/**
* @see #builderMethodName()
*/
public static final String OPTION_BUILDER_METHOD_NAME = "builderMethodName";
/**
* @see #buildMethodName()
*/
public static final String OPTION_BUILD_METHOD_NAME = "buildMethodName";
/**
* @see #componentsMethodName()
*/
public static final String OPTION_COMPONENTS_METHOD_NAME = "componentsMethodName";
/**
* @see #fileComment()
*/
public static final String OPTION_FILE_COMMENT = "fileComment";
/**
* @see #fileIndent()
*/
public static final String OPTION_FILE_INDENT = "fileIndent";
/**
* @see #prefixEnclosingClassNames()
*/
public static final String OPTION_PREFIX_ENCLOSING_CLASS_NAMES = "prefixEnclosingClassNames";
private final String suffix;
private final String copyMethodName;
private final String builderMethodName;
private final String buildMethodName;
private final String componentsMethodName;
private final String fileComment;
private final String fileIndent;
private final boolean prefixEnclosingClassNames;
public OptionBasedRecordBuilderMetaData(Map<String, String> options) {
suffix = options.getOrDefault(OPTION_SUFFIX, "Builder");
builderMethodName = options.getOrDefault(OPTION_BUILDER_METHOD_NAME, "builder");
copyMethodName = options.getOrDefault(OPTION_COPY_METHOD_NAME, builderMethodName);
buildMethodName = options.getOrDefault(OPTION_BUILD_METHOD_NAME, "build");
componentsMethodName = options.getOrDefault(OPTION_COMPONENTS_METHOD_NAME, "stream");
fileComment = options.getOrDefault(OPTION_FILE_COMMENT,
"Auto generated by io.soabase.recordbuilder.core.RecordBuilder: https://github.com/Randgalt/record-builder");
fileIndent = options.getOrDefault(OPTION_FILE_INDENT, " ");
String prefixenclosingclassnamesopt = options.get(OPTION_PREFIX_ENCLOSING_CLASS_NAMES);
if (prefixenclosingclassnamesopt == null) {
prefixEnclosingClassNames = true;
} else {
prefixEnclosingClassNames = Boolean.parseBoolean(prefixenclosingclassnamesopt);
}
}
@Override
public String suffix() {
return suffix;
}
@Override
public String copyMethodName() {
return copyMethodName;
}
@Override
public String builderMethodName() {
return builderMethodName;
}
@Override
public String buildMethodName() {
return buildMethodName;
}
@Override
public String componentsMethodName() {
return componentsMethodName;
}
@Override
public String fileComment() {
return fileComment;
}
@Override
public String fileIndent() {
return fileIndent;
}
@Override
public boolean prefixEnclosingClassNames() {
return prefixEnclosingClassNames;
}
}

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2016 Jordan Zimmerman
* Copyright 2019 Jordan Zimmerman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,23 +17,34 @@ package io.soabase.recordbuilder.processor;
import io.soabase.recordbuilder.core.RecordBuilderMetaData;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.function.Consumer;
import javax.annotation.processing.ProcessingEnvironment;
class RecordBuilderMetaDataLoader {
private final RecordBuilderMetaData metaData;
RecordBuilderMetaDataLoader(String metaDataClassName, Consumer<String> logger) {
RecordBuilderMetaData localMetaData = null;
RecordBuilderMetaDataLoader(ProcessingEnvironment processingEnv, Consumer<String> logger) {
Map<String, String> options = processingEnv.getOptions();
String metaDataClassName = options.get(RecordBuilderMetaData.JAVAC_OPTION_NAME);
if ((metaDataClassName != null) && !metaDataClassName.isEmpty()) {
RecordBuilderMetaData loadedMetaData = null;
try {
Class<?> clazz = Class.forName(metaDataClassName);
localMetaData = (RecordBuilderMetaData) clazz.getDeclaredConstructor().newInstance();
logger.accept("Found meta data: " + localMetaData.getClass());
loadedMetaData = (RecordBuilderMetaData) clazz.getDeclaredConstructor().newInstance();
logger.accept("Found meta data: " + clazz);
} catch (InvocationTargetException e) {
// log the thrown exception instead of the invocation target exception
logger.accept("Could not load meta data: " + metaDataClassName + " - " + e.getCause());
} catch (Exception e) {
logger.accept("Could not load meta data: " + metaDataClassName + " - " + e.getMessage());
logger.accept("Could not load meta data: " + metaDataClassName + " - " + e);
}
metaData = (loadedMetaData != null) ? loadedMetaData : RecordBuilderMetaData.DEFAULT;
} else {
metaData = new OptionBasedRecordBuilderMetaData(options);
}
metaData = (localMetaData != null) ? localMetaData : RecordBuilderMetaData.DEFAULT;
}
RecordBuilderMetaData getMetaData() {

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2016 Jordan Zimmerman
* Copyright 2019 Jordan Zimmerman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,7 +34,6 @@ import java.util.Set;
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.NAME;
@SupportedAnnotationTypes(NAME)
@SupportedSourceVersion(SourceVersion.RELEASE_14)
public class RecordBuilderProcessor extends AbstractProcessor {
public static final String NAME = "io.soabase.recordbuilder.core.RecordBuilder";
@@ -45,14 +44,27 @@ public class RecordBuilderProcessor extends AbstractProcessor {
annotations.forEach(annotation -> roundEnv.getElementsAnnotatedWith(annotation).forEach(this::process));
return true;
}
@Override
public SourceVersion getSupportedSourceVersion() {
// we don't directly return RELEASE_14 as that may
// not exist in prior releases
// if we're running on an older release, returning latest()
// is fine as we won't encounter any records anyway
return SourceVersion.latest();
}
private void process(Element element) {
var messager = processingEnv.getMessager();
if (element.getKind() != ElementKind.RECORD) {
// we use string based name comparison for the element kind,
// as the ElementKind.RECORD enum doesn't exist on JRE releases
// older than Java 14, and we don't want to throw unexpected
// NoSuchFieldErrors
if (!"RECORD".equals(element.getKind().name())) {
messager.printMessage(Diagnostic.Kind.ERROR, "RecordBuilder only valid for records.", element);
return;
}
var metaData = new RecordBuilderMetaDataLoader(processingEnv.getOptions().get(RecordBuilderMetaData.JAVAC_OPTION_NAME), s -> messager.printMessage(Diagnostic.Kind.NOTE, s)).getMetaData();
var metaData = new RecordBuilderMetaDataLoader(processingEnv, s -> messager.printMessage(Diagnostic.Kind.NOTE, s)).getMetaData();
process((TypeElement) element, metaData);
}

View File

@@ -3,7 +3,7 @@
<parent>
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder</artifactId>
<version>1.4.ea</version>
<version>1.6.ea</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2016 Jordan Zimmerman
* Copyright 2019 Jordan Zimmerman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2016 Jordan Zimmerman
* Copyright 2019 Jordan Zimmerman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2016 Jordan Zimmerman
* Copyright 2019 Jordan Zimmerman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,4 +1,4 @@
Copyright 2016 Jordan Zimmerman
Copyright 2019 Jordan Zimmerman
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.