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

com.javaetmoi.javabean.util.GenericsHelper Maven / Gradle / Ivy

Go to download

The JavaBeanMarshaller class is responsible for serializing an object (graph) to it's Java code.

There is a newer version: 1.0.3
Show newest version
package com.javaetmoi.javabean.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class GenericsHelper {

    private GenericsHelper() {
        // Helper class
    }

    public static Type findParameterizedType(PropertyDescriptor propertyDescriptor) {
        Type[] types = findParameterizedTypes(propertyDescriptor);
        if ((types != null) && (types.length > 0)) {
            return types[0];
        }
        return null;
    }

    public static Type[] findParameterizedTypes(PropertyDescriptor propertyDescriptor) {
        if (propertyDescriptor == null){
            return null;
        }
        Method readMethod = propertyDescriptor.getReadMethod();
        Type returnType = readMethod.getGenericReturnType();
        if (returnType instanceof ParameterizedType) {
            ParameterizedType paramType = (ParameterizedType) returnType;
            Type[] argTypes = paramType.getActualTypeArguments();
            if (argTypes.length > 0) {
                return argTypes;
            }
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy