Search in sources :

Example 26 with GrField

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

the class DelegateTransformationSupport method applyTransformation.

@Override
public void applyTransformation(@NotNull TransformationContext context) {
    for (GrField field : context.getFields()) {
        final PsiAnnotation annotation = PsiImplUtil.getAnnotation(field, GroovyCommonClassNames.GROOVY_LANG_DELEGATE);
        if (annotation == null)
            continue;
        final PsiType type = field.getDeclaredType();
        if (!(type instanceof PsiClassType))
            continue;
        final PsiClassType.ClassResolveResult delegateResult = ((PsiClassType) type).resolveGenerics();
        final PsiClass delegate = delegateResult.getElement();
        if (delegate == null)
            continue;
        DelegateProcessor processor = new DelegateProcessor(context, delegate, annotation);
        delegate.processDeclarations(processor, ResolveState.initial().put(PsiSubstitutor.KEY, delegateResult.getSubstitutor()), null, context.getCodeClass());
        if (!processor.myInterfaces)
            continue;
        Set<PsiClass> visited = ContainerUtil.newHashSet();
        Queue<Pair<PsiClass, PsiSubstitutor>> queue = ContainerUtil.newLinkedList(Pair.create(delegate, delegateResult.getSubstitutor()));
        while (!queue.isEmpty()) {
            Pair<PsiClass, PsiSubstitutor> pair = queue.poll();
            PsiClass currentClass = pair.first;
            PsiSubstitutor substitutor = pair.second;
            if (visited.add(currentClass) && currentClass.isInterface()) {
                context.addInterface(new PsiImmediateClassType(currentClass, substitutor));
                continue;
            }
            for (PsiClassType superType : currentClass.getSuperTypes()) {
                PsiClassType.ClassResolveResult resolveResult = superType.resolveGenerics();
                PsiClass superClass = resolveResult.getElement();
                if (superClass != null) {
                    queue.offer(Pair.create(superClass, TypeConversionUtil.getSuperClassSubstitutor(superClass, currentClass, substitutor)));
                }
            }
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) PsiImmediateClassType(com.intellij.psi.impl.source.PsiImmediateClassType) Pair(com.intellij.openapi.util.Pair)

Example 27 with GrField

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

the class GroovyPostHighlightingPass method doCollectInformation.

@Override
public void doCollectInformation(@NotNull final ProgressIndicator progress) {
    ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
    VirtualFile virtualFile = myFile.getViewProvider().getVirtualFile();
    if (!fileIndex.isInContent(virtualFile)) {
        return;
    }
    final InspectionProfile profile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
    final HighlightDisplayKey unusedDefKey = HighlightDisplayKey.find(GroovyUnusedDeclarationInspection.SHORT_NAME);
    final boolean deadCodeEnabled = profile.isToolEnabled(unusedDefKey, myFile);
    final UnusedDeclarationInspectionBase deadCodeInspection = (UnusedDeclarationInspectionBase) profile.getUnwrappedTool(UnusedDeclarationInspectionBase.SHORT_NAME, myFile);
    final GlobalUsageHelper usageHelper = new GlobalUsageHelper() {

        @Override
        public boolean isCurrentFileAlreadyChecked() {
            return false;
        }

        @Override
        public boolean isLocallyUsed(@NotNull PsiNamedElement member) {
            return false;
        }

        @Override
        public boolean shouldCheckUsages(@NotNull PsiMember member) {
            return deadCodeInspection == null || !deadCodeInspection.isEntryPoint(member);
        }
    };
    final List<HighlightInfo> unusedDeclarations = new ArrayList<>();
    final Map<GrParameter, Boolean> usedParams = new HashMap<>();
    myFile.accept(new PsiRecursiveElementWalkingVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof GrReferenceExpression && !((GrReferenceElement) element).isQualified()) {
                GroovyResolveResult[] results = ((GrReferenceExpression) element).multiResolve(false);
                if (results.length == 0) {
                    results = ((GrReferenceExpression) element).multiResolve(true);
                }
                for (GroovyResolveResult result : results) {
                    PsiElement resolved = result.getElement();
                    if (resolved instanceof GrParameter && resolved.getContainingFile() == myFile) {
                        usedParams.put((GrParameter) resolved, Boolean.TRUE);
                    }
                }
            }
            if (deadCodeEnabled && element instanceof GrNamedElement && element instanceof PsiModifierListOwner && !UnusedSymbolUtil.isImplicitUsage(element.getProject(), (PsiModifierListOwner) element, progress) && !GroovySuppressableInspectionTool.isElementToolSuppressedIn(element, GroovyUnusedDeclarationInspection.SHORT_NAME)) {
                PsiElement nameId = ((GrNamedElement) element).getNameIdentifierGroovy();
                if (nameId.getNode().getElementType() == GroovyTokenTypes.mIDENT) {
                    String name = ((GrNamedElement) element).getName();
                    if (element instanceof GrTypeDefinition && !UnusedSymbolUtil.isClassUsed(myProject, element.getContainingFile(), (GrTypeDefinition) element, progress, usageHelper)) {
                        HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(nameId, "Class " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL);
                        QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(element), unusedDefKey);
                        ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
                    } else if (element instanceof GrMethod) {
                        GrMethod method = (GrMethod) element;
                        if (!UnusedSymbolUtil.isMethodReferenced(method.getProject(), method.getContainingFile(), method, progress, usageHelper)) {
                            String message = (method.isConstructor() ? "Constructor" : "Method") + " " + name + " is unused";
                            HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(nameId, message, HighlightInfoType.UNUSED_SYMBOL);
                            QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(method), unusedDefKey);
                            ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
                        }
                    } else if (element instanceof GrField && isFieldUnused((GrField) element, progress, usageHelper)) {
                        HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(nameId, "Property " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL);
                        QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(element), unusedDefKey);
                        ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
                    } else if (element instanceof GrParameter) {
                        if (!usedParams.containsKey(element)) {
                            usedParams.put((GrParameter) element, Boolean.FALSE);
                        }
                    }
                }
            }
            super.visitElement(element);
        }
    });
    final Set<GrImportStatement> unusedImports = new HashSet<>(PsiUtil.getValidImportStatements(myFile));
    unusedImports.removeAll(GroovyImportUtil.findUsedImports(myFile));
    myUnusedImports = unusedImports;
    if (deadCodeEnabled) {
        for (GrParameter parameter : usedParams.keySet()) {
            if (usedParams.get(parameter))
                continue;
            PsiElement scope = parameter.getDeclarationScope();
            if (scope instanceof GrMethod) {
                GrMethod method = (GrMethod) scope;
                if (methodMayHaveUnusedParameters(method)) {
                    PsiElement identifier = parameter.getNameIdentifierGroovy();
                    HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(identifier, "Parameter " + parameter.getName() + " is unused", HighlightInfoType.UNUSED_SYMBOL);
                    QuickFixAction.registerQuickFixAction(highlightInfo, GroovyQuickFixFactory.getInstance().createRemoveUnusedGrParameterFix(parameter), unusedDefKey);
                    ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
                }
            } else if (scope instanceof GrClosableBlock) {
            //todo Max Medvedev
            }
        }
    }
    myUnusedDeclarations = unusedDeclarations;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) NotNull(org.jetbrains.annotations.NotNull) GrNamedElement(org.jetbrains.plugins.groovy.lang.psi.GrNamedElement) InspectionProfile(com.intellij.codeInspection.InspectionProfile) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) UnusedDeclarationInspectionBase(com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex)

