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:com.github.juanmf.java2plant.Parser.java

protected static void addUses(Set<Relation> relations, Class<?> fromType) {
    Method[] methods = fromType.getDeclaredMethods();
    for (Method m : methods) {
        if (!Modifier.isPrivate(m.getModifiers())) {
            addMethodUses(relations, fromType, m);
        }/*w  w  w . j av  a 2 s.com*/
    }
    Constructor<?>[] constructors = fromType.getDeclaredConstructors();
    for (Constructor<?> c : constructors) {
        if (!Modifier.isPrivate(c.getModifiers())) {
            addConstructorUses(relations, fromType, c);
        }
    }
}

From source file:com.weibo.api.motan.protocol.yar.YarProtocolUtil.java

/**
 * add arguments/*from w ww . j  ava 2 s. c om*/
 * 
 * @param interfaceClass
 * @param methodName
 * @param arguments
 * @return
 */
private static void addArguments(DefaultRequest request, Class<?> interfaceClass, String methodName,
        Object[] arguments) {
    Method targetMethod = null;
    Method[] methods = interfaceClass.getDeclaredMethods();
    for (Method m : methods) {
        // FIXME parameters may be ambiguous in weak type language, temporarily by limiting the
        // size of parameters with same method name to avoid.
        if (m.getName().equalsIgnoreCase(methodName) && m.getParameterTypes().length == arguments.length) {
            targetMethod = m;
            break;
        }
    }
    if (targetMethod == null) {
        throw new MotanServiceException("cann't find request method. method name " + methodName);
    }

    request.setParamtersDesc(ReflectUtil.getMethodParamDesc(targetMethod));

    if (arguments != null && arguments.length > 0) {
        Class<?>[] argumentClazz = targetMethod.getParameterTypes();
        request.setArguments(adaptParams(targetMethod, arguments, argumentClazz));
    }

}

From source file:com.yanzhenjie.permission.DefaultRequest.java

private static Method[] findMethodForRequestCode(@NonNull Class<?> source,
        @NonNull Class<? extends Annotation> annotation, int requestCode) {
    List<Method> methods = new ArrayList<>(1);
    for (Method method : source.getDeclaredMethods())
        if (method.getAnnotation(annotation) != null)
            if (isSameRequestCode(method, annotation, requestCode))
                methods.add(method);//from   ww w . j  av a2s. co m
    return methods.toArray(new Method[methods.size()]);
}

From source file:net.mojodna.sprout.support.SproutUtils.java

/**
 * Gets a collection of methods declared in a specified range of a given
 * class' hierarchy./* w  w  w  . java2s  .com*/
 * 
 * @param clazz Class to inspect.
 * @param upto Methods declared in this class and its subclasses will be
 * included.  Any methods declared in superclasses will be ignored.
 * @return Collection of methods declared within the specified range.
 */
public static Collection<Method> getDeclaredMethods(Class clazz, final Class upto) {
    // collect methods to register (include methods for all classes up to and including this one)
    final Collection<Method> methods = new ArrayList();
    while (!clazz.equals(upto.getSuperclass())) {
        methods.addAll(Arrays.asList(clazz.getDeclaredMethods()));
        clazz = clazz.getSuperclass();
    }

    return methods;
}

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;
    }//w  w  w .ja v  a 2s . 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:de.codesourcery.eve.apiclient.cache.FilesystemResponseCacheTest.java

private static Method findMethod(Class<?> clasz, String name) {
    for (Method m : clasz.getDeclaredMethods()) {
        final int mods = m.getModifiers();

        if (Modifier.isStatic(mods) || Modifier.isFinal(mods)) {
            continue;
        }//from www . j a v  a 2s . c o  m

        if (m.getName().equals(name)) {
            return m;
        }
    }
    return null;
}

From source file:com.duy.pascal.interperter.libraries.PascalLibraryManager.java

