Example usage for java.lang.reflect Method getDeclaringClass

List of usage examples for java.lang.reflect Method getDeclaringClass

Introduction

In this page you can find the example usage for java.lang.reflect Method getDeclaringClass.

Prototype

@Override
public Class<?> getDeclaringClass() 

Source Link

Document

Returns the Class object representing the class or interface that declares the method represented by this object.

Usage

From source file:com.wavemaker.tools.apidocs.tools.parser.util.MethodUtils.java

public static String generateMethodIdentifier(Method method) {
    final String uniqueIdentifierId = getMethodUniqueIdentifierId(method);

    return method.getDeclaringClass().getSimpleName() + "-" + uniqueIdentifierId;
}

From source file:com.jetyun.pgcd.rpc.reflect.ClassAnalyzer.java

/**
 * Analyze a class and create a ClassData object containing all of the
 * public methods (both static and non-static) in the class.
 * //from  ww w. j  av  a  2 s  . co  m
 * @param clazz
 *            class to be analyzed.
 * 
 * @return a ClassData object containing all the public static and
 *         non-static methods that can be invoked on the class.
 */
private static ClassData analyzeClass(Class clazz) {
    log.info("analyzing " + clazz.getName());
    Method methods[] = clazz.getMethods();
    ClassData cd = new ClassData();
    cd.clazz = clazz;

    // Create temporary method map
    HashMap staticMethodMap = new HashMap();
    HashMap methodMap = new HashMap();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (method.getDeclaringClass() == Object.class) {
            continue;
        }
        int mod = methods[i].getModifiers();
        if (!Modifier.isPublic(mod)) {
            continue;
        }
        Class param[] = method.getParameterTypes();

        // don't count locally resolved args
        int argCount = 0;
        for (int n = 0; n < param.length; n++) {
            if (LocalArgController.isLocalArg(param[n])) {
                continue;
            }
            argCount++;
        }

        MethodKey mk = new MethodKey(method.getName(), argCount);
        ArrayList marr = (ArrayList) methodMap.get(mk);
        if (marr == null) {
            marr = new ArrayList();
            methodMap.put(mk, marr);
        }
        marr.add(method);
        if (Modifier.isStatic(mod)) {
            marr = (ArrayList) staticMethodMap.get(mk);
            if (marr == null) {
                marr = new ArrayList();
                staticMethodMap.put(mk, marr);
            }
            marr.add(method);
        }
    }
    cd.methodMap = new HashMap();
    cd.staticMethodMap = new HashMap();
    // Convert ArrayLists to arrays
    Iterator i = methodMap.entrySet().iterator();
    while (i.hasNext()) {
        Map.Entry entry = (Map.Entry) i.next();
        MethodKey mk = (MethodKey) entry.getKey();
        ArrayList marr = (ArrayList) entry.getValue();
        if (marr.size() == 1) {
            cd.methodMap.put(mk, marr.get(0));
        } else {
            cd.methodMap.put(mk, marr.toArray(new Method[0]));
        }
    }
    i = staticMethodMap.entrySet().iterator();
    while (i.hasNext()) {
        Map.Entry entry = (Map.Entry) i.next();
        MethodKey mk = (MethodKey) entry.getKey();
        ArrayList marr = (ArrayList) entry.getValue();
        if (marr.size() == 1) {
            cd.staticMethodMap.put(mk, marr.get(0));
        } else {
            cd.staticMethodMap.put(mk, marr.toArray(new Method[0]));
        }
    }
    return cd;
}

From source file:IntrospectionUtil.java

public static boolean containsSameMethodSignature(Method method, Class c, boolean checkPackage) {
    if (checkPackage) {
        if (!c.getPackage().equals(method.getDeclaringClass().getPackage()))
            return false;
    }//from w w  w . j  a va 2 s .c  o  m

    boolean samesig = false;
    Method[] methods = c.getDeclaredMethods();
    for (int i = 0; i < methods.length && !samesig; i++) {
        if (IntrospectionUtil.isSameSignature(method, methods[i]))
            samesig = true;
    }
    return samesig;
}

From source file:edu.umich.flowfence.common.QMDescriptor.java

public static QMDescriptor forMethod(Context context, Method method) {
    Class<?> definingClass = method.getDeclaringClass();
    String methodName = method.getName();
    Class<?>[] paramTypes = method.getParameterTypes();
    if ((method.getModifiers() & Modifier.STATIC) != 0) {
        return forStatic(context, definingClass, methodName, paramTypes);
    } else {/*from   w w  w.  j  a va  2  s. co m*/
        return forInstance(context, definingClass, methodName, paramTypes);
    }
}

From source file:edu.umich.oasis.common.SodaDescriptor.java

