* @author leo
*
*/
-public class XerialException extends Exception {
+public class XerialException extends Exception
+{
private static final long serialVersionUID = 1L;
- private final ErrorCode errorCode;
+ protected final ErrorCode errorCode;
public XerialException(XerialException e) {
super(e.getErrorMessage());
this.errorCode = errorCode;
}
- public ErrorCode getErrorCode() {
- return errorCode;
- }
-
- private boolean hasXerialErrorCode() {
- return XerialErrorCode.class.isInstance(errorCode);
- }
-
- public XerialErrorCode getXerialErrorCode() {
- if (hasXerialErrorCode())
- return XerialErrorCode.class.cast(errorCode);
- else
- return XerialErrorCode.HAS_AN_EXTENDED_ERROR_CODE;
- }
-
- public <T extends Enum<T>> T getErrorCodeOfTheType(Class<T> errorCodeType) {
- return errorCodeType.cast(errorCode);
+ @SuppressWarnings("unchecked")
+ public <T> T getErrorCode() {
+ return (T) errorCode;
}
@Override
* @author leo\r
* \r
*/\r
-public class JSONException extends XerialException {\r
+public class JSONException extends XerialException\r
+{\r
private static final long serialVersionUID = 1L;\r
\r
/**\r
* \r
* @return the error code\r
*/\r
+ @SuppressWarnings("unchecked")\r
public JSONErrorCode getErrorCode() {\r
- return (JSONErrorCode) super.getErrorCode();\r
+ return JSONErrorCode.class.cast(errorCode);\r
}\r
}\r
item.setOption(optionHolder, value);
}
catch (XerialException e) {
- XerialErrorCode be = e.getXerialErrorCode();
+ XerialErrorCode be = e.getErrorCode();
switch (be) {
case InvalidFormat:
throw new OptionParserException(XerialErrorCode.INVALID_ARGUMENT, String.format("cannot set %s to %s",
value, item.toString()));
default:
- throw new OptionParserException(e.getErrorCode(), e.getMessage());
+ throw new OptionParserException(e.<XerialErrorCode> getErrorCode(), e.getMessage());
}
}
}
//--------------------------------------
package org.xerial.core;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
import org.junit.Test;
{
@Test
- public void testVarArgConstructor()
- {
+ public void testVarArgConstructor() {
XerialException e = new XerialException(XerialErrorCode.INVALID_STATE, "exception message");
- assertEquals(XerialErrorCode.INVALID_STATE.name(), e.getErrorCode().getCodeName());
+ assertEquals(XerialErrorCode.INVALID_STATE.name(), e.<XerialErrorCode> getErrorCode().getCodeName());
assertTrue(e.getMessage().contains("exception message"));
}
\r
public class TypeReferenceTest\r
{\r
- @Test\r
- void reference() {\r
+\r
+ public void reference() {\r
Type c = new TypeReference<List<String>>() {}.getElementType()[0];\r
assertEquals(String.class, c);\r
\r