Refactor converter:
Move some of the code out of Converter.kt to newly created MainMethodUtils.kt and ConstructorUtils.kt Get rid of class object of Converter class
This commit is contained in:
+35
-7
@@ -14,18 +14,45 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k.visitors
|
||||
package org.jetbrains.jet.j2k
|
||||
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.PsiMethod
|
||||
import com.intellij.psi.PsiClass
|
||||
import com.intellij.psi.PsiReferenceExpression
|
||||
import com.intellij.psi.PsiReference
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.JavaRecursiveElementVisitor
|
||||
import java.util.LinkedHashSet
|
||||
|
||||
public open class ThisVisitor() : JavaRecursiveElementVisitor() {
|
||||
public fun isConstructorPrimary(constructor: PsiMethod): Boolean {
|
||||
val parent = constructor.getParent()
|
||||
if (parent is PsiClass) {
|
||||
if (parent.getConstructors().size == 1) {
|
||||
return true
|
||||
}
|
||||
else {
|
||||
val c = getPrimaryConstructorForThisCase(parent)
|
||||
if (c != null && c.hashCode() == constructor.hashCode()) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun getPrimaryConstructorForThisCase(psiClass: PsiClass): PsiMethod? {
|
||||
val tv = FindPrimaryConstructorVisitor()
|
||||
psiClass.accept(tv)
|
||||
return tv.getPrimaryConstructor()
|
||||
}
|
||||
|
||||
private class FindPrimaryConstructorVisitor() : JavaRecursiveElementVisitor() {
|
||||
private val myResolvedConstructors = LinkedHashSet<PsiMethod>()
|
||||
|
||||
public override fun visitReferenceExpression(expression: PsiReferenceExpression?) {
|
||||
for (r : PsiReference? in expression?.getReferences()!!) {
|
||||
if (r?.getCanonicalText() == "this") {
|
||||
val res: PsiElement? = r?.resolve()
|
||||
for (r in expression?.getReferences()!!) {
|
||||
if (r.getCanonicalText() == "this") {
|
||||
val res: PsiElement? = r.resolve()
|
||||
if (res is PsiMethod && res.isConstructor()) {
|
||||
myResolvedConstructors.add(res)
|
||||
}
|
||||
@@ -33,7 +60,7 @@ public open class ThisVisitor() : JavaRecursiveElementVisitor() {
|
||||
}
|
||||
}
|
||||
|
||||
public open fun getPrimaryConstructor(): PsiMethod? {
|
||||
public fun getPrimaryConstructor(): PsiMethod? {
|
||||
if (myResolvedConstructors.size() > 0) {
|
||||
val first: PsiMethod = myResolvedConstructors.iterator().next()
|
||||
for (m in myResolvedConstructors)
|
||||
@@ -46,3 +73,4 @@ public open class ThisVisitor() : JavaRecursiveElementVisitor() {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,8 +29,6 @@ import org.jetbrains.jet.lang.types.expressions.OperatorConventions
|
||||
import java.util.*
|
||||
import com.intellij.psi.CommonClassNames.*
|
||||
import org.jetbrains.jet.lang.types.expressions.OperatorConventions.*
|
||||
import com.intellij.openapi.util.Pair
|
||||
import java.text.MessageFormat
|
||||
import com.intellij.psi.util.PsiUtil
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName
|
||||
import org.jetbrains.jet.util.QualifiedNamesUtil
|
||||
@@ -176,7 +174,7 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
if (m is Constructor) {
|
||||
if (!m.isPrimary) {
|
||||
for (fo in finalOrWithEmptyInitializer){
|
||||
val init: String = getDefaultInitializer(fo)
|
||||
val init = getDefaultInitializer(fo)
|
||||
initializers.put(fo.identifier.toKotlin(), init)
|
||||
}
|
||||
val newStatements = ArrayList<Element>()
|
||||
@@ -505,323 +503,209 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
return expression
|
||||
}
|
||||
|
||||
class object {
|
||||
public val NOT_NULL_ANNOTATIONS: Set<String> = ImmutableSet.of<String>("org.jetbrains.annotations.NotNull", "com.sun.istack.internal.NotNull", "javax.annotation.Nonnull")!!
|
||||
public val PRIMITIVE_TYPE_CONVERSIONS: Map<String, String> = ImmutableMap.builder<String, String>()
|
||||
?.put("byte", BYTE.asString())
|
||||
?.put("short", SHORT.asString())
|
||||
?.put("int", INT.asString())
|
||||
?.put("long", LONG.asString())
|
||||
?.put("float", FLOAT.asString())
|
||||
?.put("double", DOUBLE.asString())
|
||||
?.put("char", CHAR.asString())
|
||||
?.put(JAVA_LANG_BYTE, BYTE.asString())
|
||||
?.put(JAVA_LANG_SHORT, SHORT.asString())
|
||||
?.put(JAVA_LANG_INTEGER, INT.asString())
|
||||
?.put(JAVA_LANG_LONG, LONG.asString())
|
||||
?.put(JAVA_LANG_FLOAT, FLOAT.asString())
|
||||
?.put(JAVA_LANG_DOUBLE, DOUBLE.asString())
|
||||
?.put(JAVA_LANG_CHARACTER, CHAR.asString())
|
||||
?.build()!!
|
||||
private fun quoteKeywords(packageName: String): String {
|
||||
return packageName.split("\\.").map { Identifier(it).toKotlin() }.makeString(".")
|
||||
}
|
||||
|
||||
private fun quoteKeywords(packageName: String): String {
|
||||
return packageName.split("\\.").map { Identifier(it).toKotlin() }.makeString(".")
|
||||
}
|
||||
|
||||
private fun getFinalOrWithEmptyInitializer(fields: List<Field>): List<Field> {
|
||||
val result = ArrayList<Field>()
|
||||
for (f : Field in fields)
|
||||
if (f.isVal() || f.initializer.toKotlin().isEmpty()) {
|
||||
result.add(f)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun createParametersFromFields(fields: List<Field>): List<Parameter> {
|
||||
return fields.map { Parameter(Identifier("_" + it.identifier.name), it.`type`, true) }
|
||||
}
|
||||
|
||||
private fun createInitStatementsFromFields(fields: List<Field>): List<Element> {
|
||||
val result = ArrayList<Element>()
|
||||
for (f : Field in fields) {
|
||||
val identifierToKotlin: String? = f.identifier.toKotlin()
|
||||
result.add(DummyStringExpression(identifierToKotlin + " = " + "_" + identifierToKotlin))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun createPrimaryConstructorInvocation(s: String, fields: List<Field>, initializers: Map<String, String>): String {
|
||||
return s + "(" + fields.map { initializers[it.identifier.toKotlin()] }.makeString(", ") + ")"
|
||||
}
|
||||
|
||||
public open fun getDefaultInitializer(f: Field): String {
|
||||
if (f.`type`.nullable) {
|
||||
return "null"
|
||||
}
|
||||
else {
|
||||
val typeToKotlin: String = f.`type`.toKotlin()
|
||||
if (typeToKotlin.equals("Boolean"))
|
||||
return "false"
|
||||
|
||||
if (typeToKotlin.equals("Char"))
|
||||
return "' '"
|
||||
|
||||
if (typeToKotlin.equals("Double"))
|
||||
return "0." + OperatorConventions.DOUBLE + "()"
|
||||
|
||||
if (typeToKotlin.equals("Float"))
|
||||
return "0." + OperatorConventions.FLOAT + "()"
|
||||
|
||||
return "0"
|
||||
}
|
||||
}
|
||||
|
||||
private fun getPrimaryConstructorForThisCase(psiClass: PsiClass): PsiMethod? {
|
||||
val tv = ThisVisitor()
|
||||
psiClass.accept(tv)
|
||||
return tv.getPrimaryConstructor()
|
||||
}
|
||||
|
||||
public open fun isConstructorPrimary(constructor: PsiMethod): Boolean {
|
||||
val parent = constructor.getParent()
|
||||
if (parent is PsiClass) {
|
||||
if (parent.getConstructors().size == 1) {
|
||||
return true
|
||||
}
|
||||
else {
|
||||
val c: PsiMethod? = getPrimaryConstructorForThisCase(parent)
|
||||
if (c != null && c.hashCode() == constructor.hashCode()) {
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
private fun getFinalOrWithEmptyInitializer(fields: List<Field>): List<Field> {
|
||||
val result = ArrayList<Field>()
|
||||
for (f : Field in fields)
|
||||
if (f.isVal() || f.initializer.toKotlin().isEmpty()) {
|
||||
result.add(f)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
private fun removeEmpty(statements: List<Element>): List<Element> {
|
||||
return statements.filterNot {
|
||||
it == Statement.EMPTY_STATEMENT ||
|
||||
it == Expression.EMPTY_EXPRESSION ||
|
||||
it == Element.EMPTY_ELEMENT
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun isNotOpenMethod(method: PsiMethod): Boolean {
|
||||
val parent = method.getParent()
|
||||
if (parent is PsiClass) {
|
||||
val parentModifierList = parent.getModifierList()
|
||||
if ((parentModifierList != null && parentModifierList.hasExplicitModifier(PsiModifier.FINAL)) || parent.isEnum()) {
|
||||
return true
|
||||
}
|
||||
private fun createParametersFromFields(fields: List<Field>): List<Parameter> {
|
||||
return fields.map { Parameter(Identifier("_" + it.identifier.name), it.`type`, true) }
|
||||
}
|
||||
|
||||
private fun createInitStatementsFromFields(fields: List<Field>): List<Element> {
|
||||
val result = ArrayList<Element>()
|
||||
for (f : Field in fields) {
|
||||
val identifierToKotlin: String? = f.identifier.toKotlin()
|
||||
result.add(DummyStringExpression(identifierToKotlin + " = " + "_" + identifierToKotlin))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun createPrimaryConstructorInvocation(s: String, fields: List<Field>, initializers: Map<String, String>): String {
|
||||
return s + "(" + fields.map { initializers[it.identifier.toKotlin()] }.makeString(", ") + ")"
|
||||
}
|
||||
|
||||
private fun removeEmpty(statements: List<Element>): List<Element> {
|
||||
return statements.filterNot {
|
||||
it == Statement.EMPTY_STATEMENT ||
|
||||
it == Expression.EMPTY_EXPRESSION ||
|
||||
it == Element.EMPTY_ELEMENT
|
||||
}
|
||||
}
|
||||
|
||||
private fun isNotOpenMethod(method: PsiMethod): Boolean {
|
||||
val parent = method.getParent()
|
||||
if (parent is PsiClass) {
|
||||
val parentModifierList = parent.getModifierList()
|
||||
if ((parentModifierList != null && parentModifierList.hasExplicitModifier(PsiModifier.FINAL)) || parent.isEnum()) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
private fun normalCase(method: PsiMethod): Boolean {
|
||||
var counter: Int = 0
|
||||
for (s : HierarchicalMethodSignature? in method.getHierarchicalMethodSignature().getSuperSignatures())
|
||||
return false
|
||||
}
|
||||
|
||||
private fun normalCase(method: PsiMethod): Boolean {
|
||||
var counter: Int = 0
|
||||
for (s : HierarchicalMethodSignature? in method.getHierarchicalMethodSignature().getSuperSignatures())
|
||||
{
|
||||
var containingClass: PsiClass? = s?.getMethod()?.getContainingClass()
|
||||
var qualifiedName: String? = (if (containingClass != null)
|
||||
containingClass?.getQualifiedName()
|
||||
else
|
||||
"")
|
||||
if (qualifiedName != null && !qualifiedName.equals(JAVA_LANG_OBJECT))
|
||||
{
|
||||
var containingClass: PsiClass? = s?.getMethod()?.getContainingClass()
|
||||
var qualifiedName: String? = (if (containingClass != null)
|
||||
containingClass?.getQualifiedName()
|
||||
else
|
||||
"")
|
||||
if (qualifiedName != null && !qualifiedName.equals(JAVA_LANG_OBJECT))
|
||||
{
|
||||
counter++
|
||||
}
|
||||
|
||||
counter++
|
||||
}
|
||||
return counter > 0
|
||||
|
||||
}
|
||||
return counter > 0
|
||||
}
|
||||
|
||||
private fun isInheritFromObject(method: PsiMethod): Boolean {
|
||||
var superSignatures: List<HierarchicalMethodSignature?> = method.getHierarchicalMethodSignature().getSuperSignatures()
|
||||
for (s : HierarchicalMethodSignature? in superSignatures) {
|
||||
var containingClass: PsiClass? = s?.getMethod()?.getContainingClass()
|
||||
var qualifiedName: String? = (if (containingClass != null)
|
||||
containingClass?.getQualifiedName()
|
||||
else
|
||||
"")
|
||||
if (qualifiedName == JAVA_LANG_OBJECT) {
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun isOverrideObjectDirect(method: PsiMethod): Boolean {
|
||||
var superSignatures: List<HierarchicalMethodSignature?>? = method.getHierarchicalMethodSignature().getSuperSignatures()
|
||||
if (superSignatures?.size()!! == 1)
|
||||
{
|
||||
val containingClass: PsiClass? = superSignatures?.get(0)?.getMethod()?.getContainingClass()
|
||||
val qualifiedName: String? = (if (containingClass != null)
|
||||
containingClass.getQualifiedName()
|
||||
else
|
||||
"")
|
||||
if (qualifiedName == JAVA_LANG_OBJECT) {
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun isInheritFromObject(method: PsiMethod): Boolean {
|
||||
var superSignatures: List<HierarchicalMethodSignature?> = method.getHierarchicalMethodSignature().getSuperSignatures()
|
||||
for (s : HierarchicalMethodSignature? in superSignatures) {
|
||||
var containingClass: PsiClass? = s?.getMethod()?.getContainingClass()
|
||||
var qualifiedName: String? = (if (containingClass != null)
|
||||
containingClass?.getQualifiedName()
|
||||
else
|
||||
"")
|
||||
if (qualifiedName == JAVA_LANG_OBJECT) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
private fun importsToImportList(imports: Array<PsiImportStatementBase>): List<Import> {
|
||||
val result = ArrayList<Import>()
|
||||
for (i : PsiImportStatementBase? in imports) {
|
||||
if (i == null) continue
|
||||
val anImport: Import = importToImport(i)
|
||||
val name: String = anImport.name
|
||||
if (!name.isEmpty() && !NOT_NULL_ANNOTATIONS.contains(name)) {
|
||||
result.add(anImport)
|
||||
}
|
||||
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun importToImport(i: PsiImportStatementBase): Import {
|
||||
val reference: PsiJavaCodeReferenceElement? = i.getImportReference()
|
||||
if (reference != null) {
|
||||
return Import(quoteKeywords(reference.getQualifiedName()!!) + ((if (i.isOnDemand())
|
||||
".*"
|
||||
else
|
||||
"")))
|
||||
}
|
||||
|
||||
return Import("")
|
||||
}
|
||||
|
||||
public fun identifierToIdentifier(identifier: PsiIdentifier?): Identifier {
|
||||
if (identifier == null)
|
||||
return Identifier.EMPTY_IDENTIFIER
|
||||
|
||||
return Identifier(identifier.getText()!!)
|
||||
}
|
||||
|
||||
public fun modifiersListToModifiersSet(modifierList: PsiModifierList?): MutableSet<Modifier> {
|
||||
val modifiersSet: HashSet<Modifier> = hashSetOf()
|
||||
if (modifierList != null) {
|
||||
if (modifierList.hasExplicitModifier(PsiModifier.ABSTRACT))
|
||||
modifiersSet.add(Modifier.ABSTRACT)
|
||||
|
||||
if (modifierList.hasModifierProperty(PsiModifier.FINAL))
|
||||
modifiersSet.add(Modifier.FINAL)
|
||||
|
||||
if (modifierList.hasModifierProperty(PsiModifier.STATIC))
|
||||
modifiersSet.add(Modifier.STATIC)
|
||||
|
||||
if (modifierList.hasExplicitModifier(PsiModifier.PUBLIC))
|
||||
modifiersSet.add(Modifier.PUBLIC)
|
||||
|
||||
if (modifierList.hasExplicitModifier(PsiModifier.PROTECTED))
|
||||
modifiersSet.add(Modifier.PROTECTED)
|
||||
|
||||
if (modifierList.hasExplicitModifier(PsiModifier.PACKAGE_LOCAL))
|
||||
modifiersSet.add(Modifier.INTERNAL)
|
||||
|
||||
if (modifierList.hasExplicitModifier(PsiModifier.PRIVATE))
|
||||
modifiersSet.add(Modifier.PRIVATE)
|
||||
}
|
||||
|
||||
return modifiersSet
|
||||
}
|
||||
|
||||
private fun isConversionNeeded(actual: PsiType?, expected: PsiType?): Boolean {
|
||||
if (actual == null || expected == null) {
|
||||
return false
|
||||
}
|
||||
|
||||
private fun isOverrideObjectDirect(method: PsiMethod): Boolean {
|
||||
var superSignatures: List<HierarchicalMethodSignature?>? = method.getHierarchicalMethodSignature().getSuperSignatures()
|
||||
if (superSignatures?.size()!! == 1)
|
||||
{
|
||||
val containingClass: PsiClass? = superSignatures?.get(0)?.getMethod()?.getContainingClass()
|
||||
val qualifiedName: String? = (if (containingClass != null)
|
||||
containingClass.getQualifiedName()
|
||||
else
|
||||
"")
|
||||
if (qualifiedName == JAVA_LANG_OBJECT) {
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
private fun importsToImportList(imports: Array<PsiImportStatementBase>): List<Import> {
|
||||
val result = ArrayList<Import>()
|
||||
for (i : PsiImportStatementBase? in imports) {
|
||||
if (i == null) continue
|
||||
val anImport: Import = importToImport(i)
|
||||
val name: String = anImport.name
|
||||
if (!name.isEmpty() && !NOT_NULL_ANNOTATIONS.contains(name)) {
|
||||
result.add(anImport)
|
||||
}
|
||||
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun importToImport(i: PsiImportStatementBase): Import {
|
||||
val reference: PsiJavaCodeReferenceElement? = i.getImportReference()
|
||||
if (reference != null) {
|
||||
return Import(quoteKeywords(reference.getQualifiedName()!!) + ((if (i.isOnDemand())
|
||||
".*"
|
||||
else
|
||||
"")))
|
||||
}
|
||||
|
||||
return Import("")
|
||||
}
|
||||
|
||||
public open fun identifierToIdentifier(identifier: PsiIdentifier?): Identifier {
|
||||
if (identifier == null)
|
||||
return Identifier.EMPTY_IDENTIFIER
|
||||
|
||||
return Identifier(identifier.getText()!!)
|
||||
}
|
||||
|
||||
public open fun modifiersListToModifiersSet(modifierList: PsiModifierList?): MutableSet<Modifier> {
|
||||
val modifiersSet: HashSet<Modifier> = hashSetOf()
|
||||
if (modifierList != null) {
|
||||
if (modifierList.hasExplicitModifier(PsiModifier.ABSTRACT))
|
||||
modifiersSet.add(Modifier.ABSTRACT)
|
||||
|
||||
if (modifierList.hasModifierProperty(PsiModifier.FINAL))
|
||||
modifiersSet.add(Modifier.FINAL)
|
||||
|
||||
if (modifierList.hasModifierProperty(PsiModifier.STATIC))
|
||||
modifiersSet.add(Modifier.STATIC)
|
||||
|
||||
if (modifierList.hasExplicitModifier(PsiModifier.PUBLIC))
|
||||
modifiersSet.add(Modifier.PUBLIC)
|
||||
|
||||
if (modifierList.hasExplicitModifier(PsiModifier.PROTECTED))
|
||||
modifiersSet.add(Modifier.PROTECTED)
|
||||
|
||||
if (modifierList.hasExplicitModifier(PsiModifier.PACKAGE_LOCAL))
|
||||
modifiersSet.add(Modifier.INTERNAL)
|
||||
|
||||
if (modifierList.hasExplicitModifier(PsiModifier.PRIVATE))
|
||||
modifiersSet.add(Modifier.PRIVATE)
|
||||
}
|
||||
|
||||
return modifiersSet
|
||||
}
|
||||
|
||||
private fun isConversionNeeded(actual: PsiType?, expected: PsiType?): Boolean {
|
||||
if (actual == null || expected == null) {
|
||||
return false
|
||||
}
|
||||
|
||||
val typeMap = HashMap<String, String>()
|
||||
typeMap.put(JAVA_LANG_BYTE, "byte")
|
||||
typeMap.put(JAVA_LANG_SHORT, "short")
|
||||
typeMap.put(JAVA_LANG_INTEGER, "int")
|
||||
typeMap.put(JAVA_LANG_LONG, "long")
|
||||
typeMap.put(JAVA_LANG_FLOAT, "float")
|
||||
typeMap.put(JAVA_LANG_DOUBLE, "double")
|
||||
typeMap.put(JAVA_LANG_CHARACTER, "char")
|
||||
val expectedStr: String? = expected.getCanonicalText()
|
||||
val actualStr: String? = actual.getCanonicalText()
|
||||
val o1: Boolean = expectedStr == typeMap[actualStr]
|
||||
val o2: Boolean = actualStr == typeMap[expectedStr]
|
||||
return actualStr != expectedStr && (!(o1 xor o2))
|
||||
}
|
||||
val typeMap = HashMap<String, String>()
|
||||
typeMap.put(JAVA_LANG_BYTE, "byte")
|
||||
typeMap.put(JAVA_LANG_SHORT, "short")
|
||||
typeMap.put(JAVA_LANG_INTEGER, "int")
|
||||
typeMap.put(JAVA_LANG_LONG, "long")
|
||||
typeMap.put(JAVA_LANG_FLOAT, "float")
|
||||
typeMap.put(JAVA_LANG_DOUBLE, "double")
|
||||
typeMap.put(JAVA_LANG_CHARACTER, "char")
|
||||
val expectedStr: String? = expected.getCanonicalText()
|
||||
val actualStr: String? = actual.getCanonicalText()
|
||||
val o1: Boolean = expectedStr == typeMap[actualStr]
|
||||
val o2: Boolean = actualStr == typeMap[expectedStr]
|
||||
return actualStr != expectedStr && (!(o1 xor o2))
|
||||
}
|
||||
}
|
||||
|
||||
public fun createMainFunction(file: PsiFile): String {
|
||||
val classNamesWithMains = ArrayList<Pair<String?, PsiMethod?>?>()
|
||||
for (c : PsiClass? in (file as PsiJavaFile).getClasses()) {
|
||||
var main: PsiMethod? = findMainMethod(c)
|
||||
if (main != null) {
|
||||
classNamesWithMains.add(Pair<String?, PsiMethod?>(c?.getName(), main))
|
||||
}
|
||||
}
|
||||
private val NOT_NULL_ANNOTATIONS: Set<String> = ImmutableSet.of<String>("org.jetbrains.annotations.NotNull", "com.sun.istack.internal.NotNull", "javax.annotation.Nonnull")!!
|
||||
private val PRIMITIVE_TYPE_CONVERSIONS: Map<String, String> = ImmutableMap.builder<String, String>()
|
||||
?.put("byte", BYTE.asString())
|
||||
?.put("short", SHORT.asString())
|
||||
?.put("int", INT.asString())
|
||||
?.put("long", LONG.asString())
|
||||
?.put("float", FLOAT.asString())
|
||||
?.put("double", DOUBLE.asString())
|
||||
?.put("char", CHAR.asString())
|
||||
?.put(JAVA_LANG_BYTE, BYTE.asString())
|
||||
?.put(JAVA_LANG_SHORT, SHORT.asString())
|
||||
?.put(JAVA_LANG_INTEGER, INT.asString())
|
||||
?.put(JAVA_LANG_LONG, LONG.asString())
|
||||
?.put(JAVA_LANG_FLOAT, FLOAT.asString())
|
||||
?.put(JAVA_LANG_DOUBLE, DOUBLE.asString())
|
||||
?.put(JAVA_LANG_CHARACTER, CHAR.asString())
|
||||
?.build()!!
|
||||
|
||||
if (classNamesWithMains.size() > 0) {
|
||||
var className: String? = classNamesWithMains.get(0)?.getFirst()
|
||||
return MessageFormat.format("fun main(args : Array<String>) = {0}.main(args as Array<String?>?)", className)
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
}
|
||||
|
||||
private fun findMainMethod(aClass: PsiClass?): PsiMethod? {
|
||||
if (isMainClass(aClass)) {
|
||||
val mainMethods: Array<PsiMethod>? = aClass?.findMethodsByName("main", false)
|
||||
if (mainMethods != null) {
|
||||
return findMainMethod(mainMethods)
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun isMainClass(psiClass: PsiClass?): Boolean {
|
||||
if (psiClass == null || psiClass is PsiAnonymousClass)
|
||||
return false
|
||||
|
||||
if (psiClass.isInterface())
|
||||
return false
|
||||
|
||||
return psiClass.getContainingClass() == null || psiClass.hasModifierProperty(PsiModifier.STATIC)
|
||||
|
||||
}
|
||||
|
||||
private fun findMainMethod(mainMethods: Array<PsiMethod>): PsiMethod? {
|
||||
return mainMethods.find { isMainMethod(it) }
|
||||
}
|
||||
|
||||
public fun isMainMethod(method: PsiMethod?): Boolean {
|
||||
if (method == null || method.getContainingClass() == null)
|
||||
return false
|
||||
|
||||
if (PsiType.VOID != method.getReturnType())
|
||||
return false
|
||||
|
||||
if (!method.hasModifierProperty(PsiModifier.STATIC))
|
||||
return false
|
||||
|
||||
if (!method.hasModifierProperty(PsiModifier.PUBLIC))
|
||||
return false
|
||||
|
||||
val parameters: Array<PsiParameter>? = method.getParameterList().getParameters()
|
||||
if (parameters?.size!! != 1)
|
||||
return false
|
||||
|
||||
val `type`: PsiType? = parameters!![0].getType()
|
||||
if (`type` !is PsiArrayType)
|
||||
return false
|
||||
|
||||
val componentType: PsiType? = `type`.getComponentType()
|
||||
return componentType?.equalsToText("java.lang.String")!!
|
||||
}
|
||||
|
||||
public fun countWritingAccesses(element: PsiElement?, container: PsiElement?): Int {
|
||||
var counter: Int = 0
|
||||
@@ -861,7 +745,7 @@ public fun isAnnotatedAsNotNull(modifierList: PsiModifierList?): Boolean {
|
||||
val annotations: Array<PsiAnnotation> = modifierList.getAnnotations()
|
||||
for (a : PsiAnnotation in annotations) {
|
||||
val qualifiedName: String? = a.getQualifiedName()
|
||||
if (qualifiedName != null && Converter.NOT_NULL_ANNOTATIONS.contains(qualifiedName)) {
|
||||
if (qualifiedName != null && NOT_NULL_ANNOTATIONS.contains(qualifiedName)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -874,3 +758,25 @@ public fun isDefinitelyNotNull(element: PsiElement?): Boolean = when(element) {
|
||||
is PsiNewExpression -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
public fun getDefaultInitializer(f: Field): String {
|
||||
if (f.`type`.nullable) {
|
||||
return "null"
|
||||
}
|
||||
else {
|
||||
val typeToKotlin = f.`type`.toKotlin()
|
||||
if (typeToKotlin.equals("Boolean"))
|
||||
return "false"
|
||||
|
||||
if (typeToKotlin.equals("Char"))
|
||||
return "' '"
|
||||
|
||||
if (typeToKotlin.equals("Double"))
|
||||
return "0." + OperatorConventions.DOUBLE + "()"
|
||||
|
||||
if (typeToKotlin.equals("Float"))
|
||||
return "0." + OperatorConventions.FLOAT + "()"
|
||||
|
||||
return "0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.j2k
|
||||
|
||||
import java.text.MessageFormat
|
||||
import com.intellij.psi.PsiMethod
|
||||
import com.intellij.psi.PsiClass
|
||||
import com.intellij.psi.PsiJavaFile
|
||||
import java.util.ArrayList
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiAnonymousClass
|
||||
import com.intellij.psi.PsiModifier
|
||||
import com.intellij.psi.PsiType
|
||||
import com.intellij.psi.PsiParameter
|
||||
import com.intellij.psi.PsiArrayType
|
||||
|
||||
public fun createMainFunction(file: PsiFile): String {
|
||||
val classNamesWithMains = ArrayList<Pair<String, PsiMethod>>()
|
||||
for (c in (file as PsiJavaFile).getClasses()) {
|
||||
val main = findMainMethod(c)
|
||||
val name = c.getName()
|
||||
if (name != null && main != null) {
|
||||
classNamesWithMains.add(Pair(name, main))
|
||||
}
|
||||
}
|
||||
|
||||
if (classNamesWithMains.size() > 0) {
|
||||
var className = classNamesWithMains.get(0).first
|
||||
return MessageFormat.format("fun main(args : Array<String>) = {0}.main(args as Array<String?>?)", className)
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
}
|
||||
|
||||
private fun findMainMethod(aClass: PsiClass): PsiMethod? {
|
||||
if (isMainClass(aClass)) {
|
||||
return findMainMethod(aClass.findMethodsByName("main", false))
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun isMainClass(psiClass: PsiClass): Boolean {
|
||||
if (psiClass is PsiAnonymousClass)
|
||||
return false
|
||||
|
||||
if (psiClass.isInterface())
|
||||
return false
|
||||
|
||||
return psiClass.getContainingClass() == null || psiClass.hasModifierProperty(PsiModifier.STATIC)
|
||||
|
||||
}
|
||||
|
||||
private fun findMainMethod(mainMethods: Array<PsiMethod>): PsiMethod? {
|
||||
return mainMethods.find { isMainMethod(it) }
|
||||
}
|
||||
|
||||
public fun isMainMethod(method: PsiMethod): Boolean {
|
||||
if (method.getContainingClass() == null)
|
||||
return false
|
||||
|
||||
if (PsiType.VOID != method.getReturnType())
|
||||
return false
|
||||
|
||||
if (!method.hasModifierProperty(PsiModifier.STATIC))
|
||||
return false
|
||||
|
||||
if (!method.hasModifierProperty(PsiModifier.PUBLIC))
|
||||
return false
|
||||
|
||||
val parameters = method.getParameterList().getParameters()
|
||||
if (parameters.size != 1)
|
||||
return false
|
||||
|
||||
val `type` = parameters[0].getType()
|
||||
if (`type` !is PsiArrayType)
|
||||
return false
|
||||
|
||||
val componentType = `type`.getComponentType()
|
||||
return componentType.equalsToText("java.lang.String")
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
import org.jetbrains.jet.j2k.*
|
||||
import java.util.ArrayList
|
||||
|
||||
public open class Field(val identifier: Identifier,
|
||||
@@ -51,7 +51,7 @@ public open class Field(val identifier: Identifier,
|
||||
return declaration + ((if (isVal() && !isStatic() && writingAccesses != 0)
|
||||
""
|
||||
else
|
||||
" = " + Converter.getDefaultInitializer(this)))
|
||||
" = " + getDefaultInitializer(this)))
|
||||
}
|
||||
|
||||
return declaration + " = " + initializer.toKotlin()
|
||||
|
||||
@@ -17,11 +17,9 @@
|
||||
package org.jetbrains.jet.j2k.visitors
|
||||
|
||||
import com.intellij.psi.*
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
import org.jetbrains.jet.j2k.*
|
||||
import org.jetbrains.jet.j2k.ast.*
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import org.jetbrains.jet.j2k.isAnnotatedAsNotNull
|
||||
import org.jetbrains.jet.j2k.isDefinitelyNotNull
|
||||
|
||||
public open class ElementVisitor(val myConverter: Converter) : JavaElementVisitor() {
|
||||
protected var myResult: Element = Element.EMPTY_ELEMENT
|
||||
@@ -41,7 +39,7 @@ public open class ElementVisitor(val myConverter: Converter) : JavaElementVisito
|
||||
kType = kType.convertedToNotNull();
|
||||
}
|
||||
myResult = LocalVariable(Identifier(theVariable.getName()!!),
|
||||
Converter.modifiersListToModifiersSet(theVariable.getModifierList()),
|
||||
myConverter.modifiersListToModifiersSet(theVariable.getModifierList()),
|
||||
kType,
|
||||
myConverter.expressionToExpression(theVariable.getInitializer(), theVariable.getType()),
|
||||
myConverter)
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.jetbrains.jet.j2k.visitors
|
||||
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.tree.IElementType
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
import org.jetbrains.jet.j2k.ast.*
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import org.jetbrains.jet.lang.types.expressions.OperatorConventions
|
||||
@@ -27,7 +26,7 @@ import java.util.Collections
|
||||
import com.intellij.psi.CommonClassNames.*
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.jet.lang.types.lang.PrimitiveType
|
||||
import org.jetbrains.jet.j2k.isAnnotatedAsNotNull
|
||||
import org.jetbrains.jet.j2k.*
|
||||
import org.jetbrains.jet.j2k.ast.types.ArrayType
|
||||
|
||||
public open class ExpressionVisitor(converter: Converter) : StatementVisitor(converter) {
|
||||
@@ -182,7 +181,7 @@ public open class ExpressionVisitor(converter: Converter) : StatementVisitor(con
|
||||
argumentList?.getExpressions()!!
|
||||
else
|
||||
array<PsiExpression>())
|
||||
if (constructor == null || Converter.isConstructorPrimary(constructor) || isNotConvertedClass)
|
||||
if (constructor == null || isConstructorPrimary(constructor) || isNotConvertedClass)
|
||||
{
|
||||
return NewClassExpression(getConverter().elementToElement(classReference),
|
||||
getConverter().argumentsToExpressionList(expression!!),
|
||||
@@ -314,7 +313,7 @@ public open class ExpressionVisitor(converter: Converter) : StatementVisitor(con
|
||||
val operand = expression?.getOperand()
|
||||
val operandType = operand?.getType()
|
||||
val typeText = castType.getType().getCanonicalText()
|
||||
val typeConversion = Converter.PRIMITIVE_TYPE_CONVERSIONS[typeText]
|
||||
val typeConversion = PRIMITIVE_TYPE_CONVERSIONS[typeText]
|
||||
if (operandType is PsiPrimitiveType && typeConversion != null) {
|
||||
myResult = MethodCallExpression.build(getConverter().expressionToExpression(operand), typeConversion)
|
||||
}
|
||||
@@ -459,7 +458,7 @@ public open class ExpressionVisitor(converter: Converter) : StatementVisitor(con
|
||||
var context: PsiElement? = expression.getContext()
|
||||
while (context != null) {
|
||||
if (context is PsiMethod && (context as PsiMethod).isConstructor()) {
|
||||
return !Converter.isConstructorPrimary((context as PsiMethod))
|
||||
return !isConstructorPrimary((context as PsiMethod))
|
||||
}
|
||||
|
||||
context = context?.getContext()
|
||||
@@ -471,7 +470,7 @@ public open class ExpressionVisitor(converter: Converter) : StatementVisitor(con
|
||||
var context: PsiElement? = expression.getContext()
|
||||
while (context != null) {
|
||||
if (context is PsiMethod && (context as PsiMethod).isConstructor()) {
|
||||
return Converter.isConstructorPrimary(context as PsiMethod)
|
||||
return isConstructorPrimary(context as PsiMethod)
|
||||
}
|
||||
|
||||
context = context?.getContext()
|
||||
|
||||
@@ -30,7 +30,7 @@ public open class StatementVisitor(converter: Converter) : ElementVisitor(conver
|
||||
}
|
||||
|
||||
public override fun visitBlockStatement(statement: PsiBlockStatement?) {
|
||||
myResult = myConverter.blockToBlock(statement?.getCodeBlock(), true)
|
||||
myResult = getConverter().blockToBlock(statement?.getCodeBlock(), true)
|
||||
}
|
||||
|
||||
public override fun visitBreakStatement(statement: PsiBreakStatement?) {
|
||||
@@ -39,7 +39,7 @@ public open class StatementVisitor(converter: Converter) : ElementVisitor(conver
|
||||
}
|
||||
else
|
||||
{
|
||||
myResult = BreakStatement(Converter.identifierToIdentifier(statement?.getLabelIdentifier()))
|
||||
myResult = BreakStatement(getConverter().identifierToIdentifier(statement?.getLabelIdentifier()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ public open class StatementVisitor(converter: Converter) : ElementVisitor(conver
|
||||
}
|
||||
else
|
||||
{
|
||||
myResult = ContinueStatement(Converter.identifierToIdentifier(statement?.getLabelIdentifier()))
|
||||
myResult = ContinueStatement(getConverter().identifierToIdentifier(statement?.getLabelIdentifier()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ public open class StatementVisitor(converter: Converter) : ElementVisitor(conver
|
||||
}
|
||||
|
||||
public override fun visitLabeledStatement(statement: PsiLabeledStatement?) {
|
||||
myResult = LabelStatement(Converter.identifierToIdentifier(statement?.getLabelIdentifier()),
|
||||
myResult = LabelStatement(getConverter().identifierToIdentifier(statement?.getLabelIdentifier()),
|
||||
getConverter().statementToStatement(statement?.getStatement()))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user