Skip to content

Commit

Permalink
Merge pull request #1235 from bulgakovalexander/bugfix/builderWithTol…
Browse files Browse the repository at this point in the history
…erate

Bugfix/builder with tolerate
  • Loading branch information
rspilker authored Nov 21, 2016
2 parents cce6f39 + 105c32f commit 7969951
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 17 deletions.
16 changes: 10 additions & 6 deletions src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1168,9 +1168,8 @@ public static MemberExistsResult methodExists(String methodName, EclipseNode nod
if (params < minArgs || params > maxArgs) continue;
}

if (def.annotations != null) for (Annotation anno : def.annotations) {
if (typeMatches(Tolerate.class, node, anno.type)) continue top;
}

if (isTolerate(node, def)) continue top;

return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK;
}
Expand All @@ -1181,6 +1180,13 @@ public static MemberExistsResult methodExists(String methodName, EclipseNode nod
return MemberExistsResult.NOT_EXISTS;
}

public static boolean isTolerate(EclipseNode node, AbstractMethodDeclaration def) {
if (def.annotations != null) for (Annotation anno : def.annotations) {
if (typeMatches(Tolerate.class, node, anno.type)) return true;
}
return false;
}

/**
* Checks if there is a (non-default) constructor. In case of multiple constructors (overloading), only
* the first constructor decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned.
Expand All @@ -1198,9 +1204,7 @@ public static MemberExistsResult constructorExists(EclipseNode node) {
if (def instanceof ConstructorDeclaration) {
if ((def.bits & ASTNode.IsDefaultConstructor) != 0) continue;

if (def.annotations != null) for (Annotation anno : def.annotations) {
if (typeMatches(Tolerate.class, node, anno.type)) continue top;
}
if (isTolerate(node, def)) continue;

return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/lombok/eclipse/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ private void makeSimpleSetterMethodForBuilder(EclipseNode builderType, EclipseNo
for (int i = 0; i < len; i++) {
if (!(existing[i] instanceof MethodDeclaration)) continue;
char[] existingName = existing[i].selector;
if (Arrays.equals(name, existingName)) return;
if (Arrays.equals(name, existingName) && !isTolerate(fieldNode, existing[i])) return;
}

String setterName = fluent ? fieldNode.getName() : HandlerUtil.buildAccessorName("set", fieldNode.getName());
Expand Down
5 changes: 3 additions & 2 deletions src/core/lombok/javac/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,9 @@ private void makeSimpleSetterMethodForBuilder(JavacNode builderType, JavacNode f

for (JavacNode child : builderType.down()) {
if (child.getKind() != Kind.METHOD) continue;
Name existingName = ((JCMethodDecl) child.get()).name;
if (existingName.equals(fieldName)) return;
JCMethodDecl methodDecl = (JCMethodDecl) child.get();
Name existingName = methodDecl.name;
if (existingName.equals(fieldName) && !isTolerate(fieldNode, methodDecl)) return;
}

String setterName = fluent ? fieldNode.getName() : HandlerUtil.buildAccessorName("set", fieldNode.getName());
Expand Down
18 changes: 10 additions & 8 deletions src/core/lombok/javac/handlers/JavacHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -605,10 +605,7 @@ public static MemberExistsResult methodExists(String methodName, JavacNode node,
if (params < minArgs || params > maxArgs) continue;
}

List<JCAnnotation> annotations = md.getModifiers().getAnnotations();
if (annotations != null) for (JCAnnotation anno : annotations) {
if (typeMatches(Tolerate.class, node, anno.getAnnotationType())) continue top;
}
if (isTolerate(node, md)) continue top;

return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK;
}
Expand All @@ -619,6 +616,14 @@ public static MemberExistsResult methodExists(String methodName, JavacNode node,
return MemberExistsResult.NOT_EXISTS;
}

public static boolean isTolerate(JavacNode node, JCTree.JCMethodDecl md) {
List<JCAnnotation> annotations = md.getModifiers().getAnnotations();
if (annotations != null) for (JCTree.JCAnnotation anno : annotations) {
if (typeMatches(Tolerate.class, node, anno.getAnnotationType())) return true;
}
return false;
}

/**
* Checks if there is a (non-default) constructor. In case of multiple constructors (overloading), only
* the first constructor decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned.
Expand All @@ -634,10 +639,7 @@ public static MemberExistsResult constructorExists(JavacNode node) {
JCMethodDecl md = (JCMethodDecl) def;
if (md.name.contentEquals("<init>")) {
if ((md.mods.flags & Flags.GENERATEDCONSTR) != 0) continue;
List<JCAnnotation> annotations = md.getModifiers().getAnnotations();
if (annotations != null) for (JCAnnotation anno : annotations) {
if (typeMatches(Tolerate.class, node, anno.getAnnotationType())) continue top;
}
if (isTolerate(node, md)) continue;
return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK;
}
}
Expand Down
59 changes: 59 additions & 0 deletions test/transform/resource/after-delombok/BuilderWithTolerate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

import lombok.experimental.Tolerate;

public class BuilderWithTolerate {
private final int value;

public static void main(String[] args) {
BuilderWithTolerate.builder().value("42").build();
}


public static class BuilderWithTolerateBuilder {
@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
private int value;

@Tolerate
public BuilderWithTolerateBuilder value(String s) {
return this.value(Integer.parseInt(s));
}

@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
BuilderWithTolerateBuilder() {
}

@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
public BuilderWithTolerateBuilder value(final int value) {
this.value = value;
return this;
}

@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
public BuilderWithTolerate build() {
return new BuilderWithTolerate(value);
}

@java.lang.Override
@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
public java.lang.String toString() {
return "BuilderWithTolerate.BuilderWithTolerateBuilder(value=" + this.value + ")";
}
}

@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
BuilderWithTolerate(final int value) {
this.value = value;
}

@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
public static BuilderWithTolerateBuilder builder() {
return new BuilderWithTolerateBuilder();
}
}
34 changes: 34 additions & 0 deletions test/transform/resource/after-ecj/BuilderWithTolerate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import lombok.Builder;
import lombok.experimental.Tolerate;
public @Builder class BuilderWithTolerate {
public static class BuilderWithTolerateBuilder {
private @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") int value;
public @Tolerate BuilderWithTolerateBuilder value(String s) {
return this.value(Integer.parseInt(s));
}
@java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithTolerateBuilder() {
super();
}
public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithTolerateBuilder value(final int value) {
this.value = value;
return this;
}
public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithTolerate build() {
return new BuilderWithTolerate(value);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") java.lang.String toString() {
return (("BuilderWithTolerate.BuilderWithTolerateBuilder(value=" + this.value) + ")");
}
}
private final int value;
public static void main(String[] args) {
BuilderWithTolerate.builder().value("42").build();
}
@java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithTolerate(final int value) {
super();
this.value = value;
}
public static @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithTolerateBuilder builder() {
return new BuilderWithTolerateBuilder();
}
}
18 changes: 18 additions & 0 deletions test/transform/resource/before/BuilderWithTolerate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import lombok.Builder;
import lombok.experimental.Tolerate;

@Builder
public class BuilderWithTolerate {
private final int value;

public static void main(String[] args) {
BuilderWithTolerate.builder().value("42").build();
}

public static class BuilderWithTolerateBuilder {
@Tolerate
public BuilderWithTolerateBuilder value(String s) {
return this.value(Integer.parseInt(s));
}
}
}

0 comments on commit 7969951

Please sign in to comment.