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

Commit cdb0034

Browse files
authored
#412 Add annotations to exclude properties from toString and equals/hashCode
`@IgnoredByEquals` excludes a property from `equals` (and consequently `hashCode`); `@NotInToString` excludes a property from the `toString` output.
2 parents d5a222f + d0c8b52 commit cdb0034

File tree

10 files changed

+429
-10
lines changed

10 files changed

+429
-10
lines changed

generated/main/java/org/inferred/freebuilder/processor/property/Property_Builder.java

+135
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public String toString() {
7171
private String capitalizedName;
7272
private String allCapsName;
7373
private boolean usingBeanConvention;
74+
private boolean inToString;
75+
private boolean inEqualsAndHashCode;
7476
private String getterName;
7577
private boolean fullyCheckedCast;
7678
private List<Excerpt> accessorAnnotations = ImmutableList.of();
@@ -354,6 +356,79 @@ public boolean isUsingBeanConvention() {
354356
return usingBeanConvention;
355357
}
356358

359+
/**
360+
* Sets the value to be returned by {@link
361+
* org.inferred.freebuilder.processor.property.Property#isInToString()}.
362+
*
363+
* @return this {@code Builder} object
364+
*/
365+
public org.inferred.freebuilder.processor.property.Property.Builder setInToString(boolean inToString) {
366+
this.inToString = inToString;
367+
return (org.inferred.freebuilder.processor.property.Property.Builder) this;
368+
}
369+
370+
/**
371+
* Replaces the value to be returned by {@link
372+
* org.inferred.freebuilder.processor.property.Property#isInToString()} by applying {@code mapper} to it
373+
* and using the result.
374+
*
375+
* @return this {@code Builder} object
376+
* @throws NullPointerException if {@code mapper} is null or returns null
377+
* @throws IllegalStateException if the field has not been set
378+
*/
379+
public org.inferred.freebuilder.processor.property.Property.Builder mapInToString(
380+
UnaryOperator<Boolean> mapper) {
381+
Objects.requireNonNull(mapper);
382+
return setInToString(mapper.apply(isInToString()));
383+
}
384+
385+
/**
386+
* Returns the value that will be returned by {@link
387+
* org.inferred.freebuilder.processor.property.Property#isInToString()}.
388+
*
389+
* @throws IllegalStateException if the field has not been set
390+
*/
391+
public boolean isInToString() {
392+
return inToString;
393+
}
394+
395+
/**
396+
* Sets the value to be returned by {@link
397+
* org.inferred.freebuilder.processor.property.Property#isInEqualsAndHashCode()}.
398+
*
399+
* @return this {@code Builder} object
400+
*/
401+
public org.inferred.freebuilder.processor.property.Property.Builder setInEqualsAndHashCode(
402+
boolean inEqualsAndHashCode) {
403+
this.inEqualsAndHashCode = inEqualsAndHashCode;
404+
return (org.inferred.freebuilder.processor.property.Property.Builder) this;
405+
}
406+
407+
/**
408+
* Replaces the value to be returned by {@link
409+
* org.inferred.freebuilder.processor.property.Property#isInEqualsAndHashCode()} by applying {@code
410+
* mapper} to it and using the result.
411+
*
412+
* @return this {@code Builder} object
413+
* @throws NullPointerException if {@code mapper} is null or returns null
414+
* @throws IllegalStateException if the field has not been set
415+
*/
416+
public org.inferred.freebuilder.processor.property.Property.Builder mapInEqualsAndHashCode(
417+
UnaryOperator<Boolean> mapper) {
418+
Objects.requireNonNull(mapper);
419+
return setInEqualsAndHashCode(mapper.apply(isInEqualsAndHashCode()));
420+
}
421+
422+
/**
423+
* Returns the value that will be returned by {@link
424+
* org.inferred.freebuilder.processor.property.Property#isInEqualsAndHashCode()}.
425+
*
426+
* @throws IllegalStateException if the field has not been set
427+
*/
428+
public boolean isInEqualsAndHashCode() {
429+
return inEqualsAndHashCode;
430+
}
431+
357432
/**
358433
* Sets the value to be returned by {@link
359434
* org.inferred.freebuilder.processor.property.Property#getGetterName()}.
@@ -587,6 +662,12 @@ public org.inferred.freebuilder.processor.property.Property.Builder mergeFrom(
587662
|| value.isUsingBeanConvention() != defaults.isUsingBeanConvention()) {
588663
setUsingBeanConvention(value.isUsingBeanConvention());
589664
}
665+
if (!Objects.equals(value.isInToString(), defaults.isInToString())) {
666+
setInToString(value.isInToString());
667+
}
668+
if (!Objects.equals(value.isInEqualsAndHashCode(), defaults.isInEqualsAndHashCode())) {
669+
setInEqualsAndHashCode(value.isInEqualsAndHashCode());
670+
}
590671
if (defaults._unsetProperties.contains(Property.GETTER_NAME)
591672
|| !Objects.equals(value.getGetterName(), defaults.getGetterName())) {
592673
setGetterName(value.getGetterName());
@@ -640,6 +721,12 @@ public org.inferred.freebuilder.processor.property.Property.Builder mergeFrom(
640721
|| template.isUsingBeanConvention() != defaults.isUsingBeanConvention())) {
641722
setUsingBeanConvention(template.isUsingBeanConvention());
642723
}
724+
if (template.isInToString() != defaults.isInToString()) {
725+
setInToString(template.isInToString());
726+
}
727+
if (template.isInEqualsAndHashCode() != defaults.isInEqualsAndHashCode()) {
728+
setInEqualsAndHashCode(template.isInEqualsAndHashCode());
729+
}
643730
if (!base._unsetProperties.contains(Property.GETTER_NAME)
644731
&& (defaults._unsetProperties.contains(Property.GETTER_NAME)
645732
|| !Objects.equals(template.getGetterName(), defaults.getGetterName()))) {
@@ -667,6 +754,8 @@ public org.inferred.freebuilder.processor.property.Property.Builder clear() {
667754
capitalizedName = defaults.capitalizedName;
668755
allCapsName = defaults.allCapsName;
669756
usingBeanConvention = defaults.usingBeanConvention;
757+
inToString = defaults.inToString;
758+
inEqualsAndHashCode = defaults.inEqualsAndHashCode;
670759
getterName = defaults.getterName;
671760
fullyCheckedCast = defaults.fullyCheckedCast;
672761
clearAccessorAnnotations();
@@ -721,6 +810,8 @@ private static final class Value extends Rebuildable {
721810
private final String capitalizedName;
722811
private final String allCapsName;
723812
private final boolean usingBeanConvention;
813+
private final boolean inToString;
814+
private final boolean inEqualsAndHashCode;
724815
private final String getterName;
725816
private final boolean fullyCheckedCast;
726817
private final ImmutableList<Excerpt> accessorAnnotations;
@@ -732,6 +823,8 @@ private Value(Property_Builder builder) {
732823
this.capitalizedName = builder.capitalizedName;
733824
this.allCapsName = builder.allCapsName;
734825
this.usingBeanConvention = builder.usingBeanConvention;
826+
this.inToString = builder.inToString;
827+
this.inEqualsAndHashCode = builder.inEqualsAndHashCode;
735828
this.getterName = builder.getterName;
736829
this.fullyCheckedCast = builder.fullyCheckedCast;
737830
this.accessorAnnotations = ImmutableList.copyOf(builder.accessorAnnotations);
@@ -767,6 +860,16 @@ public boolean isUsingBeanConvention() {
767860
return usingBeanConvention;
768861
}
769862

863+
@Override
864+
public boolean isInToString() {
865+
return inToString;
866+
}
867+
868+
@Override
869+
public boolean isInEqualsAndHashCode() {
870+
return inEqualsAndHashCode;
871+
}
872+
770873
@Override
771874
public String getGetterName() {
772875
return getterName;
@@ -791,6 +894,8 @@ public Builder toBuilder() {
791894
builder.capitalizedName = capitalizedName;
792895
builder.allCapsName = allCapsName;
793896
builder.usingBeanConvention = usingBeanConvention;
897+
builder.inToString = inToString;
898+
builder.inEqualsAndHashCode = inEqualsAndHashCode;
794899
builder.getterName = getterName;
795900
builder.fullyCheckedCast = fullyCheckedCast;
796901
builder.accessorAnnotations = accessorAnnotations;
@@ -810,6 +915,8 @@ public boolean equals(Object obj) {
810915
&& Objects.equals(capitalizedName, other.capitalizedName)
811916
&& Objects.equals(allCapsName, other.allCapsName)
812917
&& usingBeanConvention == other.usingBeanConvention
918+
&& inToString == other.inToString
919+
&& inEqualsAndHashCode == other.inEqualsAndHashCode
813920
&& Objects.equals(getterName, other.getterName)
814921
&& fullyCheckedCast == other.fullyCheckedCast
815922
&& Objects.equals(accessorAnnotations, other.accessorAnnotations);
@@ -824,6 +931,8 @@ public int hashCode() {
824931
capitalizedName,
825932
allCapsName,
826933
usingBeanConvention,
934+
inToString,
935+
inEqualsAndHashCode,
827936
getterName,
828937
fullyCheckedCast,
829938
accessorAnnotations);
@@ -844,6 +953,10 @@ public String toString() {
844953
.append(allCapsName)
845954
.append(", usingBeanConvention=")
846955
.append(usingBeanConvention)
956+
.append(", inToString=")
957+
.append(inToString)
958+
.append(", inEqualsAndHashCode=")
959+
.append(inEqualsAndHashCode)
847960
.append(", getterName=")
848961
.append(getterName)
849962
.append(", fullyCheckedCast=")
@@ -865,6 +978,8 @@ private static final class Partial extends Rebuildable {
865978
private final String capitalizedName;
866979
private final String allCapsName;
867980
private final boolean usingBeanConvention;
981+
private final boolean inToString;
982+
private final boolean inEqualsAndHashCode;
868983
private final String getterName;
869984
private final boolean fullyCheckedCast;
870985
private final ImmutableList<Excerpt> accessorAnnotations;
@@ -877,6 +992,8 @@ private static final class Partial extends Rebuildable {
877992
this.capitalizedName = builder.capitalizedName;
878993
this.allCapsName = builder.allCapsName;
879994
this.usingBeanConvention = builder.usingBeanConvention;
995+
this.inToString = builder.inToString;
996+
this.inEqualsAndHashCode = builder.inEqualsAndHashCode;
880997
this.getterName = builder.getterName;
881998
this.fullyCheckedCast = builder.fullyCheckedCast;
882999
this.accessorAnnotations = ImmutableList.copyOf(builder.accessorAnnotations);
@@ -928,6 +1045,16 @@ public boolean isUsingBeanConvention() {
9281045
return usingBeanConvention;
9291046
}
9301047

1048+
@Override
1049+
public boolean isInToString() {
1050+
return inToString;
1051+
}
1052+
1053+
@Override
1054+
public boolean isInEqualsAndHashCode() {
1055+
return inEqualsAndHashCode;
1056+
}
1057+
9311058
@Override
9321059
public String getGetterName() {
9331060
if (_unsetProperties.contains(Property.GETTER_NAME)) {
@@ -965,6 +1092,8 @@ public Builder toBuilder() {
9651092
builder.capitalizedName = capitalizedName;
9661093
builder.allCapsName = allCapsName;
9671094
builder.usingBeanConvention = usingBeanConvention;
1095+
builder.inToString = inToString;
1096+
builder.inEqualsAndHashCode = inEqualsAndHashCode;
9681097
builder.getterName = getterName;
9691098
builder.fullyCheckedCast = fullyCheckedCast;
9701099
builder.accessorAnnotations = accessorAnnotations;
@@ -985,6 +1114,8 @@ public boolean equals(Object obj) {
9851114
&& Objects.equals(capitalizedName, other.capitalizedName)
9861115
&& Objects.equals(allCapsName, other.allCapsName)
9871116
&& usingBeanConvention == other.usingBeanConvention
1117+
&& inToString == other.inToString
1118+
&& inEqualsAndHashCode == other.inEqualsAndHashCode
9881119
&& Objects.equals(getterName, other.getterName)
9891120
&& fullyCheckedCast == other.fullyCheckedCast
9901121
&& Objects.equals(accessorAnnotations, other.accessorAnnotations)
@@ -1000,6 +1131,8 @@ public int hashCode() {
10001131
capitalizedName,
10011132
allCapsName,
10021133
usingBeanConvention,
1134+
inToString,
1135+
inEqualsAndHashCode,
10031136
getterName,
10041137
fullyCheckedCast,
10051138
accessorAnnotations,
@@ -1027,6 +1160,8 @@ public String toString() {
10271160
if (!_unsetProperties.contains(Property.USING_BEAN_CONVENTION)) {
10281161
result.append("usingBeanConvention=").append(usingBeanConvention).append(", ");
10291162
}
1163+
result.append("inToString=").append(inToString).append(", ");
1164+
result.append("inEqualsAndHashCode=").append(inEqualsAndHashCode).append(", ");
10301165
if (!_unsetProperties.contains(Property.GETTER_NAME)) {
10311166
result.append("getterName=").append(getterName).append(", ");
10321167
}

jar-footprint.txt

+2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ org/
66
org/inferred/
77
org/inferred/freebuilder/
88
org/inferred/freebuilder/FreeBuilder.class
9+
org/inferred/freebuilder/IgnoredByEquals.class
910
org/inferred/freebuilder/processor
11+
org/inferred/freebuilder/NotInToString.class
1012
org/inferred/freebuilder/shaded
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.inferred.freebuilder;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* If present, do not include this property in the generated
10+
* {@link Object#equals(Object)} and {@link Object#hashCode()}.
11+
*/
12+
@Target(ElementType.METHOD)
13+
@Retention(RetentionPolicy.SOURCE)
14+
public @interface IgnoredByEquals {
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.inferred.freebuilder;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* If present, do not include this property in the generated {@link Object#toString()}.
10+
*/
11+
@Target(ElementType.METHOD)
12+
@Retention(RetentionPolicy.SOURCE)
13+
public @interface NotInToString {
14+
}

src/main/java/org/inferred/freebuilder/processor/Analyser.java

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import com.google.common.collect.ImmutableSet;
3737
import com.google.common.collect.Sets;
3838

39+
import org.inferred.freebuilder.IgnoredByEquals;
40+
import org.inferred.freebuilder.NotInToString;
3941
import org.inferred.freebuilder.processor.Datatype.StandardMethod;
4042
import org.inferred.freebuilder.processor.Datatype.UnderrideLevel;
4143
import org.inferred.freebuilder.processor.model.MethodIntrospector;
@@ -451,6 +453,8 @@ private void addPropertyData(
451453
if (jacksonSupport.isPresent()) {
452454
jacksonSupport.get().addJacksonAnnotations(propertyBuilder, method);
453455
}
456+
propertyBuilder.setInEqualsAndHashCode(method.getAnnotation(IgnoredByEquals.class) == null);
457+
propertyBuilder.setInToString(method.getAnnotation(NotInToString.class) == null);
454458
if (propertyType.getKind().isPrimitive()) {
455459
PrimitiveType unboxedType = types.getPrimitiveType(propertyType.getKind());
456460
TypeMirror boxedType = types.erasure(types.boxedClass(unboxedType).asType());

0 commit comments

Comments
 (0)