private void generateAccessors() { // generate accessors myNameToGetter = new HashMap<String, PsiMethod>(); myNameToSetter = new HashMap<String, PsiMethod>(); for (FieldDescriptor fieldDescriptor : myFieldDescriptors) { final DocCommentPolicy<PsiDocComment> commentPolicy = new DocCommentPolicy<PsiDocComment>(myDescriptor.getJavadocPolicy()); PsiField field = fieldDescriptor.getField(); final PsiDocComment docComment = field.getDocComment(); if (myDescriptor.isToEncapsulateGet()) { final PsiMethod prototype = fieldDescriptor.getGetterPrototype(); assert prototype != null; final PsiMethod getter = addOrChangeAccessor(prototype, myNameToGetter); if (docComment != null) { final PsiDocComment getterJavadoc = (PsiDocComment) getter.addBefore(docComment, getter.getFirstChild()); commentPolicy.processNewJavaDoc(getterJavadoc); } } if (myDescriptor.isToEncapsulateSet() && !field.hasModifierProperty(PsiModifier.FINAL)) { PsiMethod prototype = fieldDescriptor.getSetterPrototype(); assert prototype != null; addOrChangeAccessor(prototype, myNameToSetter); } if (docComment != null) { commentPolicy.processOldJavaDoc(docComment); } } }
private void updateFieldVisibility() { if (myDescriptor.getFieldsVisibility() == null) return; for (FieldDescriptor descriptor : myFieldDescriptors) { setNewFieldVisibility(descriptor.getField(), myDescriptor); } }
protected boolean preprocessUsages(Ref<UsageInfo[]> refUsages) { final MultiMap<PsiElement, String> conflicts = new MultiMap<PsiElement, String>(); checkExistingMethods(conflicts, true); checkExistingMethods(conflicts, false); final Collection<PsiClass> classes = ClassInheritorsSearch.search(myClass).findAll(); for (FieldDescriptor fieldDescriptor : myFieldDescriptors) { final Set<PsiMethod> setters = new HashSet<PsiMethod>(); final Set<PsiMethod> getters = new HashSet<PsiMethod>(); for (PsiClass aClass : classes) { final PsiMethod getterOverrider = myDescriptor.isToEncapsulateGet() ? aClass.findMethodBySignature(fieldDescriptor.getGetterPrototype(), false) : null; if (getterOverrider != null) { getters.add(getterOverrider); } final PsiMethod setterOverrider = myDescriptor.isToEncapsulateSet() ? aClass.findMethodBySignature(fieldDescriptor.getSetterPrototype(), false) : null; if (setterOverrider != null) { setters.add(setterOverrider); } } if (!getters.isEmpty() || !setters.isEmpty()) { final PsiField field = fieldDescriptor.getField(); for (PsiReference reference : ReferencesSearch.search(field)) { final PsiElement place = reference.getElement(); if (place instanceof PsiReferenceExpression) { final PsiExpression qualifierExpression = ((PsiReferenceExpression) place).getQualifierExpression(); final PsiClass ancestor; if (qualifierExpression == null) { ancestor = PsiTreeUtil.getParentOfType(place, PsiClass.class, false); } else { ancestor = PsiUtil.resolveClassInType(qualifierExpression.getType()); } final boolean isGetter = !PsiUtil.isAccessedForWriting((PsiExpression) place); for (PsiMethod overridden : isGetter ? getters : setters) { if (InheritanceUtil.isInheritorOrSelf(myClass, ancestor, true)) { conflicts.putValue( overridden, "There is already a " + RefactoringUIUtil.getDescription(overridden, true) + " which would hide generated " + (isGetter ? "getter" : "setter") + " for " + place.getText()); break; } } } } } } return showConflicts(conflicts, refUsages.get()); }
@Nullable @Override protected RefactoringEventData getBeforeData() { RefactoringEventData data = new RefactoringEventData(); final List<PsiElement> fields = new ArrayList<PsiElement>(); for (FieldDescriptor fieldDescriptor : myFieldDescriptors) { fields.add(fieldDescriptor.getField()); } data.addElements(fields); return data; }
@NotNull protected UsageInfo[] findUsages() { ArrayList<EncapsulateFieldUsageInfo> array = ContainerUtil.newArrayList(); for (FieldDescriptor fieldDescriptor : myFieldDescriptors) { for (final PsiReference reference : ReferencesSearch.search(fieldDescriptor.getField())) { final PsiElement element = reference.getElement(); if (element == null) continue; final EncapsulateFieldHelper helper = EncapsulateFieldHelper.getHelper(element.getLanguage()); if (helper != null) { EncapsulateFieldUsageInfo usageInfo = helper.createUsage(myDescriptor, fieldDescriptor, reference); if (usageInfo != null) { array.add(usageInfo); } } } } EncapsulateFieldUsageInfo[] usageInfos = array.toArray(new EncapsulateFieldUsageInfo[array.size()]); return UsageViewUtil.removeDuplicatedUsages(usageInfos); }