no more SureCallChainExpression

This commit is contained in:
Dmitry Jemerov
2012-05-24 21:52:16 +02:00
committed by Pavel V. Talanov
parent 6da7fc9799
commit dfaa67ab6b
13 changed files with 23 additions and 111 deletions
+4 -42
View File
@@ -381,7 +381,7 @@ public class Converter {
new IdentifierImpl(field.getName()), // TODO new IdentifierImpl(field.getName()), // TODO
modifiers, modifiers,
typeToType(field.getType()), typeToType(field.getType()),
createSureCallOnlyForChain(field.getInitializer(), field.getType()), // TODO: add modifiers expressionToExpression(field.getInitializer(), field.getType()), // TODO: add modifiers
countWritingAccesses(field, psiClass) countWritingAccesses(field, psiClass)
); );
} }
@@ -741,6 +741,7 @@ public class Converter {
} }
public Expression expressionToExpression(PsiExpression argument, PsiType expectedType) { public Expression expressionToExpression(PsiExpression argument, PsiType expectedType) {
if (argument == null) return (IdentifierImpl) Identifier.EMPTY_IDENTIFIER;
Expression expression = expressionToExpression(argument); Expression expression = expressionToExpression(argument);
PsiType actualType = argument.getType(); PsiType actualType = argument.getType();
boolean isPrimitiveTypeOrNull = actualType == null || actualType instanceof PsiPrimitiveType; boolean isPrimitiveTypeOrNull = actualType == null || actualType instanceof PsiPrimitiveType;
@@ -751,7 +752,7 @@ public class Converter {
} }
if (actualType != null) { if (actualType != null) {
if (isConversionNeeded(actualType, expectedType)) { if (isConversionNeeded(actualType, expectedType) && !(expression instanceof LiteralExpression)) {
String conversion = PRIMITIVE_TYPE_CONVERSIONS.get(expectedType.getCanonicalText()); String conversion = PRIMITIVE_TYPE_CONVERSIONS.get(expectedType.getCanonicalText());
if (conversion != null) { if (conversion != null) {
expression = new DummyMethodCallExpression(expression, conversion, (IdentifierImpl) Identifier.EMPTY_IDENTIFIER); expression = new DummyMethodCallExpression(expression, conversion, (IdentifierImpl) Identifier.EMPTY_IDENTIFIER);
@@ -761,28 +762,6 @@ public class Converter {
return expression; return expression;
} }
@NotNull
private String createConversionForExpression(@Nullable PsiExpression expression, @NotNull PsiType expectedType) {
String conversion = "";
if (expression != null) {
PsiType actualType = expression.getType();
boolean isPrimitiveTypeOrNull = actualType == null || Node.PRIMITIVE_TYPES.contains(actualType.getCanonicalText());
boolean isRef = (expression instanceof PsiReferenceExpression && ((PsiReferenceExpression) expression).isQualified() || expression instanceof PsiMethodCallExpression);
boolean containsQuestDot = expressionToExpression(expression).toKotlin().contains("?.");
if (isPrimitiveTypeOrNull && isRef && containsQuestDot) {
conversion += ".sure()";
}
if (actualType != null) {
if (isConversionNeeded(actualType, expectedType)) {
conversion += getPrimitiveTypeConversion(expectedType.getCanonicalText());
}
}
}
return conversion;
}
private static boolean isConversionNeeded(@Nullable final PsiType actual, @Nullable final PsiType expected) { private static boolean isConversionNeeded(@Nullable final PsiType actual, @Nullable final PsiType expected) {
if (actual == null || expected == null) { if (actual == null || expected == null) {
return false; return false;
@@ -802,28 +781,11 @@ public class Converter {
return !actualStr.equals(expectedStr) && (!(o1 ^ o2)); return !actualStr.equals(expectedStr) && (!(o1 ^ o2));
} }
@NotNull // @NotNull
private static String getPrimitiveTypeConversion(@NotNull String type) {
if (PRIMITIVE_TYPE_CONVERSIONS.containsKey(type)) {
return "." + PRIMITIVE_TYPE_CONVERSIONS.get(type) + "()";
}
return "";
}
// @NotNull
// private static String applyConversion(Expression expression, String conversion) { // private static String applyConversion(Expression expression, String conversion) {
// if (conversion.isEmpty()) // if (conversion.isEmpty())
// return expression.toKotlin(); // return expression.toKotlin();
// return "(" + expression.toKotlin() + ")" + conversion; // return "(" + expression.toKotlin() + ")" + conversion;
// } // }
@NotNull
public SureCallChainExpression createSureCallOnlyForChain(@Nullable PsiExpression expression, @NotNull PsiType type) {
String conversion = (expression != null && (expression instanceof PsiReferenceExpression || expression instanceof PsiMethodCallExpression))
?
createConversionForExpression(expression, type)
: "";
return new SureCallChainExpression(expressionToExpression(expression), conversion);
}
} }
@@ -1,43 +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.ast;
import org.jetbrains.annotations.NotNull;
/**
* @author ignatov
*/
public class SureCallChainExpression extends Expression {
private final Expression myExpression;
private final String myConversion;
public SureCallChainExpression(Expression expression, String conversion) {
myExpression = expression;
myConversion = conversion;
}
@Override
public boolean isEmpty() {
return myExpression.isEmpty();
}
@NotNull
@Override
public String toKotlin() {
return myExpression.toKotlin() + myConversion;
}
}
@@ -80,16 +80,6 @@ public class AstUtil {
return conversions; return conversions;
} }
@NotNull
public static List<String> applyConversions(@NotNull List<String> first, @NotNull List<String> second) {
List<String> result = new LinkedList<String>();
assert first.size() == second.size() : "Lists must have the same size.";
for (int i = 0; i < first.size(); i++) {
result.add(applyConversionForOneItem(first.get(i), second.get(i)));
}
return result;
}
@NotNull @NotNull
public static String applyConversionForOneItem(@NotNull String f, @NotNull String s) { public static String applyConversionForOneItem(@NotNull String f, @NotNull String s) {
if (s.isEmpty()) { if (s.isEmpty()) {
@@ -24,7 +24,7 @@ public open class ElementVisitor(val myConverter : Converter) : JavaElementVisit
myResult = LocalVariable(IdentifierImpl(theVariable.getName()), myResult = LocalVariable(IdentifierImpl(theVariable.getName()),
modifiersListToModifiersSet(theVariable.getModifierList()), modifiersListToModifiersSet(theVariable.getModifierList()),
myConverter.typeToType(theVariable.getType(), isAnnotatedAsNotNull(theVariable.getModifierList())), myConverter.typeToType(theVariable.getType(), isAnnotatedAsNotNull(theVariable.getModifierList())),
myConverter.createSureCallOnlyForChain(theVariable.getInitializer(), theVariable.getType())) myConverter.expressionToExpression(theVariable.getInitializer(), theVariable.getType()))
} }
public override fun visitExpressionList(list : PsiExpressionList?) : Unit { public override fun visitExpressionList(list : PsiExpressionList?) : Unit {
@@ -170,7 +170,7 @@ public class ExpressionVisitor extends StatementVisitor {
PsiExpression condition = expression.getCondition(); PsiExpression condition = expression.getCondition();
PsiType type = condition.getType(); PsiType type = condition.getType();
Expression e = type != null ? Expression e = type != null ?
getConverter().createSureCallOnlyForChain(condition, type) : this.getConverter().expressionToExpression(condition, type) :
getConverter().expressionToExpression(condition); getConverter().expressionToExpression(condition);
myResult = new ParenthesizedExpression( myResult = new ParenthesizedExpression(
new IfStatement( new IfStatement(
@@ -208,6 +208,9 @@ public class ExpressionVisitor extends StatementVisitor {
String canonicalTypeStr = type.getCanonicalText(); String canonicalTypeStr = type.getCanonicalText();
if (canonicalTypeStr.equals("double") || canonicalTypeStr.equals(JAVA_LANG_DOUBLE)) { if (canonicalTypeStr.equals("double") || canonicalTypeStr.equals(JAVA_LANG_DOUBLE)) {
text = text.replace("D", "").replace("d", ""); text = text.replace("D", "").replace("d", "");
if (!text.contains(".")) {
text += ".0";
}
} }
if (canonicalTypeStr.equals("float") || canonicalTypeStr.equals(JAVA_LANG_FLOAT)) { if (canonicalTypeStr.equals("float") || canonicalTypeStr.equals(JAVA_LANG_FLOAT)) {
text = text.replace("F", "").replace("f", "") + "." + OperatorConventions.FLOAT + "()"; text = text.replace("F", "").replace("f", "") + "." + OperatorConventions.FLOAT + "()";
@@ -105,7 +105,7 @@ public class StatementVisitor extends ElementVisitor {
PsiExpression condition = statement.getCondition(); PsiExpression condition = statement.getCondition();
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
Expression expression = condition != null && condition.getType() != null ? Expression expression = condition != null && condition.getType() != null ?
getConverter().createSureCallOnlyForChain(condition, condition.getType()) : this.getConverter().expressionToExpression(condition, condition.getType()) :
getConverter().expressionToExpression(condition); getConverter().expressionToExpression(condition);
myResult = new DoWhileStatement( myResult = new DoWhileStatement(
expression, expression,
@@ -207,7 +207,7 @@ public class StatementVisitor extends ElementVisitor {
PsiExpression condition = statement.getCondition(); PsiExpression condition = statement.getCondition();
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
Expression expression = condition != null && condition.getType() != null ? Expression expression = condition != null && condition.getType() != null ?
getConverter().createSureCallOnlyForChain(condition, condition.getType()) : this.getConverter().expressionToExpression(condition, condition.getType()) :
getConverter().expressionToExpression(condition); getConverter().expressionToExpression(condition);
myResult = new IfStatement( myResult = new IfStatement(
expression, expression,
@@ -373,7 +373,7 @@ public class StatementVisitor extends ElementVisitor {
PsiExpression condition = statement.getCondition(); PsiExpression condition = statement.getCondition();
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
Expression expression = condition != null && condition.getType() != null ? Expression expression = condition != null && condition.getType() != null ?
getConverter().createSureCallOnlyForChain(condition, condition.getType()) : this.getConverter().expressionToExpression(condition, condition.getType()) :
getConverter().expressionToExpression(condition); getConverter().expressionToExpression(condition);
myResult = new WhileStatement( myResult = new WhileStatement(
expression, expression,
@@ -387,7 +387,7 @@ public class StatementVisitor extends ElementVisitor {
PsiExpression returnValue = statement.getReturnValue(); PsiExpression returnValue = statement.getReturnValue();
PsiType methodReturnType = getConverter().getMethodReturnType(); PsiType methodReturnType = getConverter().getMethodReturnType();
Expression expression = returnValue != null && methodReturnType != null ? Expression expression = returnValue != null && methodReturnType != null ?
getConverter().createSureCallOnlyForChain(returnValue, methodReturnType) : this.getConverter().expressionToExpression(returnValue, methodReturnType) :
getConverter().expressionToExpression(returnValue); getConverter().expressionToExpression(returnValue);
myResult = new ReturnStatement( myResult = new ReturnStatement(
expression expression
@@ -8,5 +8,5 @@ var myContainer : Container? = Container()
} }
} }
open class Test() { open class Test() {
var b : Byte = One.myContainer?.myInt.sure().toByte() var b : Byte = One.myContainer?.myInt!!.toByte()
} }
@@ -1,7 +1,7 @@
open class Test() { open class Test() {
class object { class object {
public open fun toFileSystemSafeName(name : String?) : String? { public open fun toFileSystemSafeName(name : String?) : String? {
var size : Int = name?.length().sure() var size : Int = name?.length()!!
return name return name
} }
} }
+1 -1
View File
@@ -9,6 +9,6 @@ var myContainer : Container? = Container()
} }
open class Test() { open class Test() {
open fun test() : Unit { open fun test() : Unit {
var b : Byte = One.myContainer?.myInt.sure().toByte() var b : Byte = One.myContainer?.myInt!!.toByte()
} }
} }
@@ -8,7 +8,7 @@ if (parent == null || !parent?.exists())
return false return false
} }
var result : Boolean = true var result : Boolean = true
if (parent?.isDirectory().sure()) if (parent?.isDirectory()!!)
{ {
return true return true
} }
+4 -4
View File
@@ -9,18 +9,18 @@ var myContainer : Container? = Container()
} }
open class Test() { open class Test() {
open fun test() : Unit { open fun test() : Unit {
if (One.myContainer?.myBoolean.sure()) if (One.myContainer?.myBoolean!!)
System.out?.println("Ok") System.out?.println("Ok")
var s : String? = (if (One.myContainer?.myBoolean.sure()) var s : String? = (if (One.myContainer?.myBoolean!!)
"YES" "YES"
else else
"NO") "NO")
while (One.myContainer?.myBoolean.sure()) while (One.myContainer?.myBoolean!!)
System.out?.println("Ok") System.out?.println("Ok")
do do
{ {
System.out?.println("Ok") System.out?.println("Ok")
} }
while (One.myContainer?.myBoolean.sure()) while (One.myContainer?.myBoolean!!)
} }
} }
+1 -1
View File
@@ -3,7 +3,7 @@ import java.io.Serializable
public open class Language(code : String?) : Serializable { public open class Language(code : String?) : Serializable {
protected var code : String? = null protected var code : String? = null
public open fun equals(other : Language?) : Boolean { public open fun equals(other : Language?) : Boolean {
return other?.toString()?.equals(this.toString()).sure() return other?.toString()?.equals(this.toString())!!
} }
{ {
this.code = code this.code = code
+1 -1
View File
@@ -6,7 +6,7 @@ var s2 : String? = ""
var s3 : String? = "" var s3 : String? = ""
if (s1?.isEmpty()!! && s2?.isEmpty()!!) if (s1?.isEmpty()!! && s2?.isEmpty()!!)
return "OK" return "OK"
if ((s1?.isEmpty()).sure() && (s2?.isEmpty()).sure() && (s3?.isEmpty()).sure()) if (s1?.isEmpty()!! && s2?.isEmpty()!! && s3?.isEmpty()!!)
return "OOOK" return "OOOK"
return "" return ""
} }