Search in sources :

Example 36 with GrField

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

the class RenameGrFieldProcessor method findExistingNameConflicts.

@Override
public void findExistingNameConflicts(PsiElement element, String newName, MultiMap<PsiElement, String> conflicts) {
    super.findExistingNameConflicts(element, newName, conflicts);
    GrField field = (GrField) element;
    final PsiClass containingClass = field.getContainingClass();
    if (containingClass == null)
        return;
    final PsiMethod getter = GroovyPropertyUtils.findGetterForField(field);
    if (getter instanceof GrAccessorMethod) {
        final PsiMethod newGetter = PropertyUtil.findPropertyGetter(containingClass, newName, field.hasModifierProperty(PsiModifier.STATIC), true);
        if (newGetter != null && !(newGetter instanceof GrAccessorMethod)) {
            conflicts.putValue(newGetter, GroovyRefactoringBundle.message("implicit.getter.will.by.overriden.by.method", field.getName(), newGetter.getName()));
        }
    }
    final PsiMethod setter = GroovyPropertyUtils.findSetterForField(field);
    if (setter instanceof GrAccessorMethod) {
        final PsiMethod newSetter = PropertyUtil.findPropertySetter(containingClass, newName, field.hasModifierProperty(PsiModifier.STATIC), true);
        if (newSetter != null && !(newSetter instanceof GrAccessorMethod)) {
            conflicts.putValue(newSetter, GroovyRefactoringBundle.message("implicit.setter.will.by.overriden.by.method", field.getName(), newSetter.getName()));
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)

Example 37 with GrField

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

the class RenameGroovyPropertyProcessor method prepareRenaming.

@Override
public void prepareRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames) {
    LOG.assertTrue(element instanceof PropertyForRename);
    final List<? extends PsiElement> elementsToRename = ((PropertyForRename) element).getElementsToRename();
    for (PsiElement psiElement : elementsToRename) {
        if (psiElement instanceof GrField) {
            allRenames.put(psiElement, newName);
        } else if (psiElement instanceof GrMethod) {
            if (GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) psiElement)) {
                allRenames.put(psiElement, RenamePropertyUtil.getGetterNameByOldName(newName, ((PsiMethod) psiElement).getName()));
            } else {
                allRenames.put(psiElement, GroovyPropertyUtils.getSetterName(newName));
            }
        }
    }
    allRenames.remove(element);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) PsiMethod(com.intellij.psi.PsiMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiElement(com.intellij.psi.PsiElement)

Example 38 with GrField

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

the class GrTraitUtil method doCollectCompiledTraitFields.

private static void doCollectCompiledTraitFields(ClsClassImpl trait, Collection<GrField> result) {
    VirtualFile traitFile = trait.getContainingFile().getVirtualFile();
    if (traitFile == null)
        return;
    VirtualFile helperFile = traitFile.getParent().findChild(trait.getName() + GroovyTraitFieldsFileIndex.HELPER_SUFFIX);
    if (helperFile == null)
        return;
    int key = FileBasedIndex.getFileId(helperFile);
    final List<Collection<TraitFieldDescriptor>> values = FileBasedIndex.getInstance().getValues(GroovyTraitFieldsFileIndex.INDEX_ID, key, trait.getResolveScope());
    values.forEach(descriptors -> descriptors.forEach(descriptor -> result.add(createTraitField(descriptor, trait))));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiJavaFileStub(com.intellij.psi.impl.java.stubs.PsiJavaFileStub) GrLightMethodBuilder(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightMethodBuilder) ABSTRACT(com.intellij.psi.PsiModifier.ABSTRACT) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrLightField(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightField) CachedValueProvider(com.intellij.psi.util.CachedValueProvider) PsiModificationTracker(com.intellij.psi.util.PsiModificationTracker) GrAnnotationUtil(org.jetbrains.plugins.groovy.lang.psi.impl.GrAnnotationUtil) Map(java.util.Map) InheritanceUtil(com.intellij.psi.util.InheritanceUtil) Logger(com.intellij.openapi.diagnostic.Logger) GROOVY_TRAIT(org.jetbrains.plugins.groovy.lang.psi.util.GroovyCommonClassNames.GROOVY_TRAIT) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) ClsClassImpl(com.intellij.psi.impl.compiled.ClsClassImpl) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiJavaFileStubImpl(com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl) GROOVY_TRAIT_IMPLEMENTED(org.jetbrains.plugins.groovy.lang.psi.util.GroovyCommonClassNames.GROOVY_TRAIT_IMPLEMENTED) Collection(java.util.Collection) TraitFieldDescriptor(org.jetbrains.plugins.groovy.lang.resolve.GroovyTraitFieldsFileIndex.TraitFieldDescriptor) CachedValuesManager(com.intellij.psi.util.CachedValuesManager) Nullable(org.jetbrains.annotations.Nullable) Contract(org.jetbrains.annotations.Contract) GroovyTraitFieldsFileIndex(org.jetbrains.plugins.groovy.lang.resolve.GroovyTraitFieldsFileIndex) List(java.util.List) GroovyTraitMethodsFileIndex(org.jetbrains.plugins.groovy.lang.resolve.GroovyTraitMethodsFileIndex) AnnotationUtil(com.intellij.codeInsight.AnnotationUtil) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) com.intellij.psi(com.intellij.psi) StubElement(com.intellij.psi.stubs.StubElement) NotNull(org.jetbrains.annotations.NotNull) GrModifierFlags(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierFlags) Ref(com.intellij.openapi.util.Ref) PsiMethodStub(com.intellij.psi.impl.java.stubs.PsiMethodStub) Collection(java.util.Collection)

