forked from liferay/liferay-portal
/
PortalBeanObjectFactory.java
176 lines (127 loc) · 4.49 KB
/
PortalBeanObjectFactory.java
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.json;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.util.PropsValues;
import flexjson.BeanAnalyzer;
import flexjson.BeanProperty;
import flexjson.JSONException;
import flexjson.ObjectBinder;
import flexjson.factories.BeanObjectFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Michael C. Han
*/
public class PortalBeanObjectFactory extends BeanObjectFactory {
@Override
public Object instantiate(
ObjectBinder objectBinder, Object value, Type targetType,
@SuppressWarnings("rawtypes") Class targetClass) {
if (_safeMode) {
Map<Object, Object> target = new HashMap<Object, Object>();
target.put("class", targetClass.getName());
Map<?, ?> values = (Map<?, ?>)value;
return objectBinder.bindIntoMap(values, target, null, null);
}
String targetClassName = targetClass.getName();
if (targetClassName.contains("com.liferay") &&
targetClassName.contains("Util")) {
throw new JSONException(
"Not instantiating " + targetClass.getName() + " at " +
objectBinder.getCurrentPath());
}
try {
Object target = instantiate(targetClass);
Map<?, ?> values = (Map<?, ?>)value;
if (PropsValues.JSON_DESERIALIZER_STRICT_MODE) {
removeInvalidFields(values, targetClass);
}
return objectBinder.bindIntoObject(values, target, targetType);
}
catch (Exception e) {
throw new JSONException(
"Unable to instantiate " + targetClass.getName() + " at " +
objectBinder.getCurrentPath(),
e);
}
}
public void setSafeMode(boolean safeMode) {
_safeMode = safeMode;
}
protected Map<String, Field> getDeclaredFields(
@SuppressWarnings("rawtypes") Class targetClass) {
Map<String, Field> declaredFieldsMap = _declaredFields.get(targetClass);
if (declaredFieldsMap == null) {
declaredFieldsMap = new ConcurrentHashMap<String, Field>();
Field[] declaredFields = targetClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
String fieldName = declaredField.getName();
if (fieldName.startsWith(StringPool.UNDERLINE)) {
fieldName = fieldName.substring(1);
}
declaredFieldsMap.put(fieldName, declaredField);
}
_declaredFields.put(targetClass, declaredFieldsMap);
}
return declaredFieldsMap;
}
protected boolean isValidField(
Map<String, Field> declaredFields, String beanName) {
Field declaredField = declaredFields.get(beanName);
if (declaredField == null) {
return false;
}
int modifier = declaredField.getModifiers();
if (Modifier.isStatic(modifier)) {
return false;
}
return true;
}
protected void removeInvalidFields(Map<?, ?> values, Class<?> targetClass) {
Map<String, Field> declaredFields = getDeclaredFields(targetClass);
BeanAnalyzer beanAnalyzer = BeanAnalyzer.analyze(targetClass);
for (BeanProperty beanProperty : beanAnalyzer.getProperties()) {
String beanName = beanProperty.getName();
String capitalizedBeanName = null;
Object beanValue = values.get(beanName);
if (beanValue == null) {
capitalizedBeanName = Character.toUpperCase(
beanName.charAt(0)) + beanName.substring(1);
beanValue = values.get(capitalizedBeanName);
}
if ((beanValue != null) &&
!isValidField(declaredFields, beanName)) {
if (capitalizedBeanName != null) {
beanName = capitalizedBeanName;
}
if (_log.isDebugEnabled()) {
_log.debug("Removing non-JavaBeans field " + beanName);
}
values.remove(beanName);
}
}
}
private static Log _log = LogFactoryUtil.getLog(
PortalBeanObjectFactory.class);
private Map<Class<?>, Map<String, Field>> _declaredFields =
new ConcurrentHashMap<Class<?>, Map<String, Field>>();
private boolean _safeMode;
}