-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbean_definition.go
130 lines (118 loc) · 3.75 KB
/
bean_definition.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
package pp_ioc
import (
"github.com/pkg/errors"
ps "github.com/wlad031/pp-properties/property_source"
"reflect"
"strings"
)
type beanDefinition struct {
key *bindKey
dependencies map[uint16]*dependency
priority int
paramTypes []reflect.Type
scope BeanScope
factory *beanFactory
graphIndex int
_bean *bean // Do not use it directly!
}
func (bd *beanDefinition) createBean(params []reflect.Value) (*bean, error) {
switch bd.scope {
case ScopeSingleton:
{
if bd._bean != nil {
return bd._bean, nil
} else {
instance, e := bd.factory.call(params)
if e != nil {
return nil, e
}
bd._bean = &bean{
definition: bd,
instance: instance,
}
return bd._bean, nil
}
}
case ScopePrototype:
{
instance, e := bd.factory.call(params)
if e != nil {
return nil, e
}
return &bean{
definition: bd,
instance: instance,
}, nil
}
default:
return nil, errors.New("Unknown bean scope " + bd.scope.String())
}
}
func (bd *beanDefinition) isSuitableForDependencyByQualifier(dependency *dependency) bool {
var isSuitableByName = false
for _, name := range bd.key.qualifiers {
if name == dependency.qualifier {
isSuitableByName = true
break
}
}
if !isSuitableByName {
return false
}
return true
}
// TODO: refactor this function
func (bd *beanDefinition, ) isSuitableForDependencyByType(dependency *dependency) bool {
dependencyType := dependency.type_
if dependencyType.Kind() == reflect.Ptr {
dependencyType = dependencyType.Elem()
}
if dependencyType.Kind() == reflect.Struct {
if bd.key.type_.Kind() == reflect.Struct {
return bd.key.type_ == dependencyType
}
if bd.key.type_.Kind() == reflect.Ptr {
if bd.key.type_.Elem().Kind() == reflect.Struct {
return bd.key.type_.Elem() == dependencyType
}
}
}
if dependencyType.Kind() == reflect.Interface {
if bd.key.type_.Kind() == reflect.Struct {
return bd.key.type_.Implements(dependencyType)
}
if bd.key.type_.Kind() == reflect.Interface { // TODO: may not work correctly
return bd.key.type_.Implements(dependencyType)
}
if bd.key.type_.Kind() == reflect.Ptr {
if bd.key.type_.Elem().Kind() == reflect.Struct {
return bd.key.type_.Elem().Implements(dependencyType)
}
if bd.key.type_.Elem().Kind() == reflect.Interface { // TODO: may not work correctly
return bd.key.type_.Elem().Implements(dependencyType)
}
}
}
return false
}
func (bd *beanDefinition) updateGraphIndex(newGraphIndex int) {
bd.graphIndex = newGraphIndex
}
func (bd *beanDefinition) isPropertySource() bool {
return bd.key.type_.Implements(reflect.TypeOf((*ps.PropertySource)(nil)).Elem())
}
func (bd *beanDefinition) isPostProcessor() bool {
return bd.key.type_.Implements(reflect.TypeOf((*PostProcessor)(nil)).Elem())
}
func (bd *beanDefinition) shortString() string {
return "BeanDef{" + bd.key.String() + "}"
}
func (bd *beanDefinition) String() string {
var depStrings []string
for _, dep := range bd.dependencies {
depStrings = append(depStrings, dep.String())
}
return "BeanDef{" + bd.key.String() + ":" +
bd.scope.String() + ":[" +
strings.Join(depStrings, ",") + "]}"
}