Title: [2158] trunk: JavaBeanProvider ensures that it can instantiate a type before it claims to handle it.
Revision
2158
Author
joehni
Date
2013-11-26 19:26:27 -0600 (Tue, 26 Nov 2013)

Log Message

JavaBeanProvider ensures that it can instantiate a type before it claims to handle it. Avoids problems with SecurityManager. BeanProvider does no longer use reflection to locate default constructor.

Modified Paths

Diff

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/converters/javabean/BeanProvider.java (2157 => 2158)


--- trunk/xstream/src/java/com/thoughtworks/xstream/converters/javabean/BeanProvider.java	2013-11-27 01:19:35 UTC (rev 2157)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/converters/javabean/BeanProvider.java	2013-11-27 01:26:27 UTC (rev 2158)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2005 Joe Walnes.
- * Copyright (C) 2006, 2007, 2008, 2010, 2011 XStream Committers.
+ * Copyright (C) 2006, 2007, 2008, 2010, 2011, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -26,6 +26,9 @@
 
 public class BeanProvider implements JavaBeanProvider {
 
+    /**
+     * @deprecated As of upcoming
+     */
     protected static final Object[] NO_PARAMS = new Object[0];
     protected PropertyDictionary propertyDictionary;
 
@@ -58,21 +61,15 @@
 
     public Object newInstance(Class type) {
         try {
-            return getDefaultConstrutor(type).newInstance(NO_PARAMS);
+            return type.newInstance();
         } catch (InstantiationException e) {
             throw new ObjectAccessException("Cannot construct " + type.getName(), e);
         } catch (IllegalAccessException e) {
             throw new ObjectAccessException("Cannot construct " + type.getName(), e);
-        } catch (InvocationTargetException e) {
-            if (e.getTargetException() instanceof RuntimeException) {
-                throw (RuntimeException)e.getTargetException();
-            } else if (e.getTargetException() instanceof Error) {
-                throw (Error)e.getTargetException();
-            } else {
-                throw new ObjectAccessException("Constructor for "
-                    + type.getName()
-                    + " threw an exception", e);
-            }
+        } catch (SecurityException e) {
+            throw new ObjectAccessException("Cannot construct " + type.getName(), e);
+        } catch (ExceptionInInitializerError e) {
+            throw new ObjectAccessException("Cannot construct " + type.getName(), e);
         }
     }
 
@@ -141,15 +138,21 @@
      * Returns true if the Bean provider can instantiate the specified class
      */
     public boolean canInstantiate(Class type) {
-        return getDefaultConstrutor(type) != null;
+        try {
+            return newInstance(type) != null;
+        } catch (ObjectAccessException e) {
+            return false;
+        }
     }
 
     /**
      * Returns the default constructor, or null if none is found
      * 
      * @param type
+     * @deprecated As of upcoming use {@link #newInstance(Class)} or {@link #canInstantiate(Class)} directly.
      */
     protected Constructor getDefaultConstrutor(Class type) {
+        
         Constructor[] constructors = type.getConstructors();
         for (int i = 0; i < constructors.length; i++ ) {
             Constructor c = constructors[i];

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/converters/javabean/JavaBeanConverter.java (2157 => 2158)


--- trunk/xstream/src/java/com/thoughtworks/xstream/converters/javabean/JavaBeanConverter.java	2013-11-27 01:19:35 UTC (rev 2157)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/converters/javabean/JavaBeanConverter.java	2013-11-27 01:26:27 UTC (rev 2158)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2005 Joe Walnes.
- * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 XStream Committers.
+ * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -74,11 +74,11 @@
     }
 
     /**
-     * Only checks for the availability of a public default constructor.
-     * If you need stricter checks, subclass JavaBeanConverter
+     * Checks if the bean provider can instantiate this type.
+     * If you need less strict checks, subclass JavaBeanConverter
      */
     public boolean canConvert(Class type) {
-        return (this.type == null || this.type==type) &&  beanProvider.canInstantiate(type);
+        return (this.type == null || this.type == type) && beanProvider.canInstantiate(type);
     }
 
     public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {

Modified: trunk/xstream-distribution/src/content/changes.html (2157 => 2158)


--- trunk/xstream-distribution/src/content/changes.html	2013-11-27 01:19:35 UTC (rev 2157)
+++ trunk/xstream-distribution/src/content/changes.html	2013-11-27 01:26:27 UTC (rev 2158)
@@ -45,9 +45,11 @@
     <h2>Minor changes</h2>
     
     <ul>
+    	<li>JavaBeanProvider ensures that it can instantiate a type before it claims to handle it. Avoids problems with SecurityManager.</li>
+    	<li>BeanProvider does no longer use reflection to locate default constructor. Avoids problems with SecurityManager and GAE.</li>
+    	<li>No need for reflection in StackTraceElementFactory anymore. Avoids problems with SecurityManager and GAE.</li>
     	<li>JIRA:XSTR-739 and JIRA:XSTR-746: OrderRetainingMap fails if HashMap.putAll(Map) of Java Runtime is not
     	implemented calling put for every element within the map.</li>
-    	<li>No need for reflection in StackTraceElementFactory anymore. Avoids problems with SecurityManager and GAE.</li>
     </ul>
 
     <h1 id="1.4.5">1.4.5</h1>

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to