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