下面列出了怎么用com.fasterxml.jackson.databind.introspect.AnnotationMap的API类实例代码及写法,或者点击链接到github查看源代码。
protected Optional<String> getName(Method method) {
ObjectMapper objectMapper = context.getObjectMapper();
SerializationConfig serializationConfig = objectMapper.getSerializationConfig();
if (serializationConfig != null && serializationConfig.getPropertyNamingStrategy() != null) {
String name = ClassUtils.getGetterFieldName(method);
Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
AnnotationMap annotationMap = buildAnnotationMap(declaredAnnotations);
int paramsLength = method.getParameterAnnotations().length;
AnnotationMap[] paramAnnotations = new AnnotationMap[paramsLength];
for (int i = 0; i < paramsLength; i++) {
AnnotationMap parameterAnnotationMap = buildAnnotationMap(method.getParameterAnnotations()[i]);
paramAnnotations[i] = parameterAnnotationMap;
}
AnnotatedClass annotatedClass = AnnotatedClassBuilder.build(method.getDeclaringClass(), serializationConfig);
AnnotatedMethod annotatedField = AnnotatedMethodBuilder.build(annotatedClass, method, annotationMap, paramAnnotations);
return Optional.of(serializationConfig.getPropertyNamingStrategy().nameForGetterMethod(serializationConfig, annotatedField, name));
}
return Optional.empty();
}
/**
* Extract name to be used by Katharsis from getter's name. It uses
* {@link ResourceFieldNameTransformer#getMethodName(Method)}, {@link JsonProperty} annotation and
* {@link PropertyNamingStrategy}.
*
* @param method method to extract name
* @return method name
*/
public String getName(Method method) {
String name = getMethodName(method);
if (method.isAnnotationPresent(JsonProperty.class) &&
!"".equals(method.getAnnotation(JsonProperty.class).value())) {
name = method.getAnnotation(JsonProperty.class).value();
} else if (serializationConfig != null && serializationConfig.getPropertyNamingStrategy() != null) {
Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
AnnotationMap annotationMap = buildAnnotationMap(declaredAnnotations);
int paramsLength = method.getParameterAnnotations().length;
AnnotationMap[] paramAnnotations = new AnnotationMap[paramsLength];
for (int i = 0; i < paramsLength; i++) {
AnnotationMap parameterAnnotationMap = buildAnnotationMap(method.getParameterAnnotations()[i]);
paramAnnotations[i] = parameterAnnotationMap;
}
AnnotatedClass annotatedClass = AnnotatedClassBuilder.build(method.getDeclaringClass(), serializationConfig);
AnnotatedMethod annotatedField = AnnotatedMethodBuilder.build(annotatedClass, method, annotationMap, paramAnnotations);
name = serializationConfig.getPropertyNamingStrategy().nameForGetterMethod(serializationConfig, annotatedField, name);
}
return name;
}
/**
* Creates a ConstructorParameterModelProperty which provides a ModelProperty
* for constructor parameters.
*
* @param resolvedParameterType the parameter type
* @param alternateTypeProvider provider for resolving alternatives for the given param type
* @param annotationMap map of annotations for the given parameter. it must contain a @JsonProperty annotation.
*/
public ConstructorParameterModelProperty(
ResolvedType resolvedParameterType,
AlternateTypeProvider alternateTypeProvider,
AnnotationMap annotationMap) {
this.resolvedParameterType = alternateTypeProvider.alternateFor(resolvedParameterType);
if (this.resolvedParameterType == null) {
this.resolvedParameterType = resolvedParameterType;
}
setJsonProperty(annotationMap.get(JsonProperty.class));
setTypeName();
setAllowableValues();
setQualifiedTypeName();
}
public static AnnotatedField build(final AnnotatedClass annotatedClass, final Field field,
final AnnotationMap annotationMap) {
final Constructor<?> constructor = AnnotatedField.class.getConstructors()[0];
return ExceptionUtil.wrapCatchedExceptions(new Callable<AnnotatedField>() {
@Override
public AnnotatedField call() throws Exception {
return buildAnnotatedField(annotatedClass, field, annotationMap, constructor);
}
}, "Exception while building AnnotatedField");
}
private static AnnotatedField buildAnnotatedField(AnnotatedClass annotatedClass, Field field,
AnnotationMap annotationMap, Constructor<?> constructor)
throws IllegalAccessException, InstantiationException, InvocationTargetException {
Class<?> firstParameterType = constructor.getParameterTypes()[0];
PreconditionUtil.verify(firstParameterType == AnnotatedClass.class ||
TypeResolutionContext.class.equals(firstParameterType), CANNOT_FIND_PROPER_CONSTRUCTOR);
return (AnnotatedField) constructor.newInstance(annotatedClass, field, annotationMap);
}
public static AnnotatedMethod build(final AnnotatedClass annotatedClass, final Method method,
final AnnotationMap annotationMap,
final AnnotationMap[] paramAnnotations) {
final Constructor<?> constructor = AnnotatedMethod.class.getConstructors()[0];
return ExceptionUtil.wrapCatchedExceptions(new Callable<AnnotatedMethod>() {
@Override
public AnnotatedMethod call() throws Exception {
return buildAnnotatedField(annotatedClass, method, annotationMap, paramAnnotations, constructor);
}
}, "Exception while building AnnotatedMethod");
}
private static AnnotatedMethod buildAnnotatedField(AnnotatedClass annotatedClass, Method method,
AnnotationMap annotationMap, AnnotationMap[] paramAnnotations,
Constructor<?> constructor)
throws IllegalAccessException, InstantiationException, InvocationTargetException {
Class<?> firstParameterType = constructor.getParameterTypes()[0];
PreconditionUtil.verify(firstParameterType == AnnotatedClass.class || TypeResolutionContext.class.equals(firstParameterType), CANNOT_FIND_PROPER_CONSTRUCTOR);
return (AnnotatedMethod) constructor.newInstance(annotatedClass, method, annotationMap, paramAnnotations);
}
private static AnnotationMap buildAnnotationMap(Annotation[] declaredAnnotations) {
AnnotationMap annotationMap = new AnnotationMap();
for (Annotation annotation : declaredAnnotations) {
annotationMap.add(annotation);
}
return annotationMap;
}
protected Optional<String> getName(Field field) {
ObjectMapper objectMapper = context.getObjectMapper();
SerializationConfig serializationConfig = objectMapper.getSerializationConfig();
if (serializationConfig != null && serializationConfig.getPropertyNamingStrategy() != null) {
AnnotationMap annotationMap = buildAnnotationMap(field.getDeclaredAnnotations());
AnnotatedClass annotatedClass = AnnotatedClassBuilder.build(field.getDeclaringClass(), serializationConfig);
AnnotatedField annotatedField = AnnotatedFieldBuilder.build(annotatedClass, field, annotationMap);
return Optional.of(serializationConfig.getPropertyNamingStrategy().nameForField(serializationConfig, annotatedField, field.getName()));
}
return Optional.empty();
}
public String getName(Field field) {
String name = field.getName();
if (field.isAnnotationPresent(JsonProperty.class) &&
!"".equals(field.getAnnotation(JsonProperty.class).value())) {
name = field.getAnnotation(JsonProperty.class).value();
} else if (serializationConfig != null && serializationConfig.getPropertyNamingStrategy() != null) {
AnnotationMap annotationMap = buildAnnotationMap(field.getDeclaredAnnotations());
AnnotatedClass annotatedClass = AnnotatedClassBuilder.build(field.getDeclaringClass(), serializationConfig);
AnnotatedField annotatedField = AnnotatedFieldBuilder.build(annotatedClass, field, annotationMap);
name = serializationConfig.getPropertyNamingStrategy().nameForField(serializationConfig, annotatedField, name);
}
return name;
}
private static AnnotationMap buildAnnotationMap(Annotation[] declaredAnnotations) {
AnnotationMap annotationMap = new AnnotationMap();
for (Annotation annotation : declaredAnnotations) {
annotationMap.add(annotation);
}
return annotationMap;
}
public static AnnotatedField build(AnnotatedClass annotatedClass, Field field, AnnotationMap annotationMap) {
for(Constructor<?> constructor : AnnotatedField.class.getConstructors()) {
try {
return buildAnnotatedField(annotatedClass, field, annotationMap, constructor);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new InternalException("Exception while building " + AnnotatedField.class.getCanonicalName(), e);
}
}
throw new InternalException(CANNOT_FIND_PROPER_CONSTRUCTOR);
}
private static AnnotatedField buildAnnotatedField(AnnotatedClass annotatedClass, Field field,
AnnotationMap annotationMap, Constructor<?> constructor)
throws IllegalAccessException, InstantiationException, InvocationTargetException {
Class<?> firstParameterType = constructor.getParameterTypes()[0];
if (firstParameterType == AnnotatedClass.class ||
"TypeResolutionContext".equals(firstParameterType.getSimpleName())) {
return (AnnotatedField) constructor.newInstance(annotatedClass, field, annotationMap);
} else {
throw new InternalException(CANNOT_FIND_PROPER_CONSTRUCTOR);
}
}
public static AnnotatedMethod build(AnnotatedClass annotatedClass, Method method, AnnotationMap annotationMap,
AnnotationMap[] paramAnnotations) {
for(Constructor<?> constructor : AnnotatedMethod.class.getConstructors()) {
try {
return buildAnnotatedField(annotatedClass, method, annotationMap, paramAnnotations, constructor);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new InternalException("Exception while building " + AnnotatedMethod.class.getCanonicalName(), e);
}
}
throw new InternalException(CANNOT_FIND_PROPER_CONSTRUCTOR);
}
private static AnnotatedMethod buildAnnotatedField(AnnotatedClass annotatedClass, Method method,
AnnotationMap annotationMap, AnnotationMap[] paramAnnotations,
Constructor<?> constructor)
throws IllegalAccessException, InstantiationException, InvocationTargetException {
Class<?> firstParameterType = constructor.getParameterTypes()[0];
if (firstParameterType == AnnotatedClass.class ||
"TypeResolutionContext".equals(firstParameterType.getSimpleName())) {
return (AnnotatedMethod) constructor.newInstance(annotatedClass, method, annotationMap, paramAnnotations);
} else {
throw new InternalException(CANNOT_FIND_PROPER_CONSTRUCTOR);
}
}
/**
* Creates a collection of ConstructorParameterModelProperty objects from the arguments of the given ResolvedConstructor.
* Only args annotated with @JsonProperty are included. Scala Case Classes are a special case and do not require the annotation.
*
* @param resolvedConstructor the constructor to get
* @param alternateTypeProvider for resolving alternative types for the found arguments
* @return the collection of ConstructorParameterModelProperty objects
*/
public static ImmutableList<ConstructorParameterModelProperty> getModelProperties(ResolvedConstructor resolvedConstructor, AlternateTypeProvider alternateTypeProvider){
Builder<ConstructorParameterModelProperty> listBuilder = new Builder<>();
if(resolvedConstructor.getRawMember().getAnnotation(JsonCreator.class) != null || scala.Product.class.isAssignableFrom(resolvedConstructor.getDeclaringType().getErasedType())){
//constructor for normal classes must be annotated with @JsonCreator. Scala Case Classes are a special case
for(int i=0;i<resolvedConstructor.getArgumentCount();i++){
AnnotationMap annotationMap = annotationMap(resolvedConstructor.getRawMember().getParameterAnnotations()[i]);
ResolvedType parameterType = resolvedConstructor.getArgumentType(i);
if(annotationMap.get(JsonProperty.class) != null){
listBuilder.add(new ConstructorParameterModelProperty(parameterType, alternateTypeProvider, annotationMap));
}
}
}
return listBuilder.build();
}