DomUtil.hasXml: use DomElement.exists() to avoid costly getXmlElement()

fixes + tests CR-IC-4788
This commit is contained in:
Yann Cébron
2014-03-25 08:56:51 +01:00
parent 68ba599e31
commit df25676f78
5 changed files with 61 additions and 4 deletions
@@ -65,6 +65,7 @@ public class CollectionElementInvocationHandler extends DomInvocationHandler<Abs
if (tag == null) return;
getManager().cacheHandler(getCacheKey(), tag, null);
setXmlElement(null);
deleteTag(tag);
getManager().fireEvent(new DomEvent(parent, false));
}
@@ -80,7 +80,7 @@ public abstract class DomInvocationHandler<T extends AbstractDomChildDescription
private final InvocationCache myInvocationCache;
private volatile Converter myScalarConverter = null;
private volatile SmartFMap<Method, Invocation> myAccessorInvocations = SmartFMap.emptyMap();
@Nullable protected final Stub myStub;
@Nullable protected Stub myStub;
protected DomInvocationHandler(Type type, DomParentStrategy parentStrategy,
@NotNull final EvaluatedXmlName tagName,
@@ -734,6 +734,7 @@ public abstract class DomInvocationHandler<T extends AbstractDomChildDescription
protected final void setXmlElement(final XmlElement element) {
refreshGenericInfo(element != null && !isAttribute());
myStub = null;
myParentStrategy = element == null ? myParentStrategy.clearXmlElement() : myParentStrategy.setXmlElement(element);
}
@@ -70,6 +70,12 @@ public class DomRootInvocationHandler extends DomInvocationHandler<AbstractDomCh
return myParent.hashCode();
}
@Override
public boolean exists() {
return getStub() != null ||
getXmlElement() != null;
}
@NotNull
public String getXmlElementNamespace() {
return getXmlName().getNamespace(getFile(), getFile());
@@ -43,6 +43,18 @@ public class DomBasicsTest extends DomTestCase {
assertEquals(fileElement.getRootElement(), fileElement.getRootElement());
}
public void testRootElementUndefineNotExisting() {
final XmlFile file = createXmlFile("<a/>");
final DomManagerImpl manager = getDomManager();
final DomFileElementImpl<DomElement> fileElement = manager.getFileElement(file, DomElement.class, "a");
final DomElement rootElement = fileElement.getRootElement();
assertNotNull(rootElement);
assertTrue(rootElement.exists());
rootElement.undefine();
assertFalse(rootElement.exists());
}
public void testElementCaching() throws Throwable {
final MyElement element = createElement("<a><child/></a>");
assertSame(element.getChild(), element.getChild());
@@ -167,7 +179,8 @@ public class DomBasicsTest extends DomTestCase {
assertEquals(new HashSet(Arrays.asList(foo, child, collectionChild, genericChild,
info.getAttributeChildrenDescriptions().get(0))),
new HashSet(info.getChildrenDescriptions()));
new HashSet(info.getChildrenDescriptions())
);
}
private void assertFixedChildDescription(final DomFixedChildDescription description,
@@ -225,7 +238,8 @@ public class DomBasicsTest extends DomTestCase {
}
private void assertCollectionPresentableName(final String expected, final String tagName, final DomNameStrategy strategy) {
assertEquals(expected, new CollectionChildDescriptionImpl(new XmlName(tagName), DomElement.class, null).getCommonPresentableName(strategy));
assertEquals(expected,
new CollectionChildDescriptionImpl(new XmlName(tagName), DomElement.class, null).getCommonPresentableName(strategy));
}
private void assertFixedPresentableName(final String expected, final String tagName, final DomNameStrategy strategy) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
*/
package com.intellij.util.xml.stubs;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.vfs.VirtualFileFilter;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
@@ -117,4 +118,38 @@ public class DomStubUsingTest extends DomStubTest {
((PsiManagerImpl)getPsiManager()).setAssertOnFileLoadingFilter(VirtualFileFilter.ALL, myTestRootDisposable);
DomManager.getDomManager(getProject()).getFileElement(file, Foo.class);
}
public void testStubbedElementUndefineNotExisting() {
final DomFileElement<Foo> fileElement = prepare("foo.xml", Foo.class);
final Bar bar = fileElement.getRootElement().getBars().get(0);
assertNotNull(bar);
assertTrue(bar.exists());
new WriteCommandAction.Simple(null) {
@Override
protected void run() throws Throwable {
bar.undefine();
}
}.execute().throwException();
assertFalse(bar.exists());
}
public void testRootElementUndefineNotExisting() {
final DomFileElement<Foo> fileElement = prepare("foo.xml", Foo.class);
final DomElement rootElement = fileElement.getRootElement();
assertNotNull(rootElement);
assertTrue(rootElement.exists());
new WriteCommandAction.Simple(null) {
@Override
protected void run() throws Throwable {
rootElement.undefine();
}
}.execute().throwException();
assertFalse(rootElement.exists());
}
}