com.fasterxml.jackson.databind.introspect.AnnotationMap Java Examples

The following examples show how to use com.fasterxml.jackson.databind.introspect.AnnotationMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JacksonResourceFieldInformationProvider.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #2
Source File: ResourceFieldNameTransformer.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: ConstructorParameterModelProperty.java    From chassis with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #4
Source File: AnnotatedFieldBuilder.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example #5
Source File: AnnotatedFieldBuilder.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #6
Source File: AnnotatedMethodBuilder.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example #7
Source File: AnnotatedMethodBuilder.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #8
Source File: JacksonResourceFieldInformationProvider.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
private static AnnotationMap buildAnnotationMap(Annotation[] declaredAnnotations) {
	AnnotationMap annotationMap = new AnnotationMap();
	for (Annotation annotation : declaredAnnotations) {
		annotationMap.add(annotation);
	}
	return annotationMap;
}
 
Example #9
Source File: JacksonResourceFieldInformationProvider.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #10
Source File: ResourceFieldNameTransformer.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
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;
    }
 
Example #11
Source File: ResourceFieldNameTransformer.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
private static AnnotationMap buildAnnotationMap(Annotation[] declaredAnnotations) {
    AnnotationMap annotationMap = new AnnotationMap();
    for (Annotation annotation : declaredAnnotations) {
        annotationMap.add(annotation);
    }
    return annotationMap;
}
 
Example #12
Source File: AnnotatedFieldBuilder.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #13
Source File: AnnotatedFieldBuilder.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #14
Source File: AnnotatedMethodBuilder.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #15
Source File: AnnotatedMethodBuilder.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #16
Source File: ConstructorParameterModelProperty.java    From chassis with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}