Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #174 from google/issues/98-merge.default.value
Browse files Browse the repository at this point in the history
Ignore default values when merging
  • Loading branch information
alicederyn authored Jul 1, 2016
2 parents 8ec314b + d1ffab4 commit 84b1484
Show file tree
Hide file tree
Showing 20 changed files with 4,264 additions and 3,699 deletions.
18 changes: 18 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,24 @@ eclipse.classpath {
plusConfigurations.clear()
plusConfigurations += [ configurations.compileClasspath ]
}
// Exclude everything in src except src/main
eclipse.project.file.withXml {
if (asNode().find { it.name() == 'filteredResources' } == null) {
asNode().append(new XmlParser().parseText("""
<filteredResources>
<filter>
<id>""" + System.currentTimeMillis() + """</id>
<name>src</name>
<type>9</type>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-name-matches-false-false-main</arguments>
</matcher>
</filter>
</filteredResources>
"""))
}
}

import org.gradle.plugins.ide.eclipse.model.EclipseModel
extensions.create("eclipseTest", EclipseModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public void addPartialFieldAssignment(SourceBuilder code, String finalField, Str
}

@Override
public void addMergeFromValue(SourceBuilder code, String value) {
public void addMergeFromValue(Block code, String value) {
String propertyName = property.getName();
if (propertyName.equals(value)) {
propertyName = "this." + propertyName; // see issue #78
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,13 @@ private static void addMergeFromValueMethod(SourceBuilder code, Metadata metadat
metadata.getType().getQualifiedName())
.addLine(" */")
.addLine("public %s mergeFrom(%s value) {", metadata.getBuilder(), metadata.getType());
Block body = new Block(code);
for (Property property : metadata.getProperties()) {
property.getCodeGenerator().addMergeFromValue(code, "value");
property.getCodeGenerator().addMergeFromValue(body, "value");
}
code.add(" return (%s) this;\n", metadata.getBuilder());
code.addLine("}");
code.add(body)
.addLine(" return (%s) this;", metadata.getBuilder())
.addLine("}");
}

private static void addMergeFromBuilderMethod(SourceBuilder code, Metadata metadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,57 @@ public void addFinalFieldAssignment(SourceBuilder code, String finalField, Strin
}

@Override
public void addMergeFromValue(SourceBuilder code, String value) {
code.addLine("%s(%s.%s());", setter(property), value, property.getGetterName());
public void addMergeFromValue(Block code, String value) {
Excerpt defaults = Declarations.freshBuilder(code, metadata).orNull();
if (defaults != null) {
code.add("if (");
if (!hasDefault) {
code.add("%s._unsetProperties.contains(%s.%s) || ",
defaults, metadata.getPropertyEnum(), property.getAllCapsName());
}
if (isPrimitive) {
code.add("%s.%s() != %s.%s()",
value, property.getGetterName(), defaults, getter(property));
} else {
code.add("!%s.%s().equals(%s.%s())",
value, property.getGetterName(), defaults, getter(property));
}
code.add(") {%n");
}
code.addLine(" %s(%s.%s());", setter(property), value, property.getGetterName());
if (defaults != null) {
code.addLine("}");
}
}

@Override
public void addMergeFromBuilder(Block code, String builder) {
if (!hasDefault) {
Excerpt base = Declarations.upcastToGeneratedBuilder(code, metadata, builder);
Excerpt base =
hasDefault ? null : Declarations.upcastToGeneratedBuilder(code, metadata, builder);
Excerpt defaults = Declarations.freshBuilder(code, metadata).orNull();
if (defaults != null) {
code.add("if (");
if (!hasDefault) {
code.add("!%s._unsetProperties.contains(%s.%s) && ",
base, metadata.getPropertyEnum(), property.getAllCapsName())
.add("(%s._unsetProperties.contains(%s.%s) ||",
defaults, metadata.getPropertyEnum(), property.getAllCapsName());
}
if (isPrimitive) {
code.add("%1$s.%2$s() != %3$s.%2$s()", builder, getter(property), defaults);
} else {
code.add("!%1$s.%2$s().equals(%3$s.%2$s())", builder, getter(property), defaults);
}
if (!hasDefault) {
code.add(")");
}
code.add(") {%n");
} else if (!hasDefault) {
code.addLine("if (!%s._unsetProperties.contains(%s.%s)) {",
base, metadata.getPropertyEnum(), property.getAllCapsName());
}
code.addLine(" %s(%s.%s());", setter(property), builder, getter(property));
if (!hasDefault) {
if (defaults != null || !hasDefault) {
code.addLine("}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public void addFinalFieldAssignment(SourceBuilder code, String finalField, Strin
}

@Override
public void addMergeFromValue(SourceBuilder code, String value) {
public void addMergeFromValue(Block code, String value) {
code.addLine("%s(%s.%s());", putAllMethod(property), value, property.getGetterName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void addFinalFieldAssignment(SourceBuilder code, String finalField, Strin
}

@Override
public void addMergeFromValue(SourceBuilder code, String value) {
public void addMergeFromValue(Block code, String value) {
code.addLine("%s(%s.%s());", addAllMethod(property), value, property.getGetterName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public void addFinalFieldAssignment(SourceBuilder code, String finalField, Strin
}

@Override
public void addMergeFromValue(SourceBuilder code, String value) {
public void addMergeFromValue(Block code, String value) {
code.addLine("%s(%s.%s());", putAllMethod(property), value, property.getGetterName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public void addFinalFieldAssignment(SourceBuilder code, String finalField, Strin
}

@Override
public void addMergeFromValue(SourceBuilder code, String value) {
public void addMergeFromValue(Block code, String value) {
code.addLine("%s(%s.%s());", addAllMethod(property), value, property.getGetterName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void addFinalFieldAssignment(SourceBuilder code, String finalField, Strin
}

@Override
public void addMergeFromValue(SourceBuilder code, String value) {
public void addMergeFromValue(Block code, String value) {
code.addLine("%s(%s.%s());", setter(property), value, property.getGetterName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public void addFinalFieldAssignment(SourceBuilder code, String finalField, Strin
}

@Override
public void addMergeFromValue(SourceBuilder code, String value) {
public void addMergeFromValue(Block code, String value) {
String propertyValue = value + "." + property.getGetterName() + "()";
optional.invokeIfPresent(code, propertyValue, setter(property));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void addPartialFieldAssignment(SourceBuilder code, String finalField, Str
}

/** Add a merge from value for the property to the builder's source code. */
public abstract void addMergeFromValue(SourceBuilder code, String value);
public abstract void addMergeFromValue(Block code, String value);

/** Add a merge from builder for the property to the builder's source code. */
public abstract void addMergeFromBuilder(Block code, String builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public void addFinalFieldAssignment(SourceBuilder code, String finalField, Strin
}

@Override
public void addMergeFromValue(SourceBuilder code, String value) {
public void addMergeFromValue(Block code, String value) {
code.addLine("%s(%s.%s());", putAllMethod(property), value, property.getGetterName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void addFinalFieldAssignment(SourceBuilder code, String finalField, Strin
}

@Override
public void addMergeFromValue(SourceBuilder code, String value) {
public void addMergeFromValue(Block code, String value) {
code.addLine("%s(%s.%s());", addAllMethod(property), value, property.getGetterName());
}

Expand Down
Loading

0 comments on commit 84b1484

Please sign in to comment.