Search in sources :

Example 46 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class GrChangeVariableType method doFix.

@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
    final PsiElement element = descriptor.getPsiElement();
    final PsiElement parent = element.getParent();
    try {
        final PsiType type = JavaPsiFacade.getElementFactory(project).createTypeFromText(myType, element);
        if (parent instanceof GrVariable) {
            ((GrVariable) parent).setType(type);
        } else if (element instanceof GrReferenceExpression && parent instanceof GrAssignmentExpression && ((GrAssignmentExpression) parent).getLValue() == element) {
            final PsiElement resolved = ((GrReferenceExpression) element).resolve();
            if (resolved instanceof GrVariable && !(resolved instanceof GrParameter)) {
                ((GrVariable) resolved).setType(type);
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 47 with PsiType

use of com.intellij.psi.PsiType 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;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) PsiClassType(com.intellij.psi.PsiClassType) PsiType(com.intellij.psi.PsiType)

Example 48 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class GroovyConditionalCanBeElvisInspection method checkForStringIsEmpty.

/**
   * checks for the case string.isEmpty() ? something_else : string
   */
private static boolean checkForStringIsEmpty(GrExpression condition, GrExpression elseBranch) {
    if (condition instanceof GrMethodCall)
        condition = ((GrMethodCall) condition).getInvokedExpression();
    if (!(condition instanceof GrReferenceExpression))
        return false;
    final GrExpression qualifier = ((GrReferenceExpression) condition).getQualifier();
    if (qualifier == null)
        return false;
    if (!PsiEquivalenceUtil.areElementsEquivalent(qualifier, elseBranch))
        return false;
    final PsiType type = qualifier.getType();
    if (type == null)
        return false;
    if (!type.equalsToText(CommonClassNames.JAVA_LANG_STRING))
        return false;
    final PsiElement resolved = ((GrReferenceExpression) condition).resolve();
    return resolved instanceof PsiMethod && "isEmpty".equals(((PsiMethod) resolved).getName()) && ((PsiMethod) resolved).getParameterList().getParametersCount() == 0;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 49 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class GroovyUntypedAccessInspection method buildVisitor.

@Override
@NotNull
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression refExpr) {
            super.visitReferenceExpression(refExpr);
            if (PsiUtil.isThisOrSuperRef(refExpr))
                return;
            GroovyResolveResult resolveResult = refExpr.advancedResolve();
            PsiElement resolved = resolveResult.getElement();
            if (resolved != null) {
                if (GrHighlightUtil.isDeclarationAssignment(refExpr) || resolved instanceof PsiPackage)
                    return;
            } else {
                GrExpression qualifier = refExpr.getQualifierExpression();
                if (qualifier == null && GrHighlightUtil.isDeclarationAssignment(refExpr))
                    return;
            }
            final PsiType refExprType = refExpr.getType();
            if (refExprType == null) {
                if (resolved != null) {
                    registerError(refExpr);
                }
            } else if (refExprType instanceof PsiClassType && ((PsiClassType) refExprType).resolve() == null) {
                registerError(refExpr);
            }
        }
    };
}
Also used : PsiClassType(com.intellij.psi.PsiClassType) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) PsiPackage(com.intellij.psi.PsiPackage) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Example 50 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class GrExpressionCategory method getClassType.

@Nullable
public static PsiClass getClassType(GrExpression expr) {
    final PsiType type = expr.getType();
    if (type instanceof PsiClassType) {
        PsiClassType classType = (PsiClassType) type;
        return classType.resolve();
    } else {
        final String text = type.getPresentableText();
        final Project project = expr.getProject();
        final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
        return facade.findClass(text, GlobalSearchScope.allScope(project));
    }
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) PsiClassType(com.intellij.psi.PsiClassType) Project(com.intellij.openapi.project.Project) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiType (com.intellij.psi.PsiType)157 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)24 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)24 NotNull (org.jetbrains.annotations.NotNull)21 PsiClassType (com.intellij.psi.PsiClassType)20 PsiClass (com.intellij.psi.PsiClass)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 PsiPrimitiveType (com.intellij.psi.PsiPrimitiveType)12 ArrayList (java.util.ArrayList)9 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)9 PsiArrayType (com.intellij.psi.PsiArrayType)7 PsiExpression (com.intellij.psi.PsiExpression)7 PsiField (com.intellij.psi.PsiField)7 NonNls (org.jetbrains.annotations.NonNls)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 PsiLiteralExpression (com.intellij.psi.PsiLiteralExpression)6 PsiMethod (com.intellij.psi.PsiMethod)6 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)6 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)6