Skip to content

Commit

Permalink
Merge pull request #72 from helospark/gh-71-avoid-extra-constructor
Browse files Browse the repository at this point in the history
Avoid extra constructor for records
  • Loading branch information
helospark authored Aug 20, 2024
2 parents 86fe53a + 06d03f5 commit 2052f7f
Show file tree
Hide file tree
Showing 43 changed files with 98 additions and 68 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ Most configuration is on the preferences page: Preferences -> Java -> Spark buil
Add option to use jakarta.annotation package instead of the old javax.annotation
- 0.0.28
Add support for record types
- 0.0.29
Don't generate constructor for record class

## The generated code looks like the following:

Expand Down
4 changes: 3 additions & 1 deletion SparkBuilderGeneratorPlugin/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="dependency/jcommander-1.60.jar"/>
<classpathentry kind="lib" path="dependency/mockito-all-1.10.19.jar"/>
<classpathentry kind="lib" path="dependency/testng-6.11.jar"/>
<classpathentry kind="lib" path="dependency/byte-buddy-1.14.19.jar"/>
<classpathentry kind="lib" path="dependency/mockito-core-5.12.0.jar"/>
<classpathentry kind="lib" path="dependency/byte-buddy-agent-1.14.19.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 1 addition & 1 deletion SparkBuilderGeneratorPlugin/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: SparkBuilderGenerator
Bundle-SymbolicName: com.helospark.SparkBuilderGenerator;singleton:=true
Bundle-Version: 0.0.28.qualifier
Bundle-Version: 0.0.29.qualifier
Bundle-ClassPath: .
Bundle-Activator: com.helospark.spark.builder.Activator
Require-Bundle: org.eclipse.ui,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<feature
id="com.helospark.SparkBuilderGeneratorFeature"
label="SparkBuilderGeneratorFeature"
version="0.0.28.qualifier">
version="0.0.29.qualifier">

<description url="ftp://helospark.com/SparkBuilderGeneratorPlugin/">
Spark builder generator plugin
Expand Down Expand Up @@ -72,17 +72,23 @@ includes mandatory fields
- 0.0.22
Fixing incorrect field naming with underscore prefix
- 0.0.23
Add option to keep custom methods in the builderclass, when the builder is regenerated
Add option to keep custom methods in the builderclass, when the
builder is regenerated
- 0.0.24
Add option to initialize builder fields with default value from the class&apos; field assignment
Add option to initialize builder fields with default value from
the class&apos; field assignment
- 0.0.25
Fix default value initialization based on superclass&apos; field assignment
- 0.0.26
Fix copy constructor when superclass field is not accessible from builder
Fix copy constructor when superclass field is not accessible
from builder
- 0.0.27
Add option to use jakarta.annotation package instead of the old javax.annotation
Add option to use jakarta.annotation package instead of the old
javax.annotation
- 0.0.28
Add support for Java records
- 0.0.29
Don&apos;t generate constructor for record class
</description>

<license url="https://opensource.org/licenses/MIT">
Expand Down Expand Up @@ -122,9 +128,6 @@ SOFTWARE.

<plugin
id="com.helospark.SparkBuilderGenerator"
download-size="0"
install-size="0"
version="0.0.0"
unpack="false"/>
version="0.0.0"/>

</feature>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<description name="Spark builder generator plugin" url="https://helospark.com/eclipse_plugin/SparkBuilderGeneratorPlugin">
Plugin to generate builder
</description>
<feature url="features/com.helospark.SparkBuilderGeneratorFeature_0.0.28.202308062115.jar" id="com.helospark.SparkBuilderGeneratorFeature" version="0.0.28.202308062115">
<feature url="features/com.helospark.SparkBuilderGeneratorFeature_0.0.29.202408201349.jar" id="com.helospark.SparkBuilderGeneratorFeature" version="0.0.29.202408201349">
<category name="SparkTools"/>
</feature>
<category-def name="SparkTools" label="SparkTools"/>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.helospark.spark.builder.handlers.codegenerator;

