com.intellij.psi.util.PropertyUtil Java Examples

The following examples show how to use com.intellij.psi.util.PropertyUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PolymorphismRefactoring.java    From IntelliJDeodorant with MIT License 6 votes vote down vote up
protected void generateGettersForAccessedFields() {
    Set<PsiField> accessedFields = new LinkedHashSet<>();
    accessedFields.addAll(typeCheckElimination.getAccessedFields());
    accessedFields.addAll(typeCheckElimination.getSuperAccessedFields());
    for (PsiField fragment : accessedFields) {
        if (!fragment.getModifierList().hasModifierProperty(PsiModifier.STATIC)) {
            PsiMethod getterMethodBinding = null;
            if (typeCheckElimination.getSuperAccessedFields().contains(fragment)) {
                for (PsiField fieldBinding : typeCheckElimination.getSuperAccessedFieldBindings()) {
                    if (fieldBinding.equals(fragment)) {
                        getterMethodBinding = typeCheckElimination.getGetterMethodBindingOfSuperAccessedField(fieldBinding);
                        break;
                    }
                }
            } else {
                getterMethodBinding = findGetterMethodInContext(fragment);
            }
            if (getterMethodBinding == null) {
                PsiMethod getter = PropertyUtil.generateGetterPrototype(fragment);
                fragment.getContainingClass().add(getter);
            }
        }
    }
}
 
Example #2
Source File: PolymorphismRefactoring.java    From IntelliJDeodorant with MIT License 6 votes vote down vote up
protected void generateSettersForAssignedFields() {
    Set<PsiField> assignedFields = new LinkedHashSet<>();
    assignedFields.addAll(typeCheckElimination.getAssignedFields());
    assignedFields.addAll(typeCheckElimination.getSuperAssignedFields());
    for (PsiField fragment : assignedFields) {
        PsiMethod setterMethodBinding = null;
        if (typeCheckElimination.getSuperAssignedFields().contains(fragment)) {
            for (PsiField fieldBinding : typeCheckElimination.getSuperAssignedFieldBindings()) {
                if (fieldBinding.equals(fragment)) {
                    setterMethodBinding = typeCheckElimination.getSetterMethodBindingOfSuperAssignedField(fieldBinding);
                    break;
                }
            }
        } else {
            setterMethodBinding = findSetterMethodInContext(fragment);
        }
        if (setterMethodBinding == null) {
            PsiMethod setter = PropertyUtil.generateSetterPrototype(fragment);
            fragment.getContainingClass().add(setter);
        }
    }
}
 
Example #3
Source File: RefactorSetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected void process(List<ClassMember> classMembers) {
  for (ClassMember classMember : classMembers) {
    final PsiElementClassMember elementClassMember = (PsiElementClassMember) classMember;

    PsiField psiField = (PsiField) elementClassMember.getPsiElement();
    PsiMethod psiMethod = PropertyUtil.findPropertySetter(psiField.getContainingClass(), psiField.getName(), false, false);
    if (null != psiMethod) {
      PsiModifierList modifierList = psiField.getModifierList();
      if (null != modifierList) {
        PsiAnnotation psiAnnotation = modifierList.addAnnotation(Setter.class.getName());

        psiMethod.delete();
      }
    }
  }
}
 
Example #4
Source File: RefactorGetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
  protected void process(List<ClassMember> classMembers) {
    for (ClassMember classMember : classMembers) {
      final PsiElementClassMember elementClassMember = (PsiElementClassMember) classMember;

      PsiField psiField = (PsiField) elementClassMember.getPsiElement();
      PsiMethod psiMethod = PropertyUtil.findPropertyGetter(psiField.getContainingClass(), psiField.getName(), false, false);

      if (null != psiMethod) {
        PsiModifierList modifierList = psiField.getModifierList();
        if (null != modifierList) {
          PsiAnnotation psiAnnotation = modifierList.addAnnotation(Getter.class.getName());
//          psiAnnotation.setDeclaredAttributeValue("value", )

          psiMethod.delete();
        }
      }
    }
  }
 
