Search in sources :

Example 16 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.

the class GroovyTestGenerator method addSuperClass.

private static void addSuperClass(@NotNull GrTypeDefinition targetClass, @NotNull Project project, @Nullable String superClassName) throws IncorrectOperationException {
    if (superClassName == null)
        return;
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    PsiClass superClass = findClass(project, superClassName);
    GrCodeReferenceElement superClassRef;
    if (superClass != null) {
        superClassRef = factory.createCodeReferenceElementFromClass(superClass);
    } else {
        superClassRef = factory.createCodeReferenceElementFromText(superClassName);
    }
    GrExtendsClause extendsClause = targetClass.getExtendsClause();
    if (extendsClause == null) {
        extendsClause = (GrExtendsClause) targetClass.addAfter(factory.createExtendsClause(), targetClass.getNameIdentifierGroovy());
    }
    extendsClause.add(superClassRef);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExtendsClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrExtendsClause) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)

Example 17 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.

the class GrPullUpHelper method postProcessMember.

@Override
public void postProcessMember(PsiMember member) {
    ((GrMember) member).accept(myExplicitSuperDeleter);
    ((GrMember) member).accept(myThisSuperAdjuster);
    GroovyChangeContextUtil.decodeContextInfo(member, null, null);
    ((GroovyPsiElement) member).accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            if (processRef(referenceExpression))
                return;
            super.visitReferenceExpression(referenceExpression);
        }

        @Override
        public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
            if (processRef(refElement))
                return;
            super.visitCodeReferenceElement(refElement);
        }

        private boolean processRef(@NotNull GrReferenceElement<? extends GroovyPsiElement> refElement) {
            final PsiElement qualifier = refElement.getQualifier();
            if (qualifier != null) {
                final Boolean preserveQualifier = qualifier.getCopyableUserData(PRESERVE_QUALIFIER);
                if (preserveQualifier != null && !preserveQualifier) {
                    refElement.setQualifier(null);
                    return true;
                }
            }
            return false;
        }
    });
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 18 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.

the class GrPullUpHelper method replaceMovedMemberTypeParameters.

public static void replaceMovedMemberTypeParameters(final PsiElement member, final Iterable<PsiTypeParameter> parametersIterable, final PsiSubstitutor substitutor, final GroovyPsiElementFactory factory) {
    final Map<PsiElement, PsiElement> replacement = new LinkedHashMap<>();
    for (PsiTypeParameter parameter : parametersIterable) {
        PsiType substitutedType = substitutor.substitute(parameter);
        PsiType type = substitutedType != null ? substitutedType : TypeConversionUtil.erasure(factory.createType(parameter));
        PsiElement scopeElement = member instanceof GrField ? member.getParent() : member;
        for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(scopeElement))) {
            final PsiElement element = reference.getElement();
            final PsiElement parent = element.getParent();
            if (parent instanceof PsiTypeElement) {
                replacement.put(parent, factory.createTypeElement(type));
            } else if (element instanceof GrCodeReferenceElement && type instanceof PsiClassType) {
                replacement.put(element, factory.createReferenceElementByType((PsiClassType) type));
            }
        }
    }
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(member.getProject());
    for (PsiElement element : replacement.keySet()) {
        if (element.isValid()) {
            final PsiElement replaced = element.replace(replacement.get(element));
            codeStyleManager.shortenClassReferences(replaced);
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 19 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.

the class GrRefactoringConflictsUtil method checkUsedElements.

public static void checkUsedElements(PsiMember member, PsiElement scope, @NotNull Set<GrMember> membersToMove, @Nullable Set<PsiMethod> abstractMethods, @Nullable PsiClass targetClass, @NotNull PsiElement context, MultiMap<PsiElement, String> conflicts) {
    final Set<PsiMember> moving = new HashSet<>(membersToMove);
    if (abstractMethods != null) {
        moving.addAll(abstractMethods);
    }
    if (scope instanceof GrReferenceExpression) {
        GrReferenceExpression refExpr = (GrReferenceExpression) scope;
        PsiElement refElement = refExpr.resolve();
        if (refElement instanceof PsiMember) {
            if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
                GrExpression qualifier = refExpr.getQualifierExpression();
                PsiClass accessClass = (PsiClass) (qualifier != null ? PsiUtil.getAccessObjectClass(qualifier).getElement() : null);
                RefactoringConflictsUtil.checkAccessibility((PsiMember) refElement, context, accessClass, member, conflicts);
            }
        }
    } else if (scope instanceof GrNewExpression) {
        final GrNewExpression newExpression = (GrNewExpression) scope;
        final GrAnonymousClassDefinition anonymousClass = newExpression.getAnonymousClassDefinition();
        if (anonymousClass != null) {
            if (!RefactoringHierarchyUtil.willBeInTargetClass(anonymousClass, moving, targetClass, false)) {
                RefactoringConflictsUtil.checkAccessibility(anonymousClass, context, anonymousClass, member, conflicts);
            }
        } else {
            final PsiMethod refElement = newExpression.resolveMethod();
            if (refElement != null) {
                if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
                    RefactoringConflictsUtil.checkAccessibility(refElement, context, null, member, conflicts);
                }
            }
        }
    } else if (scope instanceof GrCodeReferenceElement) {
        GrCodeReferenceElement refExpr = (GrCodeReferenceElement) scope;
        PsiElement refElement = refExpr.resolve();
        if (refElement instanceof PsiMember) {
            if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
                RefactoringConflictsUtil.checkAccessibility((PsiMember) refElement, context, null, member, conflicts);
            }
        }
    }
    for (PsiElement child : scope.getChildren()) {
        if (child instanceof PsiWhiteSpace || child instanceof PsiComment)
            continue;
        checkUsedElements(member, child, membersToMove, abstractMethods, targetClass, context, conflicts);
    }
}
Also used : GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) HashSet(com.intellij.util.containers.HashSet)

Example 20 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.

the class AnnotationChecker method checkApplicability.

public void checkApplicability(@NotNull GrAnnotation annotation, @Nullable PsiAnnotationOwner owner) {
    final GrCodeReferenceElement ref = annotation.getClassReference();
    final PsiElement resolved = ref.resolve();
    if (resolved == null)
        return;
    assert resolved instanceof PsiClass;
    PsiClass anno = (PsiClass) resolved;
    String qname = anno.getQualifiedName();
    if (!anno.isAnnotationType() && GrAnnotationCollector.findAnnotationCollector(anno) == null) {
        if (qname != null) {
            myHolder.createErrorAnnotation(ref, GroovyBundle.message("class.is.not.annotation", qname));
        }
        return;
    }
    for (CustomAnnotationChecker checker : CustomAnnotationChecker.EP_NAME.getExtensions()) {
        if (checker.checkApplicability(myHolder, annotation))
            return;
    }
    String description = CustomAnnotationChecker.isAnnotationApplicable(annotation, owner);
    if (description != null) {
        myHolder.createErrorAnnotation(ref, description).registerFix(new GrRemoveAnnotationIntention());
    }
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrRemoveAnnotationIntention(org.jetbrains.plugins.groovy.annotator.GrRemoveAnnotationIntention) PsiClass(com.intellij.psi.PsiClass) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)71 NotNull (org.jetbrains.annotations.NotNull)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 PsiElement (com.intellij.psi.PsiElement)12 Nullable (org.jetbrains.annotations.Nullable)12 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)11 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)7 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)5 GrAnonymousClassDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition)5 PsiClass (com.intellij.psi.PsiClass)4 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)4 GrTypeArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList)4 Editor (com.intellij.openapi.editor.Editor)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3