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

wip: Add flag to exclude static factory methods from generated objects #2087

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ default boolean jetbrainsContractAnnotations() {
return false;
}

/**
* If set to true, static factory methods ('of') will be excluded from generated objects with one or more fields.
* Note that for objects without any fields, this will still generate the static factory method.
*/
@Value.Default
default boolean excludeStaticFactoryMethods() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preferring this naming instead of omitGeneratedOfMethods because I think it's more descriptive.

return false;
}

Optional<String> packagePrefix();

Optional<String> apiVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ public static JavaFile generateBeanType(
.addAnnotations(safety)
.build());

if (poetFields.size() <= MAX_NUM_PARAMS_FOR_FACTORY) {
if (poetFields.isEmpty()
mpritham marked this conversation as resolved.
Show resolved Hide resolved
|| (!options.excludeStaticFactoryMethods() && poetFields.size() <= MAX_NUM_PARAMS_FOR_FACTORY)) {
typeBuilder.addMethod(createStaticFactoryMethod(
fields,
objectClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ public void testObjectGenerator_strictStagedBuilder() throws IOException {
assertThatFilesAreTheSame(files, REFERENCE_FILES_FOLDER);
}

@Test
public void testObjectGenerator_noStaticFactory() throws IOException {
ConjureDefinition def =
Conjure.parse(ImmutableList.of(new File("src/test/resources/example-types-no-static-factory.yml")));
List<Path> files = new GenerationCoordinator(
MoreExecutors.directExecutor(),
ImmutableSet.of(new ObjectGenerator(Options.builder()
.excludeStaticFactoryMethods(true)
.build())))
.emit(def, tempDir);

assertThatFilesAreTheSame(files, REFERENCE_FILES_FOLDER);
}

@Test
public void testObjectGenerator_stagedBuilderAndStrictStagedBuilder() throws IOException {
// Check that setting enabling staged and strict staged builders is equivalent to only enabling strict staged
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
types:
imports:
ExampleExternalReference:
base-type: string
external:
java: test.api.ExampleExternalReference
definitions:
default-package: com.palantir.product
objects:
StringExampleNoStaticFactory:
fields:
string: string
EmptyExampleNoStaticFactory:
fields: {}
CovariantListExampleNoStaticFactory:
fields:
items: list<any>
externalItems: list<ExampleExternalReference>
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ The recommended way to use conjure-java is via a build tool like [gradle-conjure
--jakartaPackages
Generates jax-rs annotated interfaces which use the newer 'jakarta` packages instead of the
legacy 'javax' packages.
--excludeStaticFactoryMethods
Exclude static factory methods from generated objects with one or more fields. Note that for
objects without any fields, this will still generate the static factory method.

### Known Tag Values

Expand Down