optimize exception bug
diff --git a/go.mod b/go.mod
index fb0a269..0060e17 100644
--- a/go.mod
+++ b/go.mod
@@ -1,5 +1,7 @@
module github.com/apache/dubbo-go-hessian2
+go 1.14
+
require (
github.com/dubbogo/gost v1.9.0
github.com/pkg/errors v0.9.1
diff --git a/java_biz_exception.go b/java_biz_exception.go
new file mode 100644
index 0000000..230273c
--- /dev/null
+++ b/java_biz_exception.go
@@ -0,0 +1,63 @@
+package hessian
+
+import (
+ "fmt"
+ "github.com/apache/dubbo-go-hessian2/java_exception"
+ "sync"
+)
+
+var mutex sync.Mutex
+
+func checkAndGetException(cls classInfo) (structInfo, bool){
+
+ if len(cls.fieldNameList) < 4 {
+ return structInfo{}, false
+ }
+ var (
+ throwable structInfo
+ ok bool
+ )
+ var count =0
+ for _, item := range cls.fieldNameList {
+ if item == "detailMessage" || item == "suppressedExceptions" || item == "stackTrace" || item == "cause" {
+ count ++
+ }
+ }
+ // 如果满足异常条件
+ if count == 4 {
+ mutex.Lock()
+ defer mutex.Unlock()
+ if throwable, ok = getStructInfo(cls.javaName); ok {
+ return throwable, true
+ }
+ RegisterPOJO(newBizException(cls.javaName))
+ if throwable, ok = getStructInfo(cls.javaName); ok {
+ return throwable, true
+ }
+ }
+ return throwable, count == 4
+}
+
+type BizException struct {
+ SerialVersionUID int64
+ DetailMessage string
+ SuppressedExceptions []java_exception.Throwabler
+ StackTrace []java_exception.StackTraceElement
+ Cause java_exception.Throwabler
+ name string
+}
+
+// NewThrowable is the constructor
+func newBizException(name string) *BizException {
+ return &BizException{name: name, StackTrace: []java_exception.StackTraceElement{}}
+}
+
+// Error output error message
+func (e BizException) Error() string {
+ return fmt.Sprintf("throw %v : %v", e.name, e.DetailMessage)
+}
+
+//JavaClassName java fully qualified path
+func (e BizException) JavaClassName() string {
+ return e.name
+}
\ No newline at end of file
diff --git a/java_exception/exception.go b/java_exception/exception.go
index 19f952a..28dd59c 100644
--- a/java_exception/exception.go
+++ b/java_exception/exception.go
@@ -87,7 +87,7 @@
DeclaringClass string
MethodName string
FileName string
- LineNumber int
+ LineNumber int32
}
//JavaClassName java fully qualified path
diff --git a/list.go b/list.go
index 34c8aec..f47cfad 100644
--- a/list.go
+++ b/list.go
@@ -18,6 +18,7 @@
package hessian
import (
+ "github.com/apache/dubbo-go-hessian2/java_exception"
"io"
"reflect"
"strconv"
@@ -46,6 +47,8 @@
"date": reflect.TypeOf(time.Time{}),
"object": reflect.TypeOf([]Object{}).Elem(),
"java.lang.Object": reflect.TypeOf([]Object{}).Elem(),
+ //异常
+ "java.lang.StackTraceElement": reflect.TypeOf([]*java_exception.StackTraceElement{}).Elem(),
}
)
diff --git a/object.go b/object.go
index e2ef7b3..2a86820 100644
--- a/object.go
+++ b/object.go
@@ -377,6 +377,7 @@
index, fieldStruct, err := findFieldWithCache(fieldName, typ)
if err != nil {
+ // skip field
d.DecodeValue()
continue
}
@@ -547,6 +548,10 @@
cls = d.classInfoList[idx]
s, ok = getStructInfo(cls.javaName)
if !ok {
+ // exception
+ if s , ok = checkAndGetException(cls); ok {
+ return s.typ, cls, nil
+ }
if !d.isSkip {
err = perrors.Errorf("can not find go type name %s in registry", cls.javaName)
}