FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cloudstack/server/src/com/cloud/api/ApiGsonHelper.java at vmsync · kwanggithub/cloudstack · GitHub
This repository was archived by the owner on Jan 15, 2020. It is now read-only.
kwanggithub
/
cloudstack
Public archive
forked from
apache/cloudstack
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cloudstack
/
server
/
src
/
com
/
cloud
/
api
/
ApiGsonHelper.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
155 lines (133 loc) · 6.24 KB
Breadcrumbs
cloudstack
/
server
/
src
/
com
/
cloud
/
api
/
ApiGsonHelper.java
Copy path
File metadata and controls
155 lines (133 loc) · 6.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// 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
com
.
cloud
.
api
;
import
java
.
lang
.
reflect
.
Field
;
import
java
.
lang
.
reflect
.
InvocationTargetException
;
import
java
.
lang
.
reflect
.
Method
;
import
java
.
lang
.
reflect
.
Modifier
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Date
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
import
org
.
apache
.
log4j
.
Logger
;
import
com
.
google
.
gson
.
GsonBuilder
;
import
org
.
apache
.
cloudstack
.
api
.
ResponseObject
;
import
org
.
apache
.
cloudstack
.
utils
.
GsonUtils
;
import
com
.
cloud
.
serializer
.
Param
;
import
com
.
cloud
.
utils
.
DateUtil
;
import
com
.
cloud
.
utils
.
Pair
;
public
class
ApiGsonHelper
{
private
static
final
Logger
s_logger
=
Logger
.
getLogger
(
ApiGsonHelper
.
class
);
private
static
final
GsonBuilder
s_gBuilder
;
static
{
s_gBuilder
=
new
GsonBuilder
().
setDateFormat
(
"yyyy-MM-dd'T'HH:mm:ssZ"
);
s_gBuilder
.
setVersion
(
1.3
);
s_gBuilder
.
registerTypeAdapter
(
ResponseObject
.
class
,
new
ResponseObjectTypeAdapter
());
s_gBuilder
.
registerTypeAdapter
(
Map
.
class
,
new
GsonUtils
.
StringMapTypeAdapter
());
}
public
static
GsonBuilder
getBuilder
() {
return
s_gBuilder
;
}
public
static
List
<
Pair
<
String
,
Object
>>
toPairList
(
Object
o
,
String
name
) {
List
<
Pair
<
String
,
Object
>>
l
=
new
ArrayList
<
Pair
<
String
,
Object
>>();
return
appendPairList
(
l
,
o
,
name
);
}
public
static
List
<
Pair
<
String
,
Object
>>
appendPairList
(
List
<
Pair
<
String
,
Object
>>
l
,
Object
o
,
String
name
) {
if
(
o
!=
null
) {
Class
<?>
clz
=
o
.
getClass
();
if
(
clz
.
isPrimitive
() ||
clz
.
getSuperclass
() ==
Number
.
class
||
clz
==
String
.
class
||
clz
==
Date
.
class
) {
l
.
add
(
new
Pair
<
String
,
Object
>(
name
,
o
.
toString
()));
return
l
;
}
for
(
Field
f
:
clz
.
getDeclaredFields
()) {
if
((
f
.
getModifiers
() &
Modifier
.
STATIC
) !=
0
) {
continue
;
}
Param
param
=
f
.
getAnnotation
(
Param
.
class
);
if
(
param
==
null
) {
continue
;
}
String
propName
=
f
.
getName
();
if
(!
param
.
propName
().
isEmpty
()) {
propName
=
param
.
propName
();
}
String
paramName
=
param
.
name
();
if
(
paramName
.
isEmpty
()) {
paramName
=
propName
;
}
Method
method
=
getGetMethod
(
o
,
propName
);
if
(
method
!=
null
) {
try
{
Object
fieldValue
=
method
.
invoke
(
o
);
if
(
fieldValue
!=
null
) {
if
(
f
.
getType
() ==
Date
.
class
) {
l
.
add
(
new
Pair
<
String
,
Object
>(
paramName
,
DateUtil
.
getOutputString
((
Date
)
fieldValue
)));
}
else
{
l
.
add
(
new
Pair
<
String
,
Object
>(
paramName
,
fieldValue
.
toString
()));
}
}
//else
// l.add(new Pair<String, Object>(paramName, ""));
}
catch
(
IllegalArgumentException
e
) {
s_logger
.
error
(
"Illegal argument exception when calling POJO "
+
o
.
getClass
().
getName
() +
" get method for property: "
+
propName
);
}
catch
(
IllegalAccessException
e
) {
s_logger
.
error
(
"Illegal access exception when calling POJO "
+
o
.
getClass
().
getName
() +
" get method for property: "
+
propName
);
}
catch
(
InvocationTargetException
e
) {
s_logger
.
error
(
"Invocation target exception when calling POJO "
+
o
.
getClass
().
getName
() +
" get method for property: "
+
propName
);
}
}
}
}
return
l
;
}
private
static
Method
getGetMethod
(
Object
o
,
String
propName
) {
Method
method
=
null
;
String
methodName
=
getGetMethodName
(
"get"
,
propName
);
try
{
method
=
o
.
getClass
().
getMethod
(
methodName
);
}
catch
(
SecurityException
e1
) {
s_logger
.
error
(
"Security exception in getting POJO "
+
o
.
getClass
().
getName
() +
" get method for property: "
+
propName
);
}
catch
(
NoSuchMethodException
e1
) {
if
(
s_logger
.
isTraceEnabled
()) {
s_logger
.
trace
(
"POJO "
+
o
.
getClass
().
getName
() +
" does not have "
+
methodName
+
"() method for property: "
+
propName
+
", will check is-prefixed method to see if it is boolean property"
);
}
}
if
(
method
!=
null
) {
return
method
;
}
methodName
=
getGetMethodName
(
"is"
,
propName
);
try
{
method
=
o
.
getClass
().
getMethod
(
methodName
);
}
catch
(
SecurityException
e1
) {
s_logger
.
error
(
"Security exception in getting POJO "
+
o
.
getClass
().
getName
() +
" get method for property: "
+
propName
);
}
catch
(
NoSuchMethodException
e1
) {
s_logger
.
warn
(
"POJO "
+
o
.
getClass
().
getName
() +
" does not have "
+
methodName
+
"() method for property: "
+
propName
);
}
return
method
;
}
private
static
String
getGetMethodName
(
String
prefix
,
String
fieldName
) {
StringBuffer
sb
=
new
StringBuffer
(
prefix
);
if
(
fieldName
.
length
() >=
prefix
.
length
() &&
fieldName
.
substring
(
0
,
prefix
.
length
()).
equals
(
prefix
)) {
return
fieldName
;
}
else
{
sb
.
append
(
fieldName
.
substring
(
0
,
1
).
toUpperCase
());
sb
.
append
(
fieldName
.
substring
(
1
));
}
return
sb
.
toString
();
}
}
Back
|
FazBrowse Home
|
New Git URL