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

Allow builders to return default instance in Java #16655

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -476,6 +476,9 @@ public abstract static class Builder<BuilderT extends Builder<BuilderT>>
// to dispatch dirty invalidations. See GeneratedMessage.BuilderListener.
private boolean isClean;

// Indicates that the default instance can be used
private boolean isDefault = true;

/**
* This field holds either an {@link UnknownFieldSet} or {@link UnknownFieldSet.Builder}.
*
Expand Down Expand Up @@ -527,6 +530,15 @@ protected boolean isClean() {
return isClean;
}

/**
* Gets whether the default instance can be used
*
* @return whether the default instance can be used
*/
protected boolean isDefault() {
return isDefault;
}

@Override
public BuilderT clone() {
BuilderT builder = (BuilderT) getDefaultInstanceForType().newBuilderForType();
Expand Down Expand Up @@ -834,6 +846,7 @@ protected final void onChanged() {
// Don't keep dispatching invalidations until build is called again.
isClean = false;
}
isDefault = false;
}

/**
Expand Down
Expand Up @@ -10,6 +10,7 @@
final class NewInstanceSchemaFull implements NewInstanceSchema {
@Override
public Object newInstance(Object defaultInstance) {
return ((Message) defaultInstance).toBuilder().buildPartial();
return ((GeneratedMessage) defaultInstance)
.newInstance(GeneratedMessage.UnusedPrivateParameter.INSTANCE);
}
}
Expand Up @@ -156,6 +156,20 @@ public void testMerge() {
assertThat(vehicle1.getWheelList()).isSameInstanceAs(vehicle3.getWheelList());
}

@Test
public void testUsesDefaultInstance() {
Vehicle vehicle1 = Vehicle.newBuilder().build();
// Should use the default instance -- no allocation
assertThat(vehicle1).isSameInstanceAs(Vehicle.getDefaultInstance());

Vehicle vehicle2 =
Vehicle.newBuilder()
.addWheel(Wheel.newBuilder().build())
.build();
// Should use the default instance -- no allocation
assertThat(vehicle2.getWheel(0)).isSameInstanceAs(Wheel.getDefaultInstance());
}

@Test
public void testGettingBuilderMarksFieldAsHaving() {
Vehicle.Builder vehicleBuilder = Vehicle.newBuilder();
Expand Down
9 changes: 9 additions & 0 deletions src/google/protobuf/compiler/java/immutable/message.cc
Expand Up @@ -363,6 +363,15 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
"}\n"
"\n");

printer->Print(variables,
"@java.lang.Override\n"
"@SuppressWarnings({\"unused\"})\n"
"protected java.lang.Object newInstance(\n"
" UnusedPrivateParameter unused) {\n"
" return new $classname$();\n"
"}\n"
"\n");

GenerateDescriptorMethods(printer);

// Nested types
Expand Down
Expand Up @@ -475,6 +475,7 @@ void MessageBuilderGenerator::GenerateBuildPartial(io::Printer* printer) {
printer->Print(
"@java.lang.Override\n"
"public $classname$ buildPartial() {\n"
" if (isDefault()) { onBuilt(); return $classname$.getDefaultInstance(); }\n"
" $classname$ result = new $classname$(this);\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));

Expand Down