From e31f0660f818d9037120d1c0b614d5a7fd15baf6 Mon Sep 17 00:00:00 2001 From: Chris Purcell Date: Sun, 14 Aug 2016 16:23:17 +0100 Subject: [PATCH] Fix bug with generic buildable properties Uncompilable code was being generated for generic buildable property types, due to mistakenly appending '.Builder' to the type rather than using the ParameterizedType `builderType`. --- .../processor/BuildablePropertyFactory.java | 8 +++--- .../BuildablePropertyFactoryTest.java | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/inferred/freebuilder/processor/BuildablePropertyFactory.java b/src/main/java/org/inferred/freebuilder/processor/BuildablePropertyFactory.java index 1ef49f013..0f01f3e44 100644 --- a/src/main/java/org/inferred/freebuilder/processor/BuildablePropertyFactory.java +++ b/src/main/java/org/inferred/freebuilder/processor/BuildablePropertyFactory.java @@ -207,10 +207,10 @@ private void addSetterTakingBuilder(SourceBuilder code, Metadata metadata) { .addLine(" * @return this {@code %s} object", metadata.getBuilder().getSimpleName()) .addLine(" * @throws NullPointerException if {@code builder} is null") .addLine(" */") - .addLine("public %s %s(%s.Builder builder) {", + .addLine("public %s %s(%s builder) {", metadata.getBuilder(), setter(property), - property.getType()) + builderType) .addLine(" return %s(builder.build());", setter(property)) .addLine("}"); } @@ -232,11 +232,11 @@ private void addMutate(SourceBuilder code, Metadata metadata) { .addLine(" * @return this {@code %s} object", metadata.getBuilder().getSimpleName()) .addLine(" * @throws NullPointerException if {@code mutator} is null") .addLine(" */") - .addLine("public %s %s(%s<%s.Builder> mutator) {", + .addLine("public %s %s(%s<%s> mutator) {", metadata.getBuilder(), mutator(property), consumer.getQualifiedName(), - property.getType()) + builderType) .addLine(" mutator.accept(%s);", property.getName()) .addLine(" return (%s) this;", metadata.getBuilder()) .addLine("}"); diff --git a/src/test/java/org/inferred/freebuilder/processor/BuildablePropertyFactoryTest.java b/src/test/java/org/inferred/freebuilder/processor/BuildablePropertyFactoryTest.java index ed88756ab..855da464f 100644 --- a/src/test/java/org/inferred/freebuilder/processor/BuildablePropertyFactoryTest.java +++ b/src/test/java/org/inferred/freebuilder/processor/BuildablePropertyFactoryTest.java @@ -796,6 +796,31 @@ public void testBuilderClear_freebuilderlike() { .runTest(); } + @Test + public void testGenericChildProperty() { + // Raised in issue #183 + behaviorTester + .with(new Processor()) + .with(new SourceBuilder() + .addLine("package com.example;") + .addLine("@%s", FreeBuilder.class) + .addLine("public interface PIdentityDefinition {") + .addLine(" class Builder extends PIdentityDefinition_Builder {}") + .addLine("}") + .build()) + .with(new SourceBuilder() + .addLine("package com.example;") + .addLine("@%s", FreeBuilder.class) + .addLine("public interface PAccess {") + .addLine(" class Builder extends PAccess_Builder {}") + .addLine("") + .addLine(" PIdentityDefinition getIdentity();") + .addLine("}") + .build()) + .compiles() + .withNoWarnings(); + } + @Test public void testIssue68_nameCollisionForValue() { // mergeFrom(DataType value) must resolve the name collision on "value"