mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-167269 Incorrect as -> asType(). (IDEA-CR-17817)
This commit is contained in:
+7
-1
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.codeInspection.changeToMethod.transformations;
|
||||
|
||||
import com.intellij.psi.PsiClassType;
|
||||
import com.intellij.psi.PsiType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
@@ -35,7 +37,11 @@ public class AsTypeTransformation extends Transformation<GrSafeCastExpression> {
|
||||
|
||||
@Override
|
||||
protected boolean couldApply(@NotNull GrSafeCastExpression expression) {
|
||||
return expression.getCastTypeElement() != null;
|
||||
GrTypeElement typeElement = expression.getCastTypeElement();
|
||||
if (typeElement == null ) return false;
|
||||
PsiType type = typeElement.getType();
|
||||
return type instanceof PsiClassType && ((PsiClassType)type).getParameterCount() == 0;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+20
-6
@@ -15,24 +15,38 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.codeInspection.changeToOperator.transformations;
|
||||
|
||||
import com.intellij.psi.PsiClass;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.codeInspection.changeToOperator.ChangeToOperatorInspection;
|
||||
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil;
|
||||
|
||||
import static java.lang.String.format;
|
||||
import static org.jetbrains.plugins.groovy.codeInspection.GrInspectionUtil.replaceExpression;
|
||||
import static org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes.kAS;
|
||||
|
||||
public class AsTypeTransformation extends SimpleBinaryTransformation {
|
||||
public AsTypeTransformation() {
|
||||
super(GroovyTokenTypes.kAS);
|
||||
super(kAS);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void apply(@NotNull GrMethodCall methodCall, @NotNull ChangeToOperatorInspection.Options options) {
|
||||
GrReferenceExpression rhs = (GrReferenceExpression) getRhs(methodCall);
|
||||
|
||||
GrExpression rhsQualifierExpression = rhs.isQualified() ? rhs.getQualifierExpression() : rhs;
|
||||
|
||||
if (rhsQualifierExpression == null) return;
|
||||
|
||||
replaceExpression(methodCall, format("%s %s %s", getLhs(methodCall).getText(), kAS, rhsQualifierExpression.getText()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean couldApplyInternal(@NotNull GrMethodCall methodCall, @NotNull ChangeToOperatorInspection.Options options) {
|
||||
GrExpression rhs = getRhs(methodCall);
|
||||
return super.couldApplyInternal(methodCall, options)
|
||||
&& rhs instanceof GrReferenceElement
|
||||
&& ((GrReferenceElement)rhs).resolve() instanceof PsiClass;
|
||||
&& ResolveUtil.resolvesToClass(rhs);
|
||||
}
|
||||
}
|
||||
|
||||
+8
-3
@@ -110,14 +110,19 @@ class Operators {
|
||||
"a << b" :"a.leftShift(b)",
|
||||
"a >> b" :"a.rightShift(b)",
|
||||
"a >>> b" :"a.rightShiftUnsigned(b)",
|
||||
"a in b" :"b.isCase(a)",
|
||||
"a as String" :"a.asType(String)",
|
||||
"!(a a<caret>s String)" :"!a.asType(String)"
|
||||
"a in b" :"b.isCase(a)"
|
||||
].each {
|
||||
doTest it.key, it.value
|
||||
}
|
||||
}
|
||||
|
||||
void testAsType() {
|
||||
doTest "a as String", "a.asType(String)"
|
||||
doTest "!(a a<caret>s String)", "!a.asType(String)"
|
||||
doTest "a a<caret>s List<Integer>"
|
||||
doTest "a a<caret>s List", "a.asType(List)"
|
||||
}
|
||||
|
||||
void testCompareTo() {
|
||||
doTest "a <=> b", "a.compareTo(b)"
|
||||
doTest "a < b", "a.compareTo(b) < 0"
|
||||
|
||||
+1
-1
@@ -201,7 +201,7 @@ class Operators {
|
||||
void testAsType() {
|
||||
doTest "a.asType(String)", "a as String"
|
||||
doTest "!a.asType(String)", "!(a as String)"
|
||||
doTest "a.asType(String.class)"
|
||||
doTest "a.asType(String.class)", "a as String"
|
||||
doTest "a.asType(a.getClass())"
|
||||
doTest "a.asType(UnknownClass)"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user