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:org.apache.openjpa.enhance.Reflection.java

/**
 * Affirms if the original declaration the given method is annotated
 * for reflection. /*w ww  .  j  a  v  a 2  s.  co  m*/
 */
static boolean canReflect(Method method) {
    Class cls = getDeclaringClass(method);
    if (cls != method.getDeclaringClass())
        method = getDeclaringMethod(cls, method);
    return canReflect((Reflectable) cls.getAnnotation(Reflectable.class),
            method.getAnnotation(Reflectable.class));
}

From source file:org.apache.openjpa.enhance.Reflection.java

/**
 * Gets the declaring class of the given method signature but also checks
 * if the method is declared in an interface. If yes, then returns the
 * interface. //w  w w.j  a  va  2 s  . c  o m
 */
public static Class getDeclaringClass(Method m) {
    if (m == null)
        return null;
    Class cls = m.getDeclaringClass();
    Class[] intfs = cls.getInterfaces();
    for (Class intf : intfs) {
        if (getDeclaringMethod(intf, m) != null)
            cls = intf;
    }
    return cls;
}

From source file:org.apache.jxtadoop.ipc.RPC.java

/** Expert: Make multiple, parallel calls to a set of servers. */
public static Object[] call(Method method, Object[][] params, PeerGroup pg, JxtaSocketAddress[] jsockaddrs,
        UserGroupInformation ticket, Configuration conf) throws IOException {

    Invocation[] invocations = new Invocation[params.length];
    for (int i = 0; i < params.length; i++)
        invocations[i] = new Invocation(method, params[i]);
    Client client = CLIENTS.getClient(conf);
    try {//from  w  w  w  .  j  av a  2 s .c om
        Writable[] wrappedValues = client.call(invocations, pg, jsockaddrs, method.getDeclaringClass(), ticket);

        if (method.getReturnType() == Void.TYPE) {
            return null;
        }

        Object[] values = (Object[]) Array.newInstance(method.getReturnType(), wrappedValues.length);
        for (int i = 0; i < values.length; i++)
            if (wrappedValues[i] != null)
                values[i] = ((ObjectWritable) wrappedValues[i]).get();

        return values;
    } finally {
        CLIENTS.stopClient(client);
    }
}

From source file:ca.oson.json.util.ObjectUtil.java

public static String[] getParameterNames(Method m) throws IOException {
    Class<?> declaringClass = m.getDeclaringClass();
    String resourceName = "/" + declaringClass.getName().replace('.', '/') + ".class";
    InputStream classData = declaringClass.getResourceAsStream(resourceName);

    VariableReader variableDiscoverer = new VariableReader();

    ClassReader r = new ClassReader(classData);
    r.accept(variableDiscoverer, 0);/*from  ww  w .j  a v a  2s .co m*/

    Map<Integer, String> variableNames = variableDiscoverer.getVariableNames(m);
    String[] parameterNames = new String[m.getParameterTypes().length];
    if (variableNames != null) {
        for (int i = 0; i < parameterNames.length; i++) {
            parameterNames[i] = variableNames.get(i);
        }
    }
    return parameterNames;
}

From source file:com.jeeframework.util.classes.ClassUtils.java

/**
 * Given a method, which may come from an interface, and a target class used
 * in the current reflective invocation, find the corresponding target method
 * if there is one. E.g. the method may be <code>IFoo.bar()</code> and the
 * target class may be <code>DefaultFoo</code>. In this case, the method may be
 * <code>DefaultFoo.bar()</code>. This enables attributes on that method to be found.
 * <p><b>NOTE:</b> In contrast to {@link org.springframework.aop.support.AopUtils#getMostSpecificMethod},
 * this method does <i>not</i> resolve Java 5 bridge methods automatically.
 * Call {@link org.springframework.core.BridgeMethodResolver#findBridgedMethod}
 * if bridge method resolution is desirable (e.g. for obtaining metadata from
 * the original method definition).//from w ww .j  ava 2 s  . c  o  m
 * @param method the method to be invoked, which may come from an interface
 * @param targetClass the target class for the current invocation.
 * May be <code>null</code> or may not even implement the method.
 * @return the specific target method, or the original method if the
 * <code>targetClass</code> doesn't implement it or is <code>null</code>
 * @see org.springframework.aop.support.AopUtils#getMostSpecificMethod
 */
