Browse Source

wyjątek przez lexicalcast poprawiony

Ranides Atterwim 13 years ago
parent
commit
78eae0344e

+ 18 - 12
src/main/java/net/ranides/assira/text/LexicalCast.java

@@ -102,38 +102,38 @@ public final class LexicalCast {
     
 /* ************************************************************************** */  
     
-    public static String object2json(Object value) throws JsonCastException {
+    public static String object2json(Object value) throws LexicalCastException {
         try {
             return MAPPER.writeValueAsString(value);
         } catch(IOException cause) {
-            throw new JsonCastException(value.getClass(), cause);
+            throw new LexicalCastException(value.getClass()+" cannot be converted to JSON", cause);
         }
     }
     
-    public static <T> T json2object(String value, Class<T> clazz) throws JsonCastException {
+    public static <T> T json2object(String value, Class<T> clazz) throws LexicalCastException {
         try {
             return MAPPER.readValue(value, clazz);
         } catch(IOException cause) {
-            throw new JsonCastException(value.getClass(), cause);
+            throw new LexicalCastException(clazz + " cannot be created from JSON", cause);
         }
     }
     
     @SuppressWarnings("unchecked")
-    public static <T> T json2object(String value, TypeToken<T> token) throws JsonCastException {
+    public static <T> T json2object(String value, TypeToken<T> token) throws LexicalCastException {
         try {
             return (T)MAPPER.readValue(value, TYPE_FACTORY.constructType( token.type() ) );
         } catch(IOException cause) {
-            throw new JsonCastException(value.getClass(), cause);
+            throw new LexicalCastException(token + " cannot be created from JSON", cause);
         }
     }
     
     @SuppressWarnings("unchecked")
-    public static <T> T cast(Object value, Class<T> clazz) {
+    public static <T> T cast(Object value, Class<T> clazz) throws ClassCastException {
         return (T)rawCast(value, null, clazz);
     }
     
     @SuppressWarnings("unchecked")
-    public static <T> T cast(Object value, TypeToken<T> token) throws JsonCastException {
+    public static <T> T cast(Object value, TypeToken<T> token) throws LexicalCastException {
         return (T)rawCast(value, token, TYPE_FACTORY.constructType( token.type() ).getRawClass());
     }
     
@@ -198,10 +198,16 @@ public final class LexicalCast {
                 return ((Number)value).toString();
             }
         }
-        if(token == null) {
-            return LexicalCast.json2object( LexicalCast.object2json(value), clazz);
-        } else {
-            return LexicalCast.json2object( LexicalCast.object2json(value), token);
+        try {
+            if(token == null) {
+                return LexicalCast.json2object( LexicalCast.object2json(value), clazz);
+            } else {
+                return LexicalCast.json2object( LexicalCast.object2json(value), token);
+            }
+        } catch(LexicalCastException cause) {
+            ClassCastException error = new ClassCastException(value + " cannot be cast to " + clazz);
+            error.initCause(cause);
+            throw error;
         }
         
     }

+ 3 - 13
src/main/java/net/ranides/assira/text/JsonCastException.java

@@ -10,32 +10,22 @@ package net.ranides.assira.text;
  * Wyjątek "unchecked" rzucany przez metody konwertujące obiekty do/z formatu JSON
  * @author ranides
  */
-public class JsonCastException extends RuntimeException {
+public class LexicalCastException extends RuntimeException {
 
     /**
      * Tworzy wyjątek z podanym komunikatem
      * @param message 
      */
-    public JsonCastException(String message) {
+    public LexicalCastException(String message) {
         super(message, null);
     }
     
-    /**
-     * Tworzy wyjątek zawierający klasę obiektu, którego nie udało
-     * się transformować oraz przyczynę błędu.
-     * @param clazz
-     * @param cause
-     */
-    public JsonCastException(Class<?> clazz, Throwable cause) {
-        super(clazz.getName() + " cannot be cast to JSON format", cause);
-    }
-    
     /**
      * Tworzy wyjątek z podanym komunikatem oraz przyczyną błędu.
      * @param message 
      * @param cause 
      */
-    public JsonCastException(String message, Throwable cause) {
+    public LexicalCastException(String message, Throwable cause) {
         super(message, cause);
     }