Example #5
Source File: PsiCustomUtil.java    From intellij-spring-assistant with MIT License 5 votes vote down vote up
@Nullable
private static PsiMethod findInstancePropertySetter(@NotNull PsiClass psiClass,
    @Nullable String propertyName) {
  if (StringUtil.isEmpty(propertyName))
    return null;
  final String suggestedSetterName = PropertyUtil.suggestSetterName(propertyName);
  final PsiMethod[] setters = psiClass.findMethodsByName(suggestedSetterName, true);
  for (PsiMethod setter : setters) {
    if (setter.hasModifierProperty(PUBLIC) && !setter.hasModifierProperty(STATIC)
        && isSimplePropertySetter(setter)) {
      return setter;
    }
  }
  return null;
}
 
Example #6
Source File: RefactorSetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected List<EncapsulatableClassMember> getEncapsulatableClassMembers(PsiClass psiClass) {
  final List<EncapsulatableClassMember> result = new ArrayList<>();
  for (PsiField field : psiClass.getFields()) {
    if (null != PropertyUtil.findPropertySetter(psiClass, field.getName(), false, false)) {
      result.add(new PsiFieldMember(field));
    }
  }
  return result;
}
 
Example #7
Source File: LombokGetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void processClass(@NotNull PsiClass psiClass) {
  final Map<PsiField, PsiMethod> fieldMethodMap = new HashMap<>();
  for (PsiField psiField : psiClass.getFields()) {
    PsiMethod propertyGetter = PropertyUtil.findPropertyGetter(psiClass, psiField.getName(), psiField.hasModifierProperty(PsiModifier.STATIC), false);

    if (null != propertyGetter) {
      fieldMethodMap.put(psiField, propertyGetter);
    }
  }

  processIntern(fieldMethodMap, psiClass, Getter.class);
}
 
Example #8
Source File: LombokSetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void processClass(@NotNull PsiClass psiClass) {
  final Map<PsiField, PsiMethod> fieldMethodMap = new HashMap<>();
  for (PsiField psiField : psiClass.getFields()) {
    PsiMethod propertySetter = PropertyUtil.findPropertySetter(psiClass, psiField.getName(), psiField.hasModifierProperty(PsiModifier.STATIC), false);

    if (null != propertySetter) {
      fieldMethodMap.put(psiField, propertySetter);
    }
  }

  processIntern(fieldMethodMap, psiClass, Setter.class);
}
 
Example #9
Source File: RefactorGetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected List<EncapsulatableClassMember> getEncapsulatableClassMembers(PsiClass psiClass) {
  final List<EncapsulatableClassMember> result = new ArrayList<>();
  for (PsiField field : psiClass.getFields()) {
    if (null != PropertyUtil.findPropertyGetter(psiClass, field.getName(), false, false)) {
      result.add(new PsiFieldMember(field));
    }
  }
  return result;
}
 
Example #10
Source File: InnerBuilderGenerator.java    From innerbuilder with Apache License 2.0 5 votes vote down vote up
private PsiMethod generateConstructor(final PsiClass targetClass, final PsiType builderType) {
    final PsiMethod constructor = psiElementFactory.createConstructor(targetClass.getName());
    constructor.getModifierList().setModifierProperty(PsiModifier.PRIVATE, true);

    final PsiParameter builderParameter = psiElementFactory.createParameter("builder", builderType);
    constructor.getParameterList().add(builderParameter);

    final PsiCodeBlock constructorBody = constructor.getBody();
    if (constructorBody != null) {
        for (final PsiFieldMember member : selectedFields) {
            final PsiField field = member.getElement();

            final PsiMethod setterPrototype = PropertyUtil.generateSetterPrototype(field);
            final PsiMethod setter = targetClass.findMethodBySignature(setterPrototype, true);

            final String fieldName = field.getName();
            boolean isFinal = false;
            final PsiModifierList modifierList = field.getModifierList();
            if (modifierList != null) {
                isFinal = modifierList.hasModifierProperty(PsiModifier.FINAL);
            }

            final String assignText;
            if (setter == null || isFinal) {
                assignText = String.format("%1$s = builder.%1$s;", fieldName);
            } else {
                assignText = String.format("%s(builder.%s);", setter.getName(), fieldName);
            }

            final PsiStatement assignStatement = psiElementFactory.createStatementFromText(assignText, null);
            constructorBody.add(assignStatement);
        }
    }

    return constructor;
}