Example 39 with GrField

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

the class GroovyPropertyUtils method generateGetterPrototype.

public static GrMethod generateGetterPrototype(PsiField field) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(field.getProject());
    String name = field.getName();
    String getName = getGetterNameNonBoolean(field.getName());
    try {
        PsiType type = field instanceof GrField ? ((GrField) field).getDeclaredType() : field.getType();
        GrMethod getter = factory.createMethod(getName, type);
        if (field.hasModifierProperty(PsiModifier.STATIC)) {
            PsiUtil.setModifierProperty(getter, PsiModifier.STATIC, true);
        }
        annotateWithNullableStuff(field, getter);
        GrCodeBlock body = factory.createMethodBodyFromText("\nreturn " + name + "\n");
        getter.getBlock().replace(body);
        return getter;
    } catch (IncorrectOperationException e) {
        LOG.error(e);
        return null;
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 40 with GrField

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

the class ExternalBuilderStrategySupport method applyTransformation.

@Override
public void applyTransformation(@NotNull TransformationContext context) {
    PsiAnnotation annotation = PsiImplUtil.getAnnotation(context.getCodeClass(), BUILDER_FQN);
    if (!isApplicable(annotation, EXTERNAL_STRATEGY_NAME))
        return;
    final PsiClass constructedClass = GrAnnotationUtil.inferClassAttribute(annotation, "forClass");
    if (constructedClass == null || "groovy.transform.Undefined.CLASS".equals(constructedClass.getQualifiedName()))
        return;
    if (constructedClass instanceof GrTypeDefinition) {
        for (GrField field : ((GrTypeDefinition) constructedClass).getCodeFields()) {
            context.addMethod(DefaultBuilderStrategySupport.createFieldSetter(context.getCodeClass(), field, annotation));
        }
    } else {
        for (PsiMethod setter : PropertyUtil.getAllProperties(constructedClass, true, false).values()) {
            final PsiMethod builderSetter = createFieldSetter(context.getCodeClass(), setter, annotation);
            if (builderSetter != null)
                context.addMethod(builderSetter);
        }
    }
    context.addMethod(createBuildMethod(annotation, createType(constructedClass)));
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) 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