[lombok] Strip prefixes defined in @Accessors
This commit is contained in:
committed by
TeamCityServer
parent
f2c0b8c68d
commit
17e4a6142c
@@ -21,7 +21,7 @@ Features support:
|
|||||||
- [ ] lombok.noArgsConstructor.extraPrivate
|
- [ ] lombok.noArgsConstructor.extraPrivate
|
||||||
- [ ] lombok.copyableAnnotations
|
- [ ] lombok.copyableAnnotations
|
||||||
- [ ] Copy annotations
|
- [ ] Copy annotations
|
||||||
- [ ] Strip defined prefixes - in config and @Accessors
|
- [~] Strip defined prefixes - in config and @Accessors
|
||||||
- [ ] Skip generation with AccessLevel.NONE
|
- [ ] Skip generation with AccessLevel.NONE
|
||||||
- [x] Strip 'is' prefix for boolean fields
|
- [x] Strip 'is' prefix for boolean fields
|
||||||
|
|
||||||
|
|||||||
+15
-14
@@ -37,20 +37,21 @@ class GetterProcessor(private val config: LombokConfig) : Processor {
|
|||||||
globalAccessors: Accessors
|
globalAccessors: Accessors
|
||||||
): SimpleFunctionDescriptor? {
|
): SimpleFunctionDescriptor? {
|
||||||
val accessors = Accessors.getIfAnnotated(field, config) ?: globalAccessors
|
val accessors = Accessors.getIfAnnotated(field, config) ?: globalAccessors
|
||||||
val propertyName = field.toPropertyName(accessors)
|
return field.toPropertyName(accessors)?.let { propertyName ->
|
||||||
val functionName =
|
val functionName =
|
||||||
if (accessors.fluent) {
|
if (accessors.fluent) {
|
||||||
propertyName
|
propertyName
|
||||||
} else {
|
} else {
|
||||||
val prefix = if (field.type.isPrimitiveBoolean() && !accessors.noIsPrefix) AccessorNames.IS else AccessorNames.GET
|
val prefix = if (field.type.isPrimitiveBoolean() && !accessors.noIsPrefix) AccessorNames.IS else AccessorNames.GET
|
||||||
prefix + propertyName.capitalize()
|
prefix + propertyName.capitalize()
|
||||||
}
|
}
|
||||||
return classDescriptor.createFunction(
|
classDescriptor.createFunction(
|
||||||
Name.identifier(functionName),
|
Name.identifier(functionName),
|
||||||
emptyList(),
|
emptyList(),
|
||||||
field.returnType,
|
field.returnType,
|
||||||
visibility = getter.visibility
|
visibility = getter.visibility
|
||||||
)
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-12
@@ -38,19 +38,19 @@ class SetterProcessor(private val config: LombokConfig) : Processor {
|
|||||||
globalAccessors: Accessors
|
globalAccessors: Accessors
|
||||||
): SimpleFunctionDescriptor? {
|
): SimpleFunctionDescriptor? {
|
||||||
val accessors = Accessors.getIfAnnotated(field, config) ?: globalAccessors
|
val accessors = Accessors.getIfAnnotated(field, config) ?: globalAccessors
|
||||||
val propertyName = field.toPropertyName(accessors)
|
return field.toPropertyName(accessors)?.let { propertyName ->
|
||||||
|
val functionName =
|
||||||
|
if (accessors.fluent) propertyName
|
||||||
|
else AccessorNames.SET + propertyName.capitalize()
|
||||||
|
|
||||||
val functionName =
|
val returnType = if (accessors.chain) classDescriptor.defaultType else classDescriptor.builtIns.unitType
|
||||||
if (accessors.fluent) propertyName
|
|
||||||
else AccessorNames.SET + propertyName.capitalize()
|
|
||||||
|
|
||||||
val returnType = if (accessors.chain) classDescriptor.defaultType else classDescriptor.builtIns.unitType
|
classDescriptor.createFunction(
|
||||||
|
Name.identifier(functionName),
|
||||||
return classDescriptor.createFunction(
|
listOf(ValueParameter(field.name, field.type)),
|
||||||
Name.identifier(functionName),
|
returnType,
|
||||||
listOf(ValueParameter(field.name, field.type)),
|
visibility = getter.visibility
|
||||||
returnType,
|
)
|
||||||
visibility = getter.visibility
|
}
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-4
@@ -14,14 +14,20 @@ object AccessorNames {
|
|||||||
const val SET = "set"
|
const val SET = "set"
|
||||||
}
|
}
|
||||||
|
|
||||||
fun PropertyDescriptor.toPropertyName(config: Accessors): String {
|
fun PropertyDescriptor.toPropertyName(config: Accessors): String? {
|
||||||
val isPrimitiveBoolean = type.isPrimitiveBoolean()
|
val isPrimitiveBoolean = type.isPrimitiveBoolean()
|
||||||
val prefixes = if (isPrimitiveBoolean) config.prefix + AccessorNames.IS else config.prefix
|
return if (config.prefix.isEmpty()) {
|
||||||
return toPropertyName(name.identifier, prefixes)
|
val prefixes = if (isPrimitiveBoolean) listOf(AccessorNames.IS) else emptyList()
|
||||||
|
toPropertyName(name.identifier, prefixes)
|
||||||
|
} else {
|
||||||
|
val id = name.identifier
|
||||||
|
val name = toPropertyName(id, config.prefix)
|
||||||
|
name.takeIf { it.length != id.length}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun toPropertyName(name: String, prefixesToStrip: List<String> = emptyList()): String =
|
fun toPropertyName(name: String, prefixesToStrip: List<String> = emptyList()): String =
|
||||||
name.stripPrefixes(prefixesToStrip)
|
name.stripPrefixes(prefixesToStrip).decapitalize()
|
||||||
|
|
||||||
fun toPropertyNameCapitalized(name: String, prefixesToStrip: List<String> = emptyList()): String =
|
fun toPropertyNameCapitalized(name: String, prefixesToStrip: List<String> = emptyList()): String =
|
||||||
name.stripPrefixes(prefixesToStrip).capitalize()
|
name.stripPrefixes(prefixesToStrip).capitalize()
|
||||||
|
|||||||
+31
-2
@@ -4,20 +4,37 @@ import lombok.*;
|
|||||||
import lombok.experimental.*;
|
import lombok.experimental.*;
|
||||||
|
|
||||||
@Getter @Setter
|
@Getter @Setter
|
||||||
|
@Accessors(prefix = {"f", "field"})
|
||||||
public class AccessorsTest {
|
public class AccessorsTest {
|
||||||
|
@Accessors
|
||||||
private int age = 10;
|
private int age = 10;
|
||||||
|
private int fTarget = 42;
|
||||||
|
private String fieldValue;
|
||||||
|
|
||||||
|
@Accessors
|
||||||
private boolean isHuman;
|
private boolean isHuman;
|
||||||
|
private boolean fPrefixedBoolean;
|
||||||
|
@Accessors
|
||||||
private Boolean isNonPrimitiveHuman;
|
private Boolean isNonPrimitiveHuman;
|
||||||
|
|
||||||
static void test() {
|
static void test() {
|
||||||
val obj = new AccessorsTest();
|
val obj = new AccessorsTest();
|
||||||
|
|
||||||
obj.getAge();
|
// obj.getAge();
|
||||||
|
// obj.setAge(123);
|
||||||
|
|
||||||
|
obj.getTarget();
|
||||||
|
obj.setTarget(34);
|
||||||
|
|
||||||
|
obj.getValue();
|
||||||
|
obj.setValue("sdf");
|
||||||
|
|
||||||
obj.isHuman();
|
obj.isHuman();
|
||||||
obj.setHuman(true);
|
obj.setHuman(true);
|
||||||
|
|
||||||
|
obj.isPrefixedBoolean();
|
||||||
|
obj.setPrefixedBoolean(false);
|
||||||
|
|
||||||
obj.getIsNonPrimitiveHuman();
|
obj.getIsNonPrimitiveHuman();
|
||||||
obj.setIsNonPrimitiveHuman(false);
|
obj.setIsNonPrimitiveHuman(false);
|
||||||
}
|
}
|
||||||
@@ -32,11 +49,23 @@ class Test {
|
|||||||
val obj = AccessorsTest()
|
val obj = AccessorsTest()
|
||||||
|
|
||||||
obj.getAge()
|
obj.getAge()
|
||||||
val age = obj.age
|
obj.setAge(123)
|
||||||
|
|
||||||
|
obj.getTarget()
|
||||||
|
obj.setTarget(34)
|
||||||
|
|
||||||
|
obj.getValue()
|
||||||
|
obj.setValue("sdf")
|
||||||
|
|
||||||
obj.isHuman()
|
obj.isHuman()
|
||||||
obj.setHuman(true)
|
obj.setHuman(true)
|
||||||
|
|
||||||
|
obj.isPrefixedBoolean()
|
||||||
|
obj.setPrefixedBoolean(false)
|
||||||
|
|
||||||
|
obj.getIsNonPrimitiveHuman()
|
||||||
|
obj.setIsNonPrimitiveHuman(false)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user