Skip to content

Commit 5ad9f04

Browse files
XakepSDKXakep_SDK
and
Xakep_SDK
authored
Make some java platform modules optional (#2913)
* Remove java.logging module dependency * Make java.xml module optional (static) * Make java.sql module optional (static) * Make java.desktop module optional (static) Some java environments already don't have java.sql classes Make DateDeserializers weakly depend on java.sql classes Serialization is already weak, as pe [databind#1073] java.desktop classes (java.beans.ConstructorProperties and java.beans.Transient) are used in com.fasterxml.jackson.databind.ext.Java7SupportImpl, if java.beans classes are not present, then jackson will not load Java7SupportImpl Co-authored-by: Xakep_SDK <[email protected]>
1 parent dd8d755 commit 5ad9f04

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/std/DateDeserializers.java

+23-23
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.IOException;
44
import java.lang.reflect.Constructor;
5-
import java.sql.Timestamp;
65
import java.text.*;
76
import java.util.*;
87

@@ -25,46 +24,47 @@
2524
@SuppressWarnings("serial")
2625
public class DateDeserializers
2726
{
28-
private final static HashSet<String> _classNames = new HashSet<String>();
27+
private final static HashSet<String> _utilClasses = new HashSet<String>();
28+
29+
// classes from java.sql module, this module may not be present at runtime
30+
private final static HashSet<String> _sqlClasses = new HashSet<String>();
2931
static {
30-
Class<?>[] numberTypes = new Class<?>[] {
31-
Calendar.class,
32-
GregorianCalendar.class,
33-
java.sql.Date.class,
34-
java.util.Date.class,
35-
Timestamp.class,
36-
};
37-
for (Class<?> cls : numberTypes) {
38-
_classNames.add(cls.getName());
39-
}
32+
_utilClasses.add("java.util.Calendar");
33+
_utilClasses.add("java.util.GregorianCalendar");
34+
_utilClasses.add("java.util.Date");
35+
36+
_sqlClasses.add("java.sql.Date");
37+
_sqlClasses.add("java.sql.Timestamp");
4038
}
4139

4240
public static JsonDeserializer<?> find(Class<?> rawType, String clsName)
4341
{
44-
if (_classNames.contains(clsName)) {
42+
if (_utilClasses.contains(clsName)) {
4543
// Start with the most common type
46-
if (rawType == Calendar.class) {
44+
if (rawType == java.util.Calendar.class) {
4745
return new CalendarDeserializer();
4846
}
4947
if (rawType == java.util.Date.class) {
5048
return DateDeserializer.instance;
5149
}
50+
if (rawType == java.util.GregorianCalendar.class) {
51+
return new CalendarDeserializer(GregorianCalendar.class);
52+
}
53+
}
54+
if (_sqlClasses.contains(clsName)) {
5255
if (rawType == java.sql.Date.class) {
5356
return new SqlDateDeserializer();
5457
}
55-
if (rawType == Timestamp.class) {
58+
if (rawType == java.sql.Timestamp.class) {
5659
return new TimestampDeserializer();
5760
}
58-
if (rawType == GregorianCalendar.class) {
59-
return new CalendarDeserializer(GregorianCalendar.class);
60-
}
6161
}
6262
return null;
6363
}
6464

6565
// @since 2.11
6666
public static boolean hasDeserializerFor(Class<?> rawType) {
67-
return _classNames.contains(rawType.getName());
67+
return _utilClasses.contains(rawType.getName()) || _sqlClasses.contains(rawType.getName());
6868
}
6969

7070
/*
@@ -354,9 +354,9 @@ public java.sql.Date deserialize(JsonParser p, DeserializationContext ctxt) thro
354354
* {@link DeserializationContext#parseDate} that this basic
355355
* deserializer calls.
356356
*/
357-
public static class TimestampDeserializer extends DateBasedDeserializer<Timestamp>
357+
public static class TimestampDeserializer extends DateBasedDeserializer<java.sql.Timestamp>
358358
{
359-
public TimestampDeserializer() { super(Timestamp.class); }
359+
public TimestampDeserializer() { super(java.sql.Timestamp.class); }
360360
public TimestampDeserializer(TimestampDeserializer src, DateFormat df, String formatString) {
361361
super(src, df, formatString);
362362
}
@@ -368,14 +368,14 @@ protected TimestampDeserializer withDateFormat(DateFormat df, String formatStrin
368368

369369
@Override // since 2.12
370370
public Object getEmptyValue(DeserializationContext ctxt) {
371-
return new Timestamp(0L);
371+
return new java.sql.Timestamp(0L);
372372
}
373373

374374
@Override
375375
public java.sql.Timestamp deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
376376
{
377377
Date d = _parseDate(p, ctxt);
378-
return (d == null) ? null : new Timestamp(d.getTime());
378+
return (d == null) ? null : new java.sql.Timestamp(d.getTime());
379379
}
380380
}
381381
}

src/main/java/com/fasterxml/jackson/databind/ext/Java7Handlers.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public abstract class Java7Handlers
2525
} catch (Throwable t) {
2626
// 09-Sep-2019, tatu: Could choose not to log this, but since this is less likely
2727
// to miss (than annotations), do it
28-
java.util.logging.Logger.getLogger(Java7Handlers.class.getName())
29-
.warning("Unable to load JDK7 types (java.nio.file.Path): no Java7 type support added");
28+
// 02-Nov-2020, Xakep_SDK: Remove java.logging module dependency
29+
// java.util.logging.Logger.getLogger(Java7Handlers.class.getName())
30+
// .warning("Unable to load JDK7 types (java.nio.file.Path): no Java7 type support added");
3031
}
3132
IMPL = impl;
3233
}

src/main/java/com/fasterxml/jackson/databind/ext/OptionalHandlerFactory.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.fasterxml.jackson.databind.ext;
22

3-
import java.util.logging.Logger;
4-
import java.util.logging.Level;
5-
63
import com.fasterxml.jackson.databind.*;
74
import com.fasterxml.jackson.databind.deser.Deserializers;
85
import com.fasterxml.jackson.databind.ser.Serializers;
@@ -43,6 +40,8 @@ public class OptionalHandlerFactory implements java.io.Serializable
4340
// // Since 2.7, we will assume DOM classes are always found, both due to JDK 1.6 minimum
4441
// // and because Android (and presumably GAE) have these classes
4542

43+
// // 02-Nov-2020, Xakep_SDK: java.xml module classes may be missing
44+
// // in actual runtime, if module java.xml is not present
4645
private final static Class<?> CLASS_DOM_NODE;
4746
private final static Class<?> CLASS_DOM_DOCUMENT;
4847

@@ -53,8 +52,9 @@ public class OptionalHandlerFactory implements java.io.Serializable
5352
doc = org.w3c.dom.Document.class;
5453
} catch (Throwable e) {
5554
// not optimal but will do
56-
Logger.getLogger(OptionalHandlerFactory.class.getName())
57-
.log(Level.INFO, "Could not load DOM `Node` and/or `Document` classes: no DOM support");
55+
// 02-Nov-2020, Xakep_SDK: Remove java.logging module dependency
56+
// Logger.getLogger(OptionalHandlerFactory.class.getName())
57+
// .log(Level.INFO, "Could not load DOM `Node` and/or `Document` classes: no DOM support");
5858
}
5959
CLASS_DOM_NODE = node;
6060
CLASS_DOM_DOCUMENT = doc;

src/moditect/module-info.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
// Generated 08-Mar-2019 using Moditect maven plugin
22
module com.fasterxml.jackson.databind {
3-
requires java.desktop;
4-
requires java.logging;
3+
// required for
4+
// java.beans.ConstructorProperties
5+
// java.beans.Transient
6+
// support
7+
requires static java.desktop;
58

69
requires transitive com.fasterxml.jackson.annotation;
710
requires transitive com.fasterxml.jackson.core;
811
// these types were suggested as transitive, but aren't actually
912
// exposed externally (only within internal APIs)
10-
requires java.sql;
11-
requires java.xml;
13+
requires static java.sql;
14+
requires static java.xml;
1215

1316
exports com.fasterxml.jackson.databind;
1417
exports com.fasterxml.jackson.databind.annotation;

0 commit comments

Comments
 (0)