J2K: updating generated enums to modern syntax
This commit is contained in:
@@ -93,7 +93,7 @@ class ClassBodyConverter(private val psiClass: PsiClass,
|
||||
}
|
||||
}
|
||||
|
||||
return ClassBody(null, null, convertedMembers.values().toList(), emptyList(), lBrace, rBrace)
|
||||
return ClassBody(null, null, convertedMembers.values().toList(), emptyList(), lBrace, rBrace, false)
|
||||
}
|
||||
|
||||
val useCompanionObject = shouldGenerateCompanionObject(convertedMembers)
|
||||
@@ -120,15 +120,15 @@ class ClassBodyConverter(private val psiClass: PsiClass,
|
||||
}
|
||||
|
||||
if (primaryConstructorSignature != null
|
||||
&& primaryConstructorSignature!!.annotations.isEmpty
|
||||
&& primaryConstructorSignature!!.accessModifier == null
|
||||
&& primaryConstructorSignature!!.parameterList.parameters.isEmpty()
|
||||
&& primaryConstructorSignature.annotations.isEmpty
|
||||
&& primaryConstructorSignature.accessModifier == null
|
||||
&& primaryConstructorSignature.parameterList.parameters.isEmpty()
|
||||
&& members.none { it is SecondaryConstructor }
|
||||
) {
|
||||
primaryConstructorSignature = null // no "()" after class name is needed in this case
|
||||
}
|
||||
|
||||
return ClassBody(primaryConstructorSignature, constructorConverter?.baseClassParams, members, companionObjectMembers, lBrace, rBrace)
|
||||
return ClassBody(primaryConstructorSignature, constructorConverter?.baseClassParams, members, companionObjectMembers, lBrace, rBrace, psiClass.isEnum())
|
||||
}
|
||||
|
||||
private fun Converter.convertMember(member: PsiMember,
|
||||
|
||||
@@ -278,7 +278,7 @@ class Converter private(
|
||||
// to convert fields and nested types - they are not allowed in Kotlin but we convert them and let user refactor code
|
||||
var classBody = ClassBodyConverter(psiClass, this, isOpenClass = false, isObject = false).convertBody()
|
||||
classBody = ClassBody(constructorSignature, classBody.baseClassParams, classBody.members,
|
||||
classBody.companionObjectMembers, classBody.lBrace, classBody.rBrace)
|
||||
classBody.companionObjectMembers, classBody.lBrace, classBody.rBrace, classBody.isEnumBody)
|
||||
|
||||
val annotationAnnotation = Annotation(Identifier("annotation").assignNoPrototype(), listOf(), false, false).assignNoPrototype()
|
||||
return Class(psiClass.declarationIdentifier(),
|
||||
@@ -310,8 +310,7 @@ class Converter private(
|
||||
EnumConstant(name,
|
||||
annotations,
|
||||
modifiers,
|
||||
typeConverter.convertType(field.getType(), Nullability.NotNull),
|
||||
deferredElement { codeConverter -> ExpressionList(codeConverter.convertExpressions(argumentList?.getExpressions() ?: array<PsiExpression>())).assignPrototype(argumentList) })
|
||||
deferredElement { codeConverter -> ExpressionList(codeConverter.convertExpressions(argumentList?.getExpressions() ?: arrayOf<PsiExpression>())).assignPrototype(argumentList) })
|
||||
}
|
||||
else {
|
||||
val isVal = isVal(referenceSearcher, field)
|
||||
|
||||
@@ -30,7 +30,8 @@ class ClassBody (
|
||||
val members: List<Member>,
|
||||
val companionObjectMembers: List<Member>,
|
||||
val lBrace: LBrace,
|
||||
val rBrace: RBrace) {
|
||||
val rBrace: RBrace,
|
||||
val isEnumBody: Boolean) {
|
||||
|
||||
fun append(builder: CodeBuilder) {
|
||||
val membersFiltered = members.filter { !it.isEmpty }
|
||||
@@ -38,7 +39,20 @@ class ClassBody (
|
||||
|
||||
builder append " " append lBrace append "\n"
|
||||
|
||||
builder.append(membersFiltered, "\n")
|
||||
if (!isEnumBody) {
|
||||
builder.append(membersFiltered, "\n")
|
||||
}
|
||||
else {
|
||||
val (constants, otherMembers) = membersFiltered.partition { it is EnumConstant }
|
||||
|
||||
builder.append(constants, ",\n")
|
||||
|
||||
if (otherMembers.isNotEmpty() || companionObjectMembers.isNotEmpty()) {
|
||||
builder.append(";\n")
|
||||
}
|
||||
|
||||
builder.append(otherMembers, "\n")
|
||||
}
|
||||
|
||||
appendCompanionObject(builder, membersFiltered.isNotEmpty())
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@ class EnumConstant(
|
||||
val identifier: Identifier,
|
||||
annotations: Annotations,
|
||||
modifiers: Modifiers,
|
||||
val type: Type,
|
||||
val params: DeferredElement<ExpressionList>
|
||||
) : Member(annotations, modifiers) {
|
||||
|
||||
@@ -32,6 +31,6 @@ class EnumConstant(
|
||||
return
|
||||
}
|
||||
|
||||
builder append annotations append identifier append " : " append type append "(" append params append ")"
|
||||
builder append annotations append identifier append "(" append params append ")"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
annotation class Anon(public val value: String) {
|
||||
|
||||
public enum class E {
|
||||
A
|
||||
A,
|
||||
B
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class A {
|
||||
enum class E {
|
||||
A
|
||||
B
|
||||
A,
|
||||
B,
|
||||
C
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package demo
|
||||
|
||||
enum class MyEnum private constructor(public val color: Int) {
|
||||
RED : MyEnum(10)
|
||||
BLUE : MyEnum(20)
|
||||
RED(10),
|
||||
BLUE(20)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
public enum class TestEnum {
|
||||
A
|
||||
B
|
||||
A,
|
||||
B;
|
||||
|
||||
|
||||
companion object {
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
enum class E {
|
||||
FOO
|
||||
FOO;
|
||||
|
||||
fun foo() {
|
||||
FOO.toString()
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
enum class E {
|
||||
I
|
||||
|
||||
I;
|
||||
private val name: String? = null
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
enum class Color private constructor(public val code: Int) {
|
||||
WHITE : Color(21)
|
||||
BLACK : Color(22)
|
||||
RED : Color(23)
|
||||
YELLOW : Color(24)
|
||||
BLUE : Color(25)
|
||||
WHITE(21),
|
||||
BLACK(22),
|
||||
RED(23),
|
||||
YELLOW(24),
|
||||
BLUE(25)
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
enum class Color {
|
||||
WHITE
|
||||
BLACK
|
||||
RED
|
||||
YELLOW
|
||||
BLUE
|
||||
|
||||
WHITE,
|
||||
BLACK,
|
||||
RED,
|
||||
YELLOW,
|
||||
BLUE;
|
||||
override fun toString(): String {
|
||||
return "COLOR"
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
enum class Color : Runnable {
|
||||
WHITE
|
||||
BLACK
|
||||
RED
|
||||
YELLOW
|
||||
BLUE
|
||||
WHITE,
|
||||
BLACK,
|
||||
RED,
|
||||
YELLOW,
|
||||
BLUE;
|
||||
|
||||
override fun run() {
|
||||
println("name()=" + name() + ", toString()=" + toString())
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
enum class Coin {
|
||||
PENNY
|
||||
NICKEL
|
||||
DIME
|
||||
PENNY,
|
||||
NICKEL,
|
||||
DIME,
|
||||
QUARTER
|
||||
}
|
||||
@@ -30,6 +30,7 @@ open class D : I {
|
||||
}
|
||||
|
||||
enum class E {
|
||||
;
|
||||
fun foo(): Int {
|
||||
return 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user