public static SodaDescriptor forMethod(Context context, Method method) {
    Class<?> definingClass = method.getDeclaringClass();
    String methodName = method.getName();
    Class<?>[] paramTypes = method.getParameterTypes();
    if ((method.getModifiers() & Modifier.STATIC) != 0) {
        return forStatic(context, definingClass, methodName, paramTypes);
    } else {//w ww . ja va2s . c  om
        return forInstance(context, definingClass, methodName, paramTypes);
    }
}

From source file:com.monarchapis.driver.spring.rest.ApiRequestHandlerInterceptor.java

/**
 * Looks for an annotation first on the method, then inspects the class.
 * //from  w  w w .ja va2 s. com
 * @param method
 *            The method search for the desired annotation.
 * @param clazz
 *            THe class to search if the annotation is not present on the
 *            method.
 * @return the annotation if found, null otherwise.
 */
private static <T extends Annotation> T getAnnotation(Method method, Class<T> clazz) {
    T annotation = method.getAnnotation(clazz);

    if (annotation == null) {
        Class<?> declaringClass = method.getDeclaringClass();
        annotation = declaringClass.getAnnotation(clazz);

        while (annotation == null && declaringClass.getSuperclass() != null) {
            declaringClass = declaringClass.getSuperclass();
            annotation = declaringClass.getAnnotation(clazz);
        }
    }

    return annotation;
}

From source file:com.github.cherimojava.data.mongo.entity.EntityUtils.java

/**
 * returns the getter method matching the given Adder method or throws an exception if no such method exists
 *
 * @param m adder method for which the matching getter method shall be found
 * @return getter method belonging to the given Adder method
 * @throws java.lang.IllegalArgumentException if the given Adder method has no corresponding getter method
 *///from  w  ww  .j  a  va 2  s  . co  m
static Method getGetterFromAdder(Method m) {
    try {
        return m.getDeclaringClass().getMethod(m.getName().replaceFirst("add", "get"));
    } catch (NoSuchMethodException e) {
        throw new IllegalArgumentException(format("Method %s has no corresponding getter method", m.getName()));
    }
}

From source file:com.github.cherimojava.data.mongo.entity.EntityUtils.java

/**
 * returns the setter method of the given getter method or throws an exception if no such method exists
 *
 * @param m getter method for which a matching setter method shall be found
 * @return setter method belonging to the given getter method
 * @throws java.lang.IllegalArgumentException if the given getter Method has no setter method
 *///from w  w w  .  java  2 s  .  c om
static Method getSetterFromGetter(Method m) {
    try {
        return m.getDeclaringClass()
                .getMethod(m.getName().startsWith("get") ? m.getName().replaceFirst("g", "s")
                        : m.getName().replaceFirst("is", "set"), m.getReturnType());
    } catch (NoSuchMethodException e) {
        throw new IllegalArgumentException(format("Method %s has no corresponding setter method", m.getName()));
    }
}

From source file:org.grails.datastore.mapping.reflect.ReflectionUtils.java

/**
 * Make the given method accessible, explicitly setting it accessible if necessary.
 * The <code>setAccessible(true)</code> method is only called when actually necessary,
 * to avoid unnecessary conflicts with a JVM SecurityManager (if active).
 *
 * Based on the same method in Spring core.
 *
 * @param method the method to make accessible
 * @see java.lang.reflect.Method#setAccessible
 *//*  w w w . j  a  v  a 2s. co  m*/
public static void makeAccessible(Method method) {
    if (!Modifier.isPublic(method.getModifiers())
            || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
        method.setAccessible(true);
    }
}

From source file:com.github.cherimojava.data.mongo.entity.EntityUtils.java

/**
 * returns the adder method matching the given Getter method or throws an exception if no such method exists
 *
 * @param m getter method for which a matching adder method shall be found
 * @return adder method belongign to the given Getter method
 * @throws java.lang.IllegalArgumentException if the given method has no matching adder method
 *///from   www.  j a  v  a 2 s. c om
static Method getAdderFromGetter(Method m) {
    try {
        return m.getDeclaringClass().getMethod(m.getName().replaceFirst("get", "add"),
                (Class) ((ParameterizedType) m.getGenericReturnType()).getActualTypeArguments()[0]);
    } catch (Exception e) {
        try {
            // if we had no hit, try if there's a vararg adder
            return m.getDeclaringClass().getMethod(m.getName().replaceFirst("get", "add"),
                    Array.newInstance(
                            (Class) ((ParameterizedType) m.getGenericReturnType()).getActualTypeArguments()[0],
                            0).getClass());
        } catch (Exception e1) {
            throw new IllegalArgumentException(
                    format("Method %s has no corresponding adder method", m.getName()));
        }
    }
}