[Lombok] Reorganize module structure of Lombok compiler plugin

Also rename its jar to `kotlin-lombok-compiler-plugin`

^KT-52468 Fixed
This commit is contained in:
Dmitriy Novozhilov
2022-05-23 13:24:37 +03:00
committed by teamcity
parent bfb908dcd9
commit c2bf68c9d3
68 changed files with 175 additions and 77 deletions
@@ -0,0 +1,20 @@
description = "Lombok compiler plugin (Common)"
plugins {
kotlin("jvm")
id("jps-compatible")
}
dependencies {
compileOnly(project(":core:compiler.common"))
compileOnly(project(":core:compiler.common.jvm"))
}
sourceSets {
"main" { projectDefault() }
"test" { none() }
}
runtimeJar()
sourcesJar()
javadocJar()
@@ -0,0 +1,31 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.lombok.config
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.descriptors.java.JavaVisibilities
enum class AccessLevel {
PUBLIC, MODULE, PROTECTED, PACKAGE, PRIVATE,
/** Represents not generating anything or the complete lack of a method. */
NONE;
fun toVisibility(): Visibility = toVisibility(this)
companion object {
private fun toVisibility(v: AccessLevel): Visibility {
return when (v) {
PUBLIC -> Visibilities.Public
PROTECTED -> Visibilities.Protected
PACKAGE, MODULE -> JavaVisibilities.PackageVisibility
PRIVATE -> Visibilities.Private
NONE -> Visibilities.Private
}
}
}
}
@@ -0,0 +1,93 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.lombok.config
import java.io.File
class LombokConfig(private val config: Map<String, List<String>>) {
fun getString(key: String): String? = getValue(key)?.firstOrNull()
fun getBoolean(key: String): Boolean? = getString(key)?.toBoolean()
fun getMultiString(key: String): List<String>? = getValue(key)
private fun getValue(key: String): List<String>? = config[normalizeKey(key)]
companion object {
val Empty = LombokConfig(emptyMap())
fun parse(path: File): LombokConfig = ConfigParser.parse(path)
}
}
/**
* Simplified Lombok config parser.
* Ignores everything it doesn't understand
*/
object ConfigParser {
//regex is from lombok source code
private val LINE = "(?:clear\\s+([^=]+))|(?:(\\S*?)\\s*([-+]?=)\\s*(.*?))".toRegex()
fun parse(path: File): LombokConfig {
val builder = ConfigBuilder()
path.forEachLine { parseLine(it, builder) }
return builder.build()
}
private fun parseLine(line: String, builder: ConfigBuilder) {
LINE.matchEntire(line)?.let { matchResult ->
if (matchResult.groups[1] == null) {
val keyName = matchResult.groupValues[2]
val operator = matchResult.groupValues[3]
val stringValue = matchResult.groupValues[4]
when (operator) {
"=" -> builder.setValue(keyName, stringValue)
"+=" -> builder.plusValue(keyName, stringValue)
"-=" -> builder.minusValue(keyName, stringValue)
else -> {
//do nothing
}
}
} else {
//clear
val keyName = matchResult.groupValues[1]
builder.clearValue(keyName)
}
}
}
}
class ConfigBuilder {
private val state: MutableMap<String, List<String>> = mutableMapOf()
fun setValue(name: String, value: String) {
state[normalizeKey(name)] = listOf(value)
}
fun clearValue(name: String) {
state.remove(normalizeKey(name))
}
fun plusValue(name: String, value: String) {
state.merge(normalizeKey(name), listOf(value)) { a, b -> a + b }
}
fun minusValue(name: String, value: String) {
state.merge(normalizeKey(name), listOf(value)) { a, b -> a - b }
}
fun build(): LombokConfig = LombokConfig(state)
}
//lombok config keys are case insensitive
private fun normalizeKey(key: String): String = key.lowercase()
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.lombok.utils
import org.jetbrains.kotlin.name.FqName
object LombokNames {
val ACCESSORS = FqName("lombok.experimental.Accessors")
val GETTER = FqName("lombok.Getter")
val SETTER = FqName("lombok.Setter")
val WITH = FqName("lombok.With")
val DATA = FqName("lombok.Data")
val VALUE = FqName("lombok.Value")
val NO_ARGS_CONSTRUCTOR = FqName("lombok.NoArgsConstructor")
val ALL_ARGS_CONSTRUCTOR = FqName("lombok.AllArgsConstructor")
val REQUIRED_ARGS_CONSTRUCTOR = FqName("lombok.RequiredArgsConstructor")
//taken from idea lombok plugin
val NON_NULL_ANNOTATIONS = listOf(
"androidx.annotation.NonNull",
"android.support.annotation.NonNull",
"com.sun.istack.internal.NotNull",
"edu.umd.cs.findbugs.annotations.NonNull",
"javax.annotation.Nonnull",
"lombok.NonNull",
"org.checkerframework.checker.nullness.qual.NonNull",
"org.eclipse.jdt.annotation.NonNull",
"org.eclipse.jgit.annotations.NonNull",
"org.jetbrains.annotations.NotNull",
"org.jmlspecs.annotation.NonNull",
"org.netbeans.api.annotations.common.NonNull",
"org.springframework.lang.NonNull"
).map { FqName(it) }.toSet()
}
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.lombok.utils
import java.util.*
@Suppress("UNCHECKED_CAST")
fun <E, R> Collection<E>.collectWithNotNull(f: (E) -> R?): List<Pair<E, R>> =
map { it to f(it) }.filter { it.second != null } as List<Pair<E, R>>
fun String?.trimToNull(): String? = this?.trim()?.takeIf { it.isNotEmpty() }
//because capitalize from stdlib is deprecated
fun String.capitalize(): String = replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
fun String.decapitalize(): String = replaceFirstChar { if (it.isUpperCase()) it.lowercase(Locale.getDefault()) else it.toString() }