Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit ca75a0f

Browse files
Reject non-standard Number classes during POJO serialization (RTDB) (firebase#262)
* Reject non-standard Number classes during POJO serialization * Removing string concatenation where unnecessary
1 parent f2236e0 commit ca75a0f

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

src/main/java/com/google/firebase/database/utilities/encoding/CustomClassMapper.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,20 @@ private static <T> Object serialize(T obj) {
123123
return ((Number) obj).longValue();
124124
}
125125
return doubleValue;
126-
} else if (obj instanceof Short) {
127-
throw new DatabaseException("Shorts are not supported, please use int or long");
128-
} else if (obj instanceof Byte) {
129-
throw new DatabaseException("Bytes are not supported, please use int or long");
130-
} else {
131-
// Long, Integer
126+
} else if (obj instanceof Long || obj instanceof Integer) {
132127
return obj;
128+
} else {
129+
throw new DatabaseException(
130+
String.format(
131+
"Numbers of type %s are not supported, please use an int, long, float or double",
132+
obj.getClass().getSimpleName()));
133133
}
134134
} else if (obj instanceof String) {
135135
return obj;
136136
} else if (obj instanceof Boolean) {
137137
return obj;
138138
} else if (obj instanceof Character) {
139-
throw new DatabaseException("Characters are not supported, please strings");
139+
throw new DatabaseException("Characters are not supported, please use Strings");
140140
} else if (obj instanceof Map) {
141141
Map<String, Object> result = new HashMap<>();
142142
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) obj).entrySet()) {
@@ -159,11 +159,11 @@ private static <T> Object serialize(T obj) {
159159
return result;
160160
} else {
161161
throw new DatabaseException(
162-
"Serializing Collections is not supported, " + "please use Lists instead");
162+
"Serializing Collections is not supported, please use Lists instead");
163163
}
164164
} else if (obj.getClass().isArray()) {
165165
throw new DatabaseException(
166-
"Serializing Arrays is not supported, please use Lists " + "instead");
166+
"Serializing Arrays is not supported, please use Lists instead");
167167
} else if (obj instanceof Enum) {
168168
return ((Enum<?>) obj).name();
169169
} else {
@@ -185,7 +185,7 @@ private static <T> T deserializeToType(Object obj, Type type) {
185185
throw new DatabaseException("Generic wildcard types are not supported");
186186
} else if (type instanceof GenericArrayType) {
187187
throw new DatabaseException(
188-
"Generic Arrays are not supported, please use Lists " + "instead");
188+
"Generic Arrays are not supported, please use Lists instead");
189189
} else {
190190
throw new IllegalStateException("Unknown type encountered: " + type);
191191
}
@@ -204,7 +204,7 @@ private static <T> T deserializeToClass(Object obj, Class<T> clazz) {
204204
return (T) convertString(obj);
205205
} else if (clazz.isArray()) {
206206
throw new DatabaseException(
207-
"Converting to Arrays is not supported, please use Lists" + "instead");
207+
"Converting to Arrays is not supported, please use Lists instead");
208208
} else if (clazz.getTypeParameters().length > 0) {
209209
throw new DatabaseException(
210210
"Class "
@@ -282,14 +282,9 @@ private static <T> T deserializeToPrimitive(Object obj, Class<T> clazz) {
282282
return (T) convertLong(obj);
283283
} else if (Float.class.isAssignableFrom(clazz) || float.class.isAssignableFrom(clazz)) {
284284
return (T) (Float) convertDouble(obj).floatValue();
285-
} else if (Short.class.isAssignableFrom(clazz) || short.class.isAssignableFrom(clazz)) {
286-
throw new DatabaseException("Deserializing to shorts is not supported");
287-
} else if (Byte.class.isAssignableFrom(clazz) || byte.class.isAssignableFrom(clazz)) {
288-
throw new DatabaseException("Deserializing to bytes is not supported");
289-
} else if (Character.class.isAssignableFrom(clazz) || char.class.isAssignableFrom(clazz)) {
290-
throw new DatabaseException("Deserializing to char is not supported");
291285
} else {
292-
throw new IllegalArgumentException("Unknown primitive type: " + clazz);
286+
throw new DatabaseException(
287+
String.format("Deserializing values to %s is not supported", clazz.getSimpleName()));
293288
}
294289
}
295290

@@ -716,7 +711,7 @@ public T deserialize(Map<String, Object> values, Map<TypeVariable<Class<T>>, Typ
716711
Method setter = this.setters.get(propertyName);
717712
Type[] params = setter.getGenericParameterTypes();
718713
if (params.length != 1) {
719-
throw new IllegalStateException("Setter does not have exactly one " + "parameter");
714+
throw new IllegalStateException("Setter does not have exactly one parameter");
720715
}
721716
Type resolvedType = resolveType(params[0], types);
722717
Object value = CustomClassMapper.deserializeToType(entry.getValue(), resolvedType);

0 commit comments

Comments
 (0)