Search in sources :

Example 51 with GrField

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

the class GroovyLineMarkerProvider method collectOverridingMethods.

private static void collectOverridingMethods(@NotNull final Set<PsiMethod> methods, @NotNull Collection<LineMarkerInfo> result) {
    final Set<PsiElement> overridden = new HashSet<>();
    Set<PsiClass> classes = new THashSet<>();
    for (PsiMethod method : methods) {
        ProgressManager.checkCanceled();
        final PsiClass parentClass = method.getContainingClass();
        if (parentClass != null && !CommonClassNames.JAVA_LANG_OBJECT.equals(parentClass.getQualifiedName())) {
            classes.add(parentClass);
        }
    }
    for (final PsiClass aClass : classes) {
        AllOverridingMethodsSearch.search(aClass).forEach(pair -> {
            ProgressManager.checkCanceled();
            final PsiMethod superMethod = pair.getFirst();
            if (isCorrectTarget(superMethod) && isCorrectTarget(pair.getSecond())) {
                if (methods.remove(superMethod)) {
                    overridden.add(PsiImplUtil.handleMirror(superMethod));
                }
            }
            return !methods.isEmpty();
        });
    }
    for (PsiElement element : overridden) {
        final Icon icon = AllIcons.Gutter.OverridenMethod;
        element = PsiImplUtil.handleMirror(element);
        PsiElement range = element instanceof GrNamedElement ? ((GrNamedElement) element).getNameIdentifierGroovy() : element;
        final MarkerType type = element instanceof GrField ? GroovyMarkerTypes.OVERRIDEN_PROPERTY_TYPE : GroovyMarkerTypes.GR_OVERRIDEN_METHOD;
        LineMarkerInfo info = new LineMarkerInfo<>(range, range.getTextRange(), icon, Pass.LINE_MARKERS, type.getTooltip(), type.getNavigationHandler(), GutterIconRenderer.Alignment.RIGHT);
        result.add(info);
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) GrNamedElement(org.jetbrains.plugins.groovy.lang.psi.GrNamedElement) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) MarkerType(com.intellij.codeInsight.daemon.impl.MarkerType) THashSet(gnu.trove.THashSet) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet)

Example 52 with GrField

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

the class GroovyCreateFieldFromUsageHelper method setupTemplateImpl.

@Override
public Template setupTemplateImpl(PsiField f, Object expectedTypes, PsiClass targetClass, Editor editor, PsiElement context, boolean createConstantField, PsiSubstitutor substitutor) {
    GrVariableDeclaration fieldDecl = (GrVariableDeclaration) f.getParent();
    GrField field = (GrField) fieldDecl.getVariables()[0];
    TemplateBuilderImpl builder = new TemplateBuilderImpl(fieldDecl);
    Project project = context.getProject();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    if (expectedTypes instanceof TypeConstraint[]) {
        GrTypeElement typeElement = fieldDecl.getTypeElementGroovy();
        assert typeElement != null;
        ChooseTypeExpression expr = new ChooseTypeExpression((TypeConstraint[]) expectedTypes, PsiManager.getInstance(project), typeElement.getResolveScope());
        builder.replaceElement(typeElement, expr);
    } else if (expectedTypes instanceof ExpectedTypeInfo[]) {
        new GuessTypeParameters(factory).setupTypeElement(field.getTypeElement(), (ExpectedTypeInfo[]) expectedTypes, substitutor, builder, context, targetClass);
    }
    if (createConstantField) {
        field.setInitializerGroovy(factory.createExpressionFromText("0", null));
        builder.replaceElement(field.getInitializerGroovy(), new EmptyExpression());
    }
    fieldDecl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(fieldDecl);
    Template template = builder.buildTemplate();
    TextRange range = fieldDecl.getTextRange();
    editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    if (expectedTypes instanceof ExpectedTypeInfo[]) {
        if (((ExpectedTypeInfo[]) expectedTypes).length > 1)
            template.setToShortenLongNames(false);
    }
    return template;
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GuessTypeParameters(com.intellij.codeInsight.daemon.impl.quickfix.GuessTypeParameters) TextRange(com.intellij.openapi.util.TextRange) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) EmptyExpression(com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression) Template(com.intellij.codeInsight.template.Template) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) Project(com.intellij.openapi.project.Project) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint)

Example 53 with GrField

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

the class GrUnresolvableLocalCollisionDetector method visitUpstreamCollisions.

