Search in sources :

Example 21 with GrField

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

the class GrReferenceExpressionImpl method calculateType.

@Nullable
private static PsiType calculateType(@NotNull GrReferenceExpressionImpl refExpr, boolean forceRValue) {
    final GroovyResolveResult[] results = refExpr.multiResolve(false, forceRValue);
    final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(results);
    final PsiElement resolved = result.getElement();
    for (GrExpressionTypeCalculator calculator : GrExpressionTypeCalculator.EP_NAME.getExtensions()) {
        PsiType type = calculator.calculateType(refExpr, resolved);
        if (type != null)
            return type;
    }
    if (ResolveUtil.isClassReference(refExpr)) {
        GrExpression qualifier = refExpr.getQualifier();
        LOG.assertTrue(qualifier != null);
        return qualifier.getType();
    }
    if (PsiUtil.isCompileStatic(refExpr)) {
        final PsiType type;
        if (resolved instanceof GrField) {
            type = ((GrField) resolved).getType();
        } else if (resolved instanceof GrVariable) {
            type = ((GrVariable) resolved).getDeclaredType();
        } else if (resolved instanceof GrAccessorMethod) {
            type = ((GrAccessorMethod) resolved).getProperty().getType();
        } else {
            type = null;
        }
        if (type != null) {
            return result.getSubstitutor().substitute(type);
        }
    }
    final PsiType nominal = refExpr.getNominalType(forceRValue);
    Boolean reassigned = GrReassignedLocalVarsChecker.isReassignedVar(refExpr);
    if (reassigned != null && reassigned.booleanValue()) {
        return GrReassignedLocalVarsChecker.getReassignedVarType(refExpr, true);
    }
    final PsiType inferred = getInferredTypes(refExpr, resolved);
    if (inferred == null) {
        if (nominal == null) {
            //inside nested closure we could still try to infer from variable initializer. Not sound, but makes sense
            if (resolved instanceof GrVariable) {
                LOG.assertTrue(resolved.isValid());
                return ((GrVariable) resolved).getTypeGroovy();
            }
        }
        return nominal;
    }
    if (nominal == null)
        return inferred;
    if (!TypeConversionUtil.isAssignable(TypeConversionUtil.erasure(nominal), inferred, false)) {
        if (resolved instanceof GrVariable && ((GrVariable) resolved).getTypeElementGroovy() != null) {
            return nominal;
        }
    }
    return inferred;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpressionTypeCalculator(org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrExpressionTypeCalculator) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with GrField

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

the class GrTypeDefinitionImpl method add.

@Override
public PsiElement add(@NotNull PsiElement psiElement) throws IncorrectOperationException {
    final GrTypeDefinitionBody body = getBody();
    if (body == null)
        throw new IncorrectOperationException("Class must have body");
    final PsiElement lBrace = body.getLBrace();
    if (lBrace == null)
        throw new IncorrectOperationException("No left brace");
    PsiMember member = getAnyMember(psiElement);
    PsiElement anchor = member != null ? getDefaultAnchor(body, member) : null;
    if (anchor == null) {
        anchor = lBrace.getNextSibling();
    }
    if (anchor != null) {
        ASTNode node = anchor.getNode();
        assert node != null;
        if (GroovyTokenTypes.mSEMI.equals(node.getElementType())) {
            anchor = anchor.getNextSibling();
        }
        if (psiElement instanceof GrField) {
            //add field with modifiers which are in its parent
            int i = ArrayUtilRt.find(((GrVariableDeclaration) psiElement.getParent()).getVariables(), psiElement);
            psiElement = body.addBefore(psiElement.getParent(), anchor);
            GrVariable[] vars = ((GrVariableDeclaration) psiElement).getVariables();
            for (int j = 0; j < vars.length; j++) {
                if (i != j)
                    vars[i].delete();
            }
            psiElement = vars[i];
        } else {
            psiElement = body.addBefore(psiElement, anchor);
        }
    } else {
        psiElement = body.add(psiElement);
    }
    return psiElement;
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 23 with GrField

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

the class GroovyPropertyUtils method generateSetterPrototype.

public static GrMethod generateSetterPrototype(PsiField field) {
    Project project = field.getProject();
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    String name = field.getName();
    boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
    VariableKind kind = codeStyleManager.getVariableKind(field);
    String propertyName = codeStyleManager.variableNameToPropertyName(name, kind);
    String setName = getSetterName(field.getName());
    final PsiClass containingClass = field.getContainingClass();
    try {
        GrMethod setMethod = factory.createMethod(setName, PsiType.VOID);
        String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
        final PsiType type = field instanceof GrField ? ((GrField) field).getDeclaredType() : field.getType();
        GrParameter param = factory.createParameter(parameterName, type);
        annotateWithNullableStuff(field, param);
        setMethod.getParameterList().add(param);
        PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic);
        @NonNls StringBuilder builder = new StringBuilder();
        if (name.equals(parameterName)) {
            if (!isStatic) {
                builder.append("this.");
            } else {
                String className = containingClass.getName();
                if (className != null) {
                    builder.append(className);
                    builder.append(".");
                }
            }
        }
        builder.append(name);
        builder.append("=");
        builder.append(parameterName);
        builder.append("\n");
        GrCodeBlock body = factory.createMethodBodyFromText(builder.toString());
        setMethod.getBlock().replace(body);
        return setMethod;
    } catch (IncorrectOperationException e) {
        LOG.error(e);
        return null;
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) NonNls(org.jetbrains.annotations.NonNls) 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) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) VariableKind(com.intellij.psi.codeStyle.VariableKind) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 24 with GrField

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

