Fake Exception as RuntimeExceptions and convert values unsing Proto.Type methods
1.1 --- a/json-beans/src/main/java/net/java/html/beans/JSONBeans.java Sun Aug 03 18:27:17 2014 +0200
1.2 +++ b/json-beans/src/main/java/net/java/html/beans/JSONBeans.java Sun Aug 03 21:15:07 2014 +0200
1.3 @@ -56,8 +56,6 @@
1.4 import java.lang.reflect.Method;
1.5 import java.util.HashMap;
1.6 import java.util.Map;
1.7 -import java.util.logging.Level;
1.8 -import java.util.logging.Logger;
1.9 import net.java.html.BrwsrCtx;
1.10 import org.apidesign.html.json.spi.Proto;
1.11
1.12 @@ -91,6 +89,11 @@
1.13 );
1.14 }
1.15
1.16 + @SuppressWarnings("unchecked")
1.17 + static <E extends Throwable> E toRuntime(Throwable ex) {
1.18 + return (E)ex;
1.19 + }
1.20 +
1.21 private static final class Html4JavaType extends Proto.Type {
1.22 private static Map<Object,Reference<Proto>> protos =
1.23 new HashMap<Object, Reference<Proto>>();
1.24 @@ -122,29 +125,23 @@
1.25 protected void setValue(Object model, int index, Object value) {
1.26 try {
1.27 try {
1.28 - properties[index].getWriteMethod().invoke(model, value);
1.29 + final Method m = properties[index].getWriteMethod();
1.30 + value = extractValue(m.getParameterTypes()[0], value);
1.31 + m.invoke(model, value);
1.32 } catch (InvocationTargetException ex) {
1.33 throw ex.getCause();
1.34 }
1.35 - } catch (RuntimeException ex) {
1.36 - throw (RuntimeException)ex;
1.37 - } catch (Throwable ex) {
1.38 - throw new IllegalStateException(ex);
1.39 + } catch (Throwable t) {
1.40 + throw (RuntimeException)toRuntime(t);
1.41 }
1.42 }
1.43
1.44 @Override
1.45 protected Object getValue(Object model, int index) {
1.46 try {
1.47 - try {
1.48 - return properties[index].getReadMethod().invoke(model);
1.49 - } catch (InvocationTargetException ex) {
1.50 - throw ex.getCause();
1.51 - }
1.52 - } catch (RuntimeException ex) {
1.53 - throw (RuntimeException)ex;
1.54 - } catch (Throwable ex) {
1.55 - throw new IllegalStateException(ex);
1.56 + return properties[index].getReadMethod().invoke(model);
1.57 + } catch (Exception ex) {
1.58 + throw (RuntimeException)toRuntime(ex);
1.59 }
1.60 }
1.61
1.62 @@ -158,23 +155,17 @@
1.63 try {
1.64 Object clone = model.getClass().newInstance();
1.65 for (int i = 0; i < properties.length; i++) {
1.66 - try {
1.67 - final Method write = properties[i].getWriteMethod();
1.68 - if (write == null) {
1.69 - continue;
1.70 - }
1.71 - Object value = properties[i].getReadMethod().invoke(model);
1.72 - write.invoke(model, value);
1.73 - } catch (InvocationTargetException ex) {
1.74 - throw ex.getCause();
1.75 + final Method write = properties[i].getWriteMethod();
1.76 + if (write == null) {
1.77 + continue;
1.78 }
1.79 + Object value = properties[i].getReadMethod().invoke(model);
1.80 + write.invoke(model, value);
1.81 }
1.82 findProto(clone, this, ctx);
1.83 return clone;
1.84 - } catch (RuntimeException ex) {
1.85 - throw (RuntimeException) ex;
1.86 - } catch (Throwable ex) {
1.87 - throw new IllegalStateException(ex);
1.88 + } catch (Exception ex) {
1.89 + throw (RuntimeException)toRuntime(ex);
1.90 }
1.91 }
1.92
1.93 @@ -237,7 +228,7 @@
1.94 try {
1.95 addPCL.invoke(object, this);
1.96 } catch (Exception ex) {
1.97 - throw new IllegalStateException(ex);
1.98 + throw (RuntimeException)toRuntime(ex);
1.99 }
1.100 }
1.101