[Lombok] Constructor can have only non-static fields
When using the AllArgsConstructor annotation (directly or via meta-annotation), only fields that are not static should be added as arguments. Previously, all fields were being included regardless of static-ness, which is not consistent with the behavior of Lombok. ^KT-54025 Fixed
This commit is contained in:
+3
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.lombok.k2.generators
|
||||
|
||||
import com.intellij.psi.PsiField
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.isStatic
|
||||
import org.jetbrains.kotlin.fir.java.declarations.FirJavaField
|
||||
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
|
||||
@@ -27,6 +28,8 @@ class AllArgsConstructorGeneratorPart(session: FirSession) : AbstractConstructor
|
||||
}
|
||||
|
||||
private fun FirJavaField.isFieldAllowed(): Boolean {
|
||||
if (isStatic) return false
|
||||
|
||||
// TODO: consider adding `hasInitializer` property directly to java model
|
||||
val hasInitializer = (source?.psi as? PsiField)?.hasInitializer() ?: false
|
||||
return isVar || !hasInitializer
|
||||
|
||||
+3
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.lombok.k2.generators
|
||||
|
||||
import com.intellij.psi.PsiField
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.isStatic
|
||||
import org.jetbrains.kotlin.fir.expressions.unexpandedClassId
|
||||
import org.jetbrains.kotlin.fir.java.declarations.FirJavaField
|
||||
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
|
||||
@@ -29,6 +30,8 @@ class RequiredArgsConstructorGeneratorPart(session: FirSession) : AbstractConstr
|
||||
}
|
||||
|
||||
private fun FirJavaField.isFieldRequired(): Boolean {
|
||||
if (isStatic) return false
|
||||
|
||||
// TODO: consider adding `hasInitializer` property directly to java model
|
||||
val hasInitializer = (source?.psi as? PsiField)?.hasInitializer() ?: false
|
||||
if (hasInitializer) return false
|
||||
|
||||
@@ -17,6 +17,9 @@ public class ConstructorExample {
|
||||
// Not part of constructor because final and initialized.
|
||||
@Getter private final String result = "OK";
|
||||
|
||||
// Not part of constructor because static.
|
||||
private static String constant;
|
||||
|
||||
static void javaUsage() {
|
||||
val generated = new ConstructorExample(12, "sdf", true);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@ public class ConstructorExample {
|
||||
// Not part of constructor because final and initialized.
|
||||
@Getter private final String result = "OK";
|
||||
|
||||
// Not part of constructor because static.
|
||||
private static String constant;
|
||||
|
||||
public ConstructorExample(String arg) {
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,9 @@ public class ConstructorExample {
|
||||
@NonNull
|
||||
private Long zzzz = 23L;
|
||||
|
||||
// Not required by annotation because static.
|
||||
private static String constant;
|
||||
|
||||
static void javaUsage() {
|
||||
ConstructorExample generated = new ConstructorExample("foo", true);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ public class ConstructorExample {
|
||||
|
||||
@NonNull Integer somethingElse;
|
||||
|
||||
// Not required by annotation because static.
|
||||
private static String constant;
|
||||
|
||||
static void javaUsage() {
|
||||
ConstructorExample generated = ConstructorExample.build("foo", true, 12);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user