From d0eb1689f40bb6541e2e7881ce5a92e720d1f2a5 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Wed, 18 Dec 2013 13:54:14 +0400 Subject: [PATCH] made fields final to prevent some nasty bugs like EA-52519 - IAE: TObjectHash.throwObjectContractViolation --- .../lang/annotation/HighlightSeverity.java | 24 ++-- .../daemon/impl/HighlightInfoType.java | 19 +-- .../daemon/impl/SeverityRegistrar.java | 34 ++--- .../editor/colors/TextAttributesKey.java | 35 ++--- .../editor/markup/AttributesFlyweight.java | 99 +++++++++++++- .../openapi/editor/markup/TextAttributes.java | 126 ++---------------- .../colors/impl/AbstractColorsScheme.java | 3 +- .../intellij/psi/search/TodoAttributes.java | 8 +- .../severities/HighlightSeveritiesTest.java | 36 +++-- .../ExpectedHighlightingData.java | 4 +- .../openapi/util/DefaultJDOMExternalizer.java | 54 +++++--- .../openapi/util/JDOMExternalizerUtil.java | 41 +++--- 12 files changed, 233 insertions(+), 250 deletions(-) diff --git a/platform/analysis-api/src/com/intellij/lang/annotation/HighlightSeverity.java b/platform/analysis-api/src/com/intellij/lang/annotation/HighlightSeverity.java index 7e6ea0f3b5b4..ef19e0f17d43 100644 --- a/platform/analysis-api/src/com/intellij/lang/annotation/HighlightSeverity.java +++ b/platform/analysis-api/src/com/intellij/lang/annotation/HighlightSeverity.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2013 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,10 +15,7 @@ */ package com.intellij.lang.annotation; -import com.intellij.openapi.util.DefaultJDOMExternalizer; -import com.intellij.openapi.util.InvalidDataException; -import com.intellij.openapi.util.JDOMExternalizable; -import com.intellij.openapi.util.WriteExternalException; +import com.intellij.openapi.util.*; import org.jdom.Element; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -30,9 +27,9 @@ import org.jetbrains.annotations.NotNull; * @see com.intellij.lang.annotation.Annotation */ -public class HighlightSeverity implements Comparable, JDOMExternalizable { - public String myName; - public int myVal; +public class HighlightSeverity implements Comparable { + public final String myName; + public final int myVal; /** * The standard severity level for information annotations. @@ -86,7 +83,9 @@ public class HighlightSeverity implements Comparable, JDOMExt //read external only - public HighlightSeverity() { + public HighlightSeverity(@NotNull Element element) { + myName = JDOMExternalizerUtil.readField(element, "myName"); + myVal = Integer.valueOf(JDOMExternalizerUtil.readField(element, "myVal")); } public String toString() { @@ -98,17 +97,10 @@ public class HighlightSeverity implements Comparable, JDOMExt return myVal - highlightSeverity.myVal; } - @Override - public void readExternal(Element element) throws InvalidDataException { - DefaultJDOMExternalizer.readExternal(this, element); - } - - @Override public void writeExternal(final Element element) throws WriteExternalException { DefaultJDOMExternalizer.writeExternal(this, element); } - public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; diff --git a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfoType.java b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfoType.java index d5d456163f09..d4b0b2e546c4 100644 --- a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfoType.java +++ b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfoType.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2013 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. @@ -26,7 +26,6 @@ import com.intellij.openapi.editor.colors.CodeInsightColors; import com.intellij.openapi.editor.colors.TextAttributesKey; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.InvalidDataException; -import com.intellij.openapi.util.JDOMExternalizable; import com.intellij.openapi.util.WriteExternalException; import com.intellij.profile.codeInspection.InspectionProfileManager; import com.intellij.profile.codeInspection.InspectionProjectProfileManager; @@ -101,14 +100,14 @@ public interface HighlightInfoType { TextAttributesKey getAttributesKey(); - class HighlightInfoTypeImpl implements HighlightInfoType, JDOMExternalizable { + class HighlightInfoTypeImpl implements HighlightInfoType { private final HighlightSeverity mySeverity; private final TextAttributesKey myAttributesKey; //read external only - public HighlightInfoTypeImpl() { - mySeverity = new HighlightSeverity(); - myAttributesKey = new TextAttributesKey(); + HighlightInfoTypeImpl(@NotNull Element element) throws InvalidDataException { + mySeverity = new HighlightSeverity(element); + myAttributesKey = new TextAttributesKey(element); } public HighlightInfoTypeImpl(@NotNull HighlightSeverity severity, TextAttributesKey attributesKey) { @@ -132,19 +131,11 @@ public interface HighlightInfoType { return "HighlightInfoTypeImpl[severity=" + mySeverity + ", key=" + myAttributesKey + "]"; } - @Override - public void readExternal(Element element) throws InvalidDataException { - mySeverity.readExternal(element); - myAttributesKey.readExternal(element); - } - - @Override public void writeExternal(Element element) throws WriteExternalException { mySeverity.writeExternal(element); myAttributesKey.writeExternal(element); } - public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; diff --git a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/SeverityRegistrar.java b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/SeverityRegistrar.java index f93207240faf..e5c719b70860 100644 --- a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/SeverityRegistrar.java +++ b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/SeverityRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2013 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. @@ -46,10 +46,10 @@ import java.util.List; * Date: 24-Feb-2006 */ public class SeverityRegistrar implements JDOMExternalizable, Comparator { - @NonNls private static final String INFO = "info"; + @NonNls private static final String INFO_TAG = "info"; + @NonNls private static final String COLOR_ATTRIBUTE = "color"; private final Map myMap = new THashMap(); private final Map myRendererColors = new THashMap(); - @NonNls private static final String COLOR = "color"; private final OrderMap myOrder = new OrderMap(); private JDOMExternalizableStringList myReadOrder; @@ -120,15 +120,14 @@ public class SeverityRegistrar implements JDOMExternalizable, Comparator list = getOrderAsList(); for (HighlightSeverity s : list) { - Element info = new Element(INFO); + Element info = new Element(INFO_TAG); String severity = s.toString(); final SeverityBasedTextAttributes infoType = myMap.get(severity); if (infoType != null) { infoType.writeExternal(info); final Color color = myRendererColors.get(severity); if (color != null) { - info.setAttribute(COLOR, Integer.toString(color.getRGB() & 0xFFFFFF, 16)); + info.setAttribute(COLOR_ATTRIBUTE, Integer.toString(color.getRGB() & 0xFFFFFF, 16)); } element.addContent(info); } @@ -343,14 +342,14 @@ public class SeverityRegistrar implements JDOMExternalizable, Comparator, JDOMExternalizable { +public final class TextAttributesKey implements Comparable { private static final TextAttributes NULL_ATTRIBUTES = new TextAttributes(); private static final ConcurrentHashMap ourRegistry = new ConcurrentHashMap(); private static final NullableLazyValue ourDefaultsProvider = new VolatileNullableLazyValue() { @@ -39,9 +39,8 @@ public final class TextAttributesKey implements Comparable, J } }; - public String myExternalName; - public TextAttributes myDefaultAttributes = NULL_ATTRIBUTES; - + private final String myExternalName; + private TextAttributes myDefaultAttributes = NULL_ATTRIBUTES; private TextAttributesKey myFallbackAttributeKey; private TextAttributesKey(String externalName) { @@ -49,10 +48,16 @@ public final class TextAttributesKey implements Comparable, J } //read external only - public TextAttributesKey() { + public TextAttributesKey(@NotNull Element element) throws InvalidDataException { + this(JDOMExternalizerUtil.readField(element, "myExternalName")); + Element myDefaultAttributesElement = JDOMExternalizerUtil.getOption(element, "myDefaultAttributes"); + if (myDefaultAttributesElement != null) { + myDefaultAttributes = new TextAttributes(myDefaultAttributesElement); + } } - @NotNull public static TextAttributesKey find(@NotNull @NonNls String externalName) { + @NotNull + public static TextAttributesKey find(@NotNull @NonNls String externalName) { return ourRegistry.cacheOrGet(externalName, new TextAttributesKey(externalName)); } @@ -80,14 +85,13 @@ public final class TextAttributesKey implements Comparable, J return find(externalName); } - @Override - public void readExternal(Element element) throws InvalidDataException { - DefaultJDOMExternalizer.readExternal(this, element); - } - - @Override public void writeExternal(Element element) throws WriteExternalException { - DefaultJDOMExternalizer.writeExternal(this, element); + JDOMExternalizerUtil.writeField(element, "myExternalName", myExternalName); + + if (myDefaultAttributes != NULL_ATTRIBUTES) { + Element option = JDOMExternalizerUtil.writeOption(element, "myDefaultAttributes"); + myDefaultAttributes.writeExternal(option); + } } @@ -115,8 +119,9 @@ public final class TextAttributesKey implements Comparable, J if (myDefaultAttributes == NULL_ATTRIBUTES) { myDefaultAttributes = null; final TextAttributeKeyDefaultsProvider provider = ourDefaultsProvider.getValue(); - if (provider != null) + if (provider != null) { myDefaultAttributes = provider.getDefaultAttributes(this); + } } else if (myDefaultAttributes == null) { myDefaultAttributes = NULL_ATTRIBUTES; diff --git a/platform/core-api/src/com/intellij/openapi/editor/markup/AttributesFlyweight.java b/platform/core-api/src/com/intellij/openapi/editor/markup/AttributesFlyweight.java index 411fc5ef0990..63be180854d2 100644 --- a/platform/core-api/src/com/intellij/openapi/editor/markup/AttributesFlyweight.java +++ b/platform/core-api/src/com/intellij/openapi/editor/markup/AttributesFlyweight.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2013 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. @@ -20,26 +20,31 @@ package com.intellij.openapi.editor.markup; import com.intellij.openapi.util.Comparing; +import com.intellij.openapi.util.DefaultJDOMExternalizer; +import com.intellij.openapi.util.InvalidDataException; +import com.intellij.openapi.util.JDOMExternalizerUtil; import com.intellij.util.ConcurrencyUtil; import com.intellij.util.containers.StripedLockConcurrentHashMap; import org.intellij.lang.annotations.JdkConstants; +import org.jdom.Element; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import java.awt.*; +import java.util.concurrent.ConcurrentMap; public class AttributesFlyweight { - private static final StripedLockConcurrentHashMap entries = new StripedLockConcurrentHashMap(); + private static final ConcurrentMap entries = new StripedLockConcurrentHashMap(); private static final ThreadLocal ourKey = new ThreadLocal(); private final int myHashCode; - private final Color myForeground; - private final Color myBackground; + private final Color myForeground; + private final Color myBackground; @JdkConstants.FontStyle - private final int myFontType; - private final Color myEffectColor; + private final int myFontType; + private final Color myEffectColor; private final EffectType myEffectType; - private final Color myErrorStripeColor; + private final Color myErrorStripeColor; private static class FlyweightKey implements Cloneable { private Color foreground; @@ -128,6 +133,86 @@ public class AttributesFlyweight { myHashCode = calcHashCode(foreground, background, fontType, effectColor, effectType, errorStripeColor); } + @NotNull + public static AttributesFlyweight create(@NotNull Element element) throws InvalidDataException { + Color FOREGROUND = DefaultJDOMExternalizer.toColor(JDOMExternalizerUtil.readField(element, "FOREGROUND")); + Color BACKGROUND = DefaultJDOMExternalizer.toColor(JDOMExternalizerUtil.readField(element, "BACKGROUND")); + Color EFFECT_COLOR = DefaultJDOMExternalizer.toColor(JDOMExternalizerUtil.readField(element, "EFFECT_COLOR")); + Color ERROR_STRIPE_COLOR = DefaultJDOMExternalizer.toColor(JDOMExternalizerUtil.readField(element, "ERROR_STRIPE_COLOR")); + int fontType = DefaultJDOMExternalizer.toInt(JDOMExternalizerUtil.readField(element, "FONT_TYPE", "0")); + if (fontType < 0 || fontType > 3) { + fontType = 0; + } + int FONT_TYPE = fontType; + int EFFECT_TYPE = DefaultJDOMExternalizer.toInt(JDOMExternalizerUtil.readField(element, "EFFECT_TYPE", "0")); + + return new AttributesFlyweight(FOREGROUND, BACKGROUND, FONT_TYPE, EFFECT_COLOR, toEffectType(EFFECT_TYPE), ERROR_STRIPE_COLOR); + } + + private static void writeColor(Element element, String fieldName, Color color) { + if (color != null) { + String string = Integer.toString(color.getRGB() & 0xFFFFFF, 16); + JDOMExternalizerUtil.writeField(element, fieldName, string); + } + } + + void writeExternal(@NotNull Element element) { + writeColor(element, "FOREGROUND", getForeground()); + writeColor(element, "BACKGROUND", getBackground()); + int fontType = getFontType(); + if (fontType != 0) { + JDOMExternalizerUtil.writeField(element, "FONT_TYPE", String.valueOf(fontType)); + } + writeColor(element, "EFFECT_COLOR", getEffectColor()); + writeColor(element, "ERROR_STRIPE_COLOR", getErrorStripeColor()); + JDOMExternalizerUtil.writeField(element, "EFFECT_TYPE", String.valueOf(fromEffectType(getEffectType()))); + } + + private static final int EFFECT_BORDER = 0; + private static final int EFFECT_LINE = 1; + private static final int EFFECT_WAVE = 2; + private static final int EFFECT_STRIKEOUT = 3; + private static final int EFFECT_BOLD_LINE = 4; + private static final int EFFECT_BOLD_DOTTED_LINE = 5; + + private static int fromEffectType(EffectType effectType) { + int EFFECT_TYPE; + if (effectType == EffectType.BOXED) { + EFFECT_TYPE = EFFECT_BORDER; + } + else if (effectType == EffectType.LINE_UNDERSCORE) { + EFFECT_TYPE = EFFECT_LINE; + } + else if (effectType == EffectType.BOLD_LINE_UNDERSCORE) { + EFFECT_TYPE = EFFECT_BOLD_LINE; + } + else if (effectType == EffectType.STRIKEOUT) { + EFFECT_TYPE = EFFECT_STRIKEOUT; + } + else if (effectType == EffectType.WAVE_UNDERSCORE) { + EFFECT_TYPE = EFFECT_WAVE; + } + else if (effectType == EffectType.BOLD_DOTTED_LINE) { + EFFECT_TYPE = EFFECT_BOLD_DOTTED_LINE; + } + else { + EFFECT_TYPE = -1; + } + return EFFECT_TYPE; + } + + private static EffectType toEffectType(int effectType) { + switch (effectType) { + case EFFECT_BORDER: return EffectType.BOXED; + case EFFECT_BOLD_LINE: return EffectType.BOLD_LINE_UNDERSCORE; + case EFFECT_LINE: return EffectType.LINE_UNDERSCORE; + case EFFECT_STRIKEOUT: return EffectType.STRIKEOUT; + case EFFECT_WAVE: return EffectType.WAVE_UNDERSCORE; + case EFFECT_BOLD_DOTTED_LINE: return EffectType.BOLD_DOTTED_LINE; + default: return null; + } + } + private static int calcHashCode(Color foreground, Color background, int fontType, diff --git a/platform/core-api/src/com/intellij/openapi/editor/markup/TextAttributes.java b/platform/core-api/src/com/intellij/openapi/editor/markup/TextAttributes.java index 008e7a5b722b..c9d4bd59ec59 100644 --- a/platform/core-api/src/com/intellij/openapi/editor/markup/TextAttributes.java +++ b/platform/core-api/src/com/intellij/openapi/editor/markup/TextAttributes.java @@ -16,7 +16,6 @@ package com.intellij.openapi.editor.markup; import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.util.DefaultJDOMExternalizer; import com.intellij.openapi.util.InvalidDataException; import com.intellij.openapi.util.JDOMExternalizable; import com.intellij.openapi.util.WriteExternalException; @@ -27,7 +26,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.awt.*; -import java.lang.reflect.Field; /** * Defines the visual representation (colors and effects) of text. @@ -37,7 +35,7 @@ public class TextAttributes implements JDOMExternalizable, Cloneable { public static final TextAttributes ERASE_MARKER = new TextAttributes(); - private boolean myEnforcedDefaults = false; + private boolean myEnforcedDefaults; @NotNull private AttributesFlyweight myAttrs; @@ -70,103 +68,19 @@ public class TextAttributes implements JDOMExternalizable, Cloneable { return attrs; } - private static class Externalizable implements Cloneable, JDOMExternalizable { - public Color FOREGROUND = null; - public Color BACKGROUND = null; - - @JdkConstants.FontStyle - public int FONT_TYPE = Font.PLAIN; - - public Color EFFECT_COLOR = null; - public int EFFECT_TYPE = EFFECT_BORDER; - public Color ERROR_STRIPE_COLOR = null; - - private static final int EFFECT_BORDER = 0; - private static final int EFFECT_LINE = 1; - private static final int EFFECT_WAVE = 2; - private static final int EFFECT_STRIKEOUT = 3; - private static final int EFFECT_BOLD_LINE = 4; - private static final int EFFECT_BOLD_DOTTED_LINE = 5; - - @Override - public Object clone() throws CloneNotSupportedException { - return super.clone(); - } - - @Override - public void readExternal(Element element) throws InvalidDataException { - DefaultJDOMExternalizer.readExternal(this, element); - if (FONT_TYPE < 0 || FONT_TYPE > 3) { - LOG.info("Wrong font type: " + FONT_TYPE); - FONT_TYPE = 0; - } - } - - @Override - public void writeExternal(Element element) throws WriteExternalException { - DefaultJDOMExternalizer.writeExternal(this, element, new DefaultJDOMExternalizer.JDOMFilter() { - @Override - public boolean isAccept(@NotNull Field field) { - try { - if (field.getType().equals(Color.class) && field.get(Externalizable.this) == null) return false; - if (field.getType().equals(int.class) && field.getInt(Externalizable.this) == 0) return false; - } - catch (IllegalAccessException e) { - LOG.error("Can not access: " + field.getName()); - } - return true; - } - }); - } - - private EffectType getEffectType() { - switch (EFFECT_TYPE) { - case EFFECT_BORDER: - return EffectType.BOXED; - case EFFECT_BOLD_LINE: - return EffectType.BOLD_LINE_UNDERSCORE; - case EFFECT_LINE: - return EffectType.LINE_UNDERSCORE; - case EFFECT_STRIKEOUT: - return EffectType.STRIKEOUT; - case EFFECT_WAVE: - return EffectType.WAVE_UNDERSCORE; - case EFFECT_BOLD_DOTTED_LINE: - return EffectType.BOLD_DOTTED_LINE; - default: - return null; - } - } - - private void setEffectType(EffectType effectType) { - if (effectType == EffectType.BOXED) { - EFFECT_TYPE = EFFECT_BORDER; - } - else if (effectType == EffectType.LINE_UNDERSCORE) { - EFFECT_TYPE = EFFECT_LINE; - } - else if (effectType == EffectType.BOLD_LINE_UNDERSCORE) { - EFFECT_TYPE = EFFECT_BOLD_LINE; - } - else if (effectType == EffectType.STRIKEOUT) { - EFFECT_TYPE = EFFECT_STRIKEOUT; - } - else if (effectType == EffectType.WAVE_UNDERSCORE) { - EFFECT_TYPE = EFFECT_WAVE; - } - else if (effectType == EffectType.BOLD_DOTTED_LINE) { - EFFECT_TYPE = EFFECT_BOLD_DOTTED_LINE; - } - else { - EFFECT_TYPE = -1; - } - } - } - public TextAttributes() { this(null, null, null, EffectType.BOXED, Font.PLAIN); } + private TextAttributes(@NotNull AttributesFlyweight attributesFlyweight, boolean enforced) { + myAttrs = attributesFlyweight; + myEnforcedDefaults = enforced; + } + + public TextAttributes(@NotNull Element element) throws InvalidDataException { + readExternal(element); + } + public TextAttributes(@Nullable Color foregroundColor, @Nullable Color backgroundColor, @Nullable Color effectColor, EffectType effectType, @JdkConstants.FontStyle int fontType) { setAttributes(foregroundColor, backgroundColor, effectColor, null, effectType, fontType); } @@ -262,10 +176,7 @@ public class TextAttributes implements JDOMExternalizable, Cloneable { @Override public TextAttributes clone() { - TextAttributes cloned = new TextAttributes(); - cloned.myAttrs = myAttrs; - cloned.myEnforcedDefaults = myEnforcedDefaults; - return cloned; + return new TextAttributes(myAttrs, myEnforcedDefaults); } public boolean equals(Object obj) { @@ -282,24 +193,13 @@ public class TextAttributes implements JDOMExternalizable, Cloneable { @Override public void readExternal(Element element) throws InvalidDataException { - Externalizable ext = new Externalizable(); - ext.readExternal(element); - myAttrs = AttributesFlyweight.create(ext.FOREGROUND, ext.BACKGROUND, ext.FONT_TYPE, ext.EFFECT_COLOR, ext.getEffectType(), ext.ERROR_STRIPE_COLOR); + myAttrs = AttributesFlyweight.create(element); if (isEmpty()) myEnforcedDefaults = true; } @Override public void writeExternal(Element element) throws WriteExternalException { - Externalizable ext = new Externalizable(); - - ext.FOREGROUND = myAttrs.getForeground(); - ext.BACKGROUND = myAttrs.getBackground(); - ext.FONT_TYPE = myAttrs.getFontType(); - ext.EFFECT_COLOR = myAttrs.getEffectColor(); - ext.ERROR_STRIPE_COLOR = myAttrs.getErrorStripeColor(); - ext.setEffectType(myAttrs.getEffectType()); - - ext.writeExternal(element); + myAttrs.writeExternal(element); } @Override diff --git a/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/AbstractColorsScheme.java b/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/AbstractColorsScheme.java index 3bd0a48c2eb3..5b0fd1554736 100644 --- a/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/AbstractColorsScheme.java +++ b/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/AbstractColorsScheme.java @@ -331,9 +331,8 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme { Element e = (Element)o; String key = e.getAttributeValue(NAME_ATTR); TextAttributesKey name = TextAttributesKey.find(key); - TextAttributes attr = new TextAttributes(); Element value = e.getChild(VALUE_ELEMENT); - attr.readExternal(value); + TextAttributes attr = new TextAttributes(value); myAttributesMap.put(name, attr); migrateErrorStripeColorFrom45(name, attr); } diff --git a/platform/indexing-api/src/com/intellij/psi/search/TodoAttributes.java b/platform/indexing-api/src/com/intellij/psi/search/TodoAttributes.java index d92e9565b92f..c5651b6025cb 100644 --- a/platform/indexing-api/src/com/intellij/psi/search/TodoAttributes.java +++ b/platform/indexing-api/src/com/intellij/psi/search/TodoAttributes.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2013 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. @@ -31,7 +31,7 @@ import javax.swing.*; public class TodoAttributes implements Cloneable { private Icon myIcon; - private TextAttributes myTextAttributes = new TextAttributes(); + private TextAttributes myTextAttributes; private boolean myShouldUseCustomColors; @NonNls private static final String ATTRIBUTE_ICON = "icon"; @@ -56,10 +56,12 @@ public class TodoAttributes implements Cloneable { else{ throw new InvalidDataException(icon); } - myTextAttributes.readExternal(element); if (element.getChild(ELEMENT_OPTION) == null) { myTextAttributes = defaultTodoAttributes; } + else { + myTextAttributes = new TextAttributes(element); + } // default color setting final String useCustomColors = element.getAttributeValue(USE_CUSTOM_COLORS_ATT); diff --git a/platform/platform-tests/testSrc/com/intellij/ide/severities/HighlightSeveritiesTest.java b/platform/platform-tests/testSrc/com/intellij/ide/severities/HighlightSeveritiesTest.java index 2e4f8b9dcf82..a34c0e7f7dc4 100644 --- a/platform/platform-tests/testSrc/com/intellij/ide/severities/HighlightSeveritiesTest.java +++ b/platform/platform-tests/testSrc/com/intellij/ide/severities/HighlightSeveritiesTest.java @@ -1,5 +1,17 @@ /* - * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved. + * Copyright 2000-2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ /* @@ -15,28 +27,10 @@ import junit.framework.TestCase; import org.jdom.Element; public class HighlightSeveritiesTest extends TestCase { - private int myOldSeverity; - - @Override - protected void setUp() throws Exception { - super.setUp(); - myOldSeverity = HighlightSeverity.ERROR.myVal; - } - - - @Override - protected void tearDown() throws Exception { - HighlightSeverity.ERROR.myVal = myOldSeverity; - super.tearDown(); - } - public void testSeveritiesMigration() throws Exception{ - HighlightSeverity.ERROR.myVal = 200; final Element element = new Element("temp"); new HighlightSeverity(HighlightSeverity.ERROR.myName, 500).writeExternal(element); - HighlightSeverity.ERROR.readExternal(element); - assertEquals(500, HighlightSeverity.ERROR.myVal); + HighlightSeverity newSeverity = new HighlightSeverity(element); + assertEquals(500, newSeverity.myVal); } - - } \ No newline at end of file diff --git a/platform/testFramework/src/com/intellij/testFramework/ExpectedHighlightingData.java b/platform/testFramework/src/com/intellij/testFramework/ExpectedHighlightingData.java index c6aa4ce0845f..ffa6de7fba90 100644 --- a/platform/testFramework/src/com/intellij/testFramework/ExpectedHighlightingData.java +++ b/platform/testFramework/src/com/intellij/testFramework/ExpectedHighlightingData.java @@ -29,6 +29,7 @@ import com.intellij.openapi.application.Result; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.HighlighterColors; import com.intellij.openapi.editor.RangeMarker; import com.intellij.openapi.editor.colors.TextAttributesKey; import com.intellij.openapi.editor.markup.EffectType; @@ -345,7 +346,8 @@ public class ExpectedHighlightingData { return toContinueFrom; } - private static final HighlightInfoType WHATEVER = new HighlightInfoType.HighlightInfoTypeImpl(); + private static final HighlightInfoType WHATEVER = new HighlightInfoType.HighlightInfoTypeImpl(HighlightSeverity.INFORMATION, + HighlighterColors.TEXT); public void checkLineMarkers(Collection markerInfos, String text) { String fileName = myFile == null ? "" : myFile.getName() + ": "; diff --git a/platform/util/src/com/intellij/openapi/util/DefaultJDOMExternalizer.java b/platform/util/src/com/intellij/openapi/util/DefaultJDOMExternalizer.java index 5232033e0d3a..726b5213f705 100644 --- a/platform/util/src/com/intellij/openapi/util/DefaultJDOMExternalizer.java +++ b/platform/util/src/com/intellij/openapi/util/DefaultJDOMExternalizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2013 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. @@ -202,12 +202,8 @@ public class DefaultJDOMExternalizer { } } else if (type.equals(int.class)) { - try { - field.setInt(data, Integer.parseInt(value)); - } - catch (NumberFormatException ex) { - throw new InvalidDataException(); - } + int i = toInt(value); + field.setInt(data, i); } else if (type.equals(long.class)) { try { @@ -259,19 +255,8 @@ public class DefaultJDOMExternalizer { field.set(data, value); } else if (type.equals(Color.class)) { - if (value != null) { - try { - int rgb = Integer.parseInt(value, 16); - field.set(data, new Color(rgb)); - } - catch (NumberFormatException ex) { - LOG.debug("Wrong color value: " + value, ex); - throw new InvalidDataException(); - } - } - else { - field.set(data, null); - } + Color color = toColor(value); + field.set(data, color); } else if (ReflectionCache.isAssignable(JDOMExternalizable.class, type)) { final List children = e.getChildren("value"); @@ -305,4 +290,33 @@ public class DefaultJDOMExternalizer { } } } + + public static int toInt(@NotNull String value) throws InvalidDataException { + int i; + try { + i = Integer.parseInt(value); + } + catch (NumberFormatException ex) { + throw new InvalidDataException(value, ex); + } + return i; + } + + public static Color toColor(@Nullable String value) throws InvalidDataException { + Color color; + if (value == null) { + color = null; + } + else { + try { + int rgb = Integer.parseInt(value, 16); + color = new Color(rgb); + } + catch (NumberFormatException ex) { + LOG.debug("Wrong color value: " + value, ex); + throw new InvalidDataException("Wrong color value: " + value, ex); + } + } + return color; + } } diff --git a/platform/util/src/com/intellij/openapi/util/JDOMExternalizerUtil.java b/platform/util/src/com/intellij/openapi/util/JDOMExternalizerUtil.java index 7f56aaaa184f..c9bf2171e9a0 100644 --- a/platform/util/src/com/intellij/openapi/util/JDOMExternalizerUtil.java +++ b/platform/util/src/com/intellij/openapi/util/JDOMExternalizerUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2013 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,19 +15,14 @@ */ package com.intellij.openapi.util; -import com.intellij.openapi.diagnostic.Logger; import org.jdom.Element; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.List; - @SuppressWarnings({"HardCodedStringLiteral"}) public class JDOMExternalizerUtil { - private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.util.JDOMExternalizerUtil"); - - public static void writeField(Element root, @NonNls String fieldName, String value) { + public static void writeField(@NotNull Element root, @NotNull @NonNls String fieldName, String value) { Element element = new Element("option"); element.setAttribute("name", fieldName); element.setAttribute("value", value == null ? "" : value); @@ -35,20 +30,14 @@ public class JDOMExternalizerUtil { } @NotNull - public static String readField(Element parent, @NonNls String fieldName, @NotNull String defaultValue) { + public static String readField(@NotNull Element parent, @NotNull @NonNls String fieldName, @NotNull String defaultValue) { String val = readField(parent, fieldName); - if (val != null) { - return val; - } else { - return defaultValue; - } + return val == null ? defaultValue : val; } @Nullable - public static String readField(Element parent, @NonNls String fieldName) { - List list = parent.getChildren("option"); - for (int i = 0; i < list.size(); i++) { - Element element = (Element)list.get(i); + public static String readField(@NotNull Element parent, @NotNull @NonNls String fieldName) { + for (Element element : JDOMUtil.getChildren(parent, "option")) { String childName = element.getAttributeValue("name"); if (Comparing.strEqual(childName, fieldName)) { return element.getAttributeValue("value"); @@ -56,4 +45,22 @@ public class JDOMExternalizerUtil { } return null; } + + public static Element getOption(@NotNull Element parent, @NotNull @NonNls String fieldName) { + for (Element element : JDOMUtil.getChildren(parent, "option")) { + String childName = element.getAttributeValue("name"); + if (Comparing.strEqual(childName, fieldName)) { + return element; + } + } + return null; + } + + @NotNull + public static Element writeOption(@NotNull Element root, @NotNull @NonNls String fieldName) { + Element element = new Element("option"); + element.setAttribute("name", fieldName); + root.addContent(element); + return element; + } }