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

Commit adec063

Browse files
authored
Merge pull request #416 from inferred/new.annotations.not.inherited
Stop accidentally inheriting IgnoredByEquals/NotInToString
2 parents b6ab5cf + e1aafce commit adec063

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,10 @@ private void addPropertyData(
453453
if (jacksonSupport.isPresent()) {
454454
jacksonSupport.get().addJacksonAnnotations(propertyBuilder, method);
455455
}
456-
propertyBuilder.setInEqualsAndHashCode(method.getAnnotation(IgnoredByEquals.class) == null);
457-
propertyBuilder.setInToString(method.getAnnotation(NotInToString.class) == null);
456+
if (method.getEnclosingElement().equals(valueType)) {
457+
propertyBuilder.setInEqualsAndHashCode(method.getAnnotation(IgnoredByEquals.class) == null);
458+
propertyBuilder.setInToString(method.getAnnotation(NotInToString.class) == null);
459+
}
458460
if (propertyType.getKind().isPrimitive()) {
459461
PrimitiveType unboxedType = types.getPrimitiveType(propertyType.getKind());
460462
TypeMirror boxedType = types.erasure(types.boxedClass(unboxedType).asType());

src/test/java/org/inferred/freebuilder/processor/AnalyserTest.java

+54-2
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ public void twoGetters_interface() throws CannotGenerateCodeException {
463463
}
464464

465465
@Test
466-
public void ignoredEqualsAndHashCode() throws CannotGenerateCodeException {
466+
public void ignoredByEquals() throws CannotGenerateCodeException {
467467
GeneratedBuilder builder = (GeneratedBuilder) analyser.analyse(model.newType(
468468
"package com.example;",
469469
"public class DataType {",
@@ -485,7 +485,33 @@ public void ignoredEqualsAndHashCode() throws CannotGenerateCodeException {
485485
}
486486

487487
@Test
488-
public void ignoredToString() throws CannotGenerateCodeException {
488+
public void ignoredByEquals_notInherited() throws CannotGenerateCodeException {
489+
model.newType(
490+
"package com.example;",
491+
"public interface HasName {",
492+
" @" + IgnoredByEquals.class.getName() + " public abstract String getName();",
493+
"}");
494+
GeneratedBuilder builder = (GeneratedBuilder) analyser.analyse(model.newType(
495+
"package com.example;",
496+
"public class DataType implements HasName {",
497+
" public static class Builder extends DataType_Builder {}",
498+
"}"));
499+
500+
Property name = new Property.Builder()
501+
.setAllCapsName("NAME")
502+
.setCapitalizedName("Name")
503+
.setFullyCheckedCast(true)
504+
.setGetterName("getName")
505+
.setName("name")
506+
.setType(model.typeMirror(String.class))
507+
.setUsingBeanConvention(true)
508+
.setInEqualsAndHashCode(true)
509+
.build();
510+
assertThat(builder.getGeneratorsByProperty().keySet()).containsExactly(name);
511+
}
512+
513+
@Test
514+
public void notInToString() throws CannotGenerateCodeException {
489515
GeneratedBuilder builder = (GeneratedBuilder) analyser.analyse(model.newType(
490516
"package com.example;",
491517
"public class DataType {",
@@ -506,6 +532,32 @@ public void ignoredToString() throws CannotGenerateCodeException {
506532
assertThat(builder.getGeneratorsByProperty().keySet()).containsExactly(name);
507533
}
508534

535+
@Test
536+
public void notInToString_notInherited() throws CannotGenerateCodeException {
537+
model.newType(
538+
"package com.example;",
539+
"public interface HasName {",
540+
" @" + NotInToString.class.getName() + " public abstract String getName();",
541+
"}");
542+
GeneratedBuilder builder = (GeneratedBuilder) analyser.analyse(model.newType(
543+
"package com.example;",
544+
"public class DataType implements HasName {",
545+
" public static class Builder extends DataType_Builder {}",
546+
"}"));
547+
548+
Property name = new Property.Builder()
549+
.setAllCapsName("NAME")
550+
.setCapitalizedName("Name")
551+
.setFullyCheckedCast(true)
552+
.setGetterName("getName")
553+
.setName("name")
554+
.setType(model.typeMirror(String.class))
555+
.setUsingBeanConvention(true)
556+
.setInToString(true)
557+
.build();
558+
assertThat(builder.getGeneratorsByProperty().keySet()).containsExactly(name);
559+
}
560+
509561
@Test
510562
public void ignoredEqualsAndHashCodeAndToString() throws CannotGenerateCodeException {
511563
GeneratedBuilder builder = (GeneratedBuilder) analyser.analyse(model.newType(

0 commit comments

Comments
 (0)