public static Method getMostSpecificMethod(Method method, Class targetClass) {
    if (method != null && targetClass != null && !targetClass.equals(method.getDeclaringClass())) {
        try {
            method = targetClass.getMethod(method.getName(), method.getParameterTypes());
        } catch (NoSuchMethodException ex) {
            // Perhaps the target class doesn't implement this method:
            // that's fine, just use the original method.
        }
    }
    return method;
}

From source file:com.mycila.plugin.Cglib2AopProxy.java

/**
 * Wrap a return of this if necessary to be the proxy
 *//* w w  w  . j a  v a2  s  .  c  o m*/
private static Object massageReturnTypeIfNecessary(Object proxy, Object target, Method method, Object retVal) {
    // Massage return value if necessary
    if (retVal != null && retVal == target
            && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
        // Special case: it returned "this".
        // Note that we can't help if the target sets a reference
        // to itself in another returned object.
        retVal = proxy;
    }
    return retVal;
}

From source file:com.bstek.dorado.data.method.MethodAutoMatchingUtils.java

private static MethodInfo getMethodInfo(Method[] methods) {
    boolean classIsSame = true;
    boolean methodNameIsSame = true;
    Class<?> cl = null;/*from w w  w .ja  va 2 s. c o m*/
    String methodName = null;
    for (Method method : methods) {
        if (classIsSame) {
            Class<?> declaringClass = method.getDeclaringClass();
            if (cl == null) {
                cl = declaringClass;
            } else if (!declaringClass.equals(cl)) {
                if (declaringClass.isAssignableFrom(cl)) {
                    cl = declaringClass;
                } else if (cl.isAssignableFrom(declaringClass)) {
                    // do nothing
                } else {
                    classIsSame = false;
                }
            }
        }

        if (methodNameIsSame) {
            if (methodName == null) {
                methodName = method.getName();
            } else if (!method.getName().equals(methodName)) {
                methodNameIsSame = false;
            }
        }
    }

    String className = cl.getName();
    if (!classIsSame) {
        className += "*";
    }

    if (!methodNameIsSame) {
        methodName += "*";
    }
    return new MethodInfo(className, methodName);
}

From source file:ca.uhn.fhir.rest.method.BaseMethodBinding.java

public static boolean verifyMethodHasZeroOrOneOperationAnnotation(Method theNextMethod,
        Object... theAnnotations) {
    Object obj1 = null;/*from  ww  w.  java2s .c  o m*/
    for (Object object : theAnnotations) {
        if (object != null) {
            if (obj1 == null) {
                obj1 = object;
            } else {
                throw new ConfigurationException("Method " + theNextMethod.getName() + " on type '"
                        + theNextMethod.getDeclaringClass().getSimpleName() + " has annotations @"
                        + obj1.getClass().getSimpleName() + " and @" + object.getClass().getSimpleName()
                        + ". Can not have both.");
            }

        }
    }
    if (obj1 == null) {
        return false;
        // throw new ConfigurationException("Method '" +
        // theNextMethod.getName() + "' on type '" +
        // theNextMethod.getDeclaringClass().getSimpleName() +
        // " has no FHIR method annotations.");
    }
    return true;
}

From source file:org.apache.qpid.proton.apireconciliation.reportwriter.ReconciliationReportWriter.java

private String createFullyQualifiedJavaMethodName(Method javaMethod) {
    return javaMethod.getDeclaringClass().getName() + "#" + javaMethod.getName();
}

From source file:org.apache.axis2.jaxws.description.impl.EndpointInterfaceDescriptionImpl.java

private static Method[] getSEIMethods(Class sei) {
    // Per JSR-181 all methods on the SEI are mapped to operations regardless
    // of whether they include an @WebMethod annotation.  That annotation may
    // be present to customize the mapping, but is not required (p14)
    Method[] seiMethods = sei.getMethods();
    ArrayList methodList = new ArrayList();
    if (sei != null) {
        for (Method method : seiMethods) {

            if (method.getDeclaringClass().getName().equals("java.lang.Object")) {
                continue;
            }/* w ww. j  av a  2  s . c  o  m*/
            methodList.add(method);
            if (!Modifier.isPublic(method.getModifiers())) {
                // JSR-181 says methods must be public (p14)
                throw ExceptionFactory.makeWebServiceException(Messages.getMessage("seiMethodsErr"));
            }
            // TODO: other validation per JSR-181
        }

    }
    return (Method[]) methodList.toArray(new Method[methodList.size()]);
    //        return seiMethods;
}