use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class BaseScriptAnnotationChecker method checkApplicability.
@Override
public boolean checkApplicability(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
if (GroovyCommonClassNames.GROOVY_TRANSFORM_BASE_SCRIPT.equals(annotation.getQualifiedName())) {
PsiFile file = annotation.getContainingFile();
if (file instanceof GroovyFile && !(((GroovyFile) file).isScript())) {
holder.createErrorAnnotation(annotation, GroovyBundle.message("base.script.annotation.is.allowed.only.inside.scripts"));
return true;
}
PsiElement pparent = annotation.getParent().getParent();
if (pparent instanceof GrVariableDeclaration) {
GrTypeElement typeElement = ((GrVariableDeclaration) pparent).getTypeElementGroovy();
PsiType type = typeElement != null ? typeElement.getType() : null;
if (!InheritanceUtil.isInheritor(type, GroovyCommonClassNames.GROOVY_LANG_SCRIPT)) {
String typeText = type != null ? type.getCanonicalText() : CommonClassNames.JAVA_LANG_OBJECT;
holder.createErrorAnnotation(annotation, GroovyBundle.message("declared.type.0.have.to.extend.script", typeText));
return true;
}
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GrCastFix method doCast.
static void doCast(@NotNull Project project, @NotNull PsiType type, @NotNull GrExpression expr) {
if (!type.isValid())
return;
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final GrSafeCastExpression cast = (GrSafeCastExpression) factory.createExpressionFromText("foo as String");
final GrTypeElement typeElement = factory.createTypeElement(type);
cast.getOperand().replaceWithExpression(expr, true);
cast.getCastTypeElement().replace(typeElement);
final GrExpression replaced = expr.replaceWithExpression(cast, true);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class AsTypeTransformation method couldApply.
@Override
protected boolean couldApply(@NotNull GrSafeCastExpression expression) {
GrTypeElement typeElement = expression.getCastTypeElement();
if (typeElement == null)
return false;
PsiType type = typeElement.getType();
return type instanceof PsiClassType && ((PsiClassType) type).getParameterCount() == 0;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GroovyConstructorUsagesSearcher method processGroovyConstructorUsages.
private static boolean processGroovyConstructorUsages(GrCodeReferenceElement element, final Processor<GrNewExpression> newExpressionProcessor, final LiteralConstructorSearcher literalProcessor) {
PsiElement parent = element.getParent();
if (parent instanceof GrAnonymousClassDefinition) {
parent = parent.getParent();
}
if (parent instanceof GrNewExpression) {
return newExpressionProcessor.process((GrNewExpression) parent);
}
if (parent instanceof GrTypeElement) {
final GrTypeElement typeElement = (GrTypeElement) parent;
final PsiElement grandpa = typeElement.getParent();
if (grandpa instanceof GrVariableDeclaration) {
final GrVariable[] vars = ((GrVariableDeclaration) grandpa).getVariables();
if (vars.length == 1) {
final GrVariable variable = vars[0];
if (!checkLiteralInstantiation(variable.getInitializerGroovy(), literalProcessor)) {
return false;
}
}
} else if (grandpa instanceof GrMethod) {
final GrMethod method = (GrMethod) grandpa;
if (typeElement == method.getReturnTypeElementGroovy()) {
ControlFlowUtils.visitAllExitPoints(method.getBlock(), new ControlFlowUtils.ExitPointVisitor() {
@Override
public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
if (!checkLiteralInstantiation(returnValue, literalProcessor)) {
return false;
}
return true;
}
});
}
} else if (grandpa instanceof GrTypeCastExpression) {
final GrTypeCastExpression cast = (GrTypeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
} else if (grandpa instanceof GrSafeCastExpression) {
final GrSafeCastExpression cast = (GrSafeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class EquivalenceChecker method safeCastExpressionsAreEquivalent.
private static boolean safeCastExpressionsAreEquivalent(GrSafeCastExpression expression1, GrSafeCastExpression expression2) {
final GrExpression operand1 = expression1.getOperand();
final GrExpression operand2 = expression2.getOperand();
if (!expressionsAreEquivalent(operand1, operand2)) {
return false;
}
final GrTypeElement typeElement1 = expression1.getCastTypeElement();
final GrTypeElement typeElement2 = expression2.getCastTypeElement();
final PsiType safe1 = typeElement1 == null ? null : typeElement1.getType();
final PsiType safe2 = typeElement2 == null ? null : typeElement2.getType();
return typesAreEquivalent(safe1, safe2);
}
Aggregations