Search in sources :

Example 31 with GrTypeDefinition

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

the class AutoExternalizeContributor method applyTransformation.

@Override
public void applyTransformation(@NotNull TransformationContext context) {
    GrTypeDefinition clazz = context.getCodeClass();
    if (!hasGeneratedImplementations(clazz))
        return;
    final LightMethodBuilder write = new LightMethodBuilder(clazz.getManager(), "writeExternal");
    write.addParameter("out", ObjectOutput.class.getName());
    write.addException(IOException.class.getName());
    write.setOriginInfo("created by @AutoExternalize");
    context.addMethod(write);
    final LightMethodBuilder read = new LightMethodBuilder(clazz.getManager(), "readExternal");
    read.addParameter("oin", ObjectInput.class.getName());
    read.setOriginInfo("created by @AutoExternalize");
    context.addMethod(read);
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ObjectOutput(java.io.ObjectOutput) LightMethodBuilder(com.intellij.psi.impl.light.LightMethodBuilder) IOException(java.io.IOException) ObjectInput(java.io.ObjectInput)

Example 32 with GrTypeDefinition

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

the class ConstructorAnnotationsProcessor method applyTransformation.

@Override
public void applyTransformation(@NotNull TransformationContext context) {
    GrTypeDefinition typeDefinition = context.getCodeClass();
    if (typeDefinition.getName() == null)
        return;
    PsiModifierList modifierList = typeDefinition.getModifierList();
    if (modifierList == null)
        return;
    final PsiAnnotation tupleConstructor = modifierList.findAnnotation(GroovyCommonClassNames.GROOVY_TRANSFORM_TUPLE_CONSTRUCTOR);
    final boolean immutable = PsiImplUtil.hasImmutableAnnotation(modifierList);
    final boolean canonical = modifierList.findAnnotation(GroovyCommonClassNames.GROOVY_TRANSFORM_CANONICAL) != null;
    if (!immutable && !canonical && tupleConstructor == null) {
        return;
    }
    if (tupleConstructor != null && typeDefinition.getCodeConstructors().length > 0 && !PsiUtil.getAnnoAttributeValue(tupleConstructor, "force", false)) {
        return;
    }
    final GrLightMethodBuilder fieldsConstructor = generateFieldConstructor(typeDefinition, tupleConstructor, immutable, canonical);
    final GrLightMethodBuilder mapConstructor = generateMapConstructor(typeDefinition);
    context.addMethod(fieldsConstructor);
    context.addMethod(mapConstructor);
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrLightMethodBuilder(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightMethodBuilder)

Example 33 with GrTypeDefinition

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

the class InheritConstructorContributor method applyTransformation.

@Override
public void applyTransformation(@NotNull TransformationContext context) {
    GrTypeDefinition psiClass = context.getCodeClass();
    if (psiClass.isAnonymous() || psiClass.isInterface() || psiClass.isEnum()) {
        return;
    }
    if (!hasInheritConstructorsAnnotation(psiClass))
        return;
    final PsiClass superClass = context.getSuperClass();
    if (superClass == null)
        return;
    final PsiSubstitutor superClassSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, psiClass, PsiSubstitutor.EMPTY);
    for (PsiMethod constructor : superClass.getConstructors()) {
        if (constructor.hasModifierProperty(PsiModifier.PRIVATE))
            continue;
        final GrLightMethodBuilder inheritedConstructor = new GrLightMethodBuilder(context.getManager(), context.getClassName());
        inheritedConstructor.setConstructor(true);
        inheritedConstructor.setNavigationElement(psiClass);
        inheritedConstructor.addModifier(VisibilityUtil.getVisibilityModifier(constructor.getModifierList()));
        inheritedConstructor.setOriginInfo("created by @InheritConstructors");
        for (PsiParameter parameter : constructor.getParameterList().getParameters()) {
            String name = StringUtil.notNullize(parameter.getName());
            PsiType type = superClassSubstitutor.substitute(parameter.getType());
            inheritedConstructor.addParameter(name, type, false);
        }
        context.addMethod(inheritedConstructor);
    }
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrLightMethodBuilder(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightMethodBuilder)

Example 34 with GrTypeDefinition

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition 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)

Example 35 with GrTypeDefinition

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

the class GroovyPositionManager method findEnclosingTypeDefinition.

@Nullable
private static PsiClass findEnclosingTypeDefinition(SourcePosition position) {
    PsiFile file = position.getFile();
    if (!(file instanceof GroovyFileBase))
        return null;
    PsiElement element = file.findElementAt(position.getOffset());
    while (true) {
        element = PsiTreeUtil.getParentOfType(element, GrTypeDefinition.class, GroovyFileBase.class);
        if (element instanceof GroovyFileBase) {
            return ((GroovyFileBase) element).getScriptClass();
        } else if (element instanceof GrTypeDefinition && !((GrTypeDefinition) element).isAnonymous()) {
            return (GrTypeDefinition) element;
        }
    }
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)78 PsiElement (com.intellij.psi.PsiElement)17 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)17 PsiClass (com.intellij.psi.PsiClass)16 NotNull (org.jetbrains.annotations.NotNull)15 Nullable (org.jetbrains.annotations.Nullable)14 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)9 PsiFile (com.intellij.psi.PsiFile)8 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)8 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)8 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)7 Project (com.intellij.openapi.project.Project)6 ArrayList (java.util.ArrayList)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 Editor (com.intellij.openapi.editor.Editor)5 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4