-
Notifications
You must be signed in to change notification settings - Fork 18
Fix for MalformedParametersException #33 #34
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.fasterxml.jackson.module.paramnames; | ||
|
||
import java.lang.reflect.Executable; | ||
import java.lang.reflect.Parameter; | ||
|
||
class ParameterExtractor { | ||
|
||
public Parameter[] getParameters(Executable executable) { | ||
return executable.getParameters(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,8 @@ | |
import com.fasterxml.jackson.databind.AnnotationIntrospector; | ||
import com.fasterxml.jackson.databind.introspect.*; | ||
|
||
import java.lang.reflect.*; | ||
import java.lang.reflect.MalformedParametersException; | ||
import java.lang.reflect.Parameter; | ||
|
||
/** | ||
* Introspector that uses parameter name information provided by the Java Reflection API additions in Java 8 to | ||
|
@@ -14,15 +15,16 @@ | |
* @see AnnotationIntrospector | ||
* @see Parameter | ||
*/ | ||
class ParameterNamesAnnotationIntrospector extends NopAnnotationIntrospector | ||
{ | ||
class ParameterNamesAnnotationIntrospector extends NopAnnotationIntrospector { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final JsonCreator.Mode creatorBinding; | ||
private final ParameterExtractor parameterExtractor; | ||
|
||
ParameterNamesAnnotationIntrospector(JsonCreator.Mode creatorBinding) { | ||
ParameterNamesAnnotationIntrospector(JsonCreator.Mode creatorBinding, ParameterExtractor parameterExtractor) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For backwards compatibility, would it make sense to keep old one to pass new one with default of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ParameterNamesAnnotationIntrospector isn't part of the module public API since the class is package private. Users that do override this class do so, from the issues reported so far, only for a hot fix, so I'm against keeping the old constructor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lpandzic ah ok. Makes sense. |
||
|
||
this.creatorBinding = creatorBinding; | ||
this.parameterExtractor = parameterExtractor; | ||
} | ||
|
||
@Override | ||
|
@@ -44,26 +46,28 @@ public JsonCreator.Mode findCreatorBinding(Annotated a) { | |
return creatorBinding; | ||
} | ||
|
||
/** | ||
* Returns the parameter name, or {@code null} if it could not be determined. | ||
* | ||
* @param annotatedParameter containing constructor or method from which {@link Parameter} can be extracted | ||
* | ||
* @return name or {@code null} if parameter could not be determined | ||
*/ | ||
private String findParameterName(AnnotatedParameter annotatedParameter) { | ||
|
||
AnnotatedWithParams owner = annotatedParameter.getOwner(); | ||
Parameter[] params; | ||
|
||
if (owner instanceof AnnotatedConstructor) { | ||
params = ((AnnotatedConstructor) owner).getAnnotated().getParameters(); | ||
} else if (owner instanceof AnnotatedMethod) { | ||
params = ((AnnotatedMethod) owner).getAnnotated().getParameters(); | ||
} else { | ||
try { | ||
params = getParameters(annotatedParameter.getOwner()); | ||
} catch (MalformedParametersException e) { | ||
return null; | ||
} | ||
|
||
Parameter p = params[annotatedParameter.getIndex()]; | ||
return p.isNamePresent() ? p.getName() : null; | ||
} | ||
|
||
private Parameter[] getParameters(AnnotatedWithParams owner) { | ||
if (owner instanceof AnnotatedConstructor) { | ||
return parameterExtractor.getParameters(((AnnotatedConstructor) owner).getAnnotated()); | ||
} | ||
|
||
if (owner instanceof AnnotatedMethod) { | ||
return parameterExtractor.getParameters(((AnnotatedMethod) owner).getAnnotated()); | ||
} | ||
|
||
return null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs
scope
oftest
too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, fixed. :)