Search in sources :

Example 1 with GrAnnotationMemberValue

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue in project intellij-community by JetBrains.

the class ClassItemGeneratorImpl method writeMethod.

@Override
public void writeMethod(StringBuilder builder, PsiMethod method) {
    if (method == null)
        return;
    GenerationUtil.writeDocComment(builder, method, true);
    String name = method.getName();
    boolean isAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT);
    PsiModifierList modifierList = method.getModifierList();
    final PsiClass containingClass = method.getContainingClass();
    if (method.isConstructor() && containingClass != null && containingClass.isEnum()) {
        ModifierListGenerator.writeModifiers(builder, modifierList, ModifierListGenerator.ENUM_CONSTRUCTOR_MODIFIERS);
    } else {
        ModifierListGenerator.writeModifiers(builder, modifierList);
    }
    if (method.hasTypeParameters()) {
        GenerationUtil.writeTypeParameters(builder, method, classNameProvider);
        builder.append(' ');
    }
    //append return type
    if (!method.isConstructor()) {
        PsiType retType = context.typeProvider.getReturnType(method);
        TypeWriter.writeType(builder, retType, method, classNameProvider);
        builder.append(' ');
    }
    builder.append(name);
    if (method instanceof GroovyPsiElement) {
        context.searchForLocalVarsToWrap((GroovyPsiElement) method);
    }
    GenerationUtil.writeParameterList(builder, method.getParameterList().getParameters(), classNameProvider, context);
    if (method instanceof GrAnnotationMethod) {
        GrAnnotationMemberValue defaultValue = ((GrAnnotationMethod) method).getDefaultValue();
        if (defaultValue != null) {
            builder.append("default ");
            defaultValue.accept(new AnnotationGenerator(builder, context));
        }
    }
    GenerationUtil.writeThrowsList(builder, method.getThrowsList(), getMethodExceptions(method), classNameProvider);
    if (!isAbstract) {
        /* ************ body ********* */
        if (method instanceof GrMethod) {
            if (method instanceof GrReflectedMethod && ((GrReflectedMethod) method).getSkippedParameters().length > 0) {
                builder.append("{\n").append(generateDelegateCall((GrReflectedMethod) method)).append("\n}\n");
            } else {
                new CodeBlockGenerator(builder, context.extend()).generateMethodBody((GrMethod) method);
            }
        } else if (method instanceof GrAccessorMethod) {
            writeAccessorBody(builder, method);
        } else if (method instanceof LightMethodBuilder && containingClass instanceof GroovyScriptClass) {
            if ("main".equals(method.getName())) {
                writeMainScriptMethodBody(builder, method);
            } else if ("run".equals(method.getName())) {
                writeRunScriptMethodBody(builder, method);
            }
        } else {
            builder.append("{//todo\n}");
        }
    } else {
        builder.append(';');
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LightMethodBuilder(com.intellij.psi.impl.light.LightMethodBuilder) GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)

Example 2 with GrAnnotationMemberValue

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue in project intellij-community by JetBrains.

the class CustomAnnotationChecker method checkAnnotationValueByType.

public static void checkAnnotationValueByType(@NotNull AnnotationHolder holder, @NotNull GrAnnotationMemberValue value, @Nullable PsiType ltype, boolean skipArrays) {
    final GlobalSearchScope resolveScope = value.getResolveScope();
    final PsiManager manager = value.getManager();
    if (value instanceof GrExpression) {
        final PsiType rtype;
        if (value instanceof GrClosableBlock) {
            rtype = PsiType.getJavaLangClass(manager, resolveScope);
        } else {
            rtype = ((GrExpression) value).getType();
        }
        if (rtype != null && !checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
            holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
        }
    } else if (value instanceof GrAnnotation) {
        final PsiElement resolved = ((GrAnnotation) value).getClassReference().resolve();
        if (resolved instanceof PsiClass) {
            final PsiClassType rtype = JavaPsiFacade.getElementFactory(value.getProject()).createType((PsiClass) resolved, PsiSubstitutor.EMPTY);
            if (!checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
                holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
            }
        }
    } else if (value instanceof GrAnnotationArrayInitializer) {
        if (ltype instanceof PsiArrayType) {
            final PsiType componentType = ((PsiArrayType) ltype).getComponentType();
            final GrAnnotationMemberValue[] initializers = ((GrAnnotationArrayInitializer) value).getInitializers();
            for (GrAnnotationMemberValue initializer : initializers) {
                checkAnnotationValueByType(holder, initializer, componentType, false);
            }
        } else {
            final PsiType rtype = TypesUtil.getTupleByAnnotationArrayInitializer((GrAnnotationArrayInitializer) value);
            if (!checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
                holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
            }
        }
    }
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer) GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 3 with GrAnnotationMemberValue

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue in project intellij-community by JetBrains.

