Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/core/lombok/eclipse/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,15 @@ private static final char[] prefixWith(char[] prefix, char[] name) {
}

if (methodExists("toString", job.builderType, 0) == MemberExistsResult.NOT_EXISTS) {
List<String> oldExcludes = findOldToStringExcludes(job.parentType);
List<Included<EclipseNode, ToString.Include>> fieldNodes = new ArrayList<Included<EclipseNode, ToString.Include>>();
for (BuilderFieldData bfd : job.builderFields) {
if (bfd.originalFieldNode.hasAnnotation(ToString.Exclude.class)) {
continue;
}
if (oldExcludes.contains(bfd.originalFieldNode.getName())) {
continue;
}
for (EclipseNode f : bfd.createdFields) {
fieldNodes.add(new Included<EclipseNode, ToString.Include>(f, null, true, false));
}
Expand Down Expand Up @@ -1137,4 +1144,12 @@ private SingularData getSingularData(EclipseNode node, ASTNode source, final Str

return null;
}

private List<String> findOldToStringExcludes(EclipseNode parentType) {
EclipseNode toStringAnnotationNode = findAnnotation(ToString.class, parentType);
if (toStringAnnotationNode == null) return Collections.emptyList();

AnnotationValues<ToString> toStringAnnotation = createAnnotation(ToString.class, toStringAnnotationNode);
return toStringAnnotation.getAsStringList("exclude");
}
}
16 changes: 16 additions & 0 deletions src/core/lombok/javac/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static lombok.javac.handlers.JavacHandlerUtil.*;

import java.util.ArrayList;
import java.util.Collections;

import javax.lang.model.element.Modifier;

Expand Down Expand Up @@ -505,8 +506,15 @@ static class BuilderFieldData {
}

if (methodExists("toString", job.builderType, 0) == MemberExistsResult.NOT_EXISTS) {
java.util.List<String> oldExcludes = findOldToStringExcludes(job.parentType);
java.util.List<Included<JavacNode, ToString.Include>> fieldNodes = new ArrayList<Included<JavacNode, ToString.Include>>();
for (BuilderFieldData bfd : job.builderFields) {
if (hasAnnotation(ToString.Exclude.class, bfd.originalFieldNode)) {
continue;
}
if (oldExcludes.contains(bfd.originalFieldNode.getName())) {
continue;
}
for (JavacNode f : bfd.createdFields) {
fieldNodes.add(new Included<JavacNode, ToString.Include>(f, null, true, false));
}
Expand Down Expand Up @@ -1020,4 +1028,12 @@ private SingularData getSingularData(JavacNode node, String setterPrefix) {

return null;
}

private java.util.List<String> findOldToStringExcludes(JavacNode parentType) {
JavacNode toStringAnnotationNode = findAnnotation(ToString.class, parentType);
if (toStringAnnotationNode == null) return Collections.emptyList();

AnnotationValues<ToString> toStringAnnotation = createAnnotation(ToString.class, toStringAnnotationNode);
return toStringAnnotation.getAsStringList("exclude");
}
}
46 changes: 46 additions & 0 deletions src/core/lombok/javac/handlers/HandleToStringExcludeRemove.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2025 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.javac.handlers;

import static lombok.javac.handlers.JavacHandlerUtil.*;

import com.sun.tools.javac.tree.JCTree.JCAnnotation;

import lombok.ToString;
import lombok.ToString.Exclude;
import lombok.core.AlreadyHandledAnnotations;
import lombok.core.AnnotationValues;
import lombok.core.HandlerPriority;
import lombok.javac.JavacAnnotationHandler;
import lombok.javac.JavacNode;
import lombok.spi.Provides;

@Provides
@HandlerPriority(32768)
@AlreadyHandledAnnotations
public class HandleToStringExcludeRemove extends JavacAnnotationHandler<ToString.Exclude> {
@Override public void handle(AnnotationValues<Exclude> annotation, JCAnnotation ast, JavacNode annotationNode) {
deleteAnnotationIfNeccessary(annotationNode, ToString.Exclude.class);
deleteImportFromCompilationUnit(annotationNode, ToString.class);
deleteImportFromCompilationUnit(annotationNode, ToString.Exclude.class);
}
}
7 changes: 6 additions & 1 deletion src/core/lombok/javac/handlers/JavacHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -556,17 +556,22 @@ private static void deleteAnnotationIfNeccessary0(JavacNode annotation, String..
}
}

public static void deleteImportFromCompilationUnit(JavacNode node, Class<?> annotationType) {
deleteImportFromCompilationUnit(node, annotationType.getName());
}

public static void deleteImportFromCompilationUnit(JavacNode node, String name) {
if (inNetbeansEditor(node)) return;
if (!node.shouldDeleteLombokAnnotations()) return;

JCCompilationUnit unit = (JCCompilationUnit) node.top().get();

String importName = name.replace('$', '.');
for (JCTree def : unit.defs) {
if (!(def instanceof JCImport)) continue;
JCImport imp0rt = (JCImport) def;
if (imp0rt.staticImport) continue;
if (!Javac.getQualid(imp0rt).toString().equals(name)) continue;
if (!Javac.getQualid(imp0rt).toString().equals(importName)) continue;
JavacAugments.JCImport_deletable.set(imp0rt, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
public class BuilderWithToStringExclude {
private String a;
private String secret;
@java.lang.SuppressWarnings("all")
@lombok.Generated
BuilderWithToStringExclude(final String a, final String secret) {
this.a = a;
this.secret = secret;
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public static class BuilderWithToStringExcludeBuilder {
@java.lang.SuppressWarnings("all")
@lombok.Generated
private String a;
@java.lang.SuppressWarnings("all")
@lombok.Generated
private String secret;
@java.lang.SuppressWarnings("all")
@lombok.Generated
BuilderWithToStringExcludeBuilder() {
}
/**
* @return {@code this}.
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public BuilderWithToStringExclude.BuilderWithToStringExcludeBuilder a(final String a) {
this.a = a;
return this;
}
/**
* @return {@code this}.
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public BuilderWithToStringExclude.BuilderWithToStringExcludeBuilder secret(final String secret) {
this.secret = secret;
return this;
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public BuilderWithToStringExclude build() {
return new BuilderWithToStringExclude(this.a, this.secret);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
@lombok.Generated
public java.lang.String toString() {
return "BuilderWithToStringExclude.BuilderWithToStringExcludeBuilder(a=" + this.a + ")";
}
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public static BuilderWithToStringExclude.BuilderWithToStringExcludeBuilder builder() {
return new BuilderWithToStringExclude.BuilderWithToStringExcludeBuilder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
public class BuilderWithToStringExcludeAndToString {
private String a;
private String secret;
@java.lang.SuppressWarnings("all")
@lombok.Generated
BuilderWithToStringExcludeAndToString(final String a, final String secret) {
this.a = a;
this.secret = secret;
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public static class BuilderWithToStringExcludeAndToStringBuilder {
@java.lang.SuppressWarnings("all")
@lombok.Generated
private String a;
@java.lang.SuppressWarnings("all")
@lombok.Generated
private String secret;
@java.lang.SuppressWarnings("all")
@lombok.Generated
BuilderWithToStringExcludeAndToStringBuilder() {
}
/**
* @return {@code this}.
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public BuilderWithToStringExcludeAndToString.BuilderWithToStringExcludeAndToStringBuilder a(final String a) {
this.a = a;
return this;
}
/**
* @return {@code this}.
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public BuilderWithToStringExcludeAndToString.BuilderWithToStringExcludeAndToStringBuilder secret(final String secret) {
this.secret = secret;
return this;
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public BuilderWithToStringExcludeAndToString build() {
return new BuilderWithToStringExcludeAndToString(this.a, this.secret);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
@lombok.Generated
public java.lang.String toString() {
return "BuilderWithToStringExcludeAndToString.BuilderWithToStringExcludeAndToStringBuilder(a=" + this.a + ")";
}
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public static BuilderWithToStringExcludeAndToString.BuilderWithToStringExcludeAndToStringBuilder builder() {
return new BuilderWithToStringExcludeAndToString.BuilderWithToStringExcludeAndToStringBuilder();
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
@lombok.Generated
public java.lang.String toString() {
return "BuilderWithToStringExcludeAndToString(a=" + this.a + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
public class BuilderWithToStringExcludeOld {
private String a;
private String secret;
@java.lang.SuppressWarnings("all")
@lombok.Generated
BuilderWithToStringExcludeOld(final String a, final String secret) {
this.a = a;
this.secret = secret;
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public static class BuilderWithToStringExcludeOldBuilder {
@java.lang.SuppressWarnings("all")
@lombok.Generated
private String a;
@java.lang.SuppressWarnings("all")
@lombok.Generated
private String secret;
@java.lang.SuppressWarnings("all")
@lombok.Generated
BuilderWithToStringExcludeOldBuilder() {
}
/**
* @return {@code this}.
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public BuilderWithToStringExcludeOld.BuilderWithToStringExcludeOldBuilder a(final String a) {
this.a = a;
return this;
}
/**
* @return {@code this}.
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public BuilderWithToStringExcludeOld.BuilderWithToStringExcludeOldBuilder secret(final String secret) {
this.secret = secret;
return this;
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public BuilderWithToStringExcludeOld build() {
return new BuilderWithToStringExcludeOld(this.a, this.secret);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
@lombok.Generated
public java.lang.String toString() {
return "BuilderWithToStringExcludeOld.BuilderWithToStringExcludeOldBuilder(a=" + this.a + ")";
}
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public static BuilderWithToStringExcludeOld.BuilderWithToStringExcludeOldBuilder builder() {
return new BuilderWithToStringExcludeOld.BuilderWithToStringExcludeOldBuilder();
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
@lombok.Generated
public java.lang.String toString() {
return "BuilderWithToStringExcludeOld(a=" + this.a + ")";
}
}
42 changes: 42 additions & 0 deletions test/transform/resource/after-ecj/BuilderWithToStringExclude.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import lombok.Builder;
import lombok.ToString;
import lombok.ToString.Exclude;
public @Builder class BuilderWithToStringExclude {
public static @java.lang.SuppressWarnings("all") @lombok.Generated class BuilderWithToStringExcludeBuilder {
private @java.lang.SuppressWarnings("all") @lombok.Generated String a;
private @java.lang.SuppressWarnings("all") @lombok.Generated String secret;
@java.lang.SuppressWarnings("all") @lombok.Generated BuilderWithToStringExcludeBuilder() {
super();
}
/**
* @return {@code this}.
*/
public @java.lang.SuppressWarnings("all") @lombok.Generated BuilderWithToStringExclude.BuilderWithToStringExcludeBuilder a(final String a) {
this.a = a;
return this;
}
/**
* @return {@code this}.
*/
public @java.lang.SuppressWarnings("all") @lombok.Generated BuilderWithToStringExclude.BuilderWithToStringExcludeBuilder secret(final String secret) {
this.secret = secret;
return this;
}
public @java.lang.SuppressWarnings("all") @lombok.Generated BuilderWithToStringExclude build() {
return new BuilderWithToStringExclude(this.a, this.secret);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") @lombok.Generated java.lang.String toString() {
return (("BuilderWithToStringExclude.BuilderWithToStringExcludeBuilder(a=" + this.a) + ")");
}
}
private String a;
private @ToString.Exclude String secret;
@java.lang.SuppressWarnings("all") @lombok.Generated BuilderWithToStringExclude(final String a, final String secret) {
super();
this.a = a;
this.secret = secret;
}
public static @java.lang.SuppressWarnings("all") @lombok.Generated BuilderWithToStringExclude.BuilderWithToStringExcludeBuilder builder() {
return new BuilderWithToStringExclude.BuilderWithToStringExcludeBuilder();
}
}
Loading
Loading