PSI element leak fix

This commit is contained in:
Alexey Kudravtsev
2011-04-06 12:05:29 +04:00
parent bb0fa00b6e
commit 8650883ee2
4 changed files with 56 additions and 54 deletions
@@ -51,24 +51,27 @@ public class MakeTypeExplicitFix extends ReplaceElementFix<XPathExpression> {
}
public void invokeImpl(Project project, PsiFile file) throws IncorrectOperationException {
if (myType == XPathType.BOOLEAN) {
if (myElement.getType() == XPathType.STRING) {
final String text;
if (ExpectedTypeUtil.isExplicitConversion(myElement)) {
final XPathExpression expr = ExpectedTypeUtil.unparenthesize(myElement);
assert expr != null;
XPathExpression myElement = (XPathExpression)getStartElement();
if (myType == XPathType.BOOLEAN) {
if (myElement.getType() == XPathType.STRING) {
final String text;
if (ExpectedTypeUtil.isExplicitConversion(myElement)) {
final XPathExpression expr = ExpectedTypeUtil.unparenthesize(myElement);
assert expr != null;
text = ((XPathFunctionCall)expr).getArgumentList()[0].getText();
} else {
text = myElement.getText();
}
replace("string-length(" + text + ") > 0");
return;
} else if (myElement.getType() == XPathType.NODESET) {
replace("count(" + myElement.getText() + ") > 0");
return;
}
text = ((XPathFunctionCall)expr).getArgumentList()[0].getText();
}
else {
text = myElement.getText();
}
replace("string-length(" + text + ") > 0");
return;
}
replace(myType.getName() + "(" + myElement.getText() + ")");
else if (myElement.getType() == XPathType.NODESET) {
replace("count(" + myElement.getText() + ") > 0");
return;
}
}
replace(myType.getName() + "(" + myElement.getText() + ")");
}
}
@@ -15,16 +15,15 @@
*/
package org.intellij.lang.xpath.validation.inspections.quickfix;
import org.intellij.lang.xpath.psi.XPathExpression;
import org.intellij.lang.xpath.psi.XPathFunctionCall;
import org.intellij.lang.xpath.psi.XPathBinaryExpression;
import org.intellij.lang.xpath.validation.ExpectedTypeUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.intellij.lang.xpath.psi.XPathBinaryExpression;
import org.intellij.lang.xpath.psi.XPathExpression;
import org.intellij.lang.xpath.psi.XPathFunctionCall;
import org.intellij.lang.xpath.validation.ExpectedTypeUtil;
import org.jetbrains.annotations.NotNull;
public class RemoveExplicitConversionFix extends ReplaceElementFix<XPathExpression> {
@@ -43,13 +42,18 @@ public class RemoveExplicitConversionFix extends ReplaceElementFix<XPathExpressi
return "ImplicitTypeConversion";
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!super.isAvailable(project, editor, file)) return false;
return ((XPathFunctionCall)myElement).getArgumentList().length == 1;
@Override
public boolean isAvailable(@NotNull Project project,
@NotNull PsiFile file,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
return super.isAvailable(project, file, startElement, endElement)
&& ((XPathFunctionCall)startElement).getArgumentList().length == 1;
}
public void invokeImpl(Project project, PsiFile file) throws IncorrectOperationException {
final XPathExpression arg0 = ((XPathFunctionCall)myElement).getArgumentList()[0];
PsiElement myElement = getStartElement();
final XPathExpression arg0 = ((XPathFunctionCall)myElement).getArgumentList()[0];
final XPathExpression outer = PsiTreeUtil.getParentOfType(myElement, XPathExpression.class);
if (arg0 instanceof XPathBinaryExpression && outer instanceof XPathBinaryExpression) {
// TODO make this smarter by determining operator precedence
@@ -15,6 +15,7 @@
*/
package org.intellij.lang.xpath.validation.inspections.quickfix;
import com.intellij.psi.PsiElement;
import org.intellij.lang.xpath.psi.XPathElement;
import org.intellij.lang.xpath.psi.impl.XPathChangeUtil;
@@ -27,6 +28,7 @@ public abstract class ReplaceElementFix<TYPE extends XPathElement> extends XPath
}
protected void replace(String expressionText) throws IncorrectOperationException {
myElement.replace(XPathChangeUtil.createExpression(myElement, expressionText));
PsiElement myElement = getStartElement();
myElement.replace(XPathChangeUtil.createExpression(myElement, expressionText));
}
}
@@ -23,22 +23,18 @@
package org.intellij.lang.xpath.validation.inspections.quickfix;
import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.codeInspection.SuppressIntentionAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.intellij.lang.xpath.psi.XPathExpression;
import org.intellij.lang.xpath.psi.XPathNodeTest;
import org.intellij.lang.xpath.psi.XPathType;
import org.intellij.lang.xpath.validation.inspections.XPathInspection;
import org.jetbrains.annotations.NotNull;
public interface XPathQuickFixFactory {
Fix<XPathExpression>[] createImplicitTypeConversionFixes(XPathExpression expression, XPathType type, boolean explicit);
@@ -51,23 +47,21 @@ public interface XPathQuickFixFactory {
boolean isSuppressedFor(PsiElement element, XPathInspection inspection);
abstract class Fix<E extends PsiElement> implements LocalQuickFix, IntentionAction {
protected final E myElement;
abstract class Fix<E extends PsiElement> extends LocalQuickFixAndIntentionActionOnPsiElement {
protected Fix(E element) {
myElement = element;
super(element);
}
public boolean startInWriteAction() {
return true;
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return isAvailableImpl();
}
protected boolean isAvailableImpl() {
return myElement.isValid() && myElement.getParent().isValid();
@Override
public boolean isAvailable(@NotNull Project project,
@NotNull PsiFile file,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
return startElement.isValid() && startElement.getParent().isValid();
}
@NotNull
@@ -75,23 +69,22 @@ public interface XPathQuickFixFactory {
return getName();
}
public final void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
assert myElement == descriptor.getPsiElement();
if (!isAvailableImpl()) return;
@Override
public void invoke(@NotNull Project project,
@NotNull PsiFile file,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
if(!CodeInsightUtilBase.prepareFileForWrite(file)) {
return;
}
try {
invokeImpl(project, descriptor.getPsiElement().getContainingFile());
invokeImpl(project, file);
} catch (IncorrectOperationException e) {
Logger.getInstance(getClass().getName()).error(e);
}
}
public final void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
if(!CodeInsightUtilBase.prepareFileForWrite(file)) {
return;
}
invokeImpl(project, file);
}
protected abstract void invokeImpl(Project project, PsiFile file) throws IncorrectOperationException;
}