@NotNull public XPathType getExpectedType(XPathExpression expr) { final XmlTag tag = PsiTreeUtil.getContextOfType(expr, XmlTag.class, true); if (tag != null && XsltSupport.isXsltTag(tag)) { final XsltElement element = XsltElementFactory.getInstance().wrapElement(tag, XsltElement.class); if (element instanceof XsltVariable) { return ((XsltVariable) element).getType(); } else { final XmlAttribute attr = PsiTreeUtil.getContextOfType(expr, XmlAttribute.class, true); if (attr != null) { if (element instanceof XsltWithParam) { final XmlAttribute nameAttr = tag.getAttribute("name", null); if (nameAttr != null) { final XmlAttributeValue valueElement = nameAttr.getValueElement(); if (valueElement != null) { final PsiReference[] references = valueElement.getReferences(); for (PsiReference reference : references) { final PsiElement psiElement = reference.resolve(); if (psiElement instanceof XsltVariable) { return ((XsltVariable) psiElement).getType(); } } } } } else { final String name = attr.getName(); return getTypeForTag(tag, name); } } } } return XPathType.UNKNOWN; }
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()); }
@Override public boolean processUsage( ChangeInfo changeInfo, UsageInfo usageInfo, boolean beforeMethodChange, UsageInfo[] usages) { if (!(changeInfo instanceof JavaChangeInfo)) return false; PsiElement element = usageInfo.getElement(); if (element == null) return false; if (!GroovyLanguage.INSTANCE.equals(element.getLanguage())) return false; if (beforeMethodChange) { if (usageInfo instanceof OverriderUsageInfo) { processPrimaryMethodInner( ((JavaChangeInfo) changeInfo), (GrMethod) ((OverriderUsageInfo) usageInfo).getElement(), ((OverriderUsageInfo) usageInfo).getBaseMethod()); } } else { if (usageInfo instanceof GrMethodCallUsageInfo) { processMethodUsage( element, ((JavaChangeInfo) changeInfo), ((GrMethodCallUsageInfo) usageInfo).isToChangeArguments(), ((GrMethodCallUsageInfo) usageInfo).isToCatchExceptions(), ((GrMethodCallUsageInfo) usageInfo).getMapToArguments(), ((GrMethodCallUsageInfo) usageInfo).getSubstitutor()); return true; } else if (usageInfo instanceof DefaultConstructorImplicitUsageInfo) { processConstructor( (GrMethod) ((DefaultConstructorImplicitUsageInfo) usageInfo).getConstructor(), (JavaChangeInfo) changeInfo); return true; } else if (usageInfo instanceof NoConstructorClassUsageInfo) { processClassUsage( (GrTypeDefinition) ((NoConstructorClassUsageInfo) usageInfo).getPsiClass(), ((JavaChangeInfo) changeInfo)); return true; } else if (usageInfo instanceof ChangeSignatureParameterUsageInfo) { String newName = ((ChangeSignatureParameterUsageInfo) usageInfo).newParameterName; ((PsiReference) element).handleElementRename(newName); return true; } else { PsiReference ref = element.getReference(); if (ref != null && changeInfo.getMethod() != null) { ref.bindToElement(changeInfo.getMethod()); return true; } } } return false; }
@Nullable static PsiElement resolveReference(final PsiReference psiReference) { if (psiReference instanceof PsiPolyVariantReference) { final ResolveResult[] results = ((PsiPolyVariantReference) psiReference).multiResolve(true); if (results.length == 1) return results[0].getElement(); } return psiReference.resolve(); }
// need to shorten references in type argument list public static void shortenReference(final PsiFile file, final int offset) throws IncorrectOperationException { Project project = file.getProject(); final PsiDocumentManager manager = PsiDocumentManager.getInstance(project); Document document = manager.getDocument(file); if (document == null) { PsiUtilCore.ensureValid(file); LOG.error("No document for " + file); return; } manager.commitDocument(document); final PsiReference ref = file.findReferenceAt(offset); if (ref != null) { PsiElement element = ref.getElement(); if (element != null) { JavaCodeStyleManager.getInstance(project).shortenClassReferences(element); PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document); } } }
@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); }
private void checkExistingMethods(MultiMap<PsiElement, String> conflicts, boolean isGetter) { if (isGetter) { if (!myDescriptor.isToEncapsulateGet()) return; } else { if (!myDescriptor.isToEncapsulateSet()) return; } for (FieldDescriptor descriptor : myFieldDescriptors) { PsiMethod prototype = isGetter ? descriptor.getGetterPrototype() : descriptor.getSetterPrototype(); final PsiType prototypeReturnType = prototype.getReturnType(); PsiMethod existing = myClass.findMethodBySignature(prototype, true); if (existing != null) { final PsiType returnType = existing.getReturnType(); if (!RefactoringUtil.equivalentTypes( prototypeReturnType, returnType, myClass.getManager())) { final String descr = PsiFormatUtil.formatMethod( existing, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS | PsiFormatUtilBase.SHOW_TYPE, PsiFormatUtilBase.SHOW_TYPE); String message = isGetter ? RefactoringBundle.message( "encapsulate.fields.getter.exists", CommonRefactoringUtil.htmlEmphasize(descr), CommonRefactoringUtil.htmlEmphasize(prototype.getName())) : RefactoringBundle.message( "encapsulate.fields.setter.exists", CommonRefactoringUtil.htmlEmphasize(descr), CommonRefactoringUtil.htmlEmphasize(prototype.getName())); conflicts.putValue(existing, message); } } else { PsiClass containingClass = myClass.getContainingClass(); while (containingClass != null && existing == null) { existing = containingClass.findMethodBySignature(prototype, true); if (existing != null) { for (PsiReference reference : ReferencesSearch.search(existing)) { final PsiElement place = reference.getElement(); LOG.assertTrue(place instanceof PsiReferenceExpression); final PsiExpression qualifierExpression = ((PsiReferenceExpression) place).getQualifierExpression(); final PsiClass inheritor; if (qualifierExpression == null) { inheritor = PsiTreeUtil.getParentOfType(place, PsiClass.class, false); } else { inheritor = PsiUtil.resolveClassInType(qualifierExpression.getType()); } if (InheritanceUtil.isInheritorOrSelf(inheritor, myClass, true)) { conflicts.putValue( existing, "There is already a " + RefactoringUIUtil.getDescription(existing, true) + " which would be hidden by generated " + (isGetter ? "getter" : "setter")); break; } } } containingClass = containingClass.getContainingClass(); } } } }
public boolean processUsage( ChangeInfo changeInfo, UsageInfo usage, boolean beforeMethodChange, UsageInfo[] usages) { if (!isJavaUsage(usage)) return false; if (!(changeInfo instanceof JavaChangeInfo)) return false; if (beforeMethodChange) { if (usage instanceof CallerUsageInfo) { final CallerUsageInfo callerUsageInfo = (CallerUsageInfo) usage; processCallerMethod( (JavaChangeInfo) changeInfo, callerUsageInfo.getMethod(), null, callerUsageInfo.isToInsertParameter(), callerUsageInfo.isToInsertException()); return true; } else if (usage instanceof OverriderUsageInfo) { OverriderUsageInfo info = (OverriderUsageInfo) usage; final PsiMethod method = info.getElement(); final PsiMethod baseMethod = info.getBaseMethod(); if (info.isOriginalOverrider()) { processPrimaryMethod((JavaChangeInfo) changeInfo, method, baseMethod, false); } else { processCallerMethod( (JavaChangeInfo) changeInfo, method, baseMethod, info.isToInsertArgs(), info.isToCatchExceptions()); } return true; } } else { PsiElement element = usage.getElement(); LOG.assertTrue(element != null); if (usage instanceof DefaultConstructorImplicitUsageInfo) { final DefaultConstructorImplicitUsageInfo defConstructorUsage = (DefaultConstructorImplicitUsageInfo) usage; PsiMethod constructor = defConstructorUsage.getConstructor(); if (!constructor.isPhysical()) { final boolean toPropagate = changeInfo instanceof JavaChangeInfoImpl && ((JavaChangeInfoImpl) changeInfo) .propagateParametersMethods.remove(constructor); final PsiClass containingClass = defConstructorUsage.getContainingClass(); constructor = (PsiMethod) containingClass.add(constructor); PsiUtil.setModifierProperty( constructor, VisibilityUtil.getVisibilityModifier(containingClass.getModifierList()), true); if (toPropagate) { ((JavaChangeInfoImpl) changeInfo).propagateParametersMethods.add(constructor); } } addSuperCall( (JavaChangeInfo) changeInfo, constructor, defConstructorUsage.getBaseConstructor(), usages); return true; } else if (usage instanceof NoConstructorClassUsageInfo) { addDefaultConstructor( ((JavaChangeInfo) changeInfo), ((NoConstructorClassUsageInfo) usage).getPsiClass(), usages); return true; } else if (usage instanceof MethodCallUsageInfo) { final MethodCallUsageInfo methodCallInfo = (MethodCallUsageInfo) usage; processMethodUsage( methodCallInfo.getElement(), (JavaChangeInfo) changeInfo, methodCallInfo.isToChangeArguments(), methodCallInfo.isToCatchExceptions(), methodCallInfo.getReferencedMethod(), methodCallInfo.getSubstitutor(), usages); return true; } else if (usage instanceof ChangeSignatureParameterUsageInfo) { String newName = ((ChangeSignatureParameterUsageInfo) usage).newParameterName; String oldName = ((ChangeSignatureParameterUsageInfo) usage).oldParameterName; processParameterUsage((PsiReferenceExpression) element, oldName, newName); return true; } else if (usage instanceof CallReferenceUsageInfo) { ((CallReferenceUsageInfo) usage).getReference().handleChangeSignature(changeInfo); return true; } else if (element instanceof PsiEnumConstant) { fixActualArgumentsList( ((PsiEnumConstant) element).getArgumentList(), (JavaChangeInfo) changeInfo, true, PsiSubstitutor.EMPTY); return true; } else if (!(usage instanceof OverriderUsageInfo)) { PsiReference reference = usage instanceof MoveRenameUsageInfo ? usage.getReference() : element.getReference(); if (reference != null) { PsiElement target = changeInfo.getMethod(); if (target != null) { reference.bindToElement(target); } } } } return false; }
public static int insertClassReference( PsiClass psiClass, PsiFile file, int startOffset, int endOffset) { final Project project = file.getProject(); PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); documentManager.commitAllDocuments(); final PsiManager manager = file.getManager(); final Document document = FileDocumentManager.getInstance().getDocument(file.getViewProvider().getVirtualFile()); final PsiReference reference = file.findReferenceAt(startOffset); if (reference != null) { final PsiElement resolved = reference.resolve(); if (resolved instanceof PsiClass) { if (((PsiClass) resolved).getQualifiedName() == null || manager.areElementsEquivalent(psiClass, resolved)) { return endOffset; } } } String name = psiClass.getName(); if (name == null) { return endOffset; } assert document != null; document.replaceString(startOffset, endOffset, name); int newEndOffset = startOffset + name.length(); final RangeMarker toDelete = insertTemporary(newEndOffset, document, " "); documentManager.commitAllDocuments(); PsiElement element = file.findElementAt(startOffset); if (element instanceof PsiIdentifier) { PsiElement parent = element.getParent(); if (parent instanceof PsiJavaCodeReferenceElement && !((PsiJavaCodeReferenceElement) parent).isQualified() && !(parent.getParent() instanceof PsiPackageStatement)) { PsiJavaCodeReferenceElement ref = (PsiJavaCodeReferenceElement) parent; if (psiClass.isValid() && !psiClass.getManager().areElementsEquivalent(psiClass, resolveReference(ref))) { final boolean staticImport = ref instanceof PsiImportStaticReferenceElement; PsiElement newElement; try { newElement = staticImport ? ((PsiImportStaticReferenceElement) ref).bindToTargetClass(psiClass) : ref.bindToElement(psiClass); } catch (IncorrectOperationException e) { return endOffset; // can happen if fqn contains reserved words, for example } final RangeMarker rangeMarker = document.createRangeMarker(newElement.getTextRange()); documentManager.doPostponedOperationsAndUnblockDocument(document); documentManager.commitDocument(document); newElement = CodeInsightUtilCore.findElementInRange( file, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), PsiJavaCodeReferenceElement.class, JavaLanguage.INSTANCE); rangeMarker.dispose(); if (newElement != null) { newEndOffset = newElement.getTextRange().getEndOffset(); if (!(newElement instanceof PsiReferenceExpression)) { PsiReferenceParameterList parameterList = ((PsiJavaCodeReferenceElement) newElement).getParameterList(); if (parameterList != null) { newEndOffset = parameterList.getTextRange().getStartOffset(); } } if (!staticImport && !psiClass .getManager() .areElementsEquivalent(psiClass, resolveReference((PsiReference) newElement)) && !PsiUtil.isInnerClass(psiClass)) { final String qName = psiClass.getQualifiedName(); if (qName != null) { document.replaceString( newElement.getTextRange().getStartOffset(), newEndOffset, qName); newEndOffset = newElement.getTextRange().getStartOffset() + qName.length(); } } } } } } if (toDelete.isValid()) { document.deleteString(toDelete.getStartOffset(), toDelete.getEndOffset()); } return newEndOffset; }