Skip to content

Commit

Permalink
[i1329] respect configuration for field access when generating equals…
Browse files Browse the repository at this point in the history
…, hashCode and toString from Data and Value
  • Loading branch information
rspilker committed Mar 22, 2017
1 parent 3b1792c commit 969ed91
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions doc/changelog.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Lombok Changelog
* FEATURE: `@Builder.Default` lets you configure default values for your fields when using `@Builder`. See the [Builder feature page](https://projectlombok.org/features/Builder.html) for more information. [Issue #1201](https://github.com/rzwitserloot/lombok/issues/1201)
* PLATFORM: JDK9 now supported for compilation (delomboking with java9 not yet possible). Note, you'll have to do some command line wrangling. See [Issue #985](https://github.com/rzwitserloot/lombok/issues/985)
* BUGFIX: The `onX` feature (which lets you add annotations to generated methods) did not work if the annotation you added contained named parameters, and you are compiling with JDK8's javac. We can't fix this (it's a bug in javac), but we have provided an alternate, prettier way to do `onX` on javac8+. [Issue #778](https://github.com/rzwitserloot/lombok/issues/778) [onX documentation](https://projectlombok.org/features/experimental/onX.html)
* BUGFIX: `@Data` and `@Value` now respect the configuration for field access when generating equals, hashCode and toString [Issue #1329](https://github.com/rzwitserloot/lombok/issues/1329)

### v1.16.14 (February 10th, 2017)
* FEATURE: Generated classes, methods and fields can now also annotated with `@lombok.Generated` [Issue #1014](https://github.com/rzwitserloot/lombok/issues/1014)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ public void generateEqualsAndHashCodeForType(EclipseNode typeNode, EclipseNode e
return;
}

generateMethods(typeNode, errorNode, null, null, null, false, FieldAccess.GETTER, new ArrayList<Annotation>());
Boolean doNotUseGettersConfiguration = typeNode.getAst().readConfiguration(ConfigurationKeys.EQUALS_AND_HASH_CODE_DO_NOT_USE_GETTERS);
FieldAccess access = doNotUseGettersConfiguration == null || !doNotUseGettersConfiguration ? FieldAccess.GETTER : FieldAccess.PREFER_FIELD;

generateMethods(typeNode, errorNode, null, null, null, false, access, new ArrayList<Annotation>());
}

@Override public void handle(AnnotationValues<EqualsAndHashCode> annotation, Annotation ast, EclipseNode annotationNode) {
Expand Down
6 changes: 5 additions & 1 deletion src/core/lombok/eclipse/handlers/HandleToString.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public void generateToStringForType(EclipseNode typeNode, EclipseNode errorNode)
Boolean configuration = typeNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES);
includeFieldNames = configuration != null ? configuration : ((Boolean)ToString.class.getMethod("includeFieldNames").getDefaultValue()).booleanValue();
} catch (Exception ignore) {}
generateToString(typeNode, errorNode, null, null, includeFieldNames, null, false, FieldAccess.GETTER);

Boolean doNotUseGettersConfiguration = typeNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_DO_NOT_USE_GETTERS);
FieldAccess access = doNotUseGettersConfiguration == null || !doNotUseGettersConfiguration ? FieldAccess.GETTER : FieldAccess.PREFER_FIELD;

generateToString(typeNode, errorNode, null, null, includeFieldNames, null, false, access);
}

public void handle(AnnotationValues<ToString> annotation, Annotation ast, EclipseNode annotationNode) {
Expand Down
5 changes: 4 additions & 1 deletion src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ public void generateEqualsAndHashCodeForType(JavacNode typeNode, JavacNode sourc
return;
}

generateMethods(typeNode, source, null, null, null, false, FieldAccess.GETTER, List.<JCAnnotation>nil());
Boolean doNotUseGettersConfiguration = typeNode.getAst().readConfiguration(ConfigurationKeys.EQUALS_AND_HASH_CODE_DO_NOT_USE_GETTERS);
FieldAccess access = doNotUseGettersConfiguration == null || !doNotUseGettersConfiguration ? FieldAccess.GETTER : FieldAccess.PREFER_FIELD;

generateMethods(typeNode, source, null, null, null, false, access, List.<JCAnnotation>nil());
}

public void generateMethods(JavacNode typeNode, JavacNode source, List<String> excludes, List<String> includes,
Expand Down
6 changes: 5 additions & 1 deletion src/core/lombok/javac/handlers/HandleToString.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ public void generateToStringForType(JavacNode typeNode, JavacNode errorNode) {
Boolean configuration = typeNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES);
includeFieldNames = configuration != null ? configuration : ((Boolean)ToString.class.getMethod("includeFieldNames").getDefaultValue()).booleanValue();
} catch (Exception ignore) {}
generateToString(typeNode, errorNode, null, null, includeFieldNames, null, false, FieldAccess.GETTER);

Boolean doNotUseGettersConfiguration = typeNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_DO_NOT_USE_GETTERS);
FieldAccess access = doNotUseGettersConfiguration == null || !doNotUseGettersConfiguration ? FieldAccess.GETTER : FieldAccess.PREFER_FIELD;

generateToString(typeNode, errorNode, null, null, includeFieldNames, null, false, access);
}

public void generateToString(JavacNode typeNode, JavacNode source, List<String> excludes, List<String> includes,
Expand Down
6 changes: 3 additions & 3 deletions test/transform/resource/after-delombok/DataConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public boolean equals(final java.lang.Object o) {
if (!(o instanceof DataConfiguration)) return false;
final DataConfiguration other = (DataConfiguration) o;
if (!other.canEqual((java.lang.Object) this)) return false;
if (this.getX() != other.getX()) return false;
if (this.x != other.x) return false;
return true;
}
@java.lang.SuppressWarnings("all")
Expand All @@ -32,13 +32,13 @@ protected boolean canEqual(final java.lang.Object other) {
public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
result = result * PRIME + this.x;
return result;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
public java.lang.String toString() {
return "DataConfiguration(x=" + this.getX() + ")";
return "DataConfiguration(x=" + this.x + ")";
}
}
6 changes: 3 additions & 3 deletions test/transform/resource/after-ecj/DataConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
final DataConfiguration other = (DataConfiguration) o;
if ((! other.canEqual((java.lang.Object) this)))
return false;
if ((this.getX() != other.getX()))
if ((this.x != other.x))
return false;
return true;
}
Expand All @@ -21,11 +21,11 @@
public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") int hashCode() {
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getX());
result = ((result * PRIME) + this.x);
return result;
}
public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") java.lang.String toString() {
return (("DataConfiguration(x=" + this.getX()) + ")");
return (("DataConfiguration(x=" + this.x) + ")");
}
public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") DataConfiguration(final int x) {
super();
Expand Down
2 changes: 2 additions & 0 deletions test/transform/resource/before/DataConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//CONF: lombok.anyConstructor.suppressConstructorProperties = true
//CONF: lombok.toString.doNotUseGetters = true
//CONF: lombok.equalsAndHashCode.doNotUseGetters = true
@lombok.Data
class DataConfiguration {
final int x;
Expand Down

0 comments on commit 969ed91

Please sign in to comment.