-
Notifications
You must be signed in to change notification settings - Fork 112
/
java_collection.go
136 lines (123 loc) · 3.63 KB
/
java_collection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hessian
import (
"reflect"
)
import (
perrors "github.com/pkg/errors"
)
type JavaCollectionObject interface {
Get() []interface{}
Set([]interface{})
JavaClassName() string
}
var collectionTypeMap = make(map[string]reflect.Type, 16)
func SetCollectionSerialize(collection JavaCollectionObject) {
name := collection.JavaClassName()
v := reflect.ValueOf(collection)
var typ reflect.Type
switch v.Kind() {
case reflect.Struct:
typ = v.Type()
case reflect.Ptr:
typ = v.Elem().Type()
default:
typ = reflect.TypeOf(collection)
}
SetSerializer(name, JavaCollectionSerializer{})
RegisterPOJO(collection)
collectionTypeMap[name] = typ
}
func getCollectionSerialize(name string) reflect.Type {
return collectionTypeMap[name]
}
func isCollectionSerialize(name string) bool {
return getCollectionSerialize(name) != nil
}
type JavaCollectionSerializer struct{}
func (JavaCollectionSerializer) EncObject(e *Encoder, vv POJO) error {
var err error
v, ok := vv.(JavaCollectionObject)
if !ok {
return perrors.New("can not be converted into java collection object")
}
collectionName := v.JavaClassName()
if collectionName == "" {
return perrors.New("collection name empty")
}
list := v.Get()
length := len(list)
typeName := v.JavaClassName()
err = writeCollectionBegin(length, typeName, e)
if err != nil {
return err
}
for i := 0; i < length; i++ {
if err = e.Encode(list[i]); err != nil {
return err
}
}
return nil
}
func (JavaCollectionSerializer) DecObject(d *Decoder, typ reflect.Type, cls *ClassInfo) (interface{}, error) {
// for the java impl of hessian encode collections as list, which will not be decoded as object in go impl, this method should not be called
return nil, perrors.New("unexpected collection decode call")
}
func (d *Decoder) decodeCollection(length int, listTyp string) (interface{}, error) {
typ := getCollectionSerialize(listTyp)
if typ == nil {
return nil, perrors.New("no collection deserialize set as " + listTyp)
}
v := reflect.New(typ).Interface()
collcetionV, ok := v.(JavaCollectionObject)
if !ok {
return nil, perrors.New("collection deserialize err " + listTyp)
}
list, err := d.readTypedListValue(length, "", false)
if err != nil {
return nil, err
}
listInterface := EnsureRawAny(list)
listV, listOk := listInterface.([]interface{})
if !listOk {
return nil, perrors.New("collection deserialize err " + listTyp)
}
collcetionV.Set(listV)
return collcetionV, nil
}
func writeCollectionBegin(length int, typeName string, e *Encoder) error {
var err error
if length <= int(LIST_DIRECT_MAX) {
e.Append([]byte{BC_LIST_DIRECT + byte(length)})
err = e.Encode(typeName)
if err != nil {
return err
}
} else {
e.Append([]byte{BC_LIST_FIXED})
err = e.Encode(typeName)
if err != nil {
return err
}
err = e.Encode(int32(length))
if err != nil {
return nil
}
}
return nil
}