From 326bb8673515f37aefe7a82b31ddc9f9d7c82142 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 25 Jan 2022 19:45:20 -0800 Subject: [PATCH] Use Method.getParameterCount() where possible --- release-notes/CREDITS-2.x | 4 ++++ release-notes/VERSION-2.x | 2 ++ .../jackson/databind/deser/BeanDeserializerBase.java | 4 ++-- .../jackson/databind/introspect/AnnotatedConstructor.java | 3 +-- .../databind/introspect/AnnotatedCreatorCollector.java | 2 +- .../jackson/databind/introspect/AnnotatedMethodCollector.java | 3 +-- .../java/com/fasterxml/jackson/databind/util/ClassUtil.java | 2 +- 7 files changed, 12 insertions(+), 8 deletions(-) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 58dabf32a7..f1a25ea01b 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -1419,3 +1419,7 @@ Krishna Ghimire (Krishnaghimir@github) * Reported #3369: Deserialization ignores other Object fields when Object or Array value used for enum (2.13.2) + +Christoph Dreis (dreis2211@github) + * Suggested #3293: Use Method.getParameterCount() where possible + (2.13.2) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 516724ae38..2d4c3d3829 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -6,6 +6,8 @@ Project: jackson-databind 2.13.2 (not yet released) +#3293: Use Method.getParameterCount() where possible + (suggested by Christoph D) #3344: `Set.of()` (Java 9) cannot be deserialized with polymorphic handling (reported by Sam K) #3368: `SnakeCaseStrategy` causes unexpected `MismatchedInputException` during diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java b/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java index e14fb0bfaa..87b78a20a6 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java @@ -961,8 +961,8 @@ protected SettableBeanProperty _resolveInnerClassValuedProperty(DeserializationC // and is inner class of the bean class... if ((enclosing != null) && (enclosing == _beanType.getRawClass())) { for (Constructor ctor : valueClass.getConstructors()) { - Class[] paramTypes = ctor.getParameterTypes(); - if (paramTypes.length == 1) { + if (ctor.getParameterCount() == 1) { + Class[] paramTypes = ctor.getParameterTypes(); if (enclosing.equals(paramTypes[0])) { if (ctxt.canOverrideAccessModifiers()) { ClassUtil.checkAndFixAccess(ctor, ctxt.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedConstructor.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedConstructor.java index 5b29bd4963..249645ef1c 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedConstructor.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedConstructor.java @@ -169,8 +169,7 @@ public Object getValue(Object pojo) @Override public String toString() { - // 03-Nov-2020 ckozak: This can use _constructor.getParameterCount() once java 8 is required. - final int argCount = _constructor.getParameterTypes().length; + final int argCount = _constructor.getParameterCount(); return String.format("[constructor for %s (%d arg%s), annotations: %s", ClassUtil.nameOf(_constructor.getDeclaringClass()), argCount, (argCount == 1) ? "" : "s", _annotations); diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java index 632f48970e..26893db538 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java @@ -352,7 +352,7 @@ protected AnnotatedConstructor constructNonDefaultConstructor(ClassUtil.Ctor cto protected AnnotatedMethod constructFactoryCreator(Method m, TypeResolutionContext typeResCtxt, Method mixin) { - final int paramCount = m.getParameterTypes().length; + final int paramCount = m.getParameterCount(); if (_intr == null) { // when annotation processing is disabled return new AnnotatedMethod(typeResCtxt, m, _emptyAnnotationMap(), _emptyAnnotationMaps(paramCount)); diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodCollector.java index 29db7051bb..c092f3972a 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodCollector.java @@ -184,8 +184,7 @@ private static boolean _isIncludableMemberMethod(Method m) } // also, for now we have no use for methods with more than 2 arguments: // (2 argument methods for "any setter", fwtw) - int pcount = m.getParameterTypes().length; - return (pcount <= 2); + return (m.getParameterCount() <= 2); } private final static class MethodBuilder { diff --git a/src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java b/src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java index f8d7ab0e3f..1756c2ca01 100644 --- a/src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java +++ b/src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java @@ -1448,7 +1448,7 @@ public Constructor getConstructor() { public int getParamCount() { int c = _paramCount; if (c < 0) { - c = _ctor.getParameterTypes().length; + c = _ctor.getParameterCount(); _paramCount = c; } return c;