the class GroovySuppressableInspectionTool method getInspectionIdsSuppressedInAnnotation.

@NotNull
private static Collection<String> getInspectionIdsSuppressedInAnnotation(final GrModifierList modifierList) {
    if (modifierList == null) {
        return Collections.emptyList();
    }
    PsiAnnotation annotation = modifierList.findAnnotation(BatchSuppressManager.SUPPRESS_INSPECTIONS_ANNOTATION_NAME);
    if (annotation == null) {
        return Collections.emptyList();
    }
    final GrAnnotationMemberValue attributeValue = (GrAnnotationMemberValue) annotation.findAttributeValue(null);
    Collection<String> result = new ArrayList<>();
    if (attributeValue instanceof GrAnnotationArrayInitializer) {
        for (GrAnnotationMemberValue annotationMemberValue : ((GrAnnotationArrayInitializer) attributeValue).getInitializers()) {
            final String id = getInspectionIdSuppressedInAnnotationAttribute(annotationMemberValue);
            if (id != null) {
                result.add(id);
            }
        }
    } else {
        final String id = getInspectionIdSuppressedInAnnotationAttribute(attributeValue);
        if (id != null) {
            result.add(id);
        }
    }
    return result;
}
Also used : GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) ArrayList(java.util.ArrayList) PsiAnnotation(com.intellij.psi.PsiAnnotation) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GrAnnotationMemberValue

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue in project intellij-community by JetBrains.

the class GrLightAnnotation method addAttribute.

public void addAttribute(PsiNameValuePair pair) {
    if (pair instanceof GrAnnotationNameValuePair) {
        myAnnotationArgList.addAttribute((GrAnnotationNameValuePair) pair);
    } else {
        GrAnnotationMemberValue newValue = new AnnotationArgConverter().convert(pair.getValue());
        if (newValue == null)
            return;
        String name = pair.getName();
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(pair.getProject());
        String annotationText;
        annotationText = name != null ? "@A(" + name + "=" + newValue.getText() + ")" : "@A(" + newValue.getText() + ")";
        GrAnnotation annotation = factory.createAnnotationFromText(annotationText);
        myAnnotationArgList.addAttribute(annotation.getParameterList().getAttributes()[0]);
    }
}
Also used : GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) AnnotationArgConverter(org.jetbrains.plugins.groovy.lang.psi.impl.AnnotationArgConverter)

Example 5 with GrAnnotationMemberValue

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue in project intellij-community by JetBrains.

the class GrNameValuePairElementType method createStub.

@NotNull
@Override
public GrNameValuePairStub createStub(@NotNull GrAnnotationNameValuePair psi, StubElement parentStub) {
    String name = psi.getName();
    GrAnnotationMemberValue value = psi.getValue();
    return new GrNameValuePairStub(parentStub, name, value == null ? null : value.getText());
}
Also used : GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GrNameValuePairStub(org.jetbrains.plugins.groovy.lang.psi.stubs.GrNameValuePairStub) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrAnnotationMemberValue (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue)8 GrAnnotationArrayInitializer (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)2 GrAnnotationNameValuePair (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair)2 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 ASTNode (com.intellij.lang.ASTNode)1 PsiAnnotation (com.intellij.psi.PsiAnnotation)1 PsiAnnotationOwner (com.intellij.psi.PsiAnnotationOwner)1 PsiClass (com.intellij.psi.PsiClass)1 PsiElement (com.intellij.psi.PsiElement)1 LightMethodBuilder (com.intellij.psi.impl.light.LightMethodBuilder)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)1