[lombok] added support for @Accessors(makeFinal=true) from lombok 1.18.22

GitOrigin-RevId: c1069181c1e0fcf593a940d08f1a971043d503cb
This commit is contained in:
Michail Plushnikov
2022-10-26 19:25:41 +02:00
committed by intellij-monorepo-bot
parent 1c4104425c
commit 7c68d729fe
17 changed files with 97 additions and 37 deletions

View File

@@ -25,6 +25,7 @@ public class LombokConfigCompletionContributor extends CompletionContributor {
final Collection<String> booleanOptions = ContainerUtil.set(
ConfigKey.CONFIG_STOP_BUBBLING.getConfigKey(),
ConfigKey.ACCESSORS_CHAIN.getConfigKey(), ConfigKey.ACCESSORS_FLUENT.getConfigKey(),
ConfigKey.ACCESSORS_MAKE_FINAL.getConfigKey(),
ConfigKey.ANYCONSTRUCTOR_SUPPRESS_CONSTRUCTOR_PROPERTIES.getConfigKey(),
ConfigKey.ANYCONSTRUCTOR_ADD_CONSTRUCTOR_PROPERTIES.getConfigKey(),
ConfigKey.STANDARD_EXCEPTION_ADD_CONSTRUCTOR_PROPERTIES.getConfigKey(),

View File

@@ -25,6 +25,7 @@ public enum ConfigKey {
ACCESSORS_PREFIX("lombok.accessors.prefix", "", false),
ACCESSORS_CHAIN("lombok.accessors.chain", "false"),
ACCESSORS_FLUENT("lombok.accessors.fluent", "false"),
ACCESSORS_MAKE_FINAL("lombok.accessors.makeFinal", "false"),
GETTER_NO_IS_PREFIX("lombok.getter.noIsPrefix", "false"),
SINGULAR_USE_GUAVA("lombok.singular.useGuava", "false"),

View File

@@ -20,23 +20,25 @@ import java.util.Collections;
* @author Plushnikov Michail
*/
public class AccessorsInfo {
public static final AccessorsInfo EMPTY = new AccessorsInfo(false, false, false);
public static final AccessorsInfo EMPTY = new AccessorsInfo(false, false, false, false);
private final boolean fluent;
private final boolean chain;
private final boolean makeFinal;
private final String[] prefixes;
private final boolean doNotUseIsPrefix;
private AccessorsInfo(boolean fluentValue, boolean chainValue, boolean doNotUseIsPrefix, String... prefixes) {
private AccessorsInfo(boolean fluentValue, boolean chainValue, boolean makeFinal, boolean doNotUseIsPrefix, String... prefixes) {
this.fluent = fluentValue;
this.chain = chainValue;
this.makeFinal = makeFinal;
this.doNotUseIsPrefix = doNotUseIsPrefix;
this.prefixes = null == prefixes ? ArrayUtil.EMPTY_STRING_ARRAY : prefixes;
}
@NotNull
public static AccessorsInfo build(boolean fluentValue, boolean chainValue, boolean doNotUseIsPrefix, String... prefixes) {
return new AccessorsInfo(fluentValue, chainValue, doNotUseIsPrefix, prefixes);
public static AccessorsInfo build(boolean fluentValue, boolean chainValue, boolean makeFinal, boolean doNotUseIsPrefix, String... prefixes) {
return new AccessorsInfo(fluentValue, chainValue, makeFinal, doNotUseIsPrefix, prefixes);
}
@NotNull
@@ -49,7 +51,8 @@ public class AccessorsInfo {
final PsiAnnotation accessorsFieldAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiVariable, LombokClassNames.ACCESSORS);
if (null != accessorsFieldAnnotation) {
return buildFromAnnotation(accessorsFieldAnnotation, containingClass);
} else {
}
else {
return build(containingClass);
}
}
@@ -59,7 +62,8 @@ public class AccessorsInfo {
final PsiAnnotation accessorsFieldAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiField, LombokClassNames.ACCESSORS);
if (null != accessorsFieldAnnotation) {
return buildFromAnnotation(accessorsFieldAnnotation, psiField.getContainingClass());
} else {
}
else {
return classAccessorsInfo;
}
}
@@ -75,23 +79,27 @@ public class AccessorsInfo {
containingClass = containingClass.getContainingClass();
}
return buildAccessorsInfo(psiClass, null, null, Collections.emptySet());
return buildAccessorsInfo(psiClass, null, null, null, Collections.emptySet());
}
@NotNull
private static AccessorsInfo buildFromAnnotation(@NotNull PsiAnnotation accessorsAnnotation, @Nullable PsiClass psiClass) {
Boolean chainDeclaredValue = PsiAnnotationUtil.getDeclaredBooleanAnnotationValue(accessorsAnnotation, "chain");
Boolean fluentDeclaredValue = PsiAnnotationUtil.getDeclaredBooleanAnnotationValue(accessorsAnnotation, "fluent");
Boolean makeFinal = PsiAnnotationUtil.getDeclaredBooleanAnnotationValue(accessorsAnnotation, "makeFinal");
Collection<String> prefixes = PsiAnnotationUtil.getAnnotationValues(accessorsAnnotation, "prefix", String.class);
return buildAccessorsInfo(psiClass, chainDeclaredValue, fluentDeclaredValue, prefixes);
return buildAccessorsInfo(psiClass, chainDeclaredValue, fluentDeclaredValue, makeFinal, prefixes);
}
@NotNull
private static AccessorsInfo buildAccessorsInfo(@Nullable PsiClass psiClass, @Nullable Boolean chainDeclaredValue,
@Nullable Boolean fluentDeclaredValue, @NotNull Collection<String> prefixDeclared) {
@Nullable Boolean fluentDeclaredValue,
@Nullable Boolean makeFinalDeclaredValue,
@NotNull Collection<String> prefixDeclared) {
final boolean isFluent;
final boolean isChained;
final boolean makeFinal;
final boolean doNotUseIsPrefix;
final String[] prefixes;
@@ -99,33 +107,44 @@ public class AccessorsInfo {
final ConfigDiscovery configDiscovery = ConfigDiscovery.getInstance();
if (null == fluentDeclaredValue) {
isFluent = configDiscovery.getBooleanLombokConfigProperty(ConfigKey.ACCESSORS_FLUENT, psiClass);
} else {
}
else {
isFluent = fluentDeclaredValue;
}
if (null == chainDeclaredValue) {
isChained = configDiscovery.getBooleanLombokConfigProperty(ConfigKey.ACCESSORS_CHAIN, psiClass);
} else {
}
else {
isChained = chainDeclaredValue;
}
if (null == makeFinalDeclaredValue) {
makeFinal = configDiscovery.getBooleanLombokConfigProperty(ConfigKey.ACCESSORS_MAKE_FINAL, psiClass);
}
else {
makeFinal = makeFinalDeclaredValue;
}
if (prefixDeclared.isEmpty()) {
prefixes = ArrayUtil.toStringArray(configDiscovery.getMultipleValueLombokConfigProperty(ConfigKey.ACCESSORS_PREFIX, psiClass));
} else {
}
else {
prefixes = ArrayUtil.toStringArray(prefixDeclared);
}
doNotUseIsPrefix = configDiscovery.getBooleanLombokConfigProperty(ConfigKey.GETTER_NO_IS_PREFIX, psiClass);
} else {
}
else {
isFluent = null != fluentDeclaredValue && fluentDeclaredValue;
isChained = null != chainDeclaredValue && chainDeclaredValue;
makeFinal = null != makeFinalDeclaredValue && makeFinalDeclaredValue;
prefixes = ArrayUtil.toStringArray(prefixDeclared);
doNotUseIsPrefix = false;
}
boolean isChainDeclaredOrImplicit = isChained || (isFluent && null == chainDeclaredValue);
return new AccessorsInfo(isFluent, isChainDeclaredOrImplicit, doNotUseIsPrefix, prefixes);
return build(isFluent, isChainDeclaredOrImplicit, makeFinal, doNotUseIsPrefix, prefixes);
}
public boolean isFluent() {
@@ -136,13 +155,17 @@ public class AccessorsInfo {
if (fluent == fluentValue) {
return this;
}
return new AccessorsInfo(fluentValue, chain, doNotUseIsPrefix, prefixes);
return build(fluentValue, chain, makeFinal, doNotUseIsPrefix, prefixes);
}
public boolean isChain() {
return chain;
}
public boolean isMakeFinal() {
return makeFinal;
}
public boolean isDoNotUseIsPrefix() {
return doNotUseIsPrefix;
}
@@ -177,8 +200,8 @@ public class AccessorsInfo {
final int prefixLength = prefix.length();
// we can use digits and upper case letters after a prefix, but not lower case letters
return prefixLength == 0 ||
fieldName.startsWith(prefix) && fieldName.length() > prefixLength &&
(!Character.isLetter(prefix.charAt(prefix.length() - 1)) || !Character.isLowerCase(fieldName.charAt(prefixLength)));
fieldName.startsWith(prefix) && fieldName.length() > prefixLength &&
(!Character.isLetter(prefix.charAt(prefix.length() - 1)) || !Character.isLowerCase(fieldName.charAt(prefixLength)));
}
private static String decapitalizeLikeLombok(String name) {

View File

@@ -93,7 +93,8 @@ public final class GetterFieldProcessor extends AbstractFieldProcessor {
@NotNull
public PsiMethod createGetterMethod(@NotNull PsiField psiField, @NotNull PsiClass psiClass, @NotNull String methodModifier) {
final String methodName = LombokUtils.getGetterName(psiField);
final AccessorsInfo accessorsInfo = AccessorsInfo.build(psiField);
final String methodName = LombokUtils.getGetterName(psiField, accessorsInfo);
LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiField.getManager(), methodName)
.withMethodReturnType(psiField.getType())
@@ -107,6 +108,9 @@ public final class GetterFieldProcessor extends AbstractFieldProcessor {
if (isStatic) {
methodBuilder.withModifier(PsiModifier.STATIC);
}
if(accessorsInfo.isMakeFinal()) {
methodBuilder.withModifier(PsiModifier.FINAL);
}
final String blockText = String.format("return %s.%s;", isStatic ? psiClass.getName() : "this", psiField.getName());
methodBuilder.withBodyText(blockText);

View File

@@ -97,7 +97,8 @@ public final class SetterFieldProcessor extends AbstractFieldProcessor {
final PsiType psiFieldType = psiField.getType();
final PsiAnnotation setterAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiField, LombokClassNames.SETTER);
final String methodName = LombokUtils.getSetterName(psiField);
final AccessorsInfo accessorsInfo = AccessorsInfo.build(psiField);
final String methodName = LombokUtils.getSetterName(psiField, accessorsInfo);
PsiType returnType = getReturnType(psiField);
LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiField.getManager(), methodName)
@@ -113,6 +114,9 @@ public final class SetterFieldProcessor extends AbstractFieldProcessor {
if (isStatic) {
methodBuilder.withModifier(PsiModifier.STATIC);
}
if(accessorsInfo.isMakeFinal()) {
methodBuilder.withModifier(PsiModifier.FINAL);
}
LombokLightParameter setterParameter = methodBuilder.getParameterList().getParameter(0);
if(null!=setterParameter) {

View File

@@ -181,6 +181,10 @@ public final class WitherFieldProcessor extends AbstractFieldProcessor {
.withModifier(methodModifier)
.withContract("pure = true");
if(accessorsInfo.isMakeFinal()) {
methodBuilder.withModifier(PsiModifier.FINAL);
}
PsiAnnotation witherAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiField, LombokClassNames.WITHER, LombokClassNames.WITH);
copyOnXAnnotations(witherAnnotation, methodBuilder.getModifierList(), "onMethod");

View File

@@ -279,7 +279,10 @@ public final class LombokUtils {
public static String getGetterName(final @NotNull PsiField psiField) {
final AccessorsInfo accessorsInfo = AccessorsInfo.build(psiField);
return getGetterName(psiField, accessorsInfo);
}
public static String getGetterName(@NotNull PsiField psiField, AccessorsInfo accessorsInfo) {
final String psiFieldName = psiField.getName();
final boolean isBoolean = PsiType.BOOLEAN.equals(psiField.getType());
@@ -287,12 +290,12 @@ public final class LombokUtils {
}
public static String getSetterName(@NotNull PsiField psiField) {
return getSetterName(psiField, PsiType.BOOLEAN.equals(psiField.getType()));
final AccessorsInfo accessorsInfo = AccessorsInfo.build(psiField);
return getSetterName(psiField, accessorsInfo);
}
public static String getSetterName(@NotNull PsiField psiField, boolean isBoolean) {
final AccessorsInfo accessorsInfo = AccessorsInfo.build(psiField);
return toSetterName(accessorsInfo, psiField.getName(), isBoolean);
public static String getSetterName(@NotNull PsiField psiField, AccessorsInfo accessorsInfo) {
return toSetterName(accessorsInfo, psiField.getName(), PsiType.BOOLEAN.equals(psiField.getType()));
}
/**

View File

@@ -10,4 +10,8 @@ public class AccessorsTest extends AbstractLombokParsingTestCase {
public void testAccessors$Accessors() {
doTest(true);
}
public void testAccessors$AccessorsMakeFinal() {
doTest(true);
}
}

View File

@@ -9,11 +9,11 @@ import java.util.Collections;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
public class LombokUtilsAllGetterTest {
private static final AccessorsInfo DEFAULT_ACCESSORS = AccessorsInfo.build(false, false, false);
private static final AccessorsInfo DEFAULT_ACCESSORS = AccessorsInfo.build(false, false, false, false);
private final List<String> lombokResult = new ArrayList<>();
private final List<String> result = new ArrayList<>();
@@ -86,7 +86,7 @@ public class LombokUtilsAllGetterTest {
@Test
public void testToAllGetterNames_NonBoolean_Fluent() {
makeResults("myField", false, AccessorsInfo.build(true, false, false));
makeResults("myField", false, AccessorsInfo.build(true, false, false, false));
assertThat(result, is(Collections.singletonList("myField")));
}

View File

@@ -9,7 +9,7 @@ import java.util.Collections;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
public class LombokUtilsAllSetterTest {
@@ -20,7 +20,7 @@ public class LombokUtilsAllSetterTest {
lombokResult.clear();
result.clear();
final AccessorsInfo accessorsInfo = AccessorsInfo.build(false, false, false);
final AccessorsInfo accessorsInfo = AccessorsInfo.build(false, false, false, false);
lombokResult.addAll(LombokHandlerUtil.toAllSetterNames(accessorsInfo, fieldName, isBoolean));
result.addAll(LombokUtils.toAllSetterNames(accessorsInfo, fieldName, isBoolean));

View File

@@ -9,7 +9,7 @@ import java.util.Collections;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
public class LombokUtilsAllWitherTest {

View File

@@ -5,11 +5,11 @@ import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
public class LombokUtilsGetterTest {
private static final AccessorsInfo DEFAULT_ACCESSORS = AccessorsInfo.build(false, false, false);
private static final AccessorsInfo DEFAULT_ACCESSORS = AccessorsInfo.build(false, false, false, false);
private static String makeResults(String fieldName, boolean isBoolean) {
String lombokResult = LombokHandlerUtil.toGetterName(DEFAULT_ACCESSORS, fieldName, isBoolean);

View File

@@ -5,11 +5,11 @@ import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
public class LombokUtilsPrefixedFluentTest {
private static final AccessorsInfo DEFAULT_ACCESSORS = AccessorsInfo.build(true, false, false, "m", "");
private static final AccessorsInfo DEFAULT_ACCESSORS = AccessorsInfo.build(true, false, false, false, "m", "");
private static String makeResults(String fieldName, boolean isBoolean) {
String lombokResult = LombokHandlerUtil.toGetterName(DEFAULT_ACCESSORS, fieldName, isBoolean);

View File

@@ -5,10 +5,10 @@ import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
public class LombokUtilsSetterTest {
private static final AccessorsInfo DEFAULT_ACCESSORS = AccessorsInfo.build(false, false, false);
private static final AccessorsInfo DEFAULT_ACCESSORS = AccessorsInfo.build(false, false, false, false);
private static String makeResults(String fieldName, boolean isBoolean) {
String lombokResult = LombokHandlerUtil.toSetterName(DEFAULT_ACCESSORS, fieldName, isBoolean);

View File

@@ -5,7 +5,7 @@ import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
public class LombokUtilsWitherTest {

View File

@@ -0,0 +1,11 @@
class AccessorsMakeFinal {
private String test;
/**
* @return {@code this}.
*/
@SuppressWarnings("all")
public final AccessorsMakeFinal test(final String test) {
this.test = test;
return this;
}
}

View File

@@ -0,0 +1,5 @@
class AccessorsMakeFinal {
@lombok.Setter
@lombok.experimental.Accessors(fluent = true, makeFinal = true)
private String test;
}