From ed125bc2ac285c731a2cf2c519c07941c8b86c2f Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Mon, 8 Oct 2018 15:21:37 +0200 Subject: [PATCH] cleanup, avoid sugar (.element) --- .../compiler/CompilerConfigurationImpl.java | 4 +- .../jdk/java/security/annotations.xml | 5 + lib/annotations/jdk/java/util/annotations.xml | 5 + lib/annotations/jdom/org/jdom/annotations.xml | 3 + .../xml/XmlSerializerCollectionTest.kt | 4 +- .../intellij/execution/impl/RunManagerImpl.kt | 3 +- .../src/com/intellij/util/jdom.kt | 20 ---- .../configurationStore/JbXmlOutputter.java | 96 ++++++++----------- .../openapi/util/SafeStAXStreamBuilder.java | 11 +-- .../intellij/copyright/CopyrightManager.kt | 15 +-- 10 files changed, 71 insertions(+), 95 deletions(-) create mode 100644 lib/annotations/jdk/java/security/annotations.xml create mode 100644 lib/annotations/jdk/java/util/annotations.xml diff --git a/java/compiler/impl/src/com/intellij/compiler/CompilerConfigurationImpl.java b/java/compiler/impl/src/com/intellij/compiler/CompilerConfigurationImpl.java index 309d6d462595..8bbd7f47f83a 100644 --- a/java/compiler/impl/src/com/intellij/compiler/CompilerConfigurationImpl.java +++ b/java/compiler/impl/src/com/intellij/compiler/CompilerConfigurationImpl.java @@ -62,7 +62,6 @@ import java.util.*; import java.util.stream.Collectors; import static com.intellij.compiler.ExternalCompilerConfigurationStorageKt.*; -import static com.intellij.util.JdomKt.element; import static org.jetbrains.jps.model.java.impl.compiler.ResourcePatterns.normalizeWildcards; import static org.jetbrains.jps.model.serialization.java.compiler.JpsJavaCompilerConfigurationSerializer.DEFAULT_WILDCARD_PATTERNS; @@ -213,7 +212,8 @@ public class CompilerConfigurationImpl extends CompilerConfiguration implements List moduleNames = getFilteredModuleNameList(myProject, myModuleBytecodeTarget, false); if (!StringUtil.isEmpty(myBytecodeTargetLevel) || !moduleNames.isEmpty()) { - final Element bytecodeTarget = element(state, JpsJavaCompilerConfigurationSerializer.BYTECODE_TARGET_LEVEL); + final Element bytecodeTarget = new Element(JpsJavaCompilerConfigurationSerializer.BYTECODE_TARGET_LEVEL); + state.addContent(bytecodeTarget); if (!StringUtil.isEmpty(myBytecodeTargetLevel)) { bytecodeTarget.setAttribute(JpsJavaCompilerConfigurationSerializer.TARGET_ATTRIBUTE, myBytecodeTargetLevel); } diff --git a/lib/annotations/jdk/java/security/annotations.xml b/lib/annotations/jdk/java/security/annotations.xml new file mode 100644 index 000000000000..92cd9c5e6898 --- /dev/null +++ b/lib/annotations/jdk/java/security/annotations.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/lib/annotations/jdk/java/util/annotations.xml b/lib/annotations/jdk/java/util/annotations.xml new file mode 100644 index 000000000000..f6bdece327d4 --- /dev/null +++ b/lib/annotations/jdk/java/util/annotations.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/lib/annotations/jdom/org/jdom/annotations.xml b/lib/annotations/jdom/org/jdom/annotations.xml index 585f9ceed341..d1bbc1461817 100644 --- a/lib/annotations/jdom/org/jdom/annotations.xml +++ b/lib/annotations/jdom/org/jdom/annotations.xml @@ -72,5 +72,8 @@ + + + diff --git a/platform/configuration-store-impl/testSrc/xml/XmlSerializerCollectionTest.kt b/platform/configuration-store-impl/testSrc/xml/XmlSerializerCollectionTest.kt index de0c90fc368a..f6b196f50659 100644 --- a/platform/configuration-store-impl/testSrc/xml/XmlSerializerCollectionTest.kt +++ b/platform/configuration-store-impl/testSrc/xml/XmlSerializerCollectionTest.kt @@ -9,7 +9,6 @@ import com.intellij.openapi.util.JDOMExternalizableStringList import com.intellij.openapi.util.JDOMUtil import com.intellij.testFramework.assertions.Assertions.assertThat import com.intellij.util.SmartList -import com.intellij.util.element import com.intellij.util.loadElement import com.intellij.util.xmlb.SkipDefaultsSerializationFilter import com.intellij.util.xmlb.XmlSerializer @@ -49,7 +48,8 @@ internal class XmlSerializerCollectionTest { fun jdomExternalizableStringListWithoutClassAttribute() { val testList = arrayOf("foo", "bar") val element = Element("test") - val listElement = element.element("list") + val listElement = Element("list") + element.addContent(listElement) for (id in testList) { listElement.addContent(Element("item").setAttribute("itemvalue", id)) } diff --git a/platform/lang-impl/src/com/intellij/execution/impl/RunManagerImpl.kt b/platform/lang-impl/src/com/intellij/execution/impl/RunManagerImpl.kt index 3137467c5eef..ee6c0d25e0af 100644 --- a/platform/lang-impl/src/com/intellij/execution/impl/RunManagerImpl.kt +++ b/platform/lang-impl/src/com/intellij/execution/impl/RunManagerImpl.kt @@ -492,7 +492,8 @@ open class RunManagerImpl @JvmOverloads constructor(val project: Project, shared val recent = Element(RECENT) element.addContent(recent) - val listElement = recent.element("list") + val listElement = Element("list") + recent.addContent(listElement) for (id in recentList) { listElement.addContent(Element("item").setAttribute("itemvalue", id)) } diff --git a/platform/projectModel-api/src/com/intellij/util/jdom.kt b/platform/projectModel-api/src/com/intellij/util/jdom.kt index 77cc63fdc2d5..3eebff50788c 100644 --- a/platform/projectModel-api/src/com/intellij/util/jdom.kt +++ b/platform/projectModel-api/src/com/intellij/util/jdom.kt @@ -12,7 +12,6 @@ import org.jdom.Document import org.jdom.Element import org.jdom.JDOMException import org.jdom.Parent -import org.jdom.filter.ElementFilter import org.jdom.input.SAXBuilder import org.jdom.input.sax.SAXHandler import org.xml.sax.EntityResolver @@ -63,27 +62,8 @@ fun Element.getOrCreate(name: String): Element { return element } -fun Element.get(name: String): Element? = getChild(name) - -fun Element.element(name: String): Element { - val element = Element(name) - addContent(element) - return element -} - fun Element.attribute(name: String, value: String?): Element = setAttribute(name, value) -fun Element.remove(name: String, transform: (child: Element) -> T): List { - val result = SmartList() - val groupIterator = getContent(ElementFilter(name)).iterator() - while (groupIterator.hasNext()) { - val child = groupIterator.next() - result.add(transform(child)) - groupIterator.remove() - } - return result -} - fun Element.toBufferExposingByteArray(lineSeparator: LineSeparator = LineSeparator.LF): BufferExposingByteArrayOutputStream { val out = BufferExposingByteArrayOutputStream(1024) JDOMUtil.write(this, out, lineSeparator.separatorString) diff --git a/platform/projectModel-impl/src/com/intellij/configurationStore/JbXmlOutputter.java b/platform/projectModel-impl/src/com/intellij/configurationStore/JbXmlOutputter.java index a191ef54efad..b0e2925e0871 100644 --- a/platform/projectModel-impl/src/com/intellij/configurationStore/JbXmlOutputter.java +++ b/platform/projectModel-impl/src/com/intellij/configurationStore/JbXmlOutputter.java @@ -70,7 +70,9 @@ import java.io.StringWriter; import java.io.Writer; import java.util.List; -public final class JbXmlOutputter { +// expandEmptyElements is ignored +@SuppressWarnings("Duplicates") +public class JbXmlOutputter { private static final Format DEFAULT_FORMAT = JDOMUtil.createFormat("\n"); // For normal output @@ -174,22 +176,6 @@ public final class JbXmlOutputter { printElement(out, element, 0); } - /** - * This will handle printing out a list of nodes. - * This can be useful for printing the content of an element that - * contains HTML, like "<description>JDOM is - * <b>fun>!</description>". - * - * @param list List of nodes. - * @param out Writer to use. - */ - public void output(List list, Writer out) throws IOException { - printContentRange(out, list, 0, list.size(), 0); - out.flush(); - } - - // * * * * * * * * * * Internal printing methods * * * * * * * * * * - /** * This will handle printing of the declaration. * Assumes XML version 1.0 since we don't directly know. @@ -340,7 +326,7 @@ public final class JbXmlOutputter { * This will handle printing a string. Escapes the element entities, * trims interior whitespace, etc. if necessary. */ - private void printString(@NotNull Writer out, String str) throws IOException { + private void printString(@NotNull Element parentElement, @NotNull Writer out, String str) throws IOException { if (format.getTextMode() == Format.TextMode.NORMALIZE) { str = Text.normalizeString(str); } @@ -351,7 +337,6 @@ public final class JbXmlOutputter { if (macroMap != null) { str = macroMap.substitute(str, SystemInfoRt.isFileSystemCaseSensitive); } - out.write(escapeElementEntities(str)); } @@ -378,42 +363,42 @@ public final class JbXmlOutputter { printAttributes(out, element.getAttributes()); } - // Depending on the settings (newlines, textNormalize, etc), we may - // or may not want to print all of the content, so determine the - // index of the start of the content we're interested in based on the current settings. + // depending on the settings (newlines, textNormalize, etc), we may or may not want to print all of the content, + // so determine the index of the start of the content we're interested in based on the current settings. + if (!writeContent(out, element, level)) { + return; + } + + out.write("'); + } + + protected boolean writeContent(@NotNull Writer out, @NotNull Element element, int level) throws IOException { List content = element.getContent(); int start = skipLeadingWhite(content, 0); int size = content.size(); if (start >= size) { - // Case content is empty or all insignificant whitespace - if (format.getExpandEmptyElements()) { - out.write(">'); - } - else { - out.write(" />"); - } + // content is empty or all insignificant whitespace + out.write(" />"); + return false; + } + + out.write('>'); + + // for a special case where the content is only CDATA or Text we don't want to indent after the start or before the end tag + if (nextNonText(content, start) < size) { + // case Mixed Content - normal indentation + newline(out); + printContentRange(element, out, content, start, size, level + 1); + newline(out); + indent(out, level); } else { - out.write('>'); - - // for a special case where the content is only CDATA or Text we don't want to indent after the start or before the end tag - if (nextNonText(content, start) < size) { - // case Mixed Content - normal indentation - newline(out); - printContentRange(out, content, start, size, level + 1); - newline(out); - indent(out, level); - } - else { - // case all CDATA or Text - no indentation - printTextRange(out, content, start, size); - } - out.write("'); + // case all CDATA or Text - no indentation + printTextRange(element, out, content, start, size); } + return true; } /** @@ -428,7 +413,7 @@ public final class JbXmlOutputter { * @param out Writer to use. * @param level int level of indentation. */ - private void printContentRange(Writer out, List content, int start, int end, int level) throws IOException { + private void printContentRange(@NotNull Element parentElement, @NotNull Writer out, @NotNull List content, int start, int end, int level) throws IOException { boolean firstNode; // Flag for 1st node in content Content next; // Node we're about to print int first, index; // Indexes into the list of content @@ -450,7 +435,7 @@ public final class JbXmlOutputter { newline(out); } indent(out, level); - printTextRange(out, content, first, index); + printTextRange(parentElement, out, content, first, index); } continue; } @@ -489,7 +474,7 @@ public final class JbXmlOutputter { * @param end index of last content node (exclusive). * @param out Writer to use. */ - private void printTextRange(@NotNull Writer out, List content, int start, int end) throws IOException { + private void printTextRange(@NotNull Element parentElement, @NotNull Writer out, @NotNull List content, int start, int end) throws IOException { // remove leading whitespace-only nodes start = skipLeadingWhite(content, start); if (start >= content.size()) { @@ -515,7 +500,6 @@ public final class JbXmlOutputter { throw new IllegalStateException("Should see only CDATA, Text, or EntityRef"); } - // this may save a little time if (next == null || next.isEmpty()) { continue; } @@ -535,7 +519,7 @@ public final class JbXmlOutputter { printEntityRef(out, (EntityRef)node); } else { - printString(out, next); + printString(parentElement, out, next); } previous = next; @@ -695,13 +679,11 @@ public final class JbXmlOutputter { return JDOMUtil.escapeText(str, false, true); } - private static String escapeElementEntities(String str) { + protected static String escapeElementEntities(String str) { return JDOMUtil.escapeText(str, false, false); } - // Support method to print a name without using elt.getQualifiedName() - // and thus avoiding a StringBuffer creation and memory churn - private static void printQualifiedName(Writer out, Element e) throws IOException { + private static void printQualifiedName(@NotNull Writer out, @NotNull Element e) throws IOException { if (!e.getNamespace().getPrefix().isEmpty()) { out.write(e.getNamespace().getPrefix()); out.write(':'); diff --git a/platform/util/src/com/intellij/openapi/util/SafeStAXStreamBuilder.java b/platform/util/src/com/intellij/openapi/util/SafeStAXStreamBuilder.java index 5eee12b5d567..551a31921d6c 100644 --- a/platform/util/src/com/intellij/openapi/util/SafeStAXStreamBuilder.java +++ b/platform/util/src/com/intellij/openapi/util/SafeStAXStreamBuilder.java @@ -126,7 +126,7 @@ final class SafeStAXStreamBuilder { return rootElement; } - private static Element processElementFragment(final XMLStreamReader reader, boolean isIgnoreBoundaryWhitespace) throws XMLStreamException, JDOMException { + private static Element processElementFragment(@NotNull XMLStreamReader reader, boolean isIgnoreBoundaryWhitespace) throws XMLStreamException, JDOMException { if (reader.getEventType() != START_ELEMENT) { throw new JDOMException("JDOM requires that the XMLStreamReader is at the START_ELEMENT state when retrieving an Element Fragment."); } @@ -177,9 +177,9 @@ final class SafeStAXStreamBuilder { return fragment; } - private static Element processElement(XMLStreamReader reader) { - String name = reader.getLocalName(); - final Element element = factory.element(name, Namespace.getNamespace(reader.getPrefix(), reader.getNamespaceURI())); + @NotNull + private static Element processElement(@NotNull XMLStreamReader reader) { + final Element element = factory.element(reader.getLocalName(), Namespace.getNamespace(reader.getPrefix(), reader.getNamespaceURI())); // Handle attributes for (int i = 0, len = reader.getAttributeCount(); i < len; i++) { @@ -187,8 +187,7 @@ final class SafeStAXStreamBuilder { reader.getAttributeLocalName(i), reader.getAttributeValue(i), AttributeType.getAttributeType(reader.getAttributeType(i)), - Namespace.getNamespace(reader.getAttributePrefix(i), - reader.getAttributeNamespace(i)))); + Namespace.getNamespace(reader.getAttributePrefix(i), reader.getAttributeNamespace(i)))); } // Handle Namespaces diff --git a/plugins/copyright/src/com/intellij/copyright/CopyrightManager.kt b/plugins/copyright/src/com/intellij/copyright/CopyrightManager.kt index a8225b5e25a4..312bb4a30633 100644 --- a/plugins/copyright/src/com/intellij/copyright/CopyrightManager.kt +++ b/plugins/copyright/src/com/intellij/copyright/CopyrightManager.kt @@ -28,7 +28,6 @@ import com.intellij.project.isDirectoryBased import com.intellij.psi.PsiFile import com.intellij.psi.PsiManager import com.intellij.util.attribute -import com.intellij.util.element import com.maddyhome.idea.copyright.CopyrightProfile import com.maddyhome.idea.copyright.actions.UpdateCopyrightProcessor import com.maddyhome.idea.copyright.options.LanguageOptions @@ -52,7 +51,7 @@ private val LOG = Logger.getInstance(CopyrightManager::class.java) class CopyrightManager @JvmOverloads constructor(private val project: Project, schemeManagerFactory: SchemeManagerFactory, isSupportIprProjects: Boolean = true) : PersistentStateComponent { companion object { @JvmStatic - fun getInstance(project: Project): CopyrightManager = project.service() + fun getInstance(project: Project) = project.service() } private var defaultCopyrightName: String? = null @@ -63,8 +62,8 @@ class CopyrightManager @JvmOverloads constructor(private val project: Project, s defaultCopyrightName = value?.name } - val scopeToCopyright: LinkedHashMap = LinkedHashMap() - val options: Options = Options() + val scopeToCopyright = LinkedHashMap() + val options = Options() private val schemeWriter = { scheme: CopyrightProfile -> val element = scheme.writeScheme() @@ -126,9 +125,11 @@ class CopyrightManager @JvmOverloads constructor(private val project: Project, s if (!scopeToCopyright.isEmpty()) { val map = Element(MODULE_TO_COPYRIGHT) for ((scopeName, profileName) in scopeToCopyright) { - map.element(ELEMENT) - .attribute(MODULE, scopeName) - .attribute(COPYRIGHT, profileName) + val e = Element(ELEMENT) + e + .attribute(MODULE, scopeName) + .attribute(COPYRIGHT, profileName) + map.addContent(e) } result.addContent(map) }