All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.katharsis.resource.field.AnnotatedMethodBuilder Maven / Gradle / Ivy

There is a newer version: 2.6.3
Show newest version
package io.katharsis.resource.field;

import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.AnnotationMap;
import io.katharsis.errorhandling.exception.InternalException;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Since a
 * change there was a change in the constructor parameters of {@link AnnotatedMethod} class. This class provides an
 * interface to allow Katharsis work with Jackson version that does and does not have this commit applied.
 */
public class AnnotatedMethodBuilder {
    private static final String CANNOT_FIND_PROPER_CONSTRUCTOR = "Couldn't find proper AnnotatedField 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 e) {
                throw new InternalException("Exception while building " + AnnotatedMethod.class.getCanonicalName(), e);
            } catch (InstantiationException e) {
                throw new InternalException("Exception while building " + AnnotatedMethod.class.getCanonicalName(), e);
            } catch (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);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy