Search in sources :

Example 41 with GrCodeReferenceElement

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

the class ExpressionGenerator method visitNewExpression.

@Override
public void visitNewExpression(@NotNull GrNewExpression newExpression) {
    boolean hasFieldInitialization = hasFieldInitialization(newExpression);
    StringBuilder builder;
    final PsiType type = newExpression.getType();
    final String varName;
    if (hasFieldInitialization) {
        builder = new StringBuilder();
        varName = GenerationUtil.suggestVarName(type, newExpression, context);
        TypeWriter.writeType(builder, type, newExpression);
        builder.append(' ').append(varName).append(" = ");
    } else {
        varName = null;
        builder = this.builder;
    }
    final GrTypeElement typeElement = newExpression.getTypeElement();
    final GrArrayDeclaration arrayDeclaration = newExpression.getArrayDeclaration();
    final GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
    builder.append("new ");
    if (typeElement != null) {
        final PsiType builtIn = typeElement.getType();
        LOG.assertTrue(builtIn instanceof PsiPrimitiveType);
        final PsiType boxed = TypesUtil.boxPrimitiveType(builtIn, newExpression.getManager(), newExpression.getResolveScope());
        TypeWriter.writeTypeForNew(builder, boxed, newExpression);
    } else if (referenceElement != null) {
        GenerationUtil.writeCodeReferenceElement(builder, referenceElement);
    }
    final GrArgumentList argList = newExpression.getArgumentList();
    if (argList != null) {
        GrClosureSignature signature = null;
        final GroovyResolveResult resolveResult = newExpression.advancedResolve();
        final PsiElement constructor = resolveResult.getElement();
        if (constructor instanceof PsiMethod) {
            signature = GrClosureSignatureUtil.createSignature((PsiMethod) constructor, resolveResult.getSubstitutor());
        } else if (referenceElement != null) {
            final GroovyResolveResult clazzResult = referenceElement.advancedResolve();
            final PsiElement clazz = clazzResult.getElement();
            if (clazz instanceof PsiClass && ((PsiClass) clazz).getConstructors().length == 0) {
                signature = GrClosureSignatureUtil.createSignature(PsiParameter.EMPTY_ARRAY, null);
            }
        }
        final GrNamedArgument[] namedArgs = hasFieldInitialization ? GrNamedArgument.EMPTY_ARRAY : argList.getNamedArguments();
        new ArgumentListGenerator(builder, context).generate(signature, argList.getExpressionArguments(), namedArgs, GrClosableBlock.EMPTY_ARRAY, newExpression);
    }
    final GrAnonymousClassDefinition anonymous = newExpression.getAnonymousClassDefinition();
    if (anonymous != null) {
        writeTypeBody(builder, anonymous);
    }
    if (arrayDeclaration != null) {
        final GrExpression[] boundExpressions = arrayDeclaration.getBoundExpressions();
        for (GrExpression boundExpression : boundExpressions) {
            builder.append('[');
            boundExpression.accept(this);
            builder.append(']');
        }
        if (boundExpressions.length == 0) {
            builder.append("[]");
        }
    }
    if (hasFieldInitialization) {
        builder.append(';');
        context.myStatements.add(builder.toString());
        final GrNamedArgument[] namedArguments = argList.getNamedArguments();
        for (GrNamedArgument namedArgument : namedArguments) {
            final String fieldName = namedArgument.getLabelName();
            if (fieldName == null) {
                //todo try to initialize field
                final GrArgumentLabel label = namedArgument.getLabel();
                LOG.info("cannot initialize field " + (label == null ? "<null>" : label.getText()));
            } else {
                final GroovyResolveResult resolveResult = referenceElement.advancedResolve();
                final PsiElement resolved = resolveResult.getElement();
                LOG.assertTrue(resolved instanceof PsiClass);
                initializeField(varName, type, ((PsiClass) resolved), resolveResult.getSubstitutor(), fieldName, namedArgument.getExpression());
            }
        }
    }
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 42 with GrCodeReferenceElement

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

the class AnnotationGenerator method visitAnnotation.

@Override
public void visitAnnotation(@NotNull GrAnnotation annotation) {
    builder.append('@');
    GrCodeReferenceElement classReference = annotation.getClassReference();
    GenerationUtil.writeCodeReferenceElement(builder, classReference);
    GrAnnotationArgumentList parameterList = annotation.getParameterList();
    GrAnnotationNameValuePair[] attributes = parameterList.getAttributes();
    if (attributes.length == 0)
        return;
    builder.append('(');
    for (GrAnnotationNameValuePair attribute : attributes) {
        String name = attribute.getName();
        if (name != null) {
            builder.append(name);
            builder.append(" = ");
        }
        GrAnnotationMemberValue value = attribute.getValue();
        if (value != null) {
            value.accept(this);
        }
        builder.append(", ");
    }
    builder.delete(builder.length() - 2, builder.length());
    //builder.removeFromTheEnd(2);
    builder.append(')');
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)

Example 43 with GrCodeReferenceElement

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

the class GenerationUtil method writeThisReference.

public static void writeThisReference(@Nullable PsiClass targetClass, StringBuilder buffer, ExpressionContext context) {
    if (targetClass != null && !(targetClass instanceof PsiAnonymousClass)) {
        final GrCodeReferenceElement ref = GroovyPsiElementFactory.getInstance(context.project).createCodeReferenceElementFromClass(targetClass);
        writeCodeReferenceElement(buffer, ref);
        buffer.append('.');
    }
    buffer.append("this");
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)

Example 44 with GrCodeReferenceElement

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

the class GroovyTypeCheckVisitor method visitNewExpression.

@Override
public void visitNewExpression(@NotNull GrNewExpression newExpression) {
    super.visitNewExpression(newExpression);
    if (newExpression.getArrayCount() > 0)
        return;
    GrCodeReferenceElement refElement = newExpression.getReferenceElement();
    if (refElement == null)
        return;
    GrNewExpressionInfo info = new GrNewExpressionInfo(newExpression);
    processConstructorCall(info);
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)

Example 45 with GrCodeReferenceElement

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

the class GrRefactoringConflictsUtil method analyzeModuleConflicts.

public static void analyzeModuleConflicts(final Project project, final Collection<? extends GroovyPsiElement> scopes, final UsageInfo[] usages, final VirtualFile vFile, final MultiMap<PsiElement, String> conflicts) {
    if (scopes == null)
        return;
    for (final PsiElement scope : scopes) {
        if (scope instanceof PsiPackage)
            return;
    }
    final Module targetModule = ModuleUtilCore.findModuleForFile(vFile, project);
    if (targetModule == null)
        return;
    final GlobalSearchScope resolveScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(targetModule);
    final HashSet<PsiElement> reported = new HashSet<>();
    for (final GroovyPsiElement scope : scopes) {
        scope.accept(new GroovyRecursiveElementVisitor() {

            @Override
            public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
                super.visitCodeReferenceElement(refElement);
                visit(refElement);
            }

            @Override
            public void visitReferenceExpression(@NotNull GrReferenceExpression reference) {
                super.visitReferenceExpression(reference);
                visit(reference);
            }

            private void visit(GrReferenceElement<? extends GroovyPsiElement> reference) {
                final PsiElement resolved = reference.resolve();
                if (resolved != null && !reported.contains(resolved) && !CommonRefactoringUtil.isAncestor(resolved, scopes) && !PsiSearchScopeUtil.isInScope(resolveScope, resolved) && !(resolved instanceof LightElement)) {
                    final String scopeDescription = RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(reference), true);
                    final String message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.in.module.2", RefactoringUIUtil.getDescription(resolved, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(targetModule.getName()));
                    conflicts.putValue(resolved, CommonRefactoringUtil.capitalize(message));
                    reported.add(resolved);
                }
            }
        });
    }
    boolean isInTestSources = ModuleRootManager.getInstance(targetModule).getFileIndex().isInTestSourceContent(vFile);
    NextUsage: for (UsageInfo usage : usages) {
        final PsiElement element = usage.getElement();
        if (element != null && PsiTreeUtil.getParentOfType(element, GrImportStatement.class, false) == null) {
            for (PsiElement scope : scopes) {
                if (PsiTreeUtil.isAncestor(scope, element, false))
                    continue NextUsage;
            }
            final GlobalSearchScope resolveScope1 = element.getResolveScope();
            if (!resolveScope1.isSearchInModuleContent(targetModule, isInTestSources)) {
                final PsiFile usageFile = element.getContainingFile();
                PsiElement container;
                if (usageFile instanceof PsiJavaFile) {
                    container = ConflictsUtil.getContainer(element);
                } else {
                    container = usageFile;
                }
                final String scopeDescription = RefactoringUIUtil.getDescription(container, true);
                final VirtualFile usageVFile = usageFile.getVirtualFile();
                if (usageVFile != null) {
                    Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(usageVFile);
                    if (module != null) {
                        final String message;
                        final PsiElement referencedElement;
                        if (usage instanceof MoveRenameUsageInfo) {
                            referencedElement = ((MoveRenameUsageInfo) usage).getReferencedElement();
                        } else {
                            referencedElement = usage.getElement();
                        }
                        assert referencedElement != null : usage;
                        if (module == targetModule && isInTestSources) {
                            message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.production.of.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
                        } else {
                            message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
                        }
                        conflicts.putValue(referencedElement, CommonRefactoringUtil.capitalize(message));
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) LightElement(com.intellij.psi.impl.light.LightElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet)

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