the class CollectClassMembersUtil method processClass.

private static void processClass(@NotNull PsiClass aClass, @NotNull Map<String, CandidateInfo> allFields, @NotNull Map<String, List<CandidateInfo>> allMethods, @NotNull Map<String, CandidateInfo> allInnerClasses, @NotNull Set<PsiClass> visitedClasses, @NotNull PsiSubstitutor substitutor, boolean includeSynthetic) {
    PsiUtilCore.ensureValid(aClass);
    if (!visitedClasses.add(aClass))
        return;
    if (visitedClasses.size() == 1 || !GrTraitUtil.isTrait(aClass)) {
        for (PsiField field : getFields(aClass, includeSynthetic)) {
            String name = field.getName();
            if (!allFields.containsKey(name)) {
                allFields.put(name, new CandidateInfo(field, substitutor));
            } else if (hasExplicitVisibilityModifiers(field)) {
                final CandidateInfo candidateInfo = allFields.get(name);
                final PsiElement element = candidateInfo.getElement();
                if (element instanceof GrField) {
                    final GrModifierList modifierList = ((GrField) element).getModifierList();
                    if ((modifierList == null || !modifierList.hasExplicitVisibilityModifiers()) && aClass == ((GrField) element).getContainingClass()) {
                        //replace property-field with field with explicit visibilityModifier
                        allFields.put(name, new CandidateInfo(field, substitutor));
                    }
                }
            }
        }
    }
    for (PsiMethod method : getMethods(aClass, includeSynthetic)) {
        addMethod(allMethods, method, substitutor);
    }
    for (final PsiClass inner : getInnerClasses(aClass, includeSynthetic)) {
        final String name = inner.getName();
        if (name != null && !allInnerClasses.containsKey(name)) {
            allInnerClasses.put(name, new CandidateInfo(inner, substitutor));
        }
    }
    for (PsiClass superClass : getSupers(aClass, includeSynthetic)) {
        final PsiSubstitutor superSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, aClass, substitutor);
        processClass(superClass, allFields, allMethods, allInnerClasses, visitedClasses, superSubstitutor, includeSynthetic);
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) CandidateInfo(com.intellij.psi.infos.CandidateInfo)

Example 25 with GrField

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

the class SimpleBuilderStrategySupport method applyTransformation.

@Override
public void applyTransformation(@NotNull TransformationContext context) {
    GrTypeDefinition typeDefinition = context.getCodeClass();
    final PsiAnnotation annotation = PsiImplUtil.getAnnotation(typeDefinition, BUILDER_FQN);
    if (!isApplicable(annotation, SIMPLE_STRATEGY_NAME))
        return;
    for (GrField field : typeDefinition.getCodeFields()) {
        context.addMethod(createFieldSetter(typeDefinition, field, annotation));
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiAnnotation(com.intellij.psi.PsiAnnotation)

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