private static void visitUpstreamCollisions(PsiElement element, String newName, GroovyPsiElement place, CollidingVariableVisitor visitor) {
    final GrReferenceExpression refExpr = GroovyPsiElementFactory.getInstance(place.getProject()).createReferenceExpressionFromText(newName, place);
    final GroovyResolveResult[] results = refExpr.multiResolve(false);
    for (GroovyResolveResult result : results) {
        final PsiElement resolved = result.getElement();
        if (resolved instanceof GrParameter || (resolved instanceof GrVariable && !(resolved instanceof GrField))) {
            final PsiElement parent = PsiTreeUtil.findCommonParent(resolved, element);
            if (parent != null) {
                PsiElement current = element;
                while (current != null && current != parent) {
                    if (current instanceof PsiMethod || current instanceof PsiClass || current instanceof GrClosableBlock) {
                        return;
                    }
                    current = current.getParent();
                }
            }
            if (!place.getManager().areElementsEquivalent(element, resolved)) {
                visitor.visitCollidingVariable((PsiVariable) resolved);
            }
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 54 with GrField

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

the class GrMemberInfo method extractClassMembers.

public static void extractClassMembers(PsiClass subclass, List<GrMemberInfo> result, Filter<GrMember> filter, final boolean extractInterfacesDeep) {
    if (!(subclass instanceof GrTypeDefinition))
        return;
    if (extractInterfacesDeep) {
        extractSuperInterfaces(subclass, filter, result, ContainerUtil.<PsiClass>newHashSet());
    } else {
        PsiClass[] interfaces = subclass.getInterfaces();
        GrReferenceList sourceRefList = subclass.isInterface() ? ((GrTypeDefinition) subclass).getExtendsClause() : ((GrTypeDefinition) subclass).getImplementsClause();
        for (PsiClass anInterface : interfaces) {
            if (anInterface instanceof GrTypeDefinition && filter.includeMember((GrMember) anInterface)) {
                result.add(new GrMemberInfo((GrMember) anInterface, true, sourceRefList));
            }
        }
    }
    PsiClass[] innerClasses = subclass.getInnerClasses();
    for (PsiClass innerClass : innerClasses) {
        if (innerClass instanceof GrTypeDefinition && filter.includeMember((GrMember) innerClass)) {
            result.add(new GrMemberInfo((GrMember) innerClass));
        }
    }
    GrMethod[] methods = ((GrTypeDefinition) subclass).getCodeMethods();
    for (GrMethod method : methods) {
        if (!method.isConstructor() && filter.includeMember(method)) {
            result.add(new GrMemberInfo(method));
        }
    }
    GrField[] fields = ((GrTypeDefinition) subclass).getCodeFields();
    for (final GrField field : fields) {
        if (filter.includeMember(field)) {
            result.add(new GrMemberInfo(field));
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Example 55 with GrField

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

the class ExpressionGenerator method createVarByInitializer.

private String createVarByInitializer(@NotNull GrExpression initializer) {
    GrExpression inner = initializer;
    while (inner instanceof GrParenthesizedExpression) inner = ((GrParenthesizedExpression) inner).getOperand();
    if (inner != null)
        initializer = inner;
    if (initializer instanceof GrReferenceExpression) {
        final GrExpression qualifier = ((GrReferenceExpression) initializer).getQualifier();
        if (qualifier == null) {
            final PsiElement resolved = ((GrReferenceExpression) initializer).resolve();
            if (resolved instanceof GrVariable && !(resolved instanceof GrField)) {
                //don't create new var. it is already exists
                return ((GrVariable) resolved).getName();
            }
        }
    }
    final String name = GenerationUtil.suggestVarName(initializer, context);
    final StringBuilder builder = new StringBuilder();
    builder.append("final ");
    TypeWriter.writeType(builder, initializer.getType(), initializer);
    builder.append(' ').append(name).append(" = ");
    initializer.accept(new ExpressionGenerator(builder, context));
    builder.append(';');
    context.myStatements.add(builder.toString());
    return name;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)55 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)17 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)16 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)13 GrAccessorMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)13 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)12 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)11 PsiElement (com.intellij.psi.PsiElement)10 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)9 NotNull (org.jetbrains.annotations.NotNull)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 Nullable (org.jetbrains.annotations.Nullable)7 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)7 ArrayList (java.util.ArrayList)6 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)6 Project (com.intellij.openapi.project.Project)4 PsiClass (com.intellij.psi.PsiClass)4 UsageInfo (com.intellij.usageView.UsageInfo)4