From f62c7fee85c98b0447e4ee362b25e4549a3bfc3f Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Fri, 4 Mar 2016 13:23:07 +0300 Subject: [PATCH] javafx: Improve comparison of primitive values in the inspection of redundant FXML attributes and tags, test renamed to match the inspection name (IDEA-102276) --- ...RedundantPropertyValueInspectionTest.java} | 2 +- .../plugins/javaFX/fxml/JavaFxPsiUtil.java | 2 +- ...avaFxRedundantPropertyValueInspection.java | 69 ++++++++----------- 3 files changed, 32 insertions(+), 41 deletions(-) rename plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/{JavaFxRedundantValueTest.java => JavaFxRedundantPropertyValueInspectionTest.java} (94%) diff --git a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxRedundantValueTest.java b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxRedundantPropertyValueInspectionTest.java similarity index 94% rename from plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxRedundantValueTest.java rename to plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxRedundantPropertyValueInspectionTest.java index aab4ee8d5d7f..79f5c36a8ef6 100644 --- a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxRedundantValueTest.java +++ b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxRedundantPropertyValueInspectionTest.java @@ -7,7 +7,7 @@ import org.jetbrains.plugins.javaFX.fxml.codeInsight.inspections.JavaFxRedundant /** * @author Pavel.Dolgov */ -public class JavaFxRedundantValueTest extends AbstractJavaFXQuickFixTest { +public class JavaFxRedundantPropertyValueInspectionTest extends AbstractJavaFXQuickFixTest { @Override protected void enableInspections() { myFixture.enableInspections(new JavaFxRedundantPropertyValueInspection()); diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxPsiUtil.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxPsiUtil.java index 4aa147da4651..a22bf4b264b2 100644 --- a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxPsiUtil.java +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxPsiUtil.java @@ -681,7 +681,7 @@ public class JavaFxPsiUtil { } @Nullable - public static String getBoxedPropertyType(PsiElement declaration) { + public static String getBoxedPropertyType(@Nullable PsiElement declaration) { PsiType psiType = getWritablePropertyType(declaration); if (psiType instanceof PsiPrimitiveType) { return ((PsiPrimitiveType)psiType).getBoxedTypeName(); diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/codeInsight/inspections/JavaFxRedundantPropertyValueInspection.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/codeInsight/inspections/JavaFxRedundantPropertyValueInspection.java index a98b32f30696..5ccf7887abaa 100644 --- a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/codeInsight/inspections/JavaFxRedundantPropertyValueInspection.java +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/codeInsight/inspections/JavaFxRedundantPropertyValueInspection.java @@ -8,7 +8,6 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.psi.*; -import com.intellij.psi.meta.PsiMetaData; import com.intellij.psi.xml.XmlAttribute; import com.intellij.psi.xml.XmlFile; import com.intellij.psi.xml.XmlTag; @@ -19,7 +18,6 @@ import gnu.trove.THashMap; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.javaFX.fxml.FxmlConstants; -import org.jetbrains.plugins.javaFX.fxml.JavaFxCommonNames; import org.jetbrains.plugins.javaFX.fxml.JavaFxFileTypeFactory; import org.jetbrains.plugins.javaFX.fxml.JavaFxPsiUtil; import org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyAttributeDescriptor; @@ -29,8 +27,6 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.ref.Reference; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.net.URL; import java.util.Collections; import java.util.Map; @@ -69,10 +65,10 @@ public class JavaFxRedundantPropertyValueInspection extends XmlSuppressableInspe return; } - final Object defaultValue = getDefaultValue(descriptor, attributeName, attribute.getParent()); + final String defaultValue = getDefaultValue(attributeName, attribute.getParent()); if (defaultValue == null) return; - if (isEqualValue(attributeValue, defaultValue)) { + if (isEqualValue(attributeValue, defaultValue, descriptor.getDeclaration())) { holder.registerProblem(attribute, "Attribute is redundant because it contains default value", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new RemoveAttributeIntentionFix(attributeName, attribute)); @@ -94,10 +90,10 @@ public class JavaFxRedundantPropertyValueInspection extends XmlSuppressableInspe return; } - final Object defaultValue = getDefaultValue(descriptor, tag.getName(), tag.getParentTag()); + final String defaultValue = getDefaultValue(tag.getName(), tag.getParentTag()); if (defaultValue == null) return; - if (isEqualValue(tagText, defaultValue)) { + if (isEqualValue(tagText, defaultValue, descriptor.getDeclaration())) { holder.registerProblem(tag, "Tag is redundant because it contains default value", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new RemoveTagFix(tag.getName())); @@ -107,7 +103,7 @@ public class JavaFxRedundantPropertyValueInspection extends XmlSuppressableInspe } @Nullable - private static Object getDefaultValue(PsiMetaData propertyDescriptor, @NotNull String propertyName, @Nullable XmlTag enclosingTag) { + private static String getDefaultValue(@NotNull String propertyName, @Nullable XmlTag enclosingTag) { if (enclosingTag != null) { final XmlElementDescriptor descriptor = enclosingTag.getDescriptor(); if (descriptor != null) { @@ -118,7 +114,7 @@ public class JavaFxRedundantPropertyValueInspection extends XmlSuppressableInspe if (CommonClassNames.JAVA_LANG_OBJECT.equals(qualifiedName)) break; final String defaultValue = getDefaultPropertyValue(qualifiedName, propertyName); if (defaultValue != null) { - return getBoxedValue(propertyDescriptor.getDeclaration(), defaultValue); + return defaultValue; } } } @@ -127,39 +123,34 @@ public class JavaFxRedundantPropertyValueInspection extends XmlSuppressableInspe return null; } - private static Object getBoxedValue(PsiElement declaration, String value) { - String boxedQName = JavaFxPsiUtil.getBoxedPropertyType(declaration); - if (boxedQName == null) return value; + private static boolean isEqualValue(@NotNull String attributeValue, @NotNull String defaultValue, @Nullable PsiElement declaration) { + final String boxedQName = JavaFxPsiUtil.getBoxedPropertyType(declaration); + if (boxedQName == null) { + return defaultValue.equals(attributeValue); + } try { - final Class boxedClass = Class.forName(boxedQName); - final Method method = boxedClass.getMethod(JavaFxCommonNames.VALUE_OF, String.class); - return method.invoke(boxedClass, value); - } - catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException ignored) { - return value; - } - } - - private static boolean isEqualValue(@NotNull String attributeValue, @NotNull Object defaultValue) { - if (defaultValue instanceof String && defaultValue.equals(attributeValue)) return true; - if (defaultValue instanceof Boolean) return defaultValue == Boolean.valueOf(attributeValue); - if (defaultValue instanceof Double) { - try { - return Double.compare((Double)defaultValue, Double.parseDouble(attributeValue)) == 0; - } - catch (NumberFormatException ignored) { - return false; + switch (boxedQName) { + case CommonClassNames.JAVA_LANG_BOOLEAN: + return Boolean.parseBoolean(defaultValue) == Boolean.parseBoolean(attributeValue); + case CommonClassNames.JAVA_LANG_DOUBLE: + return Double.compare(Double.parseDouble(defaultValue), Double.parseDouble(attributeValue)) == 0; + case CommonClassNames.JAVA_LANG_FLOAT: + return Float.compare(Float.parseFloat(defaultValue), Float.parseFloat(attributeValue)) == 0; + case CommonClassNames.JAVA_LANG_INTEGER: + return Integer.parseInt(defaultValue) == Integer.parseInt(attributeValue); + case CommonClassNames.JAVA_LANG_LONG: + return Long.parseLong(defaultValue) == Long.parseLong(attributeValue); + case CommonClassNames.JAVA_LANG_SHORT: + return Short.parseShort(defaultValue) == Short.parseShort(attributeValue); + case CommonClassNames.JAVA_LANG_BYTE: + return Byte.parseByte(defaultValue) == Byte.parseByte(attributeValue); + default: + return defaultValue.equals(attributeValue); } } - if (defaultValue instanceof Integer) { - try { - return Integer.compare((Integer)defaultValue, Integer.parseInt(attributeValue)) == 0; - } - catch (NumberFormatException ignored) { - return false; - } + catch (NumberFormatException ignored) { + return false; } - return false; } @Nullable