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.assertTrue(CANNOT_FIND_PROPER_CONSTRUCTOR, firstParameterType == AnnotatedClass.class || TypeResolutionContext.class.equals(firstParameterType)); 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.assertTrue(CANNOT_FIND_PROPER_CONSTRUCTOR, firstParameterType == AnnotatedClass.class || TypeResolutionContext.class.equals(firstParameterType)); 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; }
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(); }
private AbstractType getTSTypeForClass(AnnotatedMember member) { TypeBindings bindings = new TypeBindings(TypeFactory.defaultInstance(), member.getDeclaringClass()); BeanProperty prop = new BeanProperty.Std(member.getName(), member.getType(bindings), NO_NAME, new AnnotationMap(), member, false); try { return getTSTypeForProperty(prop); } catch (JsonMappingException e) { throw new RuntimeException(e); } }
private void addMethod(Method method) { FunctionType function = new FunctionType(); AnnotatedMethod annotMethod = new AnnotatedMethod(null, method, new AnnotationMap(), null); function.setResultType(getTSTypeForClass(annotMethod)); for (int i = 0; i < annotMethod.getParameterCount(); i++) { AnnotatedParameter param = annotMethod.getParameter(i); String name = "param" + i; function.getParameters().put(name, getTSTypeForClass(param)); } this.type.getMethods().put(method.getName(), function); }
private static boolean hasAnnotation(AnnotationMap annotationMap) { if (annotationMap != null) { return hasAnnotation(annotationMap.annotations()); } return false; }
private static String annotationsItToStr(AnnotationMap annotationMap) { return annotationMap != null ? annotationsItToStr(annotationMap.annotations()) : ""; }