Example usage for java.lang Class getDeclaredMethods

List of usage examples for java.lang Class getDeclaredMethods

Introduction

In this page you can find the example usage for java.lang Class getDeclaredMethods.

Prototype

@CallerSensitive
public Method[] getDeclaredMethods() throws SecurityException 

Source Link

Document

Returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.

Usage

From source file:org.apache.niolex.commons.reflect.MethodUtil.java

/**
 * Retrieve all the methods including static methods of this class, neither get super methods nor
 * interface methods.//from   w  ww  . j  a v  a 2  s  .c om
 *
 * @param clazz the class to be used
 * @return the list contains all the methods including static methods of this class
 * @throws SecurityException if a security manager is present and the reflection is rejected
 * @see #getAllMethods(Class)
 * @see #getAllMethodsIncludeInterfaces(Class)
 */
public static final List<Method> getThisMethods(Class<?> clazz) {
    List<Method> outList = new ArrayList<Method>();
    CollectionUtil.addAll(outList, clazz.getDeclaredMethods());
    return outList;
}

From source file:hydrograph.ui.expression.editor.buttons.ValidateExpressionToolButton.java

/**
 * Complies the given expression using engine's jar from ELT-Project's build path. 
 * /*from  w  w w .  java 2  s.co m*/
 * @param expressionStyledText
 * @param fieldMap
 * @param componentName
 * @return DiagnosticCollector 
 *          complete diagnosis of given expression
 * @throws JavaModelException
 * @throws InvocationTargetException
 * @throws ClassNotFoundException
 * @throws MalformedURLException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
@SuppressWarnings({ "unchecked" })
public static DiagnosticCollector<JavaFileObject> compileExpresion(String expressionStyledText,
        Map<String, Class<?>> fieldMap, String componentName)
        throws JavaModelException, InvocationTargetException, ClassNotFoundException, MalformedURLException,
        IllegalAccessException, IllegalArgumentException {
    LOGGER.debug("Compiling expression using Java-Compiler");
    String expressiontext = getExpressionText(expressionStyledText);
    DiagnosticCollector<JavaFileObject> diagnostics = null;
    Object[] returObj = getBuildPathForMethodInvocation();
    List<URL> urlList = (List<URL>) returObj[0];
    String transfromJarPath = (String) returObj[1];
    String propertyFilePath = (String) returObj[2];
    URLClassLoader child = URLClassLoader.newInstance(urlList.toArray(new URL[urlList.size()]));

    Class<?> class1 = Class.forName(HYDROGRAPH_ENGINE_EXPRESSION_VALIDATION_API_CLASS, true, child);
    Thread.currentThread().setContextClassLoader(child);
    Method[] methods = class1.getDeclaredMethods();
    for (Method method : methods) {
        if (method.getParameterTypes().length == 4
                && StringUtils.equals(method.getName(),
                        COMPILE_METHOD_OF_EXPRESSION_JAR_FOR_TRANSFORM_COMPONENTS)
                && !StringUtils.equalsIgnoreCase(componentName, hydrograph.ui.common.util.Constants.FILTER)) {
            method.getDeclaringClass().getClassLoader();
            diagnostics = (DiagnosticCollector<JavaFileObject>) method.invoke(null, expressiontext,
                    propertyFilePath, fieldMap, transfromJarPath);

            break;
        } else if (method.getParameterTypes().length == 4
                && StringUtils.equals(method.getName(), COMPILE_METHOD_OF_EXPRESSION_JAR_FOR_FILTER_COMPONENT)
                && StringUtils.equalsIgnoreCase(componentName, hydrograph.ui.common.util.Constants.FILTER)) {
            method.getDeclaringClass().getClassLoader();
            diagnostics = (DiagnosticCollector<JavaFileObject>) method.invoke(null, expressiontext,
                    propertyFilePath, fieldMap, transfromJarPath);
            break;
        }

    }

    try {
        child.close();
    } catch (IOException ioException) {
        LOGGER.error("Error occurred while closing classloader", ioException);
    }
    return diagnostics;
}

From source file:de.micromata.genome.util.runtime.ClassUtils.java

/**
 * Find first method with given name./* w w  w  .j a v  a 2s  . c o  m*/
 *
 * @param clazz the clazz
 * @param method the method
 * @return null if not found
 */
public static Method findFirstMethod(Class<?> clazz, String method) {
    for (Method m : clazz.getDeclaredMethods()) {
        if (m.getName().equals(method) == true) {
            return m;
        }
    }
    if (clazz.getSuperclass() != null) {
        return findFirstMethod(clazz.getSuperclass(), method);
    }
    return null;
}

From source file:org.apache.niolex.commons.reflect.MethodUtil.java

/**
 * Retrieve all the methods including static methods of this class and it's super classes.
 * <p>/*from   w ww.java 2  s.  c om*/
 * We don't get methods of interfaces, because every method in a interface must have
 * the real definition in the classes.
 * </p>
 *
 * @param clazz the class to be used
 * @return the list contains all the methods
 * @throws SecurityException if a security manager is present and the reflection is rejected
 * @see #getAllMethodsIncludeInterfaces(Class)
 */
public static final List<Method> getAllMethods(Class<?> clazz) {
    List<Method> outList = new ArrayList<Method>();
    do {
        CollectionUtil.addAll(outList, clazz.getDeclaredMethods());
        clazz = clazz.getSuperclass();
    } while (clazz != null);
    return outList;
}

From source file:com.rockagen.commons.util.ClassUtil.java

