package com.owlike.genson.reflect;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import com.owlike.genson.annotation.JsonProperty;
/**
* This interface is intended to be implemented by classes who want to change the way genson does
* name resolution. The resolved name will be used in the generated stream during serialization and
* injected into constructors/or setters during deserialization. If you can not resolve the name
* just return null. You can have a look at the <a href=
* "http://code.google.com/p/genson/source/browse/src/main/java/com/owlike/genson/reflect/PropertyNameResolver.java"
* >source code</a> for an example.
*
* @author eugen
* @see com.owlike.genson.annotation.JsonProperty JsonProperty
*/
public interface PropertyNameResolver
{
/**
* Resolve the parameter name on position parameterIdx in the constructor fromConstructor.
*
* @param parameterIdx
* @param fromConstructor
* @return the resolved name of the parameter or null
*/
public 1.5.0/docs/api/java/lang/String.html">String resolve
(int parameterIdx, Constructor
<?> fromConstructor
);
/**
* Resolve the name of the parameter with parameterIdx as index in fromMethod method.
*
* @param parameterIdx
* @param fromMethod
* @return the resolved name of the parameter or null
*/
public 1.5.0/docs/api/java/lang/String.html">String resolve
(int parameterIdx,
1.5.0/docs/api/java/lang/reflect/Method.html">Method fromMethod
);
/**
* Resolve the property name from this field.
*
* @param fromField - the field to use for name resolution.
* @return the resolved name or null.
*/
public 1.5.0/docs/api/java/lang/String.html">String resolve
(1.5.0/docs/api/java/lang/reflect/Field.html">Field fromField
);
/**
* Resolve the property name from this method.
*
* @param fromMethod - the method to be used for name resolution.
* @return the resolved name or null.
*/
public 1.5.0/docs/api/java/lang/String.html">String resolve
(1.5.0/docs/api/java/lang/reflect/Method.html">Method fromMethod
);
public static class CompositePropertyNameResolver
implements PropertyNameResolver
{
private List
<PropertyNameResolver
> components
;
public CompositePropertyNameResolver
(List
<PropertyNameResolver
> components
) {
if (components ==
null || components.
isEmpty()) {
throw new 1.5.0/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException(
"The composite resolver must have at least one resolver as component!");
}
this.
components =
new LinkedList
<PropertyNameResolver
>(components
);
}
public CompositePropertyNameResolver add
(PropertyNameResolver...
resolvers) {
// should at the head position so custom resolvers a privileged
components.
addAll(0,
1.5.0/docs/api/java/util/Arrays.html">Arrays.
asList(resolvers
));
return this;
}
public 1.5.0/docs/api/java/lang/String.html">String resolve
(int parameterIdx, Constructor
<?> fromConstructor
) {
1.5.0/docs/api/java/lang/String.html">String resolvedName =
null;
for (Iterator
<PropertyNameResolver
> it = components.
iterator(); resolvedName ==
null
&& it.
hasNext(); ) {
resolvedName = it.
next().
resolve(parameterIdx, fromConstructor
);
}
return resolvedName
;
}
public 1.5.0/docs/api/java/lang/String.html">String resolve
(int parameterIdx,
1.5.0/docs/api/java/lang/reflect/Method.html">Method fromMethod
) {
1.5.0/docs/api/java/lang/String.html">String resolvedName =
null;
for (Iterator
<PropertyNameResolver
> it = components.
iterator(); resolvedName ==
null
&& it.
hasNext(); ) {
resolvedName = it.
next().
resolve(parameterIdx, fromMethod
);
}
return resolvedName
;
}
public 1.5.0/docs/api/java/lang/String.html">String resolve
(1.5.0/docs/api/java/lang/reflect/Field.html">Field fromField
) {
1.5.0/docs/api/java/lang/String.html">String resolvedName =
null;
for (Iterator
<PropertyNameResolver
> it = components.
iterator(); resolvedName ==
null
&& it.
hasNext(); ) {
resolvedName = it.
next().
resolve(fromField
);
}
return resolvedName
;
}
public 1.5.0/docs/api/java/lang/String.html">String resolve
(1.5.0/docs/api/java/lang/reflect/Method.html">Method fromMethod
) {
1.5.0/docs/api/java/lang/String.html">String resolvedName =
null;
for (Iterator
<PropertyNameResolver
> it = components.
iterator(); resolvedName ==
null
&& it.
hasNext(); ) {
resolvedName = it.
next().
resolve(fromMethod
);
}
return resolvedName
;
}
}
public static class ConventionalBeanPropertyNameResolver
implements PropertyNameResolver
{
public 1.5.0/docs/api/java/lang/String.html">String resolve
(int parameterIdx, Constructor
<?> fromConstructor
) {
return null;
}
public 1.5.0/docs/api/java/lang/String.html">String resolve
(1.5.0/docs/api/java/lang/reflect/Field.html">Field fromField
) {
return fromField.
getName();
}
public 1.5.0/docs/api/java/lang/String.html">String resolve
(1.5.0/docs/api/java/lang/reflect/Method.html">Method fromMethod
) {
1.5.0/docs/api/java/lang/String.html">String name = fromMethod.
getName();
int length = -
1;
if (name.
startsWith("get"))
length =
3;
else if (name.
startsWith("is"))
length =
2;
else if (name.
startsWith("set"))
length =
3;
if (length
> -1
&& length
< name.
length()) {
return 1.5.0/docs/api/java/lang/Character.html">Character.
toLowerCase(name.
charAt(length
)) + name.
substring(length + 1
);
} else
return null;
}
public 1.5.0/docs/api/java/lang/String.html">String resolve
(int parameterIdx,
1.5.0/docs/api/java/lang/reflect/Method.html">Method fromMethod
) {
return null;
}
}
/**
* JsonProperty resolver based on @JsonProperty annotation. Can be used on fields, methods and
* constructor parameters.
*/
public static class AnnotationPropertyNameResolver
implements PropertyNameResolver
{
public AnnotationPropertyNameResolver
() {
}
public 1.5.0/docs/api/java/lang/String.html">String resolve
(int parameterIdx, Constructor
<?> fromConstructor
) {
5+0%2Fdocs%2Fapi+Annotation">Annotation[] paramAnns = fromConstructor.
getParameterAnnotations()[parameterIdx
];
1.5.0/docs/api/java/lang/String.html">String name =
null;
for (int j =
0; j
< paramAnns.
length; j++
) {
if (paramAnns
[j
] instanceof JsonProperty
) {
name =
((JsonProperty
) paramAnns
[j
]).
value();
break;
}
}
return "".
equals(name
) ? null : name
;
}
public 1.5.0/docs/api/java/lang/String.html">String resolve
(int parameterIdx,
1.5.0/docs/api/java/lang/reflect/Method.html">Method fromMethod
) {
5+0%2Fdocs%2Fapi+Annotation">Annotation[] anns = fromMethod.
getParameterAnnotations()[parameterIdx
];
1.5.0/docs/api/java/lang/String.html">String name =
null;
for (5+0%2Fdocs%2Fapi+Annotation">Annotation ann : anns
) {
if (ann
instanceof JsonProperty
) {
name =
((JsonProperty
) ann
).
value();
break;
}
}
return "".
equals(name
) ? null : name
;
}
public 1.5.0/docs/api/java/lang/String.html">String resolve
(1.5.0/docs/api/java/lang/reflect/Field.html">Field fromField
) {
return getName
(fromField
);
}
public 1.5.0/docs/api/java/lang/String.html">String resolve
(1.5.0/docs/api/java/lang/reflect/Method.html">Method fromMethod
) {
return getName
(fromMethod
);
}
protected 1.5.0/docs/api/java/lang/String.html">String getName
(1.5.0/docs/api/java/lang/reflect/AnnotatedElement.html">AnnotatedElement annElement
) {
JsonProperty name = annElement.
getAnnotation(JsonProperty.
class);
return name
!=
null && name.
value() !=
null && !name.
value().
isEmpty() ? name.
value()
:
null;
}
}
}