Java Reflection Method Name getMethodExceptionType(Class cls, String methodName, Class[] argTypes, int methodPosition, int classPosition, Class defaultType)

Here you can find the source of getMethodExceptionType(Class cls, String methodName, Class[] argTypes, int methodPosition, int classPosition, Class defaultType)

Description

get Method Exception Type

License

Apache License

Declaration

public static Class<?> getMethodExceptionType(Class<?> cls, String methodName, Class<?>[] argTypes,
            int methodPosition, int classPosition, Class<?> defaultType) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.lang.reflect.Array;

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

public class Main {
    public static Class<?> getMethodExceptionType(Class<?> cls, String methodName, Class<?>[] argTypes,
            int methodPosition, int classPosition, Class<?> defaultType) {
        try {//from  ww  w.  j av a2  s .  co m

            Method m = cls.getMethod(methodName, argTypes);
            return getMethodExceptionType(cls, m, methodPosition, classPosition, defaultType);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static Class<?> getMethodExceptionType(Class<?> cls, Method m, int methodPosition, int classPosition,
            Class<?> defaultType) {
        Class<?> fcls = null;
        Type[] types = m.getGenericExceptionTypes();
        if (types.length <= methodPosition) {
            return defaultType;
        }
        Type tp = types[methodPosition];
        if (tp instanceof Class<?>) {
            return (Class<?>) tp;
        } else if (tp instanceof TypeVariable<?>) {
            TypeVariable<?> tv = (TypeVariable<?>) tp;
            String name = tv.getName();
            Class<?> declClass = m.getDeclaringClass();
            fcls = getClassGenericTypeFromSuperClass(cls, declClass, name, null);
            if (fcls != null) {
                return fcls;
            }
        }
        fcls = getClassGenericTypeFromSuperClass(cls, classPosition, null);
        if (fcls != null) {
            return fcls;
        }
        printType("", tp);
        return defaultType;
    }

    public static Class<?> getClassGenericTypeFromSuperClass(Class<?> cls, int typePosition,
            Class<?> defaultClass) {
        Type t = cls.getGenericSuperclass();
        if (t instanceof ParameterizedType) {
            ParameterizedType ptt = (ParameterizedType) t;
            Type optt = ptt.getOwnerType();

            Type[] pts = ptt.getActualTypeArguments();
            if (pts.length <= typePosition) {
                return defaultClass;
            }
            Type pt = pts[typePosition];
            Class<?> fcls = getClass(pt);
            if (pt instanceof Class<?>) {
                return (Class<?>) pt;
            }

        }
        return defaultClass;
    }

    public static Class<?> getClassGenericTypeFromSuperClass(Class<?> rtClass, Class<?> declClass, String typeName,
            Class<?> defaultClass) {
        // printGenericClass(rtClass);
        TypeVariable<?>[] orgParamTypes = rtClass.getTypeParameters();
        Class<?> cls = rtClass.getSuperclass();
        Type gsc = rtClass.getGenericSuperclass();

        Type[] ifaces = cls.getGenericInterfaces();
        if (gsc instanceof ParameterizedType) {
            ParameterizedType tp = (ParameterizedType) gsc;
            Type rt = tp.getRawType();
            Type owner = tp.getOwnerType();
            if (rt instanceof Class<?>) {
                Class<?> rawClass = (Class<?>) rt;
                int paramPos = findParameterPosition(rawClass, typeName);
                if (paramPos == -1) {
                    return defaultClass;
                }
                Type pp = tp.getActualTypeArguments()[paramPos];
                return getClass(pp);
            }
        }
        return defaultClass;
    }

    public static void printType(Type type) {
        printType("", type);
    }

    public static void printType(String indent, Type type) {
        StringBuilder sb = new StringBuilder();
        printType(indent, type, sb);
        System.out.println(sb.toString());
    }

    public static void printType(String indent, Type type, StringBuilder sb) {
        if (type instanceof Class<?>) {
            sb.append(indent + "Class: " + type.toString() + "\n");
        } else if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            sb.append(indent + "ParameterizedType: " + pt.toString() + "\n").append(indent + " args:\n");
            for (Type t : pt.getActualTypeArguments()) {
                printType(indent + "  ", t, sb);
            }
            sb.append(indent + " rawType:\n");
            Type rp = pt.getRawType();
            printType(indent + "  ", rp, sb);
            Type owner = pt.getOwnerType();
            if (owner != null) {
                sb.append(indent + " owner:\n");
                printType(indent + "  ", owner, sb);
            }
        } else if (type instanceof TypeVariable) {
            TypeVariable tp = (TypeVariable) type;
            sb.append(indent + "TypeVariable: " + tp.toString() + "\n").append(indent + "  Bounds:\n");
            GenericDeclaration gd = tp.getGenericDeclaration();
            TypeVariable<?>[] gdtv = gd.getTypeParameters();
            // tp.get
            for (Type b : tp.getBounds()) {
                printType(indent + "    ", b, sb);
            }
            // cause recursion
            // GenericDeclaration gd = tp.getGenericDeclaration();
            // sb.append(indent + " GenericDeclaration: " + gd + "\n")//
            // .append(indent + " types:\n");
            // for (TypeVariable tv : gd.getTypeParameters()) {
            // printType(indent + " ", tv, sb);
            // }
        } else {
            sb.append(indent + "Unknown: " + type.toString() + "\n");
        }
    }

    public static Class<?> getClass(Type type) {
        if (type instanceof Class) {
            return (Class<?>) type;
        } else if (type instanceof ParameterizedType) {
            return getClass(((ParameterizedType) type).getRawType());
        } else if (type instanceof GenericArrayType) {
            Type componentType = ((GenericArrayType) type).getGenericComponentType();
            Class<?> componentClass = getClass(componentType);
            if (componentClass != null) {
                return Array.newInstance(componentClass, 0).getClass();
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public static Type[] getTypeParameters(Class<?> desiredType, Type type) {
        if (type instanceof Class) {
            Class<?> rawClass = (Class<?>) type;

            // if this is the collection class we're done
            if (desiredType.equals(type)) {
                return null;
            }

            for (Type intf : rawClass.getGenericInterfaces()) {
                Type[] collectionType = getTypeParameters(desiredType, intf);
                if (collectionType != null) {
                    return collectionType;
                }
            }

            Type[] collectionType = getTypeParameters(desiredType, rawClass.getGenericSuperclass());
            return collectionType;
        } else if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;

            Type rawType = parameterizedType.getRawType();
            if (desiredType.equals(rawType)) {
                Type[] argument = parameterizedType.getActualTypeArguments();
                return argument;
            }
            Type[] collectionTypes = getTypeParameters(desiredType, rawType);
            if (collectionTypes != null) {
                for (int i = 0; i < collectionTypes.length; i++) {
                    if (collectionTypes[i] instanceof TypeVariable) {
                        TypeVariable typeVariable = (TypeVariable) collectionTypes[i];
                        TypeVariable[] rawTypeParams = ((Class) rawType).getTypeParameters();
                        for (int j = 0; j < rawTypeParams.length; j++) {
                            if (typeVariable.getName().equals(rawTypeParams[j].getName())) {
                                collectionTypes[i] = parameterizedType.getActualTypeArguments()[j];
                            }
                        }
                    }
                }
            }
            return collectionTypes;
        }
        return null;
    }

    public static int findParameterPosition(Class<?> rawClass, String typeName) {
        TypeVariable<?>[] tps = rawClass.getTypeParameters();
        for (int i = 0; i < tps.length; ++i) {
            TypeVariable<?> rtc = tps[i];
            if (rtc.getName().equals(typeName) == true) {
                return i;
            }
        }
        return -1;
    }
}

Related

  1. getMethodByName(final Class cls, final String action)
  2. getMethodByNameFromArray(Method[] methods, String methodName)
  3. getMethodByNameSimple(Class clz, String methodName)
  4. getMethodCount(Class clazz, String methodName)
  5. getMethodCountForName(Class clazz, String methodName)
  6. getMethodFromClass(Class cls, String methodName, Class argClass, boolean onlyProtectedAndHigher)
  7. getMethodFromClassHierarchy(Class clazz, String methodName)
  8. getMethodFromClassWithInheritance(Class cls, String methodName)
  9. getMethodFullName(Method method)