Search in sources :

Example 1 with DomGenericInfo

use of com.intellij.util.xml.reflect.DomGenericInfo in project intellij-community by JetBrains.

the class DomBasicsTest method testChildrenReflection.

public void testChildrenReflection() throws Throwable {
    final MyElement element = createElement("<a><child/><child-element/><child-element/></a>");
    final DomGenericInfo info = element.getGenericInfo();
    final DomFixedChildDescription foo = info.getFixedChildDescription("foo");
    assertFixedChildDescription(foo, element.getFoo(), "foo");
    final DomFixedChildDescription child = info.getFixedChildDescription("child");
    assertFixedChildDescription(child, element.getChild(), "child");
    final DomFixedChildDescription genericChild = info.getFixedChildDescription("generic-value");
    assertGenericChildDescription(genericChild, element.getGenericValue(), "generic-value");
    final DomCollectionChildDescription collectionChild = info.getCollectionChildDescription("child-element");
    assertEquals(element.getChildElements(), collectionChild.getValues(element));
    assertEquals("child-element", collectionChild.getXmlElementName());
    assertEquals(MyElement.class, collectionChild.getType());
    assertEquals(MyElement.class.getMethod("getChildElements"), collectionChild.getGetterMethod().getMethod());
    assertEquals(new HashSet(Arrays.asList(foo, child, collectionChild, genericChild, info.getAttributeChildrenDescriptions().get(0))), new HashSet(info.getChildrenDescriptions()));
}
Also used : DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) DomGenericInfo(com.intellij.util.xml.reflect.DomGenericInfo) DomFixedChildDescription(com.intellij.util.xml.reflect.DomFixedChildDescription)

Example 2 with DomGenericInfo

use of com.intellij.util.xml.reflect.DomGenericInfo in project intellij-community by JetBrains.

the class DomNamespacesTest method testFindChildDescriptionWithoutNamespace.

public void testFindChildDescriptionWithoutNamespace() throws Throwable {
    final DomGenericInfo info = getDomManager().getGenericInfo(MyListOrSet.class);
    assertNotNull(info.getAttributeChildDescription("attr"));
    assertNotNull(info.getAttributeChildDescription("attr").getType());
    assertNotNull(info.getCollectionChildDescription("child"));
    assertNotNull(info.getCollectionChildDescription("child").getType());
    assertNotNull(info.getFixedChildDescription("ref"));
    assertNotNull(info.getFixedChildDescription("ref").getType());
}
Also used : DomGenericInfo(com.intellij.util.xml.reflect.DomGenericInfo)

Example 3 with DomGenericInfo

use of com.intellij.util.xml.reflect.DomGenericInfo in project intellij-community by JetBrains.

the class ResolvingElementQuickFix method getChildDescription.

@Nullable
public static <T extends DomElement> DomCollectionChildDescription getChildDescription(final List<DomElement> contexts, Class<T> clazz) {
    if (contexts.size() == 0) {
        return null;
    }
    final DomElement context = contexts.get(0);
    final DomGenericInfo genericInfo = context.getGenericInfo();
    final List<? extends DomCollectionChildDescription> descriptions = genericInfo.getCollectionChildrenDescriptions();
    for (DomCollectionChildDescription description : descriptions) {
        final Type type = description.getType();
        if (type.equals(clazz)) {
            return description;
        }
    }
    return null;
}
Also used : Type(java.lang.reflect.Type) DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) DomGenericInfo(com.intellij.util.xml.reflect.DomGenericInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with DomGenericInfo

use of com.intellij.util.xml.reflect.DomGenericInfo in project intellij-community by JetBrains.

the class DomBasicsTest method testAddChildrenByReflection.

public void testAddChildrenByReflection() throws Throwable {
    final MyElement element = createElement("<a><child-element/></a>");
    final DomGenericInfo info = element.getGenericInfo();
    final DomCollectionChildDescription collectionChild = info.getCollectionChildDescription("child-element");
    final List<? extends DomElement> values = collectionChild.getValues(element);
    MyElement newChild = (MyElement) collectionChild.addValue(element);
    List<DomElement> newChildren = Arrays.asList(values.get(0), newChild);
    assertEquals(newChildren, element.getChildElements());
    assertEquals(newChildren, collectionChild.getValues(element));
    MyElement lastChild = (MyElement) collectionChild.addValue(element, 0);
    newChildren = Arrays.asList(lastChild, values.get(0), newChild);
    assertEquals(newChildren, element.getChildElements());
    assertEquals(newChildren, collectionChild.getValues(element));
}
Also used : DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) DomGenericInfo(com.intellij.util.xml.reflect.DomGenericInfo)

Example 5 with DomGenericInfo

use of com.intellij.util.xml.reflect.DomGenericInfo in project intellij-community by JetBrains.

the class DomHighlightingHelperImpl method checkRequired.

@Override
@NotNull
public List<DomElementProblemDescriptor> checkRequired(final DomElement element, final DomElementAnnotationHolder holder) {
    final Required required = element.getAnnotation(Required.class);
    if (required != null) {
        final XmlElement xmlElement = element.getXmlElement();
        if (xmlElement == null) {
            if (required.value()) {
                final String xmlElementName = element.getXmlElementName();
                String namespace = element.getXmlElementNamespace();
                if (element instanceof GenericAttributeValue) {
                    return Collections.singletonList(holder.createProblem(element, IdeBundle.message("attribute.0.should.be.defined", xmlElementName), new DefineAttributeQuickFix(xmlElementName, namespace)));
                }
                return Collections.singletonList(holder.createProblem(element, HighlightSeverity.ERROR, IdeBundle.message("child.tag.0.should.be.defined", xmlElementName), new AddRequiredSubtagFix(xmlElementName, namespace)));
            }
        } else if (element instanceof GenericDomValue) {
            return ContainerUtil.createMaybeSingletonList(checkRequiredGenericValue((GenericDomValue) element, required, holder));
        }
    }
    if (DomUtil.hasXml(element)) {
        final SmartList<DomElementProblemDescriptor> list = new SmartList<>();
        final DomGenericInfo info = element.getGenericInfo();
        for (final AbstractDomChildrenDescription description : info.getChildrenDescriptions()) {
            if (description instanceof DomCollectionChildDescription && description.getValues(element).isEmpty()) {
                final DomCollectionChildDescription childDescription = (DomCollectionChildDescription) description;
                final Required annotation = description.getAnnotation(Required.class);
                if (annotation != null && annotation.value()) {
                    list.add(holder.createProblem(element, childDescription, IdeBundle.message("child.tag.0.should.be.defined", ((DomCollectionChildDescription) description).getXmlElementName())));
                }
            }
        }
        return list;
    }
    return Collections.emptyList();
}
Also used : DomGenericInfo(com.intellij.util.xml.reflect.DomGenericInfo) DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) XmlElement(com.intellij.psi.xml.XmlElement) AbstractDomChildrenDescription(com.intellij.util.xml.reflect.AbstractDomChildrenDescription) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DomGenericInfo (com.intellij.util.xml.reflect.DomGenericInfo)5 DomCollectionChildDescription (com.intellij.util.xml.reflect.DomCollectionChildDescription)4 XmlElement (com.intellij.psi.xml.XmlElement)1 SmartList (com.intellij.util.SmartList)1 AbstractDomChildrenDescription (com.intellij.util.xml.reflect.AbstractDomChildrenDescription)1 DomFixedChildDescription (com.intellij.util.xml.reflect.DomFixedChildDescription)1 Type (java.lang.reflect.Type)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1