j2k: cleanup 'public', property access syntax
This commit is contained in:
@@ -20,8 +20,8 @@ import com.intellij.openapi.project.Project
|
|||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
|
|
||||||
public class AfterConversionPass(val project: Project, val postProcessor: PostProcessor) {
|
class AfterConversionPass(val project: Project, val postProcessor: PostProcessor) {
|
||||||
public fun run(kotlinFile: KtFile, range: TextRange?) {
|
fun run(kotlinFile: KtFile, range: TextRange?) {
|
||||||
val rangeMarker = if (range != null) {
|
val rangeMarker = if (range != null) {
|
||||||
val document = kotlinFile.viewProvider.document!!
|
val document = kotlinFile.viewProvider.document!!
|
||||||
val marker = document.createRangeMarker(range.startOffset, range.endOffset)
|
val marker = document.createRangeMarker(range.startOffset, range.endOffset)
|
||||||
|
|||||||
@@ -31,11 +31,11 @@ class AnnotationConverter(private val converter: Converter) {
|
|||||||
+ NullableNotNullManager.getInstance(converter.project).nullables
|
+ NullableNotNullManager.getInstance(converter.project).nullables
|
||||||
+ listOf(CommonClassNames.JAVA_LANG_OVERRIDE, ElementType::class.java.name)).toSet()
|
+ listOf(CommonClassNames.JAVA_LANG_OVERRIDE, ElementType::class.java.name)).toSet()
|
||||||
|
|
||||||
public fun isImportNotRequired(annotationName: String): Boolean {
|
fun isImportNotRequired(annotationName: String): Boolean {
|
||||||
return annotationName in annotationsToRemove || annotationName == Target::class.java.name
|
return annotationName in annotationsToRemove || annotationName == Target::class.java.name
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertAnnotations(owner: PsiModifierListOwner): Annotations
|
fun convertAnnotations(owner: PsiModifierListOwner): Annotations
|
||||||
= convertAnnotationsOnly(owner) + convertModifiersToAnnotations(owner)
|
= convertAnnotationsOnly(owner) + convertModifiersToAnnotations(owner)
|
||||||
|
|
||||||
private fun convertAnnotationsOnly(owner: PsiModifierListOwner): Annotations {
|
private fun convertAnnotationsOnly(owner: PsiModifierListOwner): Annotations {
|
||||||
@@ -99,7 +99,7 @@ class AnnotationConverter(private val converter: Converter) {
|
|||||||
return expr.referenceName?.let { JavaAnnotationTargetMapper.mapJavaTargetArgumentByName(it) } ?: emptySet()
|
return expr.referenceName?.let { JavaAnnotationTargetMapper.mapJavaTargetArgumentByName(it) } ?: emptySet()
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertAnnotation(annotation: PsiAnnotation, newLineAfter: Boolean): Annotation? {
|
fun convertAnnotation(annotation: PsiAnnotation, newLineAfter: Boolean): Annotation? {
|
||||||
val qualifiedName = annotation.qualifiedName
|
val qualifiedName = annotation.qualifiedName
|
||||||
if (qualifiedName == CommonClassNames.JAVA_LANG_DEPRECATED && annotation.parameterList.attributes.isEmpty()) {
|
if (qualifiedName == CommonClassNames.JAVA_LANG_DEPRECATED && annotation.parameterList.attributes.isEmpty()) {
|
||||||
val deferredExpression = converter.deferredElement<Expression> { LiteralExpression("\"\"").assignNoPrototype() }
|
val deferredExpression = converter.deferredElement<Expression> { LiteralExpression("\"\"").assignNoPrototype() }
|
||||||
@@ -151,7 +151,7 @@ class AnnotationConverter(private val converter: Converter) {
|
|||||||
return Annotation(name, arguments, newLineAfter).assignPrototype(annotation)
|
return Annotation(name, arguments, newLineAfter).assignPrototype(annotation)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertAnnotationMethodDefault(method: PsiAnnotationMethod): DeferredElement<Expression>? {
|
fun convertAnnotationMethodDefault(method: PsiAnnotationMethod): DeferredElement<Expression>? {
|
||||||
val value = method.defaultValue ?: return null
|
val value = method.defaultValue ?: return null
|
||||||
return converter.deferredElement(convertAttributeValue(value, method.returnType, false, false).single())
|
return converter.deferredElement(convertAttributeValue(value, method.returnType, false, false).single())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ class ClassBodyConverter(private val psiClass: PsiClass,
|
|||||||
) {
|
) {
|
||||||
private val fieldsToDrop = HashSet<PsiField>()
|
private val fieldsToDrop = HashSet<PsiField>()
|
||||||
|
|
||||||
public fun convertBody(): ClassBody {
|
fun convertBody(): ClassBody {
|
||||||
val memberToPropertyInfo = converter.propertyDetectionCache[psiClass]
|
val memberToPropertyInfo = converter.propertyDetectionCache[psiClass]
|
||||||
|
|
||||||
for ((member, propertyInfo) in memberToPropertyInfo) {
|
for ((member, propertyInfo) in memberToPropertyInfo) {
|
||||||
|
|||||||
@@ -58,10 +58,10 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
|
|||||||
|
|
||||||
private val imports = LinkedHashSet<FqName>()
|
private val imports = LinkedHashSet<FqName>()
|
||||||
|
|
||||||
public infix fun append(text: String): CodeBuilder
|
infix fun append(text: String): CodeBuilder
|
||||||
= append(text, false)
|
= append(text, false)
|
||||||
|
|
||||||
public fun addImport(fqName: FqName) {
|
fun addImport(fqName: FqName) {
|
||||||
imports.add(fqName)
|
imports.add(fqName)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,13 +92,13 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
public val resultText: String
|
val resultText: String
|
||||||
get() = builder.toString()
|
get() = builder.toString()
|
||||||
|
|
||||||
public val importsToAdd: Set<FqName>
|
val importsToAdd: Set<FqName>
|
||||||
get() = imports
|
get() = imports
|
||||||
|
|
||||||
public infix fun append(element: Element): CodeBuilder {
|
infix fun append(element: Element): CodeBuilder {
|
||||||
if (element.isEmpty) return this // do not insert comment and spaces for empty elements to avoid multiple blank lines
|
if (element.isEmpty) return this // do not insert comment and spaces for empty elements to avoid multiple blank lines
|
||||||
|
|
||||||
if (element.prototypes == null && topElement != null) {
|
if (element.prototypes == null && topElement != null) {
|
||||||
|
|||||||
@@ -22,28 +22,28 @@ import org.jetbrains.kotlin.j2k.ast.*
|
|||||||
import org.jetbrains.kotlin.utils.addToStdlib.check
|
import org.jetbrains.kotlin.utils.addToStdlib.check
|
||||||
|
|
||||||
class CodeConverter(
|
class CodeConverter(
|
||||||
public val converter: Converter,
|
val converter: Converter,
|
||||||
private val expressionConverter: ExpressionConverter,
|
private val expressionConverter: ExpressionConverter,
|
||||||
private val statementConverter: StatementConverter,
|
private val statementConverter: StatementConverter,
|
||||||
public val methodReturnType: PsiType?
|
val methodReturnType: PsiType?
|
||||||
) {
|
) {
|
||||||
|
|
||||||
public val typeConverter: TypeConverter = converter.typeConverter
|
val typeConverter: TypeConverter = converter.typeConverter
|
||||||
public val settings: ConverterSettings = converter.settings
|
val settings: ConverterSettings = converter.settings
|
||||||
|
|
||||||
public fun withSpecialExpressionConverter(specialConverter: SpecialExpressionConverter): CodeConverter
|
fun withSpecialExpressionConverter(specialConverter: SpecialExpressionConverter): CodeConverter
|
||||||
= CodeConverter(converter, expressionConverter.withSpecialConverter(specialConverter), statementConverter, methodReturnType)
|
= CodeConverter(converter, expressionConverter.withSpecialConverter(specialConverter), statementConverter, methodReturnType)
|
||||||
|
|
||||||
public fun withSpecialStatementConverter(specialConverter: SpecialStatementConverter): CodeConverter
|
fun withSpecialStatementConverter(specialConverter: SpecialStatementConverter): CodeConverter
|
||||||
= CodeConverter(converter, expressionConverter, statementConverter.withSpecialConverter(specialConverter), methodReturnType)
|
= CodeConverter(converter, expressionConverter, statementConverter.withSpecialConverter(specialConverter), methodReturnType)
|
||||||
|
|
||||||
public fun withMethodReturnType(methodReturnType: PsiType?): CodeConverter
|
fun withMethodReturnType(methodReturnType: PsiType?): CodeConverter
|
||||||
= CodeConverter(converter, expressionConverter, statementConverter, methodReturnType)
|
= CodeConverter(converter, expressionConverter, statementConverter, methodReturnType)
|
||||||
|
|
||||||
public fun withConverter(converter: Converter): CodeConverter
|
fun withConverter(converter: Converter): CodeConverter
|
||||||
= CodeConverter(converter, expressionConverter, statementConverter, methodReturnType)
|
= CodeConverter(converter, expressionConverter, statementConverter, methodReturnType)
|
||||||
|
|
||||||
public fun convertBlock(block: PsiCodeBlock?, notEmpty: Boolean = true, statementFilter: (PsiStatement) -> Boolean = { true }): Block {
|
fun convertBlock(block: PsiCodeBlock?, notEmpty: Boolean = true, statementFilter: (PsiStatement) -> Boolean = { true }): Block {
|
||||||
if (block == null) return Block.Empty
|
if (block == null) return Block.Empty
|
||||||
|
|
||||||
val lBrace = LBrace().assignPrototype(block.lBrace)
|
val lBrace = LBrace().assignPrototype(block.lBrace)
|
||||||
@@ -51,19 +51,19 @@ class CodeConverter(
|
|||||||
return Block(block.statements.filter(statementFilter).map { convertStatement(it) }, lBrace, rBrace, notEmpty).assignPrototype(block)
|
return Block(block.statements.filter(statementFilter).map { convertStatement(it) }, lBrace, rBrace, notEmpty).assignPrototype(block)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertStatement(statement: PsiStatement?): Statement {
|
fun convertStatement(statement: PsiStatement?): Statement {
|
||||||
if (statement == null) return Statement.Empty
|
if (statement == null) return Statement.Empty
|
||||||
|
|
||||||
return statementConverter.convertStatement(statement, this).assignPrototype(statement)
|
return statementConverter.convertStatement(statement, this).assignPrototype(statement)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertExpressions(expressions: Array<PsiExpression>): List<Expression>
|
fun convertExpressions(expressions: Array<PsiExpression>): List<Expression>
|
||||||
= expressions.map { convertExpression(it) }
|
= expressions.map { convertExpression(it) }
|
||||||
|
|
||||||
public fun convertExpressions(expressions: List<PsiExpression>): List<Expression>
|
fun convertExpressions(expressions: List<PsiExpression>): List<Expression>
|
||||||
= expressions.map { convertExpression(it) }
|
= expressions.map { convertExpression(it) }
|
||||||
|
|
||||||
public fun convertExpression(expression: PsiExpression?, shouldParenthesize: Boolean = false): Expression {
|
fun convertExpression(expression: PsiExpression?, shouldParenthesize: Boolean = false): Expression {
|
||||||
if (expression == null) return Expression.Empty
|
if (expression == null) return Expression.Empty
|
||||||
|
|
||||||
val converted = expressionConverter.convertExpression(expression, this).assignPrototype(expression)
|
val converted = expressionConverter.convertExpression(expression, this).assignPrototype(expression)
|
||||||
@@ -73,7 +73,7 @@ class CodeConverter(
|
|||||||
return converted
|
return converted
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertLocalVariable(variable: PsiLocalVariable): LocalVariable {
|
fun convertLocalVariable(variable: PsiLocalVariable): LocalVariable {
|
||||||
val isVal = variable.hasModifierProperty(PsiModifier.FINAL) ||
|
val isVal = variable.hasModifierProperty(PsiModifier.FINAL) ||
|
||||||
variable.initializer == null/* we do not know actually and prefer val until we have better analysis*/ ||
|
variable.initializer == null/* we do not know actually and prefer val until we have better analysis*/ ||
|
||||||
!variable.hasWriteAccesses(converter.referenceSearcher, variable.getContainingMethod())
|
!variable.hasWriteAccesses(converter.referenceSearcher, variable.getContainingMethod())
|
||||||
@@ -87,7 +87,7 @@ class CodeConverter(
|
|||||||
isVal).assignPrototype(variable)
|
isVal).assignPrototype(variable)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertExpression(expression: PsiExpression?, expectedType: PsiType?): Expression {
|
fun convertExpression(expression: PsiExpression?, expectedType: PsiType?): Expression {
|
||||||
if (expression == null) return Identifier.Empty
|
if (expression == null) return Identifier.Empty
|
||||||
|
|
||||||
var convertedExpression = convertExpression(expression)
|
var convertedExpression = convertExpression(expression)
|
||||||
@@ -136,7 +136,7 @@ class CodeConverter(
|
|||||||
return convertedExpression.assignPrototype(expression)
|
return convertedExpression.assignPrototype(expression)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertedExpressionType(expression: PsiExpression, expectedType: PsiType): Type {
|
fun convertedExpressionType(expression: PsiExpression, expectedType: PsiType): Type {
|
||||||
var convertedExpression = convertExpression(expression)
|
var convertedExpression = convertExpression(expression)
|
||||||
val actualType = expression.type ?: return ErrorType()
|
val actualType = expression.type ?: return ErrorType()
|
||||||
var resultType = typeConverter.convertType(actualType, if (convertedExpression.isNullable) Nullability.Nullable else Nullability.NotNull)
|
var resultType = typeConverter.convertType(actualType, if (convertedExpression.isNullable) Nullability.Nullable else Nullability.NotNull)
|
||||||
|
|||||||
@@ -69,10 +69,10 @@ class ConstructorConverter(
|
|||||||
return toTargetConstructorMap
|
return toTargetConstructorMap
|
||||||
}
|
}
|
||||||
|
|
||||||
public var baseClassParams: List<DeferredElement<Expression>>? = if (constructors.isEmpty()) emptyList() else null
|
var baseClassParams: List<DeferredElement<Expression>>? = if (constructors.isEmpty()) emptyList() else null
|
||||||
private set
|
private set
|
||||||
|
|
||||||
public fun convertConstructor(constructor: PsiMethod,
|
fun convertConstructor(constructor: PsiMethod,
|
||||||
annotations: Annotations,
|
annotations: Annotations,
|
||||||
modifiers: Modifiers,
|
modifiers: Modifiers,
|
||||||
fieldsToDrop: MutableSet<PsiField>,
|
fieldsToDrop: MutableSet<PsiField>,
|
||||||
|
|||||||
@@ -49,44 +49,44 @@ class Converter private constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// state which may differ in different converter's
|
// state which may differ in different converter's
|
||||||
public class PersonalState(val specialContext: PsiElement?)
|
class PersonalState(val specialContext: PsiElement?)
|
||||||
|
|
||||||
public val project: Project = elementToConvert.project
|
val project: Project = elementToConvert.project
|
||||||
public val typeConverter: TypeConverter = TypeConverter(this)
|
val typeConverter: TypeConverter = TypeConverter(this)
|
||||||
public val annotationConverter: AnnotationConverter = AnnotationConverter(this)
|
val annotationConverter: AnnotationConverter = AnnotationConverter(this)
|
||||||
|
|
||||||
public val specialContext: PsiElement? = personalState.specialContext
|
val specialContext: PsiElement? = personalState.specialContext
|
||||||
|
|
||||||
public val referenceSearcher: ReferenceSearcher = CachingReferenceSearcher(services.referenceSearcher)
|
val referenceSearcher: ReferenceSearcher = CachingReferenceSearcher(services.referenceSearcher)
|
||||||
|
|
||||||
public val propertyDetectionCache = PropertyDetectionCache(this)
|
val propertyDetectionCache = PropertyDetectionCache(this)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
public fun create(elementToConvert: PsiElement, settings: ConverterSettings, services: JavaToKotlinConverterServices,
|
fun create(elementToConvert: PsiElement, settings: ConverterSettings, services: JavaToKotlinConverterServices,
|
||||||
inConversionScope: (PsiElement) -> Boolean, usageProcessingsCollector: (UsageProcessing) -> Unit): Converter {
|
inConversionScope: (PsiElement) -> Boolean, usageProcessingsCollector: (UsageProcessing) -> Unit): Converter {
|
||||||
return Converter(elementToConvert, settings, inConversionScope,
|
return Converter(elementToConvert, settings, inConversionScope,
|
||||||
services, CommonState(usageProcessingsCollector), PersonalState(null))
|
services, CommonState(usageProcessingsCollector), PersonalState(null))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun withSpecialContext(context: PsiElement): Converter = withState(PersonalState(context))
|
fun withSpecialContext(context: PsiElement): Converter = withState(PersonalState(context))
|
||||||
|
|
||||||
private fun withState(state: PersonalState): Converter
|
private fun withState(state: PersonalState): Converter
|
||||||
= Converter(elementToConvert, settings, inConversionScope, services, commonState, state)
|
= Converter(elementToConvert, settings, inConversionScope, services, commonState, state)
|
||||||
|
|
||||||
private fun createDefaultCodeConverter() = CodeConverter(this, DefaultExpressionConverter(), DefaultStatementConverter(), null)
|
private fun createDefaultCodeConverter() = CodeConverter(this, DefaultExpressionConverter(), DefaultStatementConverter(), null)
|
||||||
|
|
||||||
public data class IntermediateResult(
|
data class IntermediateResult(
|
||||||
val codeGenerator: (Map<PsiElement, Collection<UsageProcessing>>) -> Result,
|
val codeGenerator: (Map<PsiElement, Collection<UsageProcessing>>) -> Result,
|
||||||
val parseContext: ParseContext
|
val parseContext: ParseContext
|
||||||
)
|
)
|
||||||
|
|
||||||
public data class Result(
|
data class Result(
|
||||||
val text: String,
|
val text: String,
|
||||||
val importsToAdd: Set<FqName>
|
val importsToAdd: Set<FqName>
|
||||||
)
|
)
|
||||||
|
|
||||||
public fun convert(): IntermediateResult? {
|
fun convert(): IntermediateResult? {
|
||||||
val element = convertTopElement(elementToConvert) ?: return null
|
val element = convertTopElement(elementToConvert) ?: return null
|
||||||
val parseContext = when (elementToConvert) {
|
val parseContext = when (elementToConvert) {
|
||||||
is PsiStatement, is PsiExpression -> ParseContext.CODE_BLOCK
|
is PsiStatement, is PsiExpression -> ParseContext.CODE_BLOCK
|
||||||
@@ -137,17 +137,17 @@ class Converter private constructor(
|
|||||||
commonState.postUnfoldActions.forEach { it() }
|
commonState.postUnfoldActions.forEach { it() }
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun<TResult : Element> deferredElement(generator: (CodeConverter) -> TResult): DeferredElement<TResult> {
|
fun<TResult : Element> deferredElement(generator: (CodeConverter) -> TResult): DeferredElement<TResult> {
|
||||||
val element = DeferredElement(generator, personalState)
|
val element = DeferredElement(generator, personalState)
|
||||||
commonState.deferredElements.add(element)
|
commonState.deferredElements.add(element)
|
||||||
return element
|
return element
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun addUsageProcessing(processing: UsageProcessing) {
|
fun addUsageProcessing(processing: UsageProcessing) {
|
||||||
commonState.usageProcessingsCollector(processing)
|
commonState.usageProcessingsCollector(processing)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun addPostUnfoldDeferredElementsAction(action: () -> Unit) {
|
fun addPostUnfoldDeferredElementsAction(action: () -> Unit) {
|
||||||
commonState.postUnfoldActions.add(action)
|
commonState.postUnfoldActions.add(action)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,10 +156,10 @@ class Converter private constructor(
|
|||||||
return File(convertedChildren).assignPrototype(javaFile)
|
return File(convertedChildren).assignPrototype(javaFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertAnnotations(owner: PsiModifierListOwner): Annotations
|
fun convertAnnotations(owner: PsiModifierListOwner): Annotations
|
||||||
= annotationConverter.convertAnnotations(owner)
|
= annotationConverter.convertAnnotations(owner)
|
||||||
|
|
||||||
public fun convertClass(psiClass: PsiClass): Class {
|
fun convertClass(psiClass: PsiClass): Class {
|
||||||
if (psiClass.isAnnotationType) {
|
if (psiClass.isAnnotationType) {
|
||||||
return convertAnnotationType(psiClass)
|
return convertAnnotationType(psiClass)
|
||||||
}
|
}
|
||||||
@@ -206,7 +206,7 @@ class Converter private constructor(
|
|||||||
}.assignPrototype(psiClass)
|
}.assignPrototype(psiClass)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun needOpenModifier(psiClass: PsiClass): Boolean {
|
fun needOpenModifier(psiClass: PsiClass): Boolean {
|
||||||
return if (psiClass.hasModifierProperty(PsiModifier.FINAL) || psiClass.hasModifierProperty(PsiModifier.ABSTRACT))
|
return if (psiClass.hasModifierProperty(PsiModifier.FINAL) || psiClass.hasModifierProperty(PsiModifier.ABSTRACT))
|
||||||
false
|
false
|
||||||
else
|
else
|
||||||
@@ -294,12 +294,12 @@ class Converter private constructor(
|
|||||||
classBody).assignPrototype(psiClass)
|
classBody).assignPrototype(psiClass)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertInitializer(initializer: PsiClassInitializer): Initializer {
|
fun convertInitializer(initializer: PsiClassInitializer): Initializer {
|
||||||
return Initializer(deferredElement { codeConverter -> codeConverter.convertBlock(initializer.body) },
|
return Initializer(deferredElement { codeConverter -> codeConverter.convertBlock(initializer.body) },
|
||||||
convertModifiers(initializer, false)).assignPrototype(initializer)
|
convertModifiers(initializer, false)).assignPrototype(initializer)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertProperty(propertyInfo: PropertyInfo, classKind: ClassKind): Member {
|
fun convertProperty(propertyInfo: PropertyInfo, classKind: ClassKind): Member {
|
||||||
val field = propertyInfo.field
|
val field = propertyInfo.field
|
||||||
val getMethod = propertyInfo.getMethod
|
val getMethod = propertyInfo.getMethod
|
||||||
val setMethod = propertyInfo.setMethod
|
val setMethod = propertyInfo.setMethod
|
||||||
@@ -442,7 +442,7 @@ class Converter private constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun shouldDeclareVariableType(variable: PsiVariable, type: Type, canChangeType: Boolean): Boolean {
|
fun shouldDeclareVariableType(variable: PsiVariable, type: Type, canChangeType: Boolean): Boolean {
|
||||||
val initializer = variable.initializer
|
val initializer = variable.initializer
|
||||||
if (initializer == null || initializer.isNullLiteral()) return true
|
if (initializer == null || initializer.isNullLiteral()) return true
|
||||||
if (initializer.type is PsiPrimitiveType && type is PrimitiveType) {
|
if (initializer.type is PsiPrimitiveType && type is PrimitiveType) {
|
||||||
@@ -458,7 +458,7 @@ class Converter private constructor(
|
|||||||
return type != initializerType
|
return type != initializerType
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertMethod(
|
fun convertMethod(
|
||||||
method: PsiMethod,
|
method: PsiMethod,
|
||||||
fieldsToDrop: MutableSet<PsiField>?,
|
fieldsToDrop: MutableSet<PsiField>?,
|
||||||
constructorConverter: ConstructorConverter?,
|
constructorConverter: ConstructorConverter?,
|
||||||
@@ -583,7 +583,7 @@ class Converter private constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertCodeReferenceElement(element: PsiJavaCodeReferenceElement, hasExternalQualifier: Boolean, typeArgsConverted: List<Element>? = null): ReferenceElement {
|
fun convertCodeReferenceElement(element: PsiJavaCodeReferenceElement, hasExternalQualifier: Boolean, typeArgsConverted: List<Element>? = null): ReferenceElement {
|
||||||
val typeArgs = typeArgsConverted ?: typeConverter.convertTypes(element.typeParameters)
|
val typeArgs = typeArgsConverted ?: typeConverter.convertTypes(element.typeParameters)
|
||||||
|
|
||||||
if (element.isQualified) {
|
if (element.isQualified) {
|
||||||
@@ -623,13 +623,13 @@ class Converter private constructor(
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertTypeElement(element: PsiTypeElement?, nullability: Nullability): Type
|
fun convertTypeElement(element: PsiTypeElement?, nullability: Nullability): Type
|
||||||
= (if (element == null) ErrorType() else typeConverter.convertType(element.type, nullability)).assignPrototype(element)
|
= (if (element == null) ErrorType() else typeConverter.convertType(element.type, nullability)).assignPrototype(element)
|
||||||
|
|
||||||
private fun convertToNotNullableTypes(types: Array<out PsiType?>): List<Type>
|
private fun convertToNotNullableTypes(types: Array<out PsiType?>): List<Type>
|
||||||
= types.map { typeConverter.convertType(it, Nullability.NotNull) }
|
= types.map { typeConverter.convertType(it, Nullability.NotNull) }
|
||||||
|
|
||||||
public fun convertParameter(
|
fun convertParameter(
|
||||||
parameter: PsiParameter,
|
parameter: PsiParameter,
|
||||||
nullability: Nullability = Nullability.Default,
|
nullability: Nullability = Nullability.Default,
|
||||||
varValModifier: FunctionParameter.VarValModifier = FunctionParameter.VarValModifier.None,
|
varValModifier: FunctionParameter.VarValModifier = FunctionParameter.VarValModifier.None,
|
||||||
@@ -645,13 +645,13 @@ class Converter private constructor(
|
|||||||
convertAnnotations(parameter), modifiers, defaultValue).assignPrototype(parameter, CommentsAndSpacesInheritance.LINE_BREAKS)
|
convertAnnotations(parameter), modifiers, defaultValue).assignPrototype(parameter, CommentsAndSpacesInheritance.LINE_BREAKS)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertIdentifier(identifier: PsiIdentifier?): Identifier {
|
fun convertIdentifier(identifier: PsiIdentifier?): Identifier {
|
||||||
if (identifier == null) return Identifier.Empty
|
if (identifier == null) return Identifier.Empty
|
||||||
|
|
||||||
return Identifier(identifier.text!!).assignPrototype(identifier)
|
return Identifier(identifier.text!!).assignPrototype(identifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertModifiers(owner: PsiModifierListOwner, isMethodInOpenClass: Boolean): Modifiers {
|
fun convertModifiers(owner: PsiModifierListOwner, isMethodInOpenClass: Boolean): Modifiers {
|
||||||
var modifiers = Modifiers(MODIFIERS_MAP.filter { owner.hasModifierProperty(it.first) }.map { it.second })
|
var modifiers = Modifiers(MODIFIERS_MAP.filter { owner.hasModifierProperty(it.first) }.map { it.second })
|
||||||
.assignPrototype(owner.modifierList, CommentsAndSpacesInheritance.NO_SPACES)
|
.assignPrototype(owner.modifierList, CommentsAndSpacesInheritance.NO_SPACES)
|
||||||
|
|
||||||
@@ -685,7 +685,7 @@ class Converter private constructor(
|
|||||||
return without(Modifier.INTERNAL).with(Modifier.PUBLIC)
|
return without(Modifier.INTERNAL).with(Modifier.PUBLIC)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertAnonymousClassBody(anonymousClass: PsiAnonymousClass): AnonymousClassBody {
|
fun convertAnonymousClassBody(anonymousClass: PsiAnonymousClass): AnonymousClassBody {
|
||||||
return AnonymousClassBody(ClassBodyConverter(anonymousClass, ClassKind.ANONYMOUS_OBJECT, this).convertBody(),
|
return AnonymousClassBody(ClassBodyConverter(anonymousClass, ClassKind.ANONYMOUS_OBJECT, this).convertBody(),
|
||||||
anonymousClass.baseClassType.resolve()?.isInterface ?: false).assignPrototype(anonymousClass)
|
anonymousClass.baseClassType.resolve()?.isInterface ?: false).assignPrototype(anonymousClass)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.j2k
|
package org.jetbrains.kotlin.j2k
|
||||||
|
|
||||||
public data class ConverterSettings(
|
data class ConverterSettings(
|
||||||
var forceNotNullTypes: Boolean,
|
var forceNotNullTypes: Boolean,
|
||||||
var specifyLocalVariableTypeByDefault: Boolean,
|
var specifyLocalVariableTypeByDefault: Boolean,
|
||||||
var specifyFieldTypeByDefault: Boolean,
|
var specifyFieldTypeByDefault: Boolean,
|
||||||
@@ -24,7 +24,7 @@ public data class ConverterSettings(
|
|||||||
) {
|
) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
public val defaultSettings: ConverterSettings = ConverterSettings(
|
val defaultSettings: ConverterSettings = ConverterSettings(
|
||||||
forceNotNullTypes = true,
|
forceNotNullTypes = true,
|
||||||
specifyLocalVariableTypeByDefault = false,
|
specifyLocalVariableTypeByDefault = false,
|
||||||
specifyFieldTypeByDefault = false,
|
specifyFieldTypeByDefault = false,
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class ForConverter(
|
|||||||
private val condition = statement.condition
|
private val condition = statement.condition
|
||||||
private val body = statement.body
|
private val body = statement.body
|
||||||
|
|
||||||
public fun execute(): Statement {
|
fun execute(): Statement {
|
||||||
val foreach = convertToForeach()
|
val foreach = convertToForeach()
|
||||||
if (foreach != null) return foreach
|
if (foreach != null) return foreach
|
||||||
|
|
||||||
@@ -114,13 +114,13 @@ class ForConverter(
|
|||||||
return WhileWithInitializationPseudoStatement(initializationConverted, whileStatement, kind)
|
return WhileWithInitializationPseudoStatement(initializationConverted, whileStatement, kind)
|
||||||
}
|
}
|
||||||
|
|
||||||
public class WhileWithInitializationPseudoStatement(
|
class WhileWithInitializationPseudoStatement(
|
||||||
public val initialization: Statement,
|
val initialization: Statement,
|
||||||
public val loop: Statement,
|
val loop: Statement,
|
||||||
public val kind: WhileWithInitializationPseudoStatement.Kind
|
val kind: WhileWithInitializationPseudoStatement.Kind
|
||||||
) : Statement() {
|
) : Statement() {
|
||||||
|
|
||||||
public enum class Kind {
|
enum class Kind {
|
||||||
SIMPLE,
|
SIMPLE,
|
||||||
WITH_BLOCK,
|
WITH_BLOCK,
|
||||||
WITH_RUN_BLOCK
|
WITH_RUN_BLOCK
|
||||||
|
|||||||
@@ -34,35 +34,35 @@ import org.jetbrains.kotlin.psi.psiUtil.isAncestor
|
|||||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
public interface PostProcessor {
|
interface PostProcessor {
|
||||||
public fun insertImport(file: KtFile, fqName: FqName)
|
fun insertImport(file: KtFile, fqName: FqName)
|
||||||
|
|
||||||
public fun doAdditionalProcessing(file: KtFile, rangeMarker: RangeMarker?)
|
fun doAdditionalProcessing(file: KtFile, rangeMarker: RangeMarker?)
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum class ParseContext {
|
enum class ParseContext {
|
||||||
TOP_LEVEL,
|
TOP_LEVEL,
|
||||||
CODE_BLOCK
|
CODE_BLOCK
|
||||||
}
|
}
|
||||||
|
|
||||||
public class JavaToKotlinConverter(
|
class JavaToKotlinConverter(
|
||||||
private val project: Project,
|
private val project: Project,
|
||||||
private val settings: ConverterSettings,
|
private val settings: ConverterSettings,
|
||||||
private val services: JavaToKotlinConverterServices
|
private val services: JavaToKotlinConverterServices
|
||||||
) {
|
) {
|
||||||
private val LOG = Logger.getInstance("#org.jetbrains.kotlin.j2k.JavaToKotlinConverter")
|
private val LOG = Logger.getInstance("#org.jetbrains.kotlin.j2k.JavaToKotlinConverter")
|
||||||
|
|
||||||
public interface ExternalCodeProcessing {
|
interface ExternalCodeProcessing {
|
||||||
public fun prepareWriteOperation(progress: ProgressIndicator): () -> Unit
|
fun prepareWriteOperation(progress: ProgressIndicator): () -> Unit
|
||||||
}
|
}
|
||||||
|
|
||||||
public data class ElementResult(val text: String, val importsToAdd: Set<FqName>, val parseContext: ParseContext)
|
data class ElementResult(val text: String, val importsToAdd: Set<FqName>, val parseContext: ParseContext)
|
||||||
|
|
||||||
public data class Result(val results: List<ElementResult?>, val externalCodeProcessing: ExternalCodeProcessing?)
|
data class Result(val results: List<ElementResult?>, val externalCodeProcessing: ExternalCodeProcessing?)
|
||||||
|
|
||||||
public data class FilesResult(val results: List<String>, val externalCodeProcessing: ExternalCodeProcessing?)
|
data class FilesResult(val results: List<String>, val externalCodeProcessing: ExternalCodeProcessing?)
|
||||||
|
|
||||||
public fun filesToKotlin(files: List<PsiJavaFile>, postProcessor: PostProcessor, progress: ProgressIndicator = EmptyProgressIndicator()): FilesResult {
|
fun filesToKotlin(files: List<PsiJavaFile>, postProcessor: PostProcessor, progress: ProgressIndicator = EmptyProgressIndicator()): FilesResult {
|
||||||
val withProgressProcessor = WithProgressProcessor(progress, files)
|
val withProgressProcessor = WithProgressProcessor(progress, files)
|
||||||
|
|
||||||
val (results, externalCodeProcessing) = elementsToKotlin(files, withProgressProcessor)
|
val (results, externalCodeProcessing) = elementsToKotlin(files, withProgressProcessor)
|
||||||
@@ -90,7 +90,7 @@ public class JavaToKotlinConverter(
|
|||||||
return FilesResult(texts, externalCodeProcessing)
|
return FilesResult(texts, externalCodeProcessing)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun elementsToKotlin(inputElements: List<PsiElement>): Result {
|
fun elementsToKotlin(inputElements: List<PsiElement>): Result {
|
||||||
return elementsToKotlin(inputElements, WithProgressProcessor.DEFAULT)
|
return elementsToKotlin(inputElements, WithProgressProcessor.DEFAULT)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,7 +228,7 @@ public class JavaToKotlinConverter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class WithProgressProcessor(private val progress: ProgressIndicator?, private val files: List<PsiJavaFile>?) {
|
private class WithProgressProcessor(private val progress: ProgressIndicator?, private val files: List<PsiJavaFile>?) {
|
||||||
public companion object {
|
companion object {
|
||||||
val DEFAULT = WithProgressProcessor(null, null)
|
val DEFAULT = WithProgressProcessor(null, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,11 +303,11 @@ public class JavaToKotlinConverter(
|
|||||||
private open class DelegatingProgressIndicator : WrappedProgressIndicator, StandardProgressIndicator {
|
private open class DelegatingProgressIndicator : WrappedProgressIndicator, StandardProgressIndicator {
|
||||||
protected val delegate: ProgressIndicator
|
protected val delegate: ProgressIndicator
|
||||||
|
|
||||||
public constructor(indicator: ProgressIndicator) {
|
constructor(indicator: ProgressIndicator) {
|
||||||
delegate = indicator
|
delegate = indicator
|
||||||
}
|
}
|
||||||
|
|
||||||
public constructor() {
|
constructor() {
|
||||||
val indicator = ProgressManager.getInstance().progressIndicator
|
val indicator = ProgressManager.getInstance().progressIndicator
|
||||||
delegate = indicator ?: EmptyProgressIndicator()
|
delegate = indicator ?: EmptyProgressIndicator()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,12 +22,12 @@ import com.intellij.psi.PsiFile
|
|||||||
import com.intellij.psi.PsiFileFactory
|
import com.intellij.psi.PsiFileFactory
|
||||||
import com.intellij.psi.PsiJavaFile
|
import com.intellij.psi.PsiJavaFile
|
||||||
|
|
||||||
public object JavaToKotlinTranslator {
|
object JavaToKotlinTranslator {
|
||||||
private fun createFile(text: String, project: Project): PsiFile? {
|
private fun createFile(text: String, project: Project): PsiFile? {
|
||||||
return PsiFileFactory.getInstance(project).createFileFromText("test.java", JavaLanguage.INSTANCE, text)
|
return PsiFileFactory.getInstance(project).createFileFromText("test.java", JavaLanguage.INSTANCE, text)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun prettify(code: String?): String {
|
fun prettify(code: String?): String {
|
||||||
if (code == null) {
|
if (code == null) {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -42,7 +42,7 @@ public object JavaToKotlinTranslator {
|
|||||||
.trim()
|
.trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun generateKotlinCode(javaCode: String, project: Project): String {
|
fun generateKotlinCode(javaCode: String, project: Project): String {
|
||||||
val file = createFile(javaCode, project)
|
val file = createFile(javaCode, project)
|
||||||
if (file is PsiJavaFile) {
|
if (file is PsiJavaFile) {
|
||||||
val converter = JavaToKotlinConverter(file.project, ConverterSettings.defaultSettings, EmptyJavaToKotlinServices)
|
val converter = JavaToKotlinConverter(file.project, ConverterSettings.defaultSettings, EmptyJavaToKotlinServices)
|
||||||
@@ -53,6 +53,6 @@ public object JavaToKotlinTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//used in Kotlin Web Demo
|
//used in Kotlin Web Demo
|
||||||
public fun translateToKotlin(code: String, project: Project): String {
|
fun translateToKotlin(code: String, project: Project): String {
|
||||||
return JavaToKotlinTranslator.generateKotlinCode(code, project)
|
return JavaToKotlinTranslator.generateKotlinCode(code, project)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,9 +33,9 @@ class OverloadReducer(
|
|||||||
dropOverloadsForDefaultValues(map)
|
dropOverloadsForDefaultValues(map)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun shouldDropMethod(method: PsiMethod): Boolean = method in methodsToDrop
|
fun shouldDropMethod(method: PsiMethod): Boolean = method in methodsToDrop
|
||||||
|
|
||||||
public fun parameterDefault(method: PsiMethod, parameterIndex: Int): PsiExpression? {
|
fun parameterDefault(method: PsiMethod, parameterIndex: Int): PsiExpression? {
|
||||||
val defaults = methodToLastParameterDefaults[method] ?: return null
|
val defaults = methodToLastParameterDefaults[method] ?: return null
|
||||||
val index = method.parameterList.parametersCount - parameterIndex - 1
|
val index = method.parameterList.parametersCount - parameterIndex - 1
|
||||||
return if (index < defaults.size) defaults[index] else null
|
return if (index < defaults.size) defaults[index] else null
|
||||||
@@ -173,7 +173,7 @@ class OverloadReducer(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Converter.convertParameterList(
|
fun Converter.convertParameterList(
|
||||||
method: PsiMethod,
|
method: PsiMethod,
|
||||||
overloadReducer: OverloadReducer?,
|
overloadReducer: OverloadReducer?,
|
||||||
convertParameter: (parameter: PsiParameter, default: DeferredElement<Expression>?) -> FunctionParameter = { parameter, default -> convertParameter(parameter, defaultValue = default) },
|
convertParameter: (parameter: PsiParameter, default: DeferredElement<Expression>?) -> FunctionParameter = { parameter, default -> convertParameter(parameter, defaultValue = default) },
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.j2k
|
|||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
import com.intellij.psi.util.PsiUtil
|
import com.intellij.psi.util.PsiUtil
|
||||||
|
|
||||||
public interface ReferenceSearcher {
|
interface ReferenceSearcher {
|
||||||
fun findLocalUsages(element: PsiElement, scope: PsiElement): Collection<PsiReference>
|
fun findLocalUsages(element: PsiElement, scope: PsiElement): Collection<PsiReference>
|
||||||
fun hasInheritors(`class`: PsiClass): Boolean
|
fun hasInheritors(`class`: PsiClass): Boolean
|
||||||
fun hasOverrides(method: PsiMethod): Boolean
|
fun hasOverrides(method: PsiMethod): Boolean
|
||||||
@@ -27,10 +27,10 @@ public interface ReferenceSearcher {
|
|||||||
fun findUsagesForExternalCodeProcessing(element: PsiElement, searchJava: Boolean, searchKotlin: Boolean): Collection<PsiReference>
|
fun findUsagesForExternalCodeProcessing(element: PsiElement, searchJava: Boolean, searchKotlin: Boolean): Collection<PsiReference>
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun ReferenceSearcher.findVariableUsages(variable: PsiVariable, scope: PsiElement): Collection<PsiReferenceExpression>
|
fun ReferenceSearcher.findVariableUsages(variable: PsiVariable, scope: PsiElement): Collection<PsiReferenceExpression>
|
||||||
= findLocalUsages(variable, scope).filterIsInstance<PsiReferenceExpression>()
|
= findLocalUsages(variable, scope).filterIsInstance<PsiReferenceExpression>()
|
||||||
|
|
||||||
public fun ReferenceSearcher.findMethodCalls(method: PsiMethod, scope: PsiElement): Collection<PsiMethodCallExpression> {
|
fun ReferenceSearcher.findMethodCalls(method: PsiMethod, scope: PsiElement): Collection<PsiMethodCallExpression> {
|
||||||
return findLocalUsages(method, scope).mapNotNull {
|
return findLocalUsages(method, scope).mapNotNull {
|
||||||
if (it is PsiReferenceExpression) {
|
if (it is PsiReferenceExpression) {
|
||||||
val methodCall = it.parent as? PsiMethodCallExpression
|
val methodCall = it.parent as? PsiMethodCallExpression
|
||||||
@@ -42,7 +42,7 @@ public fun ReferenceSearcher.findMethodCalls(method: PsiMethod, scope: PsiElemen
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun PsiField.isVar(searcher: ReferenceSearcher): Boolean {
|
fun PsiField.isVar(searcher: ReferenceSearcher): Boolean {
|
||||||
if (hasModifierProperty(PsiModifier.FINAL)) return false
|
if (hasModifierProperty(PsiModifier.FINAL)) return false
|
||||||
if (!hasModifierProperty(PsiModifier.PRIVATE)) return true
|
if (!hasModifierProperty(PsiModifier.PRIVATE)) return true
|
||||||
val containingClass = containingClass ?: return true
|
val containingClass = containingClass ?: return true
|
||||||
@@ -64,10 +64,10 @@ public fun PsiField.isVar(searcher: ReferenceSearcher): Boolean {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun PsiVariable.hasWriteAccesses(searcher: ReferenceSearcher, scope: PsiElement?): Boolean
|
fun PsiVariable.hasWriteAccesses(searcher: ReferenceSearcher, scope: PsiElement?): Boolean
|
||||||
= if (scope != null) searcher.findVariableUsages(this, scope).any { PsiUtil.isAccessedForWriting(it) } else false
|
= if (scope != null) searcher.findVariableUsages(this, scope).any { PsiUtil.isAccessedForWriting(it) } else false
|
||||||
|
|
||||||
public object EmptyReferenceSearcher: ReferenceSearcher {
|
object EmptyReferenceSearcher: ReferenceSearcher {
|
||||||
override fun findLocalUsages(element: PsiElement, scope: PsiElement): Collection<PsiReference> = emptyList()
|
override fun findLocalUsages(element: PsiElement, scope: PsiElement): Collection<PsiReference> = emptyList()
|
||||||
override fun hasInheritors(`class`: PsiClass) = false
|
override fun hasInheritors(`class`: PsiClass) = false
|
||||||
override fun hasOverrides(method: PsiMethod) = false
|
override fun hasOverrides(method: PsiMethod) = false
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ package org.jetbrains.kotlin.j2k
|
|||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||||
|
|
||||||
public interface ResolverForConverter {
|
interface ResolverForConverter {
|
||||||
public fun resolveToDescriptor(declaration: KtDeclaration): DeclarationDescriptor?
|
fun resolveToDescriptor(declaration: KtDeclaration): DeclarationDescriptor?
|
||||||
}
|
}
|
||||||
|
|
||||||
object EmptyResolverForConverter : ResolverForConverter {
|
object EmptyResolverForConverter : ResolverForConverter {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.j2k.ast.*
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class SwitchConverter(private val codeConverter: CodeConverter) {
|
class SwitchConverter(private val codeConverter: CodeConverter) {
|
||||||
public fun convert(statement: PsiSwitchStatement): WhenStatement
|
fun convert(statement: PsiSwitchStatement): WhenStatement
|
||||||
= WhenStatement(codeConverter.convertExpression(statement.expression), switchBodyToWhenEntries(statement.body))
|
= WhenStatement(codeConverter.convertExpression(statement.expression), switchBodyToWhenEntries(statement.body))
|
||||||
|
|
||||||
private class Case(val label: PsiSwitchLabelStatement?, val statements: List<PsiStatement>)
|
private class Case(val label: PsiSwitchLabelStatement?, val statements: List<PsiStatement>)
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ import java.util.*
|
|||||||
class TypeConverter(val converter: Converter) {
|
class TypeConverter(val converter: Converter) {
|
||||||
private val typesBeingConverted = HashSet<PsiType>()
|
private val typesBeingConverted = HashSet<PsiType>()
|
||||||
|
|
||||||
public fun convertType(
|
fun convertType(
|
||||||
type: PsiType?,
|
type: PsiType?,
|
||||||
nullability: Nullability = Nullability.Default,
|
nullability: Nullability = Nullability.Default,
|
||||||
mutability: Mutability = Mutability.Default,
|
mutability: Mutability = Mutability.Default,
|
||||||
@@ -53,10 +53,10 @@ class TypeConverter(val converter: Converter) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertTypes(types: Array<PsiType>): List<Type>
|
fun convertTypes(types: Array<PsiType>): List<Type>
|
||||||
= types.map { convertType(it) }
|
= types.map { convertType(it) }
|
||||||
|
|
||||||
public fun convertVariableType(variable: PsiVariable): Type {
|
fun convertVariableType(variable: PsiVariable): Type {
|
||||||
val result = if (variable.isMainMethodParameter()) {
|
val result = if (variable.isMainMethodParameter()) {
|
||||||
ArrayType(ClassType(ReferenceElement(Identifier("String").assignNoPrototype(), listOf()).assignNoPrototype(), Nullability.NotNull, converter.settings).assignNoPrototype(),
|
ArrayType(ClassType(ReferenceElement(Identifier("String").assignNoPrototype(), listOf()).assignNoPrototype(), Nullability.NotNull, converter.settings).assignNoPrototype(),
|
||||||
Nullability.NotNull,
|
Nullability.NotNull,
|
||||||
@@ -68,19 +68,19 @@ class TypeConverter(val converter: Converter) {
|
|||||||
return result.assignPrototype(variable.typeElement)
|
return result.assignPrototype(variable.typeElement)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertMethodReturnType(method: PsiMethod): Type
|
fun convertMethodReturnType(method: PsiMethod): Type
|
||||||
= convertType(method.returnType, methodNullability(method), methodMutability(method)).assignPrototype(method.returnTypeElement)
|
= convertType(method.returnType, methodNullability(method), methodMutability(method)).assignPrototype(method.returnTypeElement)
|
||||||
|
|
||||||
public fun variableNullability(variable: PsiVariable): Nullability
|
fun variableNullability(variable: PsiVariable): Nullability
|
||||||
= nullabilityFlavor.forVariableType(variable)
|
= nullabilityFlavor.forVariableType(variable)
|
||||||
|
|
||||||
public fun methodNullability(method: PsiMethod): Nullability
|
fun methodNullability(method: PsiMethod): Nullability
|
||||||
= nullabilityFlavor.forMethodReturnType(method)
|
= nullabilityFlavor.forMethodReturnType(method)
|
||||||
|
|
||||||
public fun variableMutability(variable: PsiVariable): Mutability
|
fun variableMutability(variable: PsiVariable): Mutability
|
||||||
= mutabilityFlavor.forVariableType(variable)
|
= mutabilityFlavor.forVariableType(variable)
|
||||||
|
|
||||||
public fun methodMutability(method: PsiMethod): Mutability
|
fun methodMutability(method: PsiMethod): Mutability
|
||||||
= mutabilityFlavor.forMethodReturnType(method)
|
= mutabilityFlavor.forMethodReturnType(method)
|
||||||
|
|
||||||
private fun PsiVariable.isMainMethodParameter() = this is PsiParameter && (declarationScope as? PsiMethod)?.isMainMethod() ?: false
|
private fun PsiVariable.isMainMethodParameter() = this is PsiParameter && (declarationScope as? PsiMethod)?.isMainMethod() ?: false
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ class TypeVisitor(
|
|||||||
CommonClassNames.JAVA_UTIL_MAP to "kotlin.Map"
|
CommonClassNames.JAVA_UTIL_MAP to "kotlin.Map"
|
||||||
)
|
)
|
||||||
|
|
||||||
public val toKotlinMutableTypesMap: Map<String, String> = mapOf(
|
val toKotlinMutableTypesMap: Map<String, String> = mapOf(
|
||||||
CommonClassNames.JAVA_UTIL_LIST to "kotlin.MutableList",
|
CommonClassNames.JAVA_UTIL_LIST to "kotlin.MutableList",
|
||||||
CommonClassNames.JAVA_UTIL_COLLECTION to "kotlin.MutableCollection",
|
CommonClassNames.JAVA_UTIL_COLLECTION to "kotlin.MutableCollection",
|
||||||
CommonClassNames.JAVA_UTIL_SET to "kotlin.MutableSet",
|
CommonClassNames.JAVA_UTIL_SET to "kotlin.MutableSet",
|
||||||
|
|||||||
@@ -39,10 +39,10 @@ class PrimaryConstructor(
|
|||||||
|
|
||||||
override fun generateCode(builder: CodeBuilder) { throw IncorrectOperationException() }
|
override fun generateCode(builder: CodeBuilder) { throw IncorrectOperationException() }
|
||||||
|
|
||||||
public fun initializer(): Initializer
|
fun initializer(): Initializer
|
||||||
= Initializer(body!!, Modifiers.Empty).assignPrototypesFrom(this, CommentsAndSpacesInheritance(commentsBefore = false))
|
= Initializer(body!!, Modifiers.Empty).assignPrototypesFrom(this, CommentsAndSpacesInheritance(commentsBefore = false))
|
||||||
|
|
||||||
public fun createSignature(converter: Converter): PrimaryConstructorSignature {
|
fun createSignature(converter: Converter): PrimaryConstructorSignature {
|
||||||
val signature = PrimaryConstructorSignature(annotations, modifiers, parameterList)
|
val signature = PrimaryConstructorSignature(annotations, modifiers, parameterList)
|
||||||
|
|
||||||
// assign prototypes later because we don't know yet whether the body is empty or not
|
// assign prototypes later because we don't know yet whether the body is empty or not
|
||||||
@@ -56,7 +56,7 @@ class PrimaryConstructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
class PrimaryConstructorSignature(val annotations: Annotations, private val modifiers: Modifiers, val parameterList: ParameterList) : Element() {
|
class PrimaryConstructorSignature(val annotations: Annotations, private val modifiers: Modifiers, val parameterList: ParameterList) : Element() {
|
||||||
public val accessModifier: Modifier? = run {
|
val accessModifier: Modifier? = run {
|
||||||
val modifier = modifiers.accessModifier()
|
val modifier = modifiers.accessModifier()
|
||||||
if (modifier != Modifier.PUBLIC) modifier else null
|
if (modifier != Modifier.PUBLIC) modifier else null
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ fun Element.canonicalCode(): String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
abstract class Element {
|
abstract class Element {
|
||||||
public var prototypes: List<PrototypeInfo>? = null
|
var prototypes: List<PrototypeInfo>? = null
|
||||||
set(value) {
|
set(value) {
|
||||||
// do not assign prototypes to singleton instances
|
// do not assign prototypes to singleton instances
|
||||||
if (canBeSingleton) {
|
if (canBeSingleton) {
|
||||||
@@ -84,18 +84,18 @@ abstract class Element {
|
|||||||
protected open val canBeSingleton: Boolean
|
protected open val canBeSingleton: Boolean
|
||||||
get() = isEmpty
|
get() = isEmpty
|
||||||
|
|
||||||
public var createdAt: String?
|
var createdAt: String?
|
||||||
= if (saveCreationStacktraces)
|
= if (saveCreationStacktraces)
|
||||||
Exception().stackTrace.joinToString("\n")
|
Exception().stackTrace.joinToString("\n")
|
||||||
else
|
else
|
||||||
null
|
null
|
||||||
|
|
||||||
/** This method should not be used anywhere except for CodeBuilder! Use CodeBuilder.append instead. */
|
/** This method should not be used anywhere except for CodeBuilder! Use CodeBuilder.append instead. */
|
||||||
public abstract fun generateCode(builder: CodeBuilder)
|
abstract fun generateCode(builder: CodeBuilder)
|
||||||
|
|
||||||
public open fun postGenerateCode(builder: CodeBuilder) { }
|
open fun postGenerateCode(builder: CodeBuilder) { }
|
||||||
|
|
||||||
public open val isEmpty: Boolean get() = false
|
open val isEmpty: Boolean get() = false
|
||||||
|
|
||||||
object Empty : Element() {
|
object Empty : Element() {
|
||||||
override fun generateCode(builder: CodeBuilder) { }
|
override fun generateCode(builder: CodeBuilder) { }
|
||||||
@@ -110,7 +110,7 @@ abstract class Element {
|
|||||||
// this class should never be created directly - Converter.deferredElement() should be used!
|
// this class should never be created directly - Converter.deferredElement() should be used!
|
||||||
class DeferredElement<TResult : Element>(
|
class DeferredElement<TResult : Element>(
|
||||||
private val generator: (CodeConverter) -> TResult,
|
private val generator: (CodeConverter) -> TResult,
|
||||||
public val converterState: Converter.PersonalState
|
val converterState: Converter.PersonalState
|
||||||
) : Element() {
|
) : Element() {
|
||||||
|
|
||||||
private var result: TResult? = null
|
private var result: TResult? = null
|
||||||
@@ -123,7 +123,7 @@ class DeferredElement<TResult : Element>(
|
|||||||
override val canBeSingleton: Boolean
|
override val canBeSingleton: Boolean
|
||||||
get() = false
|
get() = false
|
||||||
|
|
||||||
public fun unfold(codeConverter: CodeConverter) {
|
fun unfold(codeConverter: CodeConverter) {
|
||||||
assert(result == null)
|
assert(result == null)
|
||||||
result = generator(codeConverter)
|
result = generator(codeConverter)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ class Function(
|
|||||||
override val parameterList: ParameterList
|
override val parameterList: ParameterList
|
||||||
get() = super.parameterList!!
|
get() = super.parameterList!!
|
||||||
|
|
||||||
protected override fun presentationModifiers(): Modifiers {
|
override fun presentationModifiers(): Modifiers {
|
||||||
var modifiers = this.modifiers
|
var modifiers = this.modifiers
|
||||||
if (isInInterface) {
|
if (isInInterface) {
|
||||||
modifiers = modifiers.without(Modifier.ABSTRACT)
|
modifiers = modifiers.without(Modifier.ABSTRACT)
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ class Import(val name: String) : Element() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ImportList(public var imports: List<Import>) : Element() {
|
class ImportList(var imports: List<Import>) : Element() {
|
||||||
override val isEmpty: Boolean
|
override val isEmpty: Boolean
|
||||||
get() = imports.isEmpty()
|
get() = imports.isEmpty()
|
||||||
|
|
||||||
@@ -40,10 +40,10 @@ class ImportList(public var imports: List<Import>) : Element() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Converter.convertImportList(importList: PsiImportList): ImportList =
|
fun Converter.convertImportList(importList: PsiImportList): ImportList =
|
||||||
ImportList(importList.allImportStatements.mapNotNull { convertImport(it, true) }).assignPrototype(importList)
|
ImportList(importList.allImportStatements.mapNotNull { convertImport(it, true) }).assignPrototype(importList)
|
||||||
|
|
||||||
public fun Converter.convertImport(anImport: PsiImportStatementBase, filter: Boolean): Import? {
|
fun Converter.convertImport(anImport: PsiImportStatementBase, filter: Boolean): Import? {
|
||||||
fun doConvert(): Import? {
|
fun doConvert(): Import? {
|
||||||
val reference = anImport.importReference ?: return null
|
val reference = anImport.importReference ?: return null
|
||||||
val qualifiedName = quoteKeywords(reference.qualifiedName!!)
|
val qualifiedName = quoteKeywords(reference.qualifiedName!!)
|
||||||
|
|||||||
@@ -32,19 +32,19 @@ class MethodCallExpression(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
public fun buildNotNull(receiver: Expression?,
|
fun buildNotNull(receiver: Expression?,
|
||||||
methodName: String,
|
methodName: String,
|
||||||
arguments: List<Expression> = listOf(),
|
arguments: List<Expression> = listOf(),
|
||||||
typeArguments: List<Type> = listOf()): MethodCallExpression
|
typeArguments: List<Type> = listOf()): MethodCallExpression
|
||||||
= build(receiver, methodName, arguments, typeArguments, false)
|
= build(receiver, methodName, arguments, typeArguments, false)
|
||||||
|
|
||||||
public fun buildNullable(receiver: Expression?,
|
fun buildNullable(receiver: Expression?,
|
||||||
methodName: String,
|
methodName: String,
|
||||||
arguments: List<Expression> = listOf(),
|
arguments: List<Expression> = listOf(),
|
||||||
typeArguments: List<Type> = listOf()): MethodCallExpression
|
typeArguments: List<Type> = listOf()): MethodCallExpression
|
||||||
= build(receiver, methodName, arguments, typeArguments, true)
|
= build(receiver, methodName, arguments, typeArguments, true)
|
||||||
|
|
||||||
public fun build(receiver: Expression?,
|
fun build(receiver: Expression?,
|
||||||
methodName: String,
|
methodName: String,
|
||||||
arguments: List<Expression>,
|
arguments: List<Expression>,
|
||||||
typeArguments: List<Type>,
|
typeArguments: List<Type>,
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ enum class Modifier(private val str: String) {
|
|||||||
OVERRIDE("override"),
|
OVERRIDE("override"),
|
||||||
INNER("inner");
|
INNER("inner");
|
||||||
|
|
||||||
public fun toKotlin(): String = str
|
fun toKotlin(): String = str
|
||||||
}
|
}
|
||||||
|
|
||||||
val ACCESS_MODIFIERS = setOf(Modifier.PUBLIC, Modifier.PROTECTED, Modifier.PRIVATE, Modifier.INTERNAL)
|
val ACCESS_MODIFIERS = setOf(Modifier.PUBLIC, Modifier.PROTECTED, Modifier.PRIVATE, Modifier.INTERNAL)
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ class FunctionParameter(identifier: Identifier,
|
|||||||
val modifiers: Modifiers,
|
val modifiers: Modifiers,
|
||||||
val defaultValue: DeferredElement<Expression>? = null
|
val defaultValue: DeferredElement<Expression>? = null
|
||||||
): Parameter(identifier, type) {
|
): Parameter(identifier, type) {
|
||||||
public enum class VarValModifier {
|
enum class VarValModifier {
|
||||||
None,
|
None,
|
||||||
Val,
|
Val,
|
||||||
Var
|
Var
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ private class PropertyDetector(
|
|||||||
) {
|
) {
|
||||||
private val isOpenClass = converter.needOpenModifier(psiClass)
|
private val isOpenClass = converter.needOpenModifier(psiClass)
|
||||||
|
|
||||||
public fun detectProperties(): Map<PsiMember, PropertyInfo> {
|
fun detectProperties(): Map<PsiMember, PropertyInfo> {
|
||||||
val methodsToCheck = ArrayList<Pair<PsiMethod, SuperInfo.Property?>>()
|
val methodsToCheck = ArrayList<Pair<PsiMethod, SuperInfo.Property?>>()
|
||||||
for (method in psiClass.methods) {
|
for (method in psiClass.methods) {
|
||||||
val name = method.name
|
val name = method.name
|
||||||
|
|||||||
@@ -20,6 +20,6 @@ import com.intellij.psi.PsiReference
|
|||||||
|
|
||||||
class ElementRenamedCodeProcessor(private val newName: String) : ExternalCodeProcessor {
|
class ElementRenamedCodeProcessor(private val newName: String) : ExternalCodeProcessor {
|
||||||
override fun processUsage(reference: PsiReference): Array<PsiReference>? {
|
override fun processUsage(reference: PsiReference): Array<PsiReference>? {
|
||||||
return reference.handleElementRename(newName).getReferences()
|
return reference.handleElementRename(newName).references
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.j2k.usageProcessing
|
|||||||
|
|
||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
|
|
||||||
public class MethodIntoObjectProcessing(private val method: PsiMethod, private val objectName: String) : UsageProcessing {
|
class MethodIntoObjectProcessing(private val method: PsiMethod, private val objectName: String) : UsageProcessing {
|
||||||
override val targetElement: PsiElement get() = method
|
override val targetElement: PsiElement get() = method
|
||||||
|
|
||||||
override val convertedCodeProcessor: ConvertedCodeProcessor? get() = null
|
override val convertedCodeProcessor: ConvertedCodeProcessor? get() = null
|
||||||
|
|||||||
@@ -44,10 +44,10 @@ import org.jetbrains.kotlin.utils.PathUtil
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.URLClassLoader
|
import java.net.URLClassLoader
|
||||||
|
|
||||||
public abstract class AbstractJavaToKotlinConverterForWebDemoTest : TestCase() {
|
abstract class AbstractJavaToKotlinConverterForWebDemoTest : TestCase() {
|
||||||
val DISPOSABLE = Disposer.newDisposable()
|
val DISPOSABLE = Disposer.newDisposable()
|
||||||
|
|
||||||
public fun doTest(javaPath: String) {
|
fun doTest(javaPath: String) {
|
||||||
try {
|
try {
|
||||||
val fileContents = FileUtil.loadFile(File(javaPath), true)
|
val fileContents = FileUtil.loadFile(File(javaPath), true)
|
||||||
val javaCoreEnvironment: JavaCoreProjectEnvironment = setUpJavaCoreEnvironment()
|
val javaCoreEnvironment: JavaCoreProjectEnvironment = setUpJavaCoreEnvironment()
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ import org.jetbrains.kotlin.test.KotlinTestUtils
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
public abstract class AbstractJavaToKotlinConverterMultiFileTest : AbstractJavaToKotlinConverterTest() {
|
abstract class AbstractJavaToKotlinConverterMultiFileTest : AbstractJavaToKotlinConverterTest() {
|
||||||
public fun doTest(dirPath: String) {
|
fun doTest(dirPath: String) {
|
||||||
val project = LightPlatformTestCase.getProject()!!
|
val project = LightPlatformTestCase.getProject()!!
|
||||||
val psiManager = PsiManager.getInstance(project)
|
val psiManager = PsiManager.getInstance(project)
|
||||||
|
|
||||||
|
|||||||
@@ -31,10 +31,10 @@ import org.jetbrains.kotlin.test.KotlinTestUtils
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
public abstract class AbstractJavaToKotlinConverterSingleFileTest : AbstractJavaToKotlinConverterTest() {
|
abstract class AbstractJavaToKotlinConverterSingleFileTest : AbstractJavaToKotlinConverterTest() {
|
||||||
val testHeaderPattern = Pattern.compile("//(element|expression|statement|method|class|file|comp)\n")
|
val testHeaderPattern = Pattern.compile("//(element|expression|statement|method|class|file|comp)\n")
|
||||||
|
|
||||||
public fun doTest(javaPath: String) {
|
fun doTest(javaPath: String) {
|
||||||
val project = LightPlatformTestCase.getProject()!!
|
val project = LightPlatformTestCase.getProject()!!
|
||||||
val javaFile = File(javaPath)
|
val javaFile = File(javaPath)
|
||||||
val fileContents = FileUtil.loadFile(javaFile, true)
|
val fileContents = FileUtil.loadFile(javaFile, true)
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
|||||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
public abstract class AbstractJavaToKotlinConverterTest : LightCodeInsightFixtureTestCase() {
|
abstract class AbstractJavaToKotlinConverterTest : LightCodeInsightFixtureTestCase() {
|
||||||
override fun setUp() {
|
override fun setUp() {
|
||||||
super.setUp()
|
super.setUp()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user