J2K: extract base class for Parameter
This commit is contained in:
@@ -209,12 +209,12 @@ class ConstructorConverter(
|
|||||||
Modifiers(listOf()).with(fieldCorrection.access).assignNoPrototype()
|
Modifiers(listOf()).with(fieldCorrection.access).assignNoPrototype()
|
||||||
else
|
else
|
||||||
converter.convertModifiers(field).filter { it in ACCESS_MODIFIERS }
|
converter.convertModifiers(field).filter { it in ACCESS_MODIFIERS }
|
||||||
Parameter(name,
|
FunctionParameter(name,
|
||||||
type,
|
type,
|
||||||
if (field.isVal(converter.referenceSearcher)) Parameter.VarValModifier.Val else Parameter.VarValModifier.Var,
|
if (field.isVal(converter.referenceSearcher)) FunctionParameter.VarValModifier.Val else FunctionParameter.VarValModifier.Var,
|
||||||
converter.convertAnnotations(parameter) + converter.convertAnnotations(field),
|
converter.convertAnnotations(parameter) + converter.convertAnnotations(field),
|
||||||
accessModifiers,
|
accessModifiers,
|
||||||
default)
|
default)
|
||||||
.assignPrototypes(
|
.assignPrototypes(
|
||||||
PrototypeInfo(parameter, CommentsAndSpacesInheritance.LINE_BREAKS),
|
PrototypeInfo(parameter, CommentsAndSpacesInheritance.LINE_BREAKS),
|
||||||
PrototypeInfo(field, CommentsAndSpacesInheritance.NO_SPACES)
|
PrototypeInfo(field, CommentsAndSpacesInheritance.NO_SPACES)
|
||||||
|
|||||||
@@ -236,15 +236,15 @@ class Converter private constructor(
|
|||||||
val annotationMethods = psiClass.getMethods().filterIsInstance<PsiAnnotationMethod>()
|
val annotationMethods = psiClass.getMethods().filterIsInstance<PsiAnnotationMethod>()
|
||||||
val (methodsNamedValue, otherMethods) = annotationMethods.partition { it.getName() == "value" }
|
val (methodsNamedValue, otherMethods) = annotationMethods.partition { it.getName() == "value" }
|
||||||
|
|
||||||
fun createParameter(type: Type, method: PsiAnnotationMethod): Parameter {
|
fun createParameter(type: Type, method: PsiAnnotationMethod): FunctionParameter {
|
||||||
type.assignPrototype(method.getReturnTypeElement(), CommentsAndSpacesInheritance.NO_SPACES)
|
type.assignPrototype(method.getReturnTypeElement(), CommentsAndSpacesInheritance.NO_SPACES)
|
||||||
|
|
||||||
return Parameter(method.declarationIdentifier(),
|
return FunctionParameter(method.declarationIdentifier(),
|
||||||
type,
|
type,
|
||||||
Parameter.VarValModifier.Val,
|
FunctionParameter.VarValModifier.Val,
|
||||||
convertAnnotations(method),
|
convertAnnotations(method),
|
||||||
paramModifiers,
|
paramModifiers,
|
||||||
annotationConverter.convertAnnotationMethodDefault(method)).assignPrototype(method, CommentsAndSpacesInheritance.NO_SPACES)
|
annotationConverter.convertAnnotationMethodDefault(method)).assignPrototype(method, CommentsAndSpacesInheritance.NO_SPACES)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun convertType(psiType: PsiType?): Type {
|
fun convertType(psiType: PsiType?): Type {
|
||||||
@@ -427,7 +427,7 @@ class Converter private constructor(
|
|||||||
newLineAfter = false).assignNoPrototype())).assignNoPrototype()
|
newLineAfter = false).assignNoPrototype())).assignNoPrototype()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (function.parameterList.parameters.any { it.defaultValue != null } && !function.modifiers.isPrivate) {
|
if (function.parameterList.parameters.any { it is FunctionParameter && it.defaultValue != null } && !function.modifiers.isPrivate) {
|
||||||
function.annotations += Annotations(
|
function.annotations += Annotations(
|
||||||
listOf(Annotation(Identifier("jvmOverloads").assignNoPrototype(),
|
listOf(Annotation(Identifier("jvmOverloads").assignNoPrototype(),
|
||||||
listOf(),
|
listOf(),
|
||||||
@@ -535,17 +535,17 @@ class Converter private constructor(
|
|||||||
public fun convertParameter(
|
public fun convertParameter(
|
||||||
parameter: PsiParameter,
|
parameter: PsiParameter,
|
||||||
nullability: Nullability = Nullability.Default,
|
nullability: Nullability = Nullability.Default,
|
||||||
varValModifier: Parameter.VarValModifier = Parameter.VarValModifier.None,
|
varValModifier: FunctionParameter.VarValModifier = FunctionParameter.VarValModifier.None,
|
||||||
modifiers: Modifiers = Modifiers.Empty,
|
modifiers: Modifiers = Modifiers.Empty,
|
||||||
defaultValue: DeferredElement<Expression>? = null
|
defaultValue: DeferredElement<Expression>? = null
|
||||||
): Parameter {
|
): FunctionParameter {
|
||||||
var type = typeConverter.convertVariableType(parameter)
|
var type = typeConverter.convertVariableType(parameter)
|
||||||
when (nullability) {
|
when (nullability) {
|
||||||
Nullability.NotNull -> type = type.toNotNullType()
|
Nullability.NotNull -> type = type.toNotNullType()
|
||||||
Nullability.Nullable -> type = type.toNullableType()
|
Nullability.Nullable -> type = type.toNullableType()
|
||||||
}
|
}
|
||||||
return Parameter(parameter.declarationIdentifier(), type, varValModifier,
|
return FunctionParameter(parameter.declarationIdentifier(), type, varValModifier,
|
||||||
convertAnnotations(parameter), modifiers, defaultValue).assignPrototype(parameter, CommentsAndSpacesInheritance.LINE_BREAKS)
|
convertAnnotations(parameter), modifiers, defaultValue).assignPrototype(parameter, CommentsAndSpacesInheritance.LINE_BREAKS)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertIdentifier(identifier: PsiIdentifier?): Identifier {
|
public fun convertIdentifier(identifier: PsiIdentifier?): Identifier {
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ class OverloadReducer(
|
|||||||
public fun Converter.convertParameterList(
|
public fun Converter.convertParameterList(
|
||||||
method: PsiMethod,
|
method: PsiMethod,
|
||||||
overloadReducer: OverloadReducer?,
|
overloadReducer: OverloadReducer?,
|
||||||
convertParameter: (parameter: PsiParameter, default: DeferredElement<Expression>?) -> Parameter = { parameter, default -> convertParameter(parameter, defaultValue = default) },
|
convertParameter: (parameter: PsiParameter, default: DeferredElement<Expression>?) -> FunctionParameter = { parameter, default -> convertParameter(parameter, defaultValue = default) },
|
||||||
correctCodeConverter: CodeConverter.() -> CodeConverter = { this }
|
correctCodeConverter: CodeConverter.() -> CodeConverter = { this }
|
||||||
): ParameterList {
|
): ParameterList {
|
||||||
val parameterList = method.getParameterList()
|
val parameterList = method.getParameterList()
|
||||||
|
|||||||
@@ -204,11 +204,11 @@ class DefaultStatementConverter : JavaElementVisitor(), StatementConverter {
|
|||||||
listOf(parameterType)
|
listOf(parameterType)
|
||||||
for (t in types) {
|
for (t in types) {
|
||||||
var convertedType = codeConverter.typeConverter.convertType(t, Nullability.NotNull)
|
var convertedType = codeConverter.typeConverter.convertType(t, Nullability.NotNull)
|
||||||
val convertedParameter = Parameter(parameter.declarationIdentifier(),
|
val convertedParameter = FunctionParameter(parameter.declarationIdentifier(),
|
||||||
convertedType,
|
convertedType,
|
||||||
Parameter.VarValModifier.None,
|
FunctionParameter.VarValModifier.None,
|
||||||
annotations,
|
annotations,
|
||||||
Modifiers.Empty).assignPrototype(parameter)
|
Modifiers.Empty).assignPrototype(parameter)
|
||||||
catches.add(CatchStatement(convertedParameter, blockConverted).assignNoPrototype())
|
catches.add(CatchStatement(convertedParameter, blockConverted).assignNoPrototype())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,12 +18,15 @@ package org.jetbrains.kotlin.j2k.ast
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.j2k.*
|
import org.jetbrains.kotlin.j2k.*
|
||||||
|
|
||||||
class Parameter(val identifier: Identifier,
|
abstract class Parameter(val identifier: Identifier,
|
||||||
val type: Type,
|
val type: Type?): Element()
|
||||||
val varVal: Parameter.VarValModifier,
|
|
||||||
val annotations: Annotations,
|
class FunctionParameter(identifier: Identifier,
|
||||||
val modifiers: Modifiers,
|
type: Type,
|
||||||
val defaultValue: DeferredElement<Expression>? = null) : Element() {
|
val varVal: FunctionParameter.VarValModifier,
|
||||||
|
val annotations: Annotations,
|
||||||
|
val modifiers: Modifiers,
|
||||||
|
val defaultValue: DeferredElement<Expression>? = null) : Parameter(identifier, type) {
|
||||||
public enum class VarValModifier {
|
public enum class VarValModifier {
|
||||||
None,
|
None,
|
||||||
Val,
|
Val,
|
||||||
@@ -42,7 +45,7 @@ class Parameter(val identifier: Identifier,
|
|||||||
VarValModifier.Val -> builder.append("val ")
|
VarValModifier.Val -> builder.append("val ")
|
||||||
}
|
}
|
||||||
|
|
||||||
builder append identifier append ":" append type
|
builder append identifier append ":" append type!!
|
||||||
|
|
||||||
if (defaultValue != null) {
|
if (defaultValue != null) {
|
||||||
builder append " = " append defaultValue
|
builder append " = " append defaultValue
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ class ThrowStatement(val expression: Expression) : Expression() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CatchStatement(val variable: Parameter, val block: Block) : Statement() {
|
class CatchStatement(val variable: FunctionParameter, val block: Block) : Statement() {
|
||||||
override fun generateCode(builder: CodeBuilder) {
|
override fun generateCode(builder: CodeBuilder) {
|
||||||
builder append "catch (" append variable append ") " append block
|
builder append "catch (" append variable append ") " append block
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user