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