import static com.helospark.spark.builder.handlers.codegenerator.component.helper.IsRecordTypePredicate.isRecordDeclaration;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
Expand Down Expand Up @@ -58,8 +60,10 @@ public void generateBuilder(CompilationUnitModificationDomain compilationUnitMod

TypeDeclaration builderType = regularBuilderClassCreator.createBuilderClass(ast, originalType, preference,
compilationUnitModificationDomain.getSavedCustomMethodDeclarations());
defaultConstructorAppender.addDefaultConstructorIfNeeded(compilationUnitModificationDomain, preference.getBuilderFields());
privateConstructorPopulator.addPrivateConstructorToCompilationUnit(ast, originalType, builderType, listRewrite, preference.getBuilderFields());
if (!isRecordDeclaration(originalType)) {
defaultConstructorAppender.addDefaultConstructorIfNeeded(compilationUnitModificationDomain, preference.getBuilderFields());
privateConstructorPopulator.addPrivateConstructorToCompilationUnit(ast, originalType, builderType, listRewrite, preference.getBuilderFields());
}
builderMethodPopulator.addBuilderMethodToCompilationUnit(ast, listRewrite, originalType, builderType, preference);
instanceCopyBuilderMethodPopulator.addInstanceCopyBuilderMethodToCompilationUnitIfNeeded(compilationUnitModificationDomain, builderType, preference);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.helospark.spark.builder.handlers.codegenerator;

import static com.helospark.spark.builder.handlers.codegenerator.component.helper.IsRecordTypePredicate.isRecordDeclaration;

import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -61,10 +63,13 @@ public void generateBuilder(CompilationUnitModificationDomain modificationDomain

// TODO: eventually have a better design to avoid nulls here
List<TypeDeclaration> stageInterfaces = createStageInterfaces(modificationDomain, stagedBuilderStages);
TypeDeclaration builderType = stagedBuilderClassCreator.createBuilderClass(modificationDomain, stagedBuilderStages, stageInterfaces);
List<BuilderField> fields = collectAllFieldsFromAllStages(stagedBuilderStages);
TypeDeclaration builderType = stagedBuilderClassCreator.createBuilderClass(modificationDomain, stagedBuilderStages, stageInterfaces, fields);

defaultConstructorAppender.addDefaultConstructorIfNeeded(modificationDomain, collectAllFieldsFromAllStages(stagedBuilderStages));
privateConstructorPopulator.addPrivateConstructorToCompilationUnit(ast, originalType, builderType, listRewrite, collectAllFieldsFromAllStages(stagedBuilderStages));
if (!isRecordDeclaration(originalType)) {
defaultConstructorAppender.addDefaultConstructorIfNeeded(modificationDomain, fields);
privateConstructorPopulator.addPrivateConstructorToCompilationUnit(ast, originalType, builderType, listRewrite, fields);
}
stagedBuilderStaticBuilderCreatorMethodCreator.addBuilderMethodToCompilationUnit(modificationDomain, builderType, stagedBuilderStages);

stageInterfaces.stream().forEach(stageInterface -> listRewrite.insertLast(stageInterface, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public TypeDeclaration createBuilderClass(AST ast, AbstractTypeDeclaration origi
builderType.bodyDeclarations().add(customMethod);
}

MethodDeclaration method = buildMethodCreatorFragment.addBuildMethodToBuilder(ast, originalType);
MethodDeclaration method = buildMethodCreatorFragment.addBuildMethodToBuilder(ast, originalType, builderFields);
javadocAdder.addJavadocForBuildMethod(ast, method);
builderType.bodyDeclarations().add(method);
return builderType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public StagedBuilderClassCreator(PrivateConstructorAdderFragment privateConstruc
}

public TypeDeclaration createBuilderClass(CompilationUnitModificationDomain modificationDomain,
List<StagedBuilderProperties> stagedBuilderProperties, List<TypeDeclaration> stageInterfaces) {
List<StagedBuilderProperties> stagedBuilderProperties, List<TypeDeclaration> stageInterfaces, List<BuilderField> fields) {
AST ast = modificationDomain.getAst();
AbstractTypeDeclaration originalType = modificationDomain.getOriginalType();
TypeDeclaration builderType = emptyBuilderClassGeneratorFragment.createBuilderClass(ast, originalType);
Expand All @@ -67,7 +67,7 @@ public TypeDeclaration createBuilderClass(CompilationUnitModificationDomain modi
for (MethodDeclaration customMethod : modificationDomain.getSavedCustomMethodDeclarations()) {
builderType.bodyDeclarations().add(customMethod);
}
MethodDeclaration method = buildMethodCreatorFragment.addBuildMethodToBuilder(ast, originalType);
MethodDeclaration method = buildMethodCreatorFragment.addBuildMethodToBuilder(ast, originalType, fields);
builderType.bodyDeclarations().add(method);
markerAnnotationAttacher.attachAnnotation(ast, method, OVERRIDE_ANNOTATION);
setSuperInterfaces(ast, builderType, stageInterfaces);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
package com.helospark.spark.builder.handlers.codegenerator.component.fragment.builderclass.buildmethod;

import java.util.List;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;

import com.helospark.spark.builder.handlers.codegenerator.component.helper.IsRecordTypePredicate;
import com.helospark.spark.builder.handlers.codegenerator.domain.BuilderField;

/**
* Fragment to add create the build() method' body.
* Generated code is something like:
* Generated code for classes something like:
* <pre>
* {
* return new Clazz(this);
* }
* </pre>
*
* and for record:
* <pre>
* {
* return new Clazz(param1, param2);
* }
* </pre>
* @author helospark
*/
public class BuildMethodBodyCreatorFragment {

public Block createBody(AST ast, AbstractTypeDeclaration originalType) {
public Block createBody(AST ast, AbstractTypeDeclaration originalType, List<BuilderField> builderFields) {
ClassInstanceCreation newClassInstanceCreation = ast.newClassInstanceCreation();
newClassInstanceCreation.setType(ast.newSimpleType(ast.newName(originalType.getName().toString())));
newClassInstanceCreation.arguments().add(ast.newThisExpression());

if (IsRecordTypePredicate.isRecordDeclaration(originalType)) {
for (BuilderField field : builderFields) {
newClassInstanceCreation.arguments().add(ast.newSimpleName(field.getBuilderFieldName()));
}
} else {
newClassInstanceCreation.arguments().add(ast.newThisExpression());
}
ReturnStatement statement = ast.newReturnStatement();
statement.setExpression(newClassInstanceCreation);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.helospark.spark.builder.handlers.codegenerator.component.fragment.builderclass.buildmethod;

import java.util.List;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.MethodDeclaration;

import com.helospark.spark.builder.handlers.codegenerator.domain.BuilderField;

/**
* Fragment to create the build() method.
* Generated code is something like:
Expand All @@ -25,8 +29,8 @@ public BuildMethodCreatorFragment(BuildMethodDeclarationCreatorFragment buildMet
this.buildMethodBodyCreatorFragment = buildMethodBodyCreatorFragment;
}

public MethodDeclaration addBuildMethodToBuilder(AST ast, AbstractTypeDeclaration originalType) {
Block block = buildMethodBodyCreatorFragment.createBody(ast, originalType);
public MethodDeclaration addBuildMethodToBuilder(AST ast, AbstractTypeDeclaration originalType, List<BuilderField> builderFields) {
Block block = buildMethodBodyCreatorFragment.createBody(ast, originalType, builderFields);
MethodDeclaration method = buildMethodDeclarationCreatorFragment.createMethod(ast, originalType);
method.setBody(block);
return method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;
import static org.testng.Assert.assertEquals;

import java.io.IOException;
Expand Down Expand Up @@ -73,7 +73,7 @@ public class BaseBuilderGeneratorIT {
protected StagedBuilderStagePropertyInputDialogOpener stagedBuilderStagePropertyInputDialogOpener;

protected void init() throws JavaModelException {
initMocks(this);
openMocks(this);
DiContainer.clearDiContainer();

// Override mock dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static java.util.Optional.of;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static java.util.Arrays.asList;
import static java.util.Optional.of;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.Collections;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static java.util.Optional.of;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.helospark.spark.builder.handlers.it;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.Collections;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.helospark.spark.builder.handlers.it;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.Collections;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.helospark.spark.builder.handlers.it;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.HashMap;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.List;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.helospark.spark.builder.handlers.it;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.helospark.spark.builder.handlers.it;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.Collections;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.helospark.spark.builder.handlers.it;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.helospark.spark.builder.handlers.it;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.Arrays;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static java.util.Optional.of;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;

import java.util.List;

Expand Down
Loading

0 comments on commit 2052f7f

Please sign in to comment.