Example 28 with GrField

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

the class GroovyGenerateConstructorHandler method chooseOriginalMembers.

@Override
@Nullable
protected ClassMember[] chooseOriginalMembers(PsiClass aClass, Project project) {
    final ClassMember[] classMembers = chooseOriginalMembersImpl(aClass, project);
    if (classMembers == null)
        return null;
    List<ClassMember> res = new ArrayList<>();
    final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
    for (ClassMember classMember : classMembers) {
        if (classMember instanceof PsiMethodMember) {
            final PsiMethod method = ((PsiMethodMember) classMember).getElement();
            PsiMethod copy = factory.createMethodFromText(GroovyToJavaGenerator.generateMethodStub(method), method);
            if (method instanceof GrMethod) {
                GrParameter[] parameters = ((GrMethod) method).getParameterList().getParameters();
                PsiParameter[] copyParameters = copy.getParameterList().getParameters();
                for (int i = 0; i < parameters.length; i++) {
                    if (parameters[i].getTypeElementGroovy() == null) {
                        copyParameters[i].setName(DEF_PSEUDO_ANNO + parameters[i].getName());
                    }
                }
            }
            res.add(new PsiMethodMember(copy));
        } else if (classMember instanceof PsiFieldMember) {
            final PsiField field = ((PsiFieldMember) classMember).getElement();
            String prefix = field instanceof GrField && ((GrField) field).getTypeElementGroovy() == null ? DEF_PSEUDO_ANNO : "";
            res.add(new PsiFieldMember(factory.createFieldFromText(field.getType().getCanonicalText() + " " + prefix + field.getName(), aClass)));
        }
    }
    return res.toArray(new ClassMember[res.size()]);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) ArrayList(java.util.ArrayList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with GrField

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

the class DefaultTransformationSupport method applyTransformation.

@Override
public void applyTransformation(@NotNull TransformationContext context) {
    for (GrField field : context.getFields()) {
        if (!field.isProperty())
            continue;
        final String fieldName = field.getName();
        String nameNonBoolean = getGetterNameNonBoolean(fieldName);
        if (!hasContradictingMethods(context, nameNonBoolean, false)) {
            context.addMethod(new GrAccessorMethodImpl(field, false, nameNonBoolean));
            if (PsiType.BOOLEAN.equals(field.getDeclaredType())) {
                String nameBoolean = getGetterNameBoolean(fieldName);
                if (!hasContradictingMethods(context, nameBoolean, false)) {
                    context.addMethod(new GrAccessorMethodImpl(field, false, nameBoolean));
                }
            }
        }
        if (!field.hasModifierProperty(PsiModifier.FINAL)) {
            String setterName = getSetterName(fieldName);
            if (!hasContradictingMethods(context, setterName, true)) {
                context.addMethod(new GrAccessorMethodImpl(field, true, setterName));
            }
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethodImpl(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrAccessorMethodImpl)

Example 30 with GrField

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

the class ResolverProcessor method isAccessible.

protected boolean isAccessible(@NotNull PsiNamedElement namedElement) {
    if (namedElement instanceof GrField) {
        final GrField field = (GrField) namedElement;
        if (PsiUtil.isAccessible(myPlace, field)) {
            return true;
        }
        for (GrAccessorMethod method : field.getGetters()) {
            if (PsiUtil.isAccessible(myPlace, method)) {
                return true;
            }
        }
        final GrAccessorMethod setter = field.getSetter();
        if (setter != null && PsiUtil.isAccessible(myPlace, setter)) {
            return true;
        }
        return false;
    }
    return !(namedElement instanceof PsiMember) || PsiUtil.isAccessible(myPlace, ((PsiMember) namedElement));
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) PsiMember(com.intellij.psi.PsiMember)

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