/**
 * obtain methods list of specified class If recursively is true, obtain
 * methods from all class hierarchy/*w w w.  j  a v  a 2  s.com*/
 * 
 * @param clazz class
 *            where fields are searching
 * @param recursively
 *            param
 * @return array of methods
 */
public static Method[] getDeclaredMethods(Class<?> clazz, boolean recursively) {
    List<Method> methods = new LinkedList<Method>();
    Method[] declaredMethods = clazz.getDeclaredMethods();
    Collections.addAll(methods, declaredMethods);

    Class<?> superClass = clazz.getSuperclass();

    if (superClass != null && recursively) {
        Method[] declaredMethodsOfSuper = getDeclaredMethods(superClass, true);
        if (declaredMethodsOfSuper.length > 0)
            Collections.addAll(methods, declaredMethodsOfSuper);
    }
    return methods.toArray(new Method[methods.size()]);
}

From source file:com.github.shareme.blackandroids.greenandroid.easpermissions.EasyPermissions.java

private static void runAnnotatedMethods(Object object, int requestCode) {
    Class clazz = object.getClass();
    if (isUsingAndroidAnnotations(object)) {
        clazz = clazz.getSuperclass();//from  w  w  w  . ja  v  a2 s .  c  o m
    }
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isAnnotationPresent(AfterPermissionGranted.class)) {
            // Check for annotated methods with matching request code.
            AfterPermissionGranted ann = method.getAnnotation(AfterPermissionGranted.class);
            if (ann.value() == requestCode) {
                // Method must be void so that we can invoke it
                if (method.getParameterTypes().length > 0) {
                    throw new RuntimeException("Cannot execute non-void method " + method.getName());
                }

                try {
                    // Make method accessible if private
                    if (!method.isAccessible()) {
                        method.setAccessible(true);
                    }
                    method.invoke(object);
                } catch (IllegalAccessException e) {
                    Timber.e(TAG, "runDefaultMethod:IllegalAccessException", e);
                } catch (InvocationTargetException e) {
                    Timber.e(TAG, "runDefaultMethod:InvocationTargetException", e);
                }
            }
        }
    }
}

From source file:gov.nih.nci.iso21090.hibernate.property.CollectionPropertyAccessor.java

/**
 * @param theClass Target class in which the value is to be set
 * @param propertyName Target property name
 * @return returns setter method instance
 *///  w  ww.j  a v a2  s .c  o  m
@SuppressWarnings("PMD.UnusedFormalParameter")
private static Method setterMethod(Class theClass, String propertyName) {

    //CollectionPropertyGetter getter  =  getGetterOrNull(theClass, propertyName);
    //Class returnType  =  (getter == null) ? null : getter.getReturnType();

    Method[] methods = theClass.getDeclaredMethods();
    Method potentialSetter = null;
    for (int i = 0; i < methods.length; i++) {
        String methodName = methods[i].getName();

        if ("addPart".equals(methodName) && methods[i].getParameterTypes().length == 1) {
            //String testStdMethod  =  Introspector.decapitalize(methodName.substring(3));
            //String testOldMethod  =  methodName.substring(3);
            //if (testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName)) {
            potentialSetter = methods[i];
            //if (returnType == null || methods[i].getParameterTypes()[0].equals(returnType)) {
            //    return potentialSetter;
            //}
            //}
        }
    }
    return potentialSetter;
}

From source file:ei.ne.ke.cassandra.cql3.EntitySpecificationUtils.java

/**
 *
 *
 * @param entityClazz/* ww w .  ja  v a 2s . c  o m*/
 * @param propertyName
 * @return
 */
public static <T> Method findSetterMethod(Class<T> entityClazz, String propertyName) {
    for (Method methodCandidate : entityClazz.getDeclaredMethods()) {
        String methodCandidateName = methodCandidate.getName();
        if (methodCandidateName.startsWith("set") && methodCandidateName.endsWith(propertyName)) {
            return methodCandidate;
        }
    }
    return null;
}

From source file:ei.ne.ke.cassandra.cql3.EntitySpecificationUtils.java

/**
 *
 *
 * @param entityClazz/*from   ww w .j a  v a2 s  . c  om*/
 * @param propertyName
 * @return
 */
public static <T> Method findGetterMethod(Class<T> entityClazz, String propertyName) {
    for (Method methodCandidate : entityClazz.getDeclaredMethods()) {
        String methodCandidateName = methodCandidate.getName();
        if ((methodCandidateName.startsWith("get") || methodCandidateName.startsWith("is"))
                && methodCandidateName.endsWith(propertyName)) {
            return methodCandidate;
        }
    }
    return null;
}

From source file:IntrospectionUtil.java

protected static Method findInheritedMethod(Package pack, Class clazz, String methodName, Class[] args,
        boolean strictArgs) throws NoSuchMethodException {
    if (clazz == null)
        throw new NoSuchMethodException("No class");
    if (methodName == null)
        throw new NoSuchMethodException("No method name");

    Method method = null;//w  w  w. j  a  v  a2s  . co  m
    Method[] methods = clazz.getDeclaredMethods();
    for (int i = 0; i < methods.length && method == null; i++) {
        if (methods[i].getName().equals(methodName) && isInheritable(pack, methods[i])
                && checkParams(methods[i].getParameterTypes(), args, strictArgs))
            method = methods[i];
    }
    if (method != null) {
        return method;
    } else
        return findInheritedMethod(clazz.getPackage(), clazz.getSuperclass(), methodName, args, strictArgs);
}