diff --git a/plugins/lombok/lombok-compiler-plugin/README.md b/plugins/lombok/lombok-compiler-plugin/README.md index 6cb5ef691a2..fdd84e2e55e 100644 --- a/plugins/lombok/lombok-compiler-plugin/README.md +++ b/plugins/lombok/lombok-compiler-plugin/README.md @@ -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 diff --git a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/processor/GetterProcessor.kt b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/processor/GetterProcessor.kt index 85e7bc1def9..2149bce2cbd 100644 --- a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/processor/GetterProcessor.kt +++ b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/processor/GetterProcessor.kt @@ -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 + ) + } } } diff --git a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/processor/SetterProcessor.kt b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/processor/SetterProcessor.kt index 80c6d4ef85f..283c51fac0d 100644 --- a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/processor/SetterProcessor.kt +++ b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/processor/SetterProcessor.kt @@ -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 + ) + } } } diff --git a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/utils/nameUtils.kt b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/utils/nameUtils.kt index fbd0424d4c4..67969479470 100644 --- a/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/utils/nameUtils.kt +++ b/plugins/lombok/lombok-compiler-plugin/src/org/jetbrains/kotlin/lombok/utils/nameUtils.kt @@ -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 = emptyList()): String = - name.stripPrefixes(prefixesToStrip) + name.stripPrefixes(prefixesToStrip).decapitalize() fun toPropertyNameCapitalized(name: String, prefixesToStrip: List = emptyList()): String = name.stripPrefixes(prefixesToStrip).capitalize() diff --git a/plugins/lombok/lombok-compiler-plugin/testData/compile/accessorsStripPrefix.kt b/plugins/lombok/lombok-compiler-plugin/testData/compile/accessorsStripPrefix.kt index 973089f42f7..0b7234a550d 100644 --- a/plugins/lombok/lombok-compiler-plugin/testData/compile/accessorsStripPrefix.kt +++ b/plugins/lombok/lombok-compiler-plugin/testData/compile/accessorsStripPrefix.kt @@ -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) + } }