Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix addSingleItemCollectionBuilders when useImmutableCollections is false #130

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ class CollectionBuilderUtils {
private boolean needsSetShim;
private boolean needsCollectionShim;

private boolean needsListMutableMaker;
private boolean needsMapMutableMaker;
private boolean needsSetMutableMaker;

private static final Class<?> listType = List.class;
private static final Class<?> mapType = Map.class;
private static final Class<?> setType = Set.class;
Expand Down Expand Up @@ -93,7 +89,6 @@ class CollectionBuilderUtils {
}

enum SingleItemsMetaDataMode {
STANDARD,
STANDARD_FOR_SETTER,
EXCLUDE_WILDCARD_TYPES
}
Expand Down Expand Up @@ -122,8 +117,6 @@ Optional<SingleItemsMetaData> singleItemsMetaData(RecordClassType component, Sin
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()));
Expand Down Expand Up @@ -160,18 +153,15 @@ boolean isSet(RecordClassType component) {
}

void addShimCall(CodeBlock.Builder builder, RecordClassType component) {
if (useImmutableCollections) {
if (useImmutableCollections || addSingleItemCollectionBuilders) {
if (isList(component)) {
needsListShim = true;
needsListMutableMaker = true;
builder.add("$L($L)", listShimName, component.name());
} else if (isMap(component)) {
needsMapShim = true;
needsMapMutableMaker = true;
builder.add("$L($L)", mapShimName, component.name());
} else if (isSet(component)) {
needsSetShim = true;
needsSetMutableMaker = true;
builder.add("$L($L)", setShimName, component.name());
} else if (component.rawTypeName().equals(collectionTypeName)) {
needsCollectionShim = true;
Expand Down Expand Up @@ -211,10 +201,6 @@ String mutableMakerName(RecordClassType component) {
}

void addShims(TypeSpec.Builder builder) {
if (!useImmutableCollections) {
return;
}

if (needsListShim) {
builder.addMethod(buildShimMethod(listShimName, listTypeName, collectionType, parameterizedListType, tType));
}
Expand All @@ -227,24 +213,19 @@ void addShims(TypeSpec.Builder builder) {
if (needsCollectionShim) {
builder.addMethod(buildCollectionsShimMethod());
}
}

void addMutableMakers(TypeSpec.Builder builder) {
if (!useImmutableCollections) {
return;
}

if (needsListMutableMaker) {
builder.addMethod(buildMutableMakerMethod(listMakerMethodName, mutableListSpec.name, parameterizedListType, tType));
builder.addType(mutableListSpec);
}
if (needsSetMutableMaker) {
builder.addMethod(buildMutableMakerMethod(setMakerMethodName, mutableSetSpec.name, parameterizedSetType, tType));
builder.addType(mutableSetSpec);
}
if (needsMapMutableMaker) {
builder.addMethod(buildMutableMakerMethod(mapMakerMethodName, mutableMapSpec.name, parameterizedMapType, kType, vType));
builder.addType(mutableMapSpec);
if (addSingleItemCollectionBuilders) {
if (needsListShim) {
builder.addType(mutableListSpec);
builder.addMethod(buildMutableMakerMethod(listMakerMethodName, mutableListSpec.name, parameterizedListType, tType));
}
if (needsSetShim) {
builder.addType(mutableSetSpec);
builder.addMethod(buildMutableMakerMethod(setMakerMethodName, mutableSetSpec.name, parameterizedSetType, tType));
}
if (needsMapShim) {
builder.addType(mutableMapSpec);
builder.addMethod(buildMutableMakerMethod(mapMakerMethodName, mutableMapSpec.name, parameterizedMapType, kType, vType));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
*/
package io.soabase.recordbuilder.processor;

import static io.soabase.recordbuilder.processor.CollectionBuilderUtils.SingleItemsMetaDataMode.EXCLUDE_WILDCARD_TYPES;
import static io.soabase.recordbuilder.processor.CollectionBuilderUtils.SingleItemsMetaDataMode.STANDARD_FOR_SETTER;
import static io.soabase.recordbuilder.processor.ElementUtils.getBuilderName;
import static io.soabase.recordbuilder.processor.ElementUtils.getWithMethodName;
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.generatedRecordBuilderAnnotation;
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.recordBuilderGeneratedAnnotation;
import com.squareup.javapoet.*;
import io.soabase.recordbuilder.core.RecordBuilder;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.*;
Expand All @@ -33,8 +29,12 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;

import com.squareup.javapoet.*;
import io.soabase.recordbuilder.core.RecordBuilder;
import static io.soabase.recordbuilder.processor.CollectionBuilderUtils.SingleItemsMetaDataMode.EXCLUDE_WILDCARD_TYPES;
import static io.soabase.recordbuilder.processor.CollectionBuilderUtils.SingleItemsMetaDataMode.STANDARD_FOR_SETTER;
import static io.soabase.recordbuilder.processor.ElementUtils.getBuilderName;
import static io.soabase.recordbuilder.processor.ElementUtils.getWithMethodName;
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.generatedRecordBuilderAnnotation;
import static io.soabase.recordbuilder.processor.RecordBuilderProcessor.recordBuilderGeneratedAnnotation;

class InternalRecordBuilderProcessor {
private final RecordBuilder.Options metaData;
Expand Down Expand Up @@ -107,11 +107,9 @@ class InternalRecordBuilderProcessor {
if (metaData.addConcreteSettersForOptional()) {
add1ConcreteOptionalSetterMethod(component);
}
var collectionMetaData = collectionBuilderUtils.singleItemsMetaData(component, EXCLUDE_WILDCARD_TYPES);
collectionMetaData.ifPresent(meta -> add1CollectionBuilders(meta, component));
collectionBuilderUtils.singleItemsMetaData(component, EXCLUDE_WILDCARD_TYPES).ifPresent(meta -> add1CollectionBuilders(meta, component));
});
collectionBuilderUtils.addShims(builder);
collectionBuilderUtils.addMutableMakers(builder);
builderType = builder.build();
}

Expand Down Expand Up @@ -806,13 +804,13 @@ private String capitalize(String s) {

private void add1CollectionBuilders(CollectionBuilderUtils.SingleItemsMetaData meta, RecordClassType component) {
if (collectionBuilderUtils.isList(component) || collectionBuilderUtils.isSet(component)) {
add1ListBuilder(meta, component);
add1SingleItemsListBuilder(meta, component);
} else if (collectionBuilderUtils.isMap(component)) {
add1MapBuilder(meta, component);
add1SingleItemsMapBuilder(meta, component);
}
}

private void add1MapBuilder(CollectionBuilderUtils.SingleItemsMetaData meta, RecordClassType component) {
private void add1SingleItemsMapBuilder(CollectionBuilderUtils.SingleItemsMetaData meta, RecordClassType component) {
/*
For a single map record component, add a methods similar to:

Expand All @@ -836,15 +834,7 @@ public T addP(Iterable<? extends Map.Entry<K, V> i) {
*/
for (var i = 0; i < 3; ++i) {
var codeBlockBuilder = CodeBlock.builder();
if (collectionBuilderUtils.isImmutableCollection(component)) {
codeBlockBuilder
.addStatement("this.$L = $L($L)", component.name(), collectionBuilderUtils.mutableMakerName(component), component.name());
} else {
codeBlockBuilder
.beginControlFlow("if (this.$L == null)", component.name())
.addStatement("this.$L = new $T<>()", component.name(), meta.singleItemCollectionClass())
.endControlFlow();
}
codeBlockBuilder.addStatement("this.$L = $L($L)", component.name(), collectionBuilderUtils.mutableMakerName(component), component.name());
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)
Expand All @@ -867,7 +857,7 @@ public T addP(Iterable<? extends Map.Entry<K, V> i) {
}
}

private void add1ListBuilder(CollectionBuilderUtils.SingleItemsMetaData meta, RecordClassType component) {
private void add1SingleItemsListBuilder(CollectionBuilderUtils.SingleItemsMetaData meta, RecordClassType component) {
/*
For a single list or set record component, add methods similar to:

Expand Down Expand Up @@ -901,15 +891,7 @@ public T addP(Iterable<? extends I> i) {
parameter = ParameterizedTypeName.get(parameterClass, WildcardTypeName.subtypeOf(meta.typeArguments().get(0)));
}
var codeBlockBuilder = CodeBlock.builder();
if (collectionBuilderUtils.isImmutableCollection(component)) {
codeBlockBuilder
.addStatement("this.$L = $L($L)", component.name(), collectionBuilderUtils.mutableMakerName(component), component.name());
} else {
codeBlockBuilder
.beginControlFlow("if (this.$L == null)", component.name())
.addStatement("this.$L = new $T<>()", component.name(), meta.singleItemCollectionClass())
.endControlFlow();
}
codeBlockBuilder.addStatement("this.$L = $L($L)", component.name(), collectionBuilderUtils.mutableMakerName(component), component.name());
codeBlockBuilder
.add(addClockBlock.build())
.addStatement("return this");
Expand Down
Original file line number Diff line number Diff line change
@@ -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.issue129;

import java.util.List;
import java.util.Map;

@RecordStyle
public record CombinedFields<K, V>(
Map<K, V> kvMap,
String combinedField,
List<String> lines) implements CombinedFieldsBuilder.Bean<K, V>, CombinedFieldsBuilder.With<K, V> {
}
Original file line number Diff line number Diff line change
@@ -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.issue129;

import io.soabase.recordbuilder.core.RecordBuilder;

import java.lang.annotation.*;

@RecordBuilder.Template(options = @RecordBuilder.Options(
addSingleItemCollectionBuilders = true,
addFunctionalMethodsToWith = true,
booleanPrefix = "is",
setterPrefix = "set",
beanClassName = "Bean"))
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
@Inherited
public @interface RecordStyle {

}