KT-652 Convert correctly statements with prohibited qualifiers inside

This commit is contained in:
Sergey Ignatov
2011-11-29 12:44:49 +04:00
parent 9065a54462
commit 876ba8736b
4 changed files with 44 additions and 7 deletions
@@ -9,6 +9,7 @@ public class IdentifierImpl extends Expression implements Identifier {
private final String myName; private final String myName;
private boolean myHasDollar = false; private boolean myHasDollar = false;
private boolean myIsNullable = true; private boolean myIsNullable = true;
private boolean myQuotingNeeded = true;
public IdentifierImpl(String name) { public IdentifierImpl(String name) {
myName = name; myName = name;
@@ -25,6 +26,11 @@ public class IdentifierImpl extends Expression implements Identifier {
myIsNullable = isNullable; myIsNullable = isNullable;
} }
public IdentifierImpl(String name, boolean hasDollar, boolean isNullable, boolean quotingNeeded) {
this(name, hasDollar, isNullable);
myQuotingNeeded = quotingNeeded;
}
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
return myName.length() == 0; return myName.length() == 0;
@@ -39,17 +45,17 @@ public class IdentifierImpl extends Expression implements Identifier {
return myIsNullable; return myIsNullable;
} }
private static String ifNeedQuote(String name) { private String ifNeedQuote() {
if (ONLY_KOTLIN_KEYWORDS.contains(name) || name.contains("$")) if (myQuotingNeeded && (ONLY_KOTLIN_KEYWORDS.contains(myName) || myName.contains("$")))
return quote(name); return quote(myName);
return name; return myName;
} }
@NotNull @NotNull
@Override @Override
public String toKotlin() { public String toKotlin() {
if (myHasDollar) if (myHasDollar)
return DOLLAR + ifNeedQuote(myName); return DOLLAR + ifNeedQuote();
return ifNeedQuote(myName); return ifNeedQuote();
} }
} }
@@ -160,6 +160,7 @@ public class ExpressionVisitor extends StatementVisitor {
final Object value = expression.getValue(); final Object value = expression.getValue();
String text = expression.getText(); String text = expression.getText();
boolean isQuotingNeeded = true;
final PsiType type = expression.getType(); final PsiType type = expression.getType();
if (type != null) { if (type != null) {
@@ -172,8 +173,13 @@ public class ExpressionVisitor extends StatementVisitor {
text = text.replace("L", "").replace("l", ""); text = text.replace("L", "").replace("l", "");
if (canonicalTypeStr.equals("int") || canonicalTypeStr.equals("java.lang.Integer")) // need for hex support if (canonicalTypeStr.equals("int") || canonicalTypeStr.equals("java.lang.Integer")) // need for hex support
text = value != null ? value.toString() : text; text = value != null ? value.toString() : text;
if (canonicalTypeStr.equals("java.lang.String"))
isQuotingNeeded = false;
if (canonicalTypeStr.equals("char") || canonicalTypeStr.equals("java.lang.Character"))
isQuotingNeeded = false;
} }
myResult = new LiteralExpression(new IdentifierImpl(text)); myResult = new LiteralExpression(new IdentifierImpl(text, false, false, isQuotingNeeded));
} }
@Override @Override
@@ -0,0 +1,14 @@
package demo;
class Test {
void test() {
String name = "$$$$";
name = name.replaceAll("\\$[0-9]+", "\\$")
char c = '$';
System.out.println(c);
Character C = '$';
System.out.println(C);
}
}
@@ -0,0 +1,11 @@
namespace demo
open class Test() {
open fun test() : Unit {
var name : String? = "$$$$"
name = name?.replaceAll("\\$[0-9]+", "\\$")
var c : Char = '$'
System.out?.println(c)
var C : Char? = '$'
System.out?.println(C)
}
}