public static ArrayList<DescriptionImpl> getAllMethodDescription(Class<?>... classes) {
    ArrayList<DescriptionImpl> suggestItems = new ArrayList<>();
    for (Class<?> aClass : classes) {
        Method[] methods = aClass.getDeclaredMethods();
        for (Method method : methods) {
            if (AndroidLibraryUtils.getSdkVersion() >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                if (method.getAnnotation(PascalMethod.class) != null) {
                    PascalMethod annotation = method.getAnnotation(PascalMethod.class);
                    String description = annotation.description();
                    // TODO: 17-Aug-17
                    //                        suggestItems.add(new FunctionDescription(StructureType.TYPE_FUNCTION,
                    //                                Name.create(method.getName()), description, method.));
                }/*from w  ww  . ja  v a2s.c om*/
            } else {
                if (Modifier.isPublic(method.getModifiers())) {
                    suggestItems.add(new DescriptionImpl(StructureType.TYPE_FUNCTION, method.getName()));
                }
            }
        }
    }
    return suggestItems;
}

From source file:com.github.itoshige.testrail.store.SyncManager.java

/**
 * delete not existed method in testrail and caseStore.
 * //from   ww w. j a va 2  s. c om
 * @param sectionId
 * @param testClass
 */
private static void deleteNotExistedMethod(String sectionId, final Class<?> testClass) {
    Set<String> junitMethodNames = new HashSet<String>();
    for (final Method junitMethod : TestRailUnitUtil.getDeclaredTestMethods(testClass.getDeclaredMethods())) {
        junitMethodNames.add(junitMethod.getName());
    }

    // find not existed titles in testrail.
    List<CaseStoreKey> notExistedTitles = new ArrayList<CaseStoreKey>();
    for (CaseStoreKey key : CaseStore.getIns().getSectionId2Tiltes()) {
        if (key.getProjectId().equals(sectionId) && !junitMethodNames.contains(key.getTitle()))
            notExistedTitles.add(key);
    }

    for (CaseStoreKey key : notExistedTitles) {
        // delete testrail
        TestRailClient.deleteCase(CaseStore.getIns().getCaseId(key));
        // remove store
        CaseStore.getIns().remove(key);
    }
}

From source file:com.ms.commons.test.common.ReflectUtil.java

public static Method getDeclaredMethod(Class<?> clazz, String methodName) {
    try {/* w  ww  . j  a va 2 s.  c o  m*/
        Method foundMethod = null;
        Method[] methods = clazz.getDeclaredMethods();
        for (Method m : methods) {
            if (methodName.equals(m.getName())) {
                if (foundMethod != null) {
                    throw new RuntimeException("Found two method named: " + methodName + " in class: " + clazz);
                }
                foundMethod = m;
            }
        }
        if (foundMethod == null) {
            throw new NoSuchMethodException("Cannot find method named: " + methodName + " in class: " + clazz);
        }
        return foundMethod;
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        if (clazz == Object.class) {
            return null;
        } else {
            return getDeclaredMethod(clazz.getSuperclass(), methodName);
        }
    }
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * <p>Generate a hash code for the given annotation using the algorithm
 * presented in the {@link Annotation#hashCode()} API docs.</p>
 *
 * @param a the Annotation for a hash code calculation is desired, not
 * {@code null}/*  www.  ja  v a  2  s  . co  m*/
 * @return the calculated hash code
 * @throws RuntimeException if an {@code Exception} is encountered during
 * annotation member access
 * @throws IllegalStateException if an annotation method invocation returns
 * {@code null}
 */
public static int hashCode(Annotation a) {
    int result = 0;
    Class<? extends Annotation> type = a.annotationType();
    for (Method m : type.getDeclaredMethods()) {
        try {
            Object value = m.invoke(a);
            if (value == null) {
                throw new IllegalStateException(String.format("Annotation method %s returned null", m));
            }
            result += hashMember(m.getName(), value);
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    return result;
}