]> source.dussan.org Git - vaadin-framework.git/commitdiff
Re-added Property.toString warning messages (#10916)
authorArtur Signell <artur@vaadin.com>
Mon, 22 Apr 2013 12:06:09 +0000 (15:06 +0300)
committerVaadin Code Review <review@vaadin.com>
Wed, 24 Apr 2013 09:41:22 +0000 (09:41 +0000)
* Made it possible to toggle the toString() behavior using the "legacyPropertyToString" init parameter. The default value is "warning" which enables the legacy Property.toString implementation and logs a warning message when it is used. Other supported values are "true" which enables the legacy mode and "false" which disables it.

Change-Id: Ife19352b86590464c8e441b7f82f4fec3b1f3235

WebContent/WEB-INF/web.xml
server/src/com/vaadin/data/util/AbstractProperty.java
server/src/com/vaadin/data/util/IndexedContainer.java
server/src/com/vaadin/data/util/LegacyPropertyHelper.java [new file with mode: 0644]
server/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java
server/src/com/vaadin/server/Constants.java
server/src/com/vaadin/server/DefaultDeploymentConfiguration.java
server/src/com/vaadin/server/DeploymentConfiguration.java
server/src/com/vaadin/ui/AbstractField.java
server/src/com/vaadin/ui/Label.java
server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java

index 391589829362db4efee48e947c75d114061fb2ee..fa1f40248b49841c551419bf4f8e149202b0b327 100644 (file)
                <servlet-name>VaadinApplicationRunner</servlet-name>
                <servlet-class>com.vaadin.launcher.ApplicationRunnerServlet</servlet-class>
                <!-- Non-default values for testing purposes -->
+               <init-param>
+                       <param-name>legacyPropertyToString</param-name>
+                       <param-value>false</param-value>
+               </init-param>
                <init-param>
                        <param-name>heartbeatInterval</param-name>
                        <param-value>301</param-value>
index 499421a8b45b67516c5aa2faa3b2743cca477166..903f2f50f28e9ca05b3517c7460bd35870ef90de 100644 (file)
@@ -18,7 +18,6 @@ package com.vaadin.data.util;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import com.vaadin.data.Property;
@@ -71,27 +70,33 @@ public abstract class AbstractProperty<T> implements Property<T>,
     }
 
     /**
-     * Returns the value of the <code>Property</code> in human readable textual
-     * format.
+     * Returns a string representation of this object. The returned string
+     * representation depends on if the legacy Property toString mode is enabled
+     * or disabled.
+     * <p>
+     * If legacy Property toString mode is enabled, returns the value of the
+     * <code>Property</code> converted to a String.
+     * </p>
+     * <p>
+     * If legacy Property toString mode is disabled, the string representation
+     * has no special meaning
+     * </p>
      * 
-     * @return String representation of the value stored in the Property
-     * @deprecated As of 7.0, use {@link #getValue()} instead and possibly
-     *             toString on that
+     * @see LegacyPropertyHelper#isLegacyToStringEnabled()
+     * 
+     * @return A string representation of the value value stored in the Property
+     *         or a string representation of the Property object.
+     * @deprecated As of 7.0. To get the property value, use {@link #getValue()}
+     *             instead (and possibly toString on that)
      */
     @Deprecated
     @Override
     public String toString() {
-        getLogger()
-                .log(Level.WARNING,
-                        "You are using Property.toString() instead of getValue() to get the value for a {0}."
-                                + "This will not be supported starting from Vaadin 7.1 "
-                                + "(your debugger might call toString() and cause this message to appear).",
-                        getClass().getSimpleName());
-        T v = getValue();
-        if (v == null) {
-            return null;
+        if (!LegacyPropertyHelper.isLegacyToStringEnabled()) {
+            return super.toString();
+        } else {
+            return LegacyPropertyHelper.legacyPropertyToString(this);
         }
-        return v.toString();
     }
 
     /* Events */
index a54bf26e52907209efdcab2f73c14a583bf37cf9..31f37ac2770e82102c4ef91f4701b0361c4d3d46 100644 (file)
@@ -28,7 +28,6 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import com.vaadin.data.Container;
@@ -954,29 +953,32 @@ public class IndexedContainer extends
         }
 
         /**
-         * Returns the value of the Property in human readable textual format.
-         * The return value should be assignable to the <code>setValue</code>
-         * method if the Property is not in read-only mode.
+         * Returns a string representation of this object. The returned string
+         * representation depends on if the legacy Property toString mode is
+         * enabled or disabled.
+         * <p>
+         * If legacy Property toString mode is enabled, returns the value of the
+         * <code>Property</code> converted to a String.
+         * </p>
+         * <p>
+         * If legacy Property toString mode is disabled, the string
+         * representation has no special meaning
+         * </p>
          * 
-         * @return <code>String</code> representation of the value stored in the
-         *         Property
-         * @deprecated As of 7.0, use {@link #getValue()} instead and possibly
-         *             toString on that
+         * @return A string representation of the value value stored in the
+         *         Property or a string representation of the Property object.
+         * @deprecated As of 7.0. To get the property value, use
+         *             {@link #getValue()} instead (and possibly toString on
+         *             that)
          */
         @Deprecated
         @Override
         public String toString() {
-            getLogger()
-                    .log(Level.WARNING,
-                            "You are using IndexedContainerProperty.toString() instead of getValue() to get the value for a {0}."
-                                    + " This will not be supported starting from Vaadin 7.1 "
-                                    + "(your debugger might call toString() and cause this message to appear).",
-                            getClass().getSimpleName());
-            Object v = getValue();
-            if (v == null) {
-                return null;
+            if (!LegacyPropertyHelper.isLegacyToStringEnabled()) {
+                return super.toString();
+            } else {
+                return LegacyPropertyHelper.legacyPropertyToString(this);
             }
-            return v.toString();
         }
 
         private Logger getLogger() {
diff --git a/server/src/com/vaadin/data/util/LegacyPropertyHelper.java b/server/src/com/vaadin/data/util/LegacyPropertyHelper.java
new file mode 100644 (file)
index 0000000..0276e35
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2000-2013 Vaadin Ltd.
+ * 
+ * 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.
+ */
+package com.vaadin.data.util;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import com.vaadin.data.Property;
+import com.vaadin.server.Constants;
+import com.vaadin.server.DeploymentConfiguration.LegacyProperyToStringMode;
+import com.vaadin.server.VaadinService;
+
+/**
+ * Helper class which provides methods for handling Property.toString in a
+ * Vaadin 6 compatible way
+ * 
+ * @author Vaadin Ltd
+ * @since 7.1
+ * @deprecated This is only used internally for backwards compatibility
+ */
+@Deprecated
+public class LegacyPropertyHelper {
+
+    /**
+     * Returns the property value converted to a String.
+     * 
+     * @param p
+     *            The property
+     * @return A string representation of the property value, compatible with
+     *         how Property implementations in Vaadin 6 do it
+     */
+    public static String legacyPropertyToString(Property p) {
+        maybeLogLegacyPropertyToStringWarning(p);
+        Object value = p.getValue();
+        if (value == null) {
+            return null;
+        }
+        return value.toString();
+    }
+
+    public static void maybeLogLegacyPropertyToStringWarning(Property p) {
+        if (!logLegacyToStringWarning()) {
+            return;
+        }
+
+        getLogger().log(Level.WARNING,
+                Constants.WARNING_LEGACY_PROPERTY_TOSTRING,
+                p.getClass().getName());
+    }
+
+    /**
+     * Checks if legacy Property.toString() implementation is enabled. The
+     * legacy Property.toString() will return the value of the property somehow
+     * converted to a String. If the legacy mode is disabled, toString() will
+     * return super.toString().
+     * <p>
+     * The legacy toString mode can be toggled using the
+     * "legacyPropertyToString" init parameter
+     * </p>
+     * 
+     * @return true if legacy Property.toString() mode is enabled, false
+     *         otherwise
+     */
+    public static boolean isLegacyToStringEnabled() {
+        if (VaadinService.getCurrent() == null) {
+            // This should really not happen but we need to handle it somehow.
+            // IF it happens it seems more safe to use the legacy mode and log.
+            return true;
+        }
+        return VaadinService.getCurrent().getDeploymentConfiguration()
+                .getLegacyPropertyToStringMode().useLegacyMode();
+    }
+
+    private static boolean logLegacyToStringWarning() {
+        if (VaadinService.getCurrent() == null) {
+            // This should really not happen but we need to handle it somehow.
+            // IF it happens it seems more safe to use the legacy mode and log.
+            return true;
+        }
+        return VaadinService.getCurrent().getDeploymentConfiguration()
+                .getLegacyPropertyToStringMode() == LegacyProperyToStringMode.WARNING;
+
+    }
+
+    private static Logger getLogger() {
+        return Logger.getLogger(LegacyPropertyHelper.class.getName());
+    }
+
+}
index 378372d04416cd22d94c85355f10eb92546b95ae..d8448a2b50ce6a27897e6dec648d4b6778cddc90 100644 (file)
@@ -18,10 +18,10 @@ package com.vaadin.data.util.sqlcontainer;
 import java.sql.Date;
 import java.sql.Time;
 import java.sql.Timestamp;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import com.vaadin.data.Property;
+import com.vaadin.data.util.LegacyPropertyHelper;
 import com.vaadin.data.util.converter.Converter.ConversionException;
 
 /**
@@ -255,26 +255,33 @@ final public class ColumnProperty implements Property {
     }
 
     /**
-     * Returns the value of the Property in human readable textual format.
+     * Returns a string representation of this object. The returned string
+     * representation depends on if the legacy Property toString mode is enabled
+     * or disabled.
+     * <p>
+     * If legacy Property toString mode is enabled, returns the value of this
+     * <code>Property</code> converted to a String.
+     * </p>
+     * <p>
+     * If legacy Property toString mode is disabled, the string representation
+     * has no special meaning
+     * </p>
      * 
-     * @see java.lang.Object#toString()
-     * @deprecated As of 7.0, use {@link #getValue()} instead and possibly
-     *             toString on that
+     * @see LegacyPropertyHelper#isLegacyToStringEnabled()
+     * 
+     * @return A string representation of the value value stored in the Property
+     *         or a string representation of the Property object.
+     * @deprecated As of 7.0. To get the property value, use {@link #getValue()}
+     *             instead (and possibly toString on that)
      */
     @Deprecated
     @Override
     public String toString() {
-        getLogger()
-                .log(Level.WARNING,
-                        "You are using ColumnProperty.toString() instead of getValue() to get the value for a {0}. "
-                                + "This will not be supported starting from Vaadin 7.1 (your debugger might call toString() "
-                                + "and cause this message to appear).",
-                        getClass().getSimpleName());
-        Object v = getValue();
-        if (v == null) {
-            return null;
+        if (!LegacyPropertyHelper.isLegacyToStringEnabled()) {
+            return super.toString();
+        } else {
+            return LegacyPropertyHelper.legacyPropertyToString(this);
         }
-        return v.toString();
     }
 
     private static Logger getLogger() {
index 7636c43c57652a286f56e83fe23a6699bd4698e9..f8d8105286c19123f30fb4f54f32100cbe3829d4 100644 (file)
@@ -99,6 +99,19 @@ public interface Constants {
             + ".\n"
             + "=================================================================";
 
+    public static final String WARNING_LEGACY_PROPERTY_TOSTRING = "You are using toString() instead of getValue() to get the value for a Property of type {0}"
+            + ". This is strongly discouraged and only provided for backwards compatibility with Vaadin 6. "
+            + "To disable this warning message and retain the behavior, set the init parameter \""
+            + Constants.SERVLET_PARAMETER_LEGACY_PROPERTY_TOSTRING
+            + "\" to \"true\". To disable the legacy functionality, set \""
+            + Constants.SERVLET_PARAMETER_LEGACY_PROPERTY_TOSTRING
+            + "\" to false."
+            + " (Note that your debugger might call toString() and trigger this message).";
+
+    static final String WARNING_UNKNOWN_LEGACY_PROPERTY_TOSTRING_VALUE = "Unknown value '{0}' for parameter "
+            + Constants.SERVLET_PARAMETER_LEGACY_PROPERTY_TOSTRING
+            + ". Supported values are 'false','warning','true'";
+
     static final String URL_PARAMETER_THEME = "theme";
 
     static final String SERVLET_PARAMETER_PRODUCTION_MODE = "productionMode";
@@ -108,6 +121,7 @@ public interface Constants {
     static final String SERVLET_PARAMETER_CLOSE_IDLE_SESSIONS = "closeIdleSessions";
     static final String SERVLET_PARAMETER_PUSH_MODE = "pushMode";
     static final String SERVLET_PARAMETER_UI_PROVIDER = "UIProvider";
+    static final String SERVLET_PARAMETER_LEGACY_PROPERTY_TOSTRING = "legacyPropertyToString";
 
     // Configurable parameter names
     static final String PARAMETER_VAADIN_RESOURCES = "Resources";
index d11bd699976c31b6885d5669c04c9db404a9b5c6..80c3644d77962e6047e409c57bcbb075ebeda5e1 100644 (file)
@@ -17,6 +17,7 @@
 package com.vaadin.server;
 
 import java.util.Properties;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import com.vaadin.shared.communication.PushMode;
@@ -37,6 +38,7 @@ public class DefaultDeploymentConfiguration implements DeploymentConfiguration {
     private boolean closeIdleSessions;
     private PushMode pushMode;
     private final Class<?> systemPropertyBaseClass;
+    private LegacyProperyToStringMode legacyPropertyToStringMode;
 
     /**
      * Create a new deployment configuration instance.
@@ -59,6 +61,26 @@ public class DefaultDeploymentConfiguration implements DeploymentConfiguration {
         checkHeartbeatInterval();
         checkCloseIdleSessions();
         checkPushMode();
+        checkLegacyPropertyToString();
+    }
+
+    private void checkLegacyPropertyToString() {
+        String param = getApplicationOrSystemProperty(
+                Constants.SERVLET_PARAMETER_LEGACY_PROPERTY_TOSTRING, "warning");
+        if ("true".equals(param)) {
+            legacyPropertyToStringMode = LegacyProperyToStringMode.ENABLED;
+        } else if ("false".equals(param)) {
+            legacyPropertyToStringMode = LegacyProperyToStringMode.DISABLED;
+        } else {
+            if (!"warning".equals(param)) {
+                getLogger()
+                        .log(Level.WARNING,
+                                Constants.WARNING_UNKNOWN_LEGACY_PROPERTY_TOSTRING_VALUE,
+                                param);
+            }
+            legacyPropertyToStringMode = LegacyProperyToStringMode.WARNING;
+
+        }
     }
 
     @Override
@@ -270,4 +292,11 @@ public class DefaultDeploymentConfiguration implements DeploymentConfiguration {
     private Logger getLogger() {
         return Logger.getLogger(getClass().getName());
     }
+
+    @Override
+    @Deprecated
+    public LegacyProperyToStringMode getLegacyPropertyToStringMode() {
+        return legacyPropertyToStringMode;
+    }
+
 }
index 23edf8052a33aa52e8f87e710212dc47778e4ddb..bf9c019b6d0f15a0f002859ec587e19482901d04 100644 (file)
@@ -19,6 +19,7 @@ package com.vaadin.server;
 import java.io.Serializable;
 import java.util.Properties;
 
+import com.vaadin.data.util.AbstractProperty;
 import com.vaadin.shared.communication.PushMode;
 
 /**
@@ -30,6 +31,23 @@ import com.vaadin.shared.communication.PushMode;
  * @since 7.0.0
  */
 public interface DeploymentConfiguration extends Serializable {
+
+    /**
+     * Determines the mode of the "legacyPropertyToString" parameter.
+     * 
+     * @author Vaadin Ltd
+     * @since 7.1
+     */
+    @Deprecated
+    public enum LegacyProperyToStringMode {
+        DISABLED, WARNING, ENABLED;
+
+        public boolean useLegacyMode() {
+            return this == WARNING || this == ENABLED;
+        }
+
+    }
+
     /**
      * Returns whether Vaadin is in production mode.
      * 
@@ -111,4 +129,13 @@ public interface DeploymentConfiguration extends Serializable {
     public String getApplicationOrSystemProperty(String propertyName,
             String defaultValue);
 
+    /**
+     * Returns to legacy Property.toString() mode used. See
+     * {@link AbstractProperty#isLegacyToStringEnabled()} for more information.
+     * 
+     * @return The Property.toString() mode in use.
+     */
+    @Deprecated
+    public LegacyProperyToStringMode getLegacyPropertyToStringMode();
+
 }
index eb8fc30a45c29ce3da8f5d98ea0ffc70ee734209..6648f69ff9dc5cda383f2383140c4a87fb4c1081 100644 (file)
@@ -25,13 +25,13 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Locale;
-import java.util.logging.Logger;
 
 import com.vaadin.data.Buffered;
 import com.vaadin.data.Property;
 import com.vaadin.data.Validatable;
 import com.vaadin.data.Validator;
 import com.vaadin.data.Validator.InvalidValueException;
+import com.vaadin.data.util.LegacyPropertyHelper;
 import com.vaadin.data.util.converter.Converter;
 import com.vaadin.data.util.converter.Converter.ConversionException;
 import com.vaadin.data.util.converter.ConverterUtil;
@@ -75,9 +75,6 @@ public abstract class AbstractField<T> extends AbstractComponent implements
 
     /* Private members */
 
-    private static final Logger logger = Logger.getLogger(AbstractField.class
-            .getName());
-
     /**
      * Value of the abstract field.
      */
@@ -371,6 +368,39 @@ public abstract class AbstractField<T> extends AbstractComponent implements
         return buffered;
     }
 
+    /**
+     * Returns a string representation of this object. The returned string
+     * representation depends on if the legacy Property toString mode is enabled
+     * or disabled.
+     * <p>
+     * If legacy Property toString mode is enabled, returns the value of this
+     * <code>Field</code> converted to a String.
+     * </p>
+     * <p>
+     * If legacy Property toString mode is disabled, the string representation
+     * has no special meaning
+     * </p>
+     * 
+     * @see LegacyPropertyHelper#isLegacyToStringEnabled()
+     * 
+     * @return A string representation of the value value stored in the Property
+     *         or a string representation of the Property object.
+     * @deprecated As of 7.0. Use {@link #getValue()} to get the value of the
+     *             field, {@link #getConvertedValue()} to get the field value
+     *             converted to the data model type or
+     *             {@link #getPropertyDataSource()} .getValue() to get the value
+     *             of the data source.
+     */
+    @Deprecated
+    @Override
+    public String toString() {
+        if (!LegacyPropertyHelper.isLegacyToStringEnabled()) {
+            return super.toString();
+        } else {
+            return LegacyPropertyHelper.legacyPropertyToString(this);
+        }
+    }
+
     /* Property interface implementation */
 
     /**
index 72f556ee5b1452bf822ffe9eb0c930f7fc0e77ed..d037652a099800f0163645f86e8783d1cad76a6a 100644 (file)
@@ -21,6 +21,8 @@ import java.util.Locale;
 import java.util.logging.Logger;
 
 import com.vaadin.data.Property;
+import com.vaadin.data.util.AbstractProperty;
+import com.vaadin.data.util.LegacyPropertyHelper;
 import com.vaadin.data.util.converter.Converter;
 import com.vaadin.data.util.converter.ConverterUtil;
 import com.vaadin.shared.ui.label.ContentMode;
@@ -525,4 +527,35 @@ public class Label extends AbstractComponent implements Property<String>,
         markAsDirty();
     }
 
+    /**
+     * Returns a string representation of this object. The returned string
+     * representation depends on if the legacy Property toString mode is enabled
+     * or disabled.
+     * <p>
+     * If legacy Property toString mode is enabled, returns the value displayed
+     * by this label.
+     * </p>
+     * <p>
+     * If legacy Property toString mode is disabled, the string representation
+     * has no special meaning
+     * </p>
+     * 
+     * @see AbstractProperty#isLegacyToStringEnabled()
+     * 
+     * @return The value displayed by this label or a string representation of
+     *         this Label object.
+     * 
+     * @deprecated As of 7.0, use {@link #getValue()} to get the value of the
+     *             label or {@link #getPropertyDataSource()}.getValue() to get
+     *             the value of the data source.
+     */
+    @Deprecated
+    @Override
+    public String toString() {
+        if (!LegacyPropertyHelper.isLegacyToStringEnabled()) {
+            return super.toString();
+        } else {
+            return LegacyPropertyHelper.legacyPropertyToString(this);
+        }
+    }
 }
index cd53a564d242f5985bf19ddc7e227f684d5742ad..d113efdfaf899c426c590820fdda742a1eceab8f 100644 (file)
@@ -18,7 +18,9 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration {
     private PushMode pushMode = PushMode.DISABLED;
     private Properties initParameters = new Properties();
     private Map<String, String> applicationOrSystemProperty = new HashMap<String, String>();
+    private LegacyProperyToStringMode legacyPropertyToStringMode = LegacyProperyToStringMode.DISABLED;
 
+    @Override
     public boolean isProductionMode() {
         return productionMode;
     }
@@ -27,6 +29,7 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration {
         this.productionMode = productionMode;
     }
 
+    @Override
     public boolean isXsrfProtectionEnabled() {
         return xsrfProtectionEnabled;
     }
@@ -35,6 +38,7 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration {
         this.xsrfProtectionEnabled = xsrfProtectionEnabled;
     }
 
+    @Override
     public int getResourceCacheTime() {
         return resourceCacheTime;
     }
@@ -43,6 +47,7 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration {
         this.resourceCacheTime = resourceCacheTime;
     }
 
+    @Override
     public int getHeartbeatInterval() {
         return heartbeatInterval;
     }
@@ -51,6 +56,7 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration {
         this.heartbeatInterval = heartbeatInterval;
     }
 
+    @Override
     public boolean isCloseIdleSessions() {
         return closeIdleSessions;
     }
@@ -59,6 +65,7 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration {
         this.closeIdleSessions = closeIdleSessions;
     }
 
+    @Override
     public PushMode getPushMode() {
         return pushMode;
     }
@@ -67,6 +74,7 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration {
         this.pushMode = pushMode;
     }
 
+    @Override
     public Properties getInitParameters() {
         return initParameters;
     }
@@ -89,4 +97,15 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration {
         }
     }
 
+    @Override
+    @Deprecated
+    public LegacyProperyToStringMode getLegacyPropertyToStringMode() {
+        return legacyPropertyToStringMode;
+    }
+
+    public void setLegacyPropertyToStringMode(
+            LegacyProperyToStringMode legacyPropertyToStringMode) {
+        this.legacyPropertyToStringMode = legacyPropertyToStringMode;
+    }
+
 }