From 39190533b186b2308e51d7b1d0de9e15085d4aaf Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 31 May 2012 15:09:36 +0200 Subject: [PATCH] to compilable kotlin --- .../jet/j2k/visitors/ExpressionVisitor.kt | 16 +++-- ...ssionVisitorForDirectObjectInheritors.java | 70 ------------------- ...ressionVisitorForDirectObjectInheritors.kt | 40 +++++++++++ 3 files changed, 49 insertions(+), 77 deletions(-) delete mode 100644 src/org/jetbrains/jet/j2k/visitors/ExpressionVisitorForDirectObjectInheritors.java create mode 100644 src/org/jetbrains/jet/j2k/visitors/ExpressionVisitorForDirectObjectInheritors.kt diff --git a/src/org/jetbrains/jet/j2k/visitors/ExpressionVisitor.kt b/src/org/jetbrains/jet/j2k/visitors/ExpressionVisitor.kt index 64b4fbb0672..79b2307e40a 100644 --- a/src/org/jetbrains/jet/j2k/visitors/ExpressionVisitor.kt +++ b/src/org/jetbrains/jet/j2k/visitors/ExpressionVisitor.kt @@ -138,14 +138,16 @@ public open class ExpressionVisitor(converter: Converter): StatementVisitor(conv } public override fun visitMethodCallExpression(expression: PsiMethodCallExpression?): Unit { - if (!SuperVisitor.isSuper(expression!!.getMethodExpression()) || !isInsidePrimaryConstructor(expression!!)) - { - myResult = MethodCallExpression(getConverter().expressionToExpression(expression?.getMethodExpression()), - getConverter().argumentsToExpressionList(expression!!), - getConverter().typesToTypeList(expression?.getTypeArguments()!!), - getConverter().typeToType(expression?.getType()).nullable) - } + convertMethodCallExpression(expression!!) + } + protected fun convertMethodCallExpression(expression: PsiMethodCallExpression) { + if (!SuperVisitor.isSuper(expression.getMethodExpression()) || !isInsidePrimaryConstructor(expression)) { + myResult = MethodCallExpression(getConverter().expressionToExpression(expression.getMethodExpression()), + getConverter().argumentsToExpressionList(expression), + getConverter().typesToTypeList(expression.getTypeArguments()), + getConverter().typeToType(expression.getType()).nullable) + } } public override fun visitNewExpression(expression: PsiNewExpression?): Unit { diff --git a/src/org/jetbrains/jet/j2k/visitors/ExpressionVisitorForDirectObjectInheritors.java b/src/org/jetbrains/jet/j2k/visitors/ExpressionVisitorForDirectObjectInheritors.java deleted file mode 100644 index edf5b3857e4..00000000000 --- a/src/org/jetbrains/jet/j2k/visitors/ExpressionVisitorForDirectObjectInheritors.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2010-2012 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.visitors; - -import com.intellij.psi.*; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.j2k.Converter; -import org.jetbrains.jet.j2k.ast.DummyMethodCallExpression; -import org.jetbrains.jet.j2k.ast.DummyStringExpression; -import org.jetbrains.jet.j2k.ast.Identifier; - -import static com.intellij.psi.CommonClassNames.JAVA_LANG_OBJECT; - -/** - * @author ignatov - */ -public class ExpressionVisitorForDirectObjectInheritors extends ExpressionVisitor { - public ExpressionVisitorForDirectObjectInheritors(@NotNull Converter converter) { - super(converter); - } - - @Override - public void visitMethodCallExpression(@NotNull final PsiMethodCallExpression expression) { - if (superMethodInvocation(expression.getMethodExpression(), "hashCode")) { - myResult = new DummyMethodCallExpression(new Identifier("System"), "identityHashCode", new Identifier("this")); - } - else if (superMethodInvocation(expression.getMethodExpression(), "equals")) { - myResult = new DummyMethodCallExpression(new Identifier("this"), "identityEquals", getConverter().elementToElement(expression.getArgumentList())); - } - else if (superMethodInvocation(expression.getMethodExpression(), "toString")) { - myResult = new DummyStringExpression(String.format("getJavaClass<%s>.getName() + '@' + Integer.toHexString(hashCode())", getClassName(expression.getMethodExpression()))); - } - else { - super.visitMethodCallExpression(expression); - } - } - - @Override - public void visitReferenceExpression(@NotNull final PsiReferenceExpression expression) { - super.visitReferenceExpression(expression); - } - - private static boolean superMethodInvocation(@NotNull final PsiReferenceExpression expression, final String methodName) { - String referenceName = expression.getReferenceName(); - PsiExpression qualifierExpression = expression.getQualifierExpression(); - if (referenceName != null && referenceName.equals(methodName)) { - if (qualifierExpression instanceof PsiSuperExpression) { - PsiType type = qualifierExpression.getType(); - if (type != null && type.getCanonicalText().equals(JAVA_LANG_OBJECT)) { - return true; - } - } - } - return false; - } -} diff --git a/src/org/jetbrains/jet/j2k/visitors/ExpressionVisitorForDirectObjectInheritors.kt b/src/org/jetbrains/jet/j2k/visitors/ExpressionVisitorForDirectObjectInheritors.kt new file mode 100644 index 00000000000..26041a30ba1 --- /dev/null +++ b/src/org/jetbrains/jet/j2k/visitors/ExpressionVisitorForDirectObjectInheritors.kt @@ -0,0 +1,40 @@ +package org.jetbrains.jet.j2k.visitors + +import com.intellij.psi.* +import org.jetbrains.jet.j2k.Converter +import org.jetbrains.jet.j2k.ast.DummyMethodCallExpression +import org.jetbrains.jet.j2k.ast.DummyStringExpression +import org.jetbrains.jet.j2k.ast.Identifier +import com.intellij.psi.CommonClassNames.JAVA_LANG_OBJECT + +public open class ExpressionVisitorForDirectObjectInheritors(converter: Converter): ExpressionVisitor(converter) { + public override fun visitMethodCallExpression(expression: PsiMethodCallExpression?): Unit { + val methodExpression = expression?.getMethodExpression()!! + if (superMethodInvocation(methodExpression, "hashCode")) { + myResult = DummyMethodCallExpression(Identifier("System"), "identityHashCode", Identifier("this")) + } + else if (superMethodInvocation(methodExpression, "equals")) { + myResult = DummyMethodCallExpression(Identifier("this"), "identityEquals", getConverter().elementToElement(expression?.getArgumentList())) + } + else if (superMethodInvocation(methodExpression, "toString")) { + myResult = DummyStringExpression(String.format("getJavaClass<%s>.getName() + '@' + Integer.toHexString(hashCode())", ExpressionVisitor.getClassName(methodExpression))) + } + else { + convertMethodCallExpression(expression!!) + } + } + + class object { + private fun superMethodInvocation(expression: PsiReferenceExpression, methodName: String?): Boolean { + val referenceName: String? = expression.getReferenceName() + val qualifierExpression: PsiExpression? = expression.getQualifierExpression() + if (referenceName == methodName && qualifierExpression is PsiSuperExpression) { + val `type`: PsiType? = qualifierExpression.getType() + if (`type` != null && `type`.getCanonicalText() == JAVA_LANG_OBJECT) { + return true + } + } + return false + } + } +}