protected void generatePsiElements( @NotNull PsiField psiField, @NotNull PsiAnnotation psiAnnotation, @NotNull List<? super PsiElement> target) { final String fieldName = psiField.getName(); final PsiType psiFieldType = psiField.getType(); final String methodName = getFindByName(psiField); PsiClass psiClass = psiField.getContainingClass(); assert psiClass != null; UserMapKeys.addWriteUsageFor(psiField); LombokLightMethodBuilder method = new LombokLightMethodBuilder(psiField.getManager(), methodName) .withMethodReturnType(PsiTypesUtil.getClassType(psiClass)) .withContainingClass(psiClass) .withParameter(fieldName, psiFieldType) .withNavigationElement(psiField); method.withModifier(PsiModifier.STATIC); method.withModifier(PsiModifier.PUBLIC); target.add(method); }
private static boolean isAvailable(PsiField field) { PsiClass containingClass = field == null ? null : field.getContainingClass(); return field != null && field.getManager().isInProject(field) && !field.hasModifierProperty(PsiModifier.STATIC) && containingClass != null && !(containingClass instanceof PsiSyntheticClass) && containingClass.getName() != null; }
public static boolean isFieldEquivalentTo(@NotNull PsiField field, PsiElement another) { if (!(another instanceof PsiField)) return false; String name1 = field.getName(); if (name1 == null) return false; if (!another.isValid()) return false; String name2 = ((PsiField) another).getName(); if (!name1.equals(name2)) return false; PsiClass aClass1 = field.getContainingClass(); PsiClass aClass2 = ((PsiField) another).getContainingClass(); return aClass1 != null && aClass2 != null && field.getManager().areElementsEquivalent(aClass1, aClass2); }
private boolean shouldQualify(PsiField field, InsertionContext context) { if (myHelper != null && !myHelper.willBeImported()) { return true; } if (getAttribute(FORCE_QUALIFY) != null) { return true; } PsiReference reference = context.getFile().findReferenceAt(context.getStartOffset()); if (reference instanceof PsiReferenceExpression && !((PsiReferenceExpression) reference).isQualified()) { final PsiVariable target = JavaPsiFacade.getInstance(context.getProject()) .getResolveHelper() .resolveReferencedVariable(field.getName(), (PsiElement) reference); return !field .getManager() .areElementsEquivalent(target, CompletionUtil.getOriginalOrSelf(field)); } return false; }
private void findUsagesForField(PsiField field, List<FixableUsageInfo> usages) { final PsiManager psiManager = field.getManager(); final Project project = psiManager.getProject(); final GlobalSearchScope scope = GlobalSearchScope.allScope(project); final String qualifiedName = getQualifiedName(); @NonNls String getter = null; if (myGenerateAccessors) { getter = GenerateMembersUtil.suggestGetterName(field); } else { final PsiMethod fieldGetter = PropertyUtil.findPropertyGetter(sourceClass, field.getName(), false, false); if (fieldGetter != null && isInMovedElement(fieldGetter)) { getter = fieldGetter.getName(); } } @NonNls String setter = null; if (myGenerateAccessors) { setter = GenerateMembersUtil.suggestSetterName(field); } else { final PsiMethod fieldSetter = PropertyUtil.findPropertySetter(sourceClass, field.getName(), false, false); if (fieldSetter != null && isInMovedElement(fieldSetter)) { setter = fieldSetter.getName(); } } final boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC); for (PsiReference reference : ReferencesSearch.search(field, scope)) { final PsiElement element = reference.getElement(); if (isInMovedElement(element)) { continue; } if (element instanceof PsiReferenceExpression) { final PsiReferenceExpression exp = (PsiReferenceExpression) element; if (RefactoringUtil.isPlusPlusOrMinusMinus(exp.getParent())) { usages.add( isStatic ? new ReplaceStaticVariableIncrementDecrement(exp, qualifiedName) : new ReplaceInstanceVariableIncrementDecrement( exp, delegateFieldName, setter, getter, field.getName())); } else if (RefactoringUtil.isAssignmentLHS(exp)) { usages.add( isStatic ? new ReplaceStaticVariableAssignment(exp, qualifiedName) : new ReplaceInstanceVariableAssignment( PsiTreeUtil.getParentOfType(exp, PsiAssignmentExpression.class), delegateFieldName, setter, getter, field.getName())); } else { usages.add( isStatic ? new ReplaceStaticVariableAccess( exp, qualifiedName, enumConstants.contains(field)) : new ReplaceInstanceVariableAccess( exp, delegateFieldName, getter, field.getName())); } if (!isStatic) { delegationRequired = true; } } else if (element instanceof PsiDocTagValue) { usages.add(new BindJavadocReference(element, qualifiedName, field.getName())); } } }