public PsiMember fromString(final String s, final ConvertContext context) { if (s == null) return null; final PsiClass psiClass = getTargetClass(context); if (psiClass == null) return null; for (PropertyMemberType type : getMemberTypes(context)) { switch (type) { case FIELD: final PsiField field = psiClass.findFieldByName(s, isLookDeep()); if (field != null) return field; break; case GETTER: final PsiMethod getter = PropertyUtil.findPropertyGetter(psiClass, getPropertyName(s, context), false, isLookDeep()); if (getter != null) return getter; break; case SETTER: final PsiMethod setter = PropertyUtil.findPropertySetter(psiClass, getPropertyName(s, context), false, isLookDeep()); if (setter != null) return setter; break; } } return null; }
@Nullable public PsiMember findExistingMember(@NotNull PsiClass aClass, @NotNull PropertyMemberType memberType) { if (memberType == PropertyMemberType.FIELD) { return aClass.findFieldByName(getFieldName(aClass), false); } else if (memberType == PropertyMemberType.GETTER) { try { PsiElementFactory psiElementFactory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory(); PsiField field = psiElementFactory.createField(myAttributeName, myType); PsiMethod[] templates = GetterSetterPrototypeProvider.generateGetterSetters(field, myMemberType == PropertyMemberType.GETTER); for (PsiMethod template : templates) { PsiMethod existingMethod = aClass.findMethodBySignature(template, true); if (existingMethod != null) return existingMethod; } } catch (IncorrectOperationException e) { assert false : e; } } return null; }
public LocalQuickFix[] getQuickFixes(final ConvertContext context) { final String targetName = ((GenericValue)context.getInvocationElement()).getStringValue(); if (!PsiNameHelper.getInstance(context.getProject()).isIdentifier(targetName)) return super.getQuickFixes(context); final PsiClass targetClass = getTargetClass(context); if (targetClass == null) return super.getQuickFixes(context); final PropertyMemberType memberType = getMemberTypes(context)[0]; final PsiType psiType = getPsiType(context); final IntentionAction fix = QuickFixFactory.getInstance().createCreateFieldOrPropertyFix(targetClass, targetName, psiType, memberType); return fix instanceof LocalQuickFix? new LocalQuickFix[] {(LocalQuickFix)fix} : super.getQuickFixes(context); }
public GenerateFieldOrPropertyHandler(String attributeName, PsiType type, final PropertyMemberType memberType, final PsiAnnotation... annotations) { super(""); myAttributeName = attributeName; myType = type; myMemberType = memberType; myAnnotations = annotations; }
public CreateFieldOrPropertyFix(final PsiClass aClass, final String name, final PsiType type, @NotNull PropertyMemberType memberType, final PsiAnnotation[] annotations) { myClass = aClass; myName = name; myType = type; myMemberType = memberType; myAnnotations = annotations; }
public LocalQuickFix[] getQuickFixes(final ConvertContext context) { final String targetName = ((GenericValue)context.getInvocationElement()).getStringValue(); if (!JavaPsiFacade.getInstance(context.getProject()).getNameHelper().isIdentifier(targetName)) return super.getQuickFixes(context); final PsiClass targetClass = getTargetClass(context); if (targetClass == null) return super.getQuickFixes(context); final PropertyMemberType memberType = getMemberTypes(context)[0]; final PsiType psiType = getPsiType(context); final IntentionAction fix = QuickFixFactory.getInstance().createCreateFieldOrPropertyFix(targetClass, targetName, psiType, memberType); return fix instanceof LocalQuickFix? new LocalQuickFix[] {(LocalQuickFix)fix} : super.getQuickFixes(context); }
public CreateFieldOrPropertyFix(final PsiClass aClass, final String name, final PsiType type, final PropertyMemberType memberType, final PsiAnnotation[] annotations) { myClass = aClass; myName = name; myType = type; myMemberType = memberType; myAnnotations = annotations; }
@Nullable public PsiMember findExistingMember(@NotNull PsiClass aClass, @NotNull PropertyMemberType memberType) { if(memberType == PropertyMemberType.FIELD) { return aClass.findFieldByName(getFieldName(aClass), false); } else if(memberType == PropertyMemberType.GETTER) { try { PsiElementFactory psiElementFactory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory(); PsiField field = psiElementFactory.createField(myAttributeName, myType); PsiMethod[] templates = GetterSetterPrototypeProvider.generateGetterSetters(field, myMemberType == PropertyMemberType.GETTER); for(PsiMethod template : templates) { PsiMethod existingMethod = aClass.findMethodBySignature(template, true); if(existingMethod != null) { return existingMethod; } } } catch(IncorrectOperationException e) { assert false : e; } } return null; }
@NotNull @Override public IntentionAction createCreateFieldOrPropertyFix(@NotNull final PsiClass aClass, @NotNull final String name, @NotNull final PsiType type, @NotNull final PropertyMemberType targetMember, @NotNull final PsiAnnotation... annotations) { return new CreateFieldOrPropertyFix(aClass, name, type, targetMember, annotations); }
@NotNull protected abstract PropertyMemberType[] getMemberTypes(final ConvertContext context);
@NotNull @Override public IntentionAction createCreateFieldOrPropertyFix(@NotNull PsiClass psiClass, @NotNull String s, @NotNull PsiType psiType, @NotNull PropertyMemberType propertyMemberType, @NotNull PsiAnnotation... psiAnnotations) { return QuickFixes.EMPTY_FIX; }
private String getFieldName(PsiClass aClass) { return myMemberType == PropertyMemberType.FIELD? myAttributeName : JavaCodeStyleManager .getInstance(aClass.getProject()).propertyNameToVariableName(myAttributeName, VariableKind.FIELD); }
@Override @NotNull public String getText() { return QuickFixBundle.message(myMemberType == PropertyMemberType.FIELD ? "create.field.text":"create.property.text", myName); }
@NotNull public abstract IntentionAction createCreateFieldOrPropertyFix(@NotNull PsiClass aClass, @NotNull String name, @NotNull PsiType type, @NotNull PropertyMemberType targetMember, @NotNull PsiAnnotation... annotations);
@Override public IntentionAction createCreateFieldOrPropertyFix(final PsiClass aClass, final String name, final PsiType type, final PropertyMemberType targetMember, final PsiAnnotation... annotations) { return new CreateFieldOrPropertyFix(aClass, name, type, targetMember, annotations); }
@NotNull protected PropertyMemberType[] getMemberTypes(final ConvertContext context) { return new PropertyMemberType[] { PropertyMemberType.SETTER, PropertyMemberType.FIELD}; }
private String getFieldName(PsiClass aClass) { return myMemberType == PropertyMemberType.FIELD ? myAttributeName : JavaCodeStyleManager.getInstance(aClass.getProject()).propertyNameToVariableName(myAttributeName, VariableKind.FIELD); }
@Override @NotNull public String getText() { return JavaQuickFixBundle.message(myMemberType == PropertyMemberType.FIELD ? "create.field.text":"create.property.text", myName); }
public abstract IntentionAction createCreateFieldOrPropertyFix(final PsiClass aClass, final String name, final PsiType type, final PropertyMemberType targetMember, final PsiAnnotation... annotations);