Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class Main {
    private static final String TYPE_NAME_PREFIX = "class ";

    static boolean isGenericTypeAssignableFrom(ParameterizedType type, Class<?> clazz) {
        Type[] actualTypes = type.getActualTypeArguments();
        for (Type actualType : actualTypes) {
            Class<?> aClazz = null;
            try {
                aClazz = getClass(actualType);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            if (aClazz != null && clazz.isAssignableFrom(aClazz)) {
                return true;
            }
        }
        return false;
    }

    private static Class<?> getClass(Type type) throws ClassNotFoundException {
        String className = getClassName(type);
        if (className == null || className.isEmpty()) {
            return null;
        }
        return Class.forName(className);
    }

    private static String getClassName(Type type) {
        if (type == null) {
            return "";
        }
        String clazzName = type.toString();
        if (clazzName.startsWith(TYPE_NAME_PREFIX)) {
            clazzName = clazzName.replace(TYPE_NAME_PREFIX, "");
        }
        return clazzName;
    }
}