From 2335f251665d43b4c8cebe00b980f07ef33bdb17 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Wed, 1 May 2019 05:45:10 +0200 Subject: [fixes #2104] superbuilder + non-list-singulars wouldn’t work due to hardcoded call to emptyList. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eclipse/handlers/EclipseSingularsRecipes.java | 3 +++ .../lombok/eclipse/handlers/HandleSuperBuilder.java | 11 +++++------ .../handlers/singulars/EclipseGuavaSingularizer.java | 19 +++++++++++++++---- .../singulars/EclipseJavaUtilListSingularizer.java | 12 +++++++++++- .../singulars/EclipseJavaUtilMapSingularizer.java | 14 ++++++++++++++ .../singulars/EclipseJavaUtilSetSingularizer.java | 16 +++++++++++++++- .../lombok/javac/handlers/HandleSuperBuilder.java | 7 ++++--- .../lombok/javac/handlers/JavacSingularsRecipes.java | 2 ++ .../handlers/singulars/JavacGuavaSingularizer.java | 4 ++++ .../singulars/JavacJavaUtilListSingularizer.java | 4 ++++ .../singulars/JavacJavaUtilMapSingularizer.java | 6 ++++++ .../singulars/JavacJavaUtilSetSingularizer.java | 6 ++++++ 12 files changed, 89 insertions(+), 15 deletions(-) (limited to 'src/core/lombok') diff --git a/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java b/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java index 67a9f97d..a8a780d6 100755 --- a/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java +++ b/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java @@ -395,5 +395,8 @@ public class EclipseSingularsRecipes { return new SingleNameReference(builderVariable.toCharArray(), 0); } } + + protected abstract char[][] getEmptyMakerReceiver(String targetFqn); + protected abstract char[] getEmptyMakerSelector(String targetFqn); } } diff --git a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java index 554ab2f0..3a387688 100755 --- a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013-2018 The Project Lombok Authors. + * Copyright (C) 2013-2019 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 @@ -108,7 +108,6 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { private static final char[] TO_BUILDER_METHOD_NAME = TO_BUILDER_METHOD_NAME_STRING.toCharArray(); private static final char[] FILL_VALUES_METHOD_NAME = "$fillValuesFrom".toCharArray(); private static final char[] FILL_VALUES_STATIC_METHOD_NAME = "$fillValuesFromInstanceIntoBuilder".toCharArray(); - private static final char[] EMPTY_LIST = "emptyList".toCharArray(); private static final char[] INSTANCE_VARIABLE_NAME = "instance".toCharArray(); private static final String BUILDER_VARIABLE_NAME_STRING = "b"; private static final char[] BUILDER_VARIABLE_NAME = BUILDER_VARIABLE_NAME_STRING.toCharArray(); @@ -741,10 +740,10 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { ms.arguments = tgt; } else { Expression ifNull = new EqualExpression(tgt[0], new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL); - MessageSend emptyList = new MessageSend(); - emptyList.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.UTIL, "Collections".toCharArray()); - emptyList.selector = EMPTY_LIST; - ms.arguments = new Expression[] {new ConditionalExpression(ifNull, emptyList, tgt[1])}; + MessageSend emptyCollection = new MessageSend(); + emptyCollection.receiver = generateQualifiedNameRef(source, bfd.singularData.getSingularizer().getEmptyMakerReceiver(bfd.singularData.getTargetFqn())); + emptyCollection.selector = bfd.singularData.getSingularizer().getEmptyMakerSelector(bfd.singularData.getTargetFqn()); + ms.arguments = new Expression[] {new ConditionalExpression(ifNull, emptyCollection, tgt[1])}; } ms.receiver = new SingleNameReference(BUILDER_VARIABLE_NAME, 0); ms.selector = setterName; diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java index 54c059b8..642ba9b7 100755 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java @@ -63,6 +63,9 @@ import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; abstract class EclipseGuavaSingularizer extends EclipseSingularizer { + protected static final char[] OF = {'o', 'f'}; + protected static final char[][] CGCC = {{'c', 'o', 'm'}, {'g', 'o', 'o', 'g', 'l', 'e'}, {'c', 'o', 'm', 'm', 'o', 'n'}, {'c', 'o', 'l', 'l', 'e', 'c', 't'}}; + protected String getSimpleTargetTypeName(SingularData data) { return GuavaTypeMap.getGuavaTypeName(data.getTargetFqn()); } @@ -75,15 +78,23 @@ abstract class EclipseGuavaSingularizer extends EclipseSingularizer { protected char[][] makeGuavaTypeName(String simpleName, boolean addBuilder) { char[][] tokenizedName = new char[addBuilder ? 6 : 5][]; - tokenizedName[0] = new char[] {'c', 'o', 'm'}; - tokenizedName[1] = new char[] {'g', 'o', 'o', 'g', 'l', 'e'}; - tokenizedName[2] = new char[] {'c', 'o', 'm', 'm', 'o', 'n'}; - tokenizedName[3] = new char[] {'c', 'o', 'l', 'l', 'e', 'c', 't'}; + tokenizedName[0] = CGCC[0]; + tokenizedName[1] = CGCC[1]; + tokenizedName[2] = CGCC[2]; + tokenizedName[3] = CGCC[3]; tokenizedName[4] = simpleName.toCharArray(); if (addBuilder) tokenizedName[5] = new char[] { 'B', 'u', 'i', 'l', 'd', 'e', 'r'}; return tokenizedName; } + @Override protected char[] getEmptyMakerSelector(String targetFqn) { + return OF; + } + + @Override protected char[][] getEmptyMakerReceiver(String targetFqn) { + return CGCC; + } + @Override public List generateFields(SingularData data, EclipseNode builderType) { String simpleTypeName = getSimpleTargetTypeName(data); char[][] tokenizedName = makeGuavaTypeName(simpleTypeName, true); diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSingularizer.java index 80d49fe7..7ea04821 100755 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSingularizer.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 The Project Lombok Authors. + * Copyright (C) 2015-2019 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 @@ -55,6 +55,16 @@ public class EclipseJavaUtilListSingularizer extends EclipseJavaUtilListSetSingu return LombokImmutableList.of("java.util.List", "java.util.Collection", "java.lang.Iterable"); } + private static final char[] EMPTY_LIST = {'e', 'm', 'p', 't', 'y', 'L', 'i', 's', 't'}; + + @Override protected char[][] getEmptyMakerReceiver(String targetFqn) { + return JAVA_UTIL_COLLECTIONS; + } + + @Override protected char[] getEmptyMakerSelector(String targetFqn) { + return EMPTY_LIST; + } + @Override public void appendBuildCode(SingularData data, EclipseNode builderType, List statements, char[] targetVariableName, String builderVariable) { if (useGuavaInstead(builderType)) { guavaListSetSingularizer.appendBuildCode(data, builderType, statements, targetVariableName, builderVariable); diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java index 9ad1b6fe..b642522f 100755 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java @@ -68,6 +68,20 @@ public class EclipseJavaUtilMapSingularizer extends EclipseJavaUtilSingularizer return LombokImmutableList.of("java.util.Map", "java.util.SortedMap", "java.util.NavigableMap"); } + private static final char[] EMPTY_SORTED_MAP = {'e', 'm', 'p', 't', 'y', 'S', 'o', 'r', 't', 'e', 'd', 'M', 'a', 'p'}; + private static final char[] EMPTY_NAVIGABLE_MAP = {'e', 'm', 'p', 't', 'y', 'N', 'a', 'v', 'i', 'g', 'a', 'b', 'l', 'e', 'M', 'a', 'p'}; + private static final char[] EMPTY_MAP = {'e', 'm', 'p', 't', 'y', 'M', 'a', 'p'}; + + @Override protected char[][] getEmptyMakerReceiver(String targetFqn) { + return JAVA_UTIL_COLLECTIONS; + } + + @Override protected char[] getEmptyMakerSelector(String targetFqn) { + if (targetFqn.endsWith("SortedMap")) return EMPTY_SORTED_MAP; + if (targetFqn.endsWith("NavigableMap")) return EMPTY_NAVIGABLE_MAP; + return EMPTY_MAP; + } + @Override public List listFieldsToBeGenerated(SingularData data, EclipseNode builderType) { if (useGuavaInstead(builderType)) { return guavaMapSingularizer.listFieldsToBeGenerated(data, builderType); diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilSetSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilSetSingularizer.java index 200e615e..2076ec7d 100644 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilSetSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilSetSingularizer.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 The Project Lombok Authors. + * Copyright (C) 2015-2019 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 @@ -37,6 +37,20 @@ public class EclipseJavaUtilSetSingularizer extends EclipseJavaUtilListSetSingul return LombokImmutableList.of("java.util.Set", "java.util.SortedSet", "java.util.NavigableSet"); } + private static final char[] EMPTY_SORTED_SET = {'e', 'm', 'p', 't', 'y', 'S', 'o', 'r', 't', 'e', 'd', 'S', 'e', 't'}; + private static final char[] EMPTY_NAVIGABLE_SET = {'e', 'm', 'p', 't', 'y', 'N', 'a', 'v', 'i', 'g', 'a', 'b', 'l', 'e', 'S', 'e', 't'}; + private static final char[] EMPTY_SET = {'e', 'm', 'p', 't', 'y', 'S', 'e', 't'}; + + @Override protected char[][] getEmptyMakerReceiver(String targetFqn) { + return JAVA_UTIL_COLLECTIONS; + } + + @Override protected char[] getEmptyMakerSelector(String targetFqn) { + if (targetFqn.endsWith("SortedSet")) return EMPTY_SORTED_SET; + if (targetFqn.endsWith("NavigableSet")) return EMPTY_NAVIGABLE_SET; + return EMPTY_SET; + } + @Override public void appendBuildCode(SingularData data, EclipseNode builderType, List statements, char[] targetVariableName, String builderVariable) { if (useGuavaInstead(builderType)) { guavaListSetSingularizer.appendBuildCode(data, builderType, statements, targetVariableName, builderVariable); diff --git a/src/core/lombok/javac/handlers/HandleSuperBuilder.java b/src/core/lombok/javac/handlers/HandleSuperBuilder.java index 798ff7b4..290ef89f 100644 --- a/src/core/lombok/javac/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/javac/handlers/HandleSuperBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013-2018 The Project Lombok Authors. + * Copyright (C) 2013-2019 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 @@ -725,8 +725,9 @@ public class HandleSuperBuilder extends JavacAnnotationHandler { arg = tgt[0]; } else { JCExpression eqNull = maker.Binary(CTC_EQUAL, tgt[0], maker.Literal(CTC_BOT, null)); - JCExpression emptyList = maker.Apply(List.nil(), chainDots(type, "java", "util", "Collections", "emptyList"), List.nil()); - arg = maker.Conditional(eqNull, emptyList, tgt[1]); + String emptyMaker = bfd.singularData.getSingularizer().getEmptyMaker(bfd.singularData.getTargetFqn()); + JCExpression emptyCollection = maker.Apply(List.nil(), chainDots(type, emptyMaker.split("\\.")), List.nil()); + arg = maker.Conditional(eqNull, emptyCollection, tgt[1]); } JCMethodInvocation apply = maker.Apply(List.nil(), maker.Select(maker.Ident(type.toName(BUILDER_VARIABLE_NAME)), bfd.name), List.of(arg)); JCExpressionStatement exec = maker.Exec(apply); diff --git a/src/core/lombok/javac/handlers/JavacSingularsRecipes.java b/src/core/lombok/javac/handlers/JavacSingularsRecipes.java index a51079a8..a5895951 100644 --- a/src/core/lombok/javac/handlers/JavacSingularsRecipes.java +++ b/src/core/lombok/javac/handlers/JavacSingularsRecipes.java @@ -437,5 +437,7 @@ public class JavacSingularsRecipes { protected abstract String getAddMethodName(); protected abstract int getTypeArgumentsCount(); + + protected abstract String getEmptyMaker(String target); } } diff --git a/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java index c0dd7a42..5b022206 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java @@ -51,6 +51,10 @@ abstract class JavacGuavaSingularizer extends JavacSingularizer { return GuavaTypeMap.getGuavaTypeName(data.getTargetFqn()); } + @Override protected String getEmptyMaker(String target) { + return target + ".of"; + } + protected String getBuilderMethodName(SingularData data) { String simpleTypeName = getSimpleTargetTypeName(data); if ("ImmutableSortedSet".equals(simpleTypeName) || "ImmutableSortedMap".equals(simpleTypeName)) return "naturalOrder"; diff --git a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSingularizer.java index e5c5a170..b4ad3428 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSingularizer.java @@ -46,6 +46,10 @@ public class JavacJavaUtilListSingularizer extends JavacJavaUtilListSetSingulari return LombokImmutableList.of("java.util.List", "java.util.Collection", "java.lang.Iterable"); } + @Override protected String getEmptyMaker(String target) { + return "java.util.Collections.emptyList"; + } + @Override public void appendBuildCode(SingularData data, JavacNode builderType, JCTree source, ListBuffer statements, Name targetVariableName, String builderVariable) { JavacTreeMaker maker = builderType.getTreeMaker(); List jceBlank = List.nil(); diff --git a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilMapSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilMapSingularizer.java index 7b9ee653..3e498cac 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilMapSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilMapSingularizer.java @@ -54,6 +54,12 @@ public class JavacJavaUtilMapSingularizer extends JavacJavaUtilSingularizer { return LombokImmutableList.of("java.util.Map", "java.util.SortedMap", "java.util.NavigableMap"); } + @Override protected String getEmptyMaker(String target) { + if (target.endsWith("NavigableMap")) return "java.util.Collections.emptyNavigableMap"; + if (target.endsWith("SortedMap")) return "java.util.Collections.emptySortedMap"; + return "java.util.Collections.emptyMap"; + } + @Override protected JavacSingularizer getGuavaInstead(JavacNode node) { return new JavacGuavaMapSingularizer(); } diff --git a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilSetSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilSetSingularizer.java index 22d74896..7c870c0a 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilSetSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilSetSingularizer.java @@ -40,6 +40,12 @@ public class JavacJavaUtilSetSingularizer extends JavacJavaUtilListSetSingulariz return LombokImmutableList.of("java.util.Set", "java.util.SortedSet", "java.util.NavigableSet"); } + @Override protected String getEmptyMaker(String target) { + if (target.endsWith("SortedSet")) return "java.util.Collections.emptySortedSet"; + if (target.endsWith("NavigableSet")) return "java.util.Collections.emptyNavigableSet"; + return "java.util.Collections.emptySet"; + } + @Override public void appendBuildCode(SingularData data, JavacNode builderType, JCTree source, ListBuffer statements, Name targetVariableName, String builderVariable) { JavacTreeMaker maker = builderType.getTreeMaker(); -- cgit