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

jnr.ffi.provider.jffi.ConverterMetaData Maven / Gradle / Ivy

package jnr.ffi.provider.jffi;

import jnr.ffi.mapper.FromNativeContext;
import jnr.ffi.mapper.FromNativeConverter;
import jnr.ffi.mapper.ToNativeContext;
import jnr.ffi.mapper.ToNativeConverter;
import jnr.ffi.util.Annotations;

import java.lang.annotation.Annotation;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.reflect.Method;
import java.util.*;

/**
 *
 */
class ConverterMetaData {
    private static volatile Reference> cacheReference;
    final Collection classAnnotations;
    final Collection toNativeMethodAnnotations, fromNativeMethodAnnotations, nativeTypeMethodAnnotations;
    final Collection toNativeAnnotations, fromNativeAnnotations;

    ConverterMetaData(Class converterClass, Class nativeType) {
        classAnnotations = Annotations.sortedAnnotationCollection(converterClass.getAnnotations());
        nativeTypeMethodAnnotations = getConverterMethodAnnotations(converterClass, "nativeType");
        fromNativeMethodAnnotations = getConverterMethodAnnotations(converterClass, "fromNative", nativeType, FromNativeContext.class);
        toNativeMethodAnnotations = getConverterMethodAnnotations(converterClass, "toNative", nativeType, ToNativeContext.class);;
        toNativeAnnotations = Annotations.mergeAnnotations(classAnnotations, toNativeMethodAnnotations, nativeTypeMethodAnnotations);
        fromNativeAnnotations = Annotations.mergeAnnotations(classAnnotations, fromNativeMethodAnnotations, nativeTypeMethodAnnotations);
    }

    
    private static Collection getToNativeMethodAnnotations(Class converterClass, Class resultClass) {
        try {
            final Method baseMethod = converterClass.getMethod("toNative", Object.class, ToNativeContext.class);
            for (Method m : converterClass.getMethods()) {
                if (!m.getName().equals("toNative")) {
                    continue;
                }
                if (!resultClass.isAssignableFrom(m.getReturnType())) {
                    continue;
                }

                Class[] methodParameterTypes = m.getParameterTypes();
                if (methodParameterTypes.length != 2 || !methodParameterTypes[1].isAssignableFrom(ToNativeContext.class)) {
                    continue;
                }

                return Annotations.mergeAnnotations(Annotations.sortedAnnotationCollection(m.getAnnotations()), Annotations.sortedAnnotationCollection(baseMethod.getAnnotations()));
            }

            return Annotations.EMPTY_ANNOTATIONS;
        } catch (SecurityException se) {
            return Annotations.EMPTY_ANNOTATIONS;
        } catch (NoSuchMethodException ignored) {
            return Annotations.EMPTY_ANNOTATIONS;
        }
    }

    @SuppressWarnings("unchecked")
    private static Collection getConverterMethodAnnotations(Class converterClass, String methodName, Class... parameterClasses) {
        try {
            return Annotations.sortedAnnotationCollection(converterClass.getMethod(methodName).getAnnotations());
        } catch (NoSuchMethodException ignored) {
            return Annotations.EMPTY_ANNOTATIONS;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
    
    private static ConverterMetaData getMetaData(Class converterClass, Class nativeType) {
        Map cache = cacheReference != null ? cacheReference.get() : null;
        ConverterMetaData metaData;
        if (cache != null && (metaData = cache.get(converterClass)) != null) {
            return metaData;
        }

        return addMetaData(converterClass, nativeType);
    }

    private static synchronized ConverterMetaData addMetaData(Class converterClass, Class nativeType) {
        Map cache = cacheReference != null ? cacheReference.get() : null;
        ConverterMetaData metaData;
        if (cache != null && (metaData = cache.get(converterClass)) != null) {
            return metaData;
        }

        Map m = new HashMap(cache != null ? cache : Collections.EMPTY_MAP);
        m.put(converterClass, metaData = new ConverterMetaData(converterClass, nativeType));
        cacheReference = new SoftReference>(cache = new IdentityHashMap(m));

        return metaData;
    }

    static Collection getAnnotations(ToNativeConverter toNativeConverter) {
        return toNativeConverter != null
                ? getMetaData(toNativeConverter.getClass(), toNativeConverter.nativeType()).toNativeAnnotations
                : Annotations.EMPTY_ANNOTATIONS;
    }

    static Collection getAnnotations(FromNativeConverter fromNativeConverter) {
        return fromNativeConverter != null
                ? getMetaData(fromNativeConverter.getClass(), fromNativeConverter.nativeType()).fromNativeAnnotations
                : Annotations.EMPTY_ANNOTATIONS;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy