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.gekoh.yagen.hst.CreateEntities.java

private Set<AccessibleObject> getFieldsAndMethods(Class clazz) {
    Set<AccessibleObject> fieldOrMethods = new HashSet<AccessibleObject>(
            Arrays.asList(clazz.getDeclaredFields()));
    fieldOrMethods.addAll(Arrays.asList(clazz.getDeclaredMethods()));

    return fieldOrMethods;
}

From source file:com.nxttxn.vramel.impl.converter.AnnotationTypeConverterLoader.java

/**
 * Loads all of the converter methods for the given type
 *///from  w  w  w .j av a  2  s . c  o m
protected void loadConverterMethods(TypeConverterRegistry registry, Class<?> type) {
    if (visitedClasses.contains(type)) {
        return;
    }
    visitedClasses.add(type);
    try {
        Method[] methods = type.getDeclaredMethods();
        CachingInjector<?> injector = null;

        for (Method method : methods) {
            // this may be prone to ClassLoader or packaging problems when the same class is defined
            // in two different jars (as is the case sometimes with specs).
            if (ObjectHelper.hasAnnotation(method, Converter.class, true)) {
                injector = handleHasConverterAnnotation(registry, type, injector, method);
            } else if (ObjectHelper.hasAnnotation(method, FallbackConverter.class, true)) {
                injector = handleHasFallbackConverterAnnotation(registry, type, injector, method);
            }
        }

        Class<?> superclass = type.getSuperclass();
        if (superclass != null && !superclass.equals(Object.class)) {
            loadConverterMethods(registry, superclass);
        }
    } catch (NoClassDefFoundError e) {
        LOG.warn("Ignoring converter type: " + type.getCanonicalName()
                + " as a dependent class could not be found: " + e, e);
    }
}

From source file:it.openutils.mgnlaws.magnolia.init.ClasspathProviderImpl.java

private Method getMethod(Class theclass, String methodName) throws NoSuchMethodException {
    Method[] declaredMethods = theclass.getDeclaredMethods();

    for (Method method : declaredMethods) {
        if (method.getName().equals(methodName)) {
            return method;
        }/* ww  w.j  av  a2  s  .c om*/
    }

    throw new NoSuchMethodException(theclass.getName() + "." + methodName + "()");
}

From source file:org.acegisecurity.intercept.method.MethodDefinitionMap.java

/**
 * Add configuration attributes for a secure method. Method names can end or start with <code>&#42</code>
 * for matching multiple methods.//from  ww w.ja va  2  s .c o  m
 *
 * @param clazz target interface or class
 * @param mappedName mapped method name
 * @param attr required authorities associated with the method
 *
 * @throws IllegalArgumentException DOCUMENT ME!
 */
public void addSecureMethod(Class clazz, String mappedName, ConfigAttributeDefinition attr) {
    String name = clazz.getName() + '.' + mappedName;

    if (logger.isDebugEnabled()) {
        logger.debug("Adding secure method [" + name + "] with attributes [" + attr + "]");
    }

    Method[] methods = clazz.getDeclaredMethods();
    List matchingMethods = new ArrayList();

    for (int i = 0; i < methods.length; i++) {
        if (methods[i].getName().equals(mappedName) || isMatch(methods[i].getName(), mappedName)) {
            matchingMethods.add(methods[i]);
        }
    }

    if (matchingMethods.isEmpty()) {
        throw new IllegalArgumentException("Couldn't find method '" + mappedName + "' on " + clazz);
    }

    // register all matching methods
    for (Iterator it = matchingMethods.iterator(); it.hasNext();) {
        Method method = (Method) it.next();
        String regMethodName = (String) this.nameMap.get(method);

        if ((regMethodName == null)
                || (!regMethodName.equals(name) && (regMethodName.length() <= name.length()))) {
            // no already registered method name, or more specific
            // method name specification now -> (re-)register method
            if (regMethodName != null) {
                logger.debug("Replacing attributes for secure method [" + method + "]: current name [" + name
                        + "] is more specific than [" + regMethodName + "]");
            }

            this.nameMap.put(method, name);
            addSecureMethod(method, attr);
        } else {
            logger.debug("Keeping attributes for secure method [" + method + "]: current name [" + name
                    + "] is not more specific than [" + regMethodName + "]");
        }
    }
}

From source file:com.kelveden.rastajax.core.ResourceClassLoader.java

private List<Parameter> loadClassProperties(final Class<?> resourceClass) {

    final String logPrefix = " |-";

    final List<Parameter> fields = new ArrayList<Parameter>();

    for (Method method : resourceClass.getDeclaredMethods()) {
        final Set<Annotation> annotations = JaxRsAnnotationScraper.scrapeJaxRsAnnotationsFrom(resourceClass,
                method);//from ww w .  j a  va 2s .  c  om

        try {
            final Parameter parameter = buildParameterFromJaxRsAnnotations(annotations, method.getReturnType());

            if (parameter != null) {
                LOGGER.debug("{} Found {} property '{}' of type '{}'.", logPrefix,
                        parameter.getJaxRsAnnotationType().getSimpleName(), parameter.getName(),
                        parameter.getType().getName());

                fields.add(parameter);
            } else {
                LOGGER.debug(
                        "{} Method {} was not annotated with any annotations that describe the JAX-RS parameter type and so will be ignored.",
                        logPrefix, method.getName());
            }

        } catch (IllegalAccessException e) {
            throw new ResourceClassLoadingException(String.format("Could not load property '%s' on class '%s'",
                    method.getName(), resourceClass.getName()), e);

        } catch (InvocationTargetException e) {
            throw new ResourceClassLoadingException(String.format("Could not load property '%s' on class '%s'",
                    method.getName(), resourceClass.getName()), e);

        } catch (NoSuchMethodException e) {
            throw new ResourceClassLoadingException(String.format("Could not load property'%s' on class '%s'",
                    method.getName(), resourceClass.getName()), e);
        }
    }

    return fields;
}

From source file:org.apache.tajo.rpc.NettyRpcServer.java

public NettyRpcServer(Object proxy, Class<?> interfaceClass, InetSocketAddress bindAddress) {
    super(bindAddress);
    this.instance = proxy;
    this.clazz = instance.getClass();
    this.methods = new HashMap<String, Method>();
    this.builderMethods = new HashMap<String, Method>();
    this.pipeline = new ProtoPipelineFactory(new ServerHandler(), Invocation.getDefaultInstance());

    super.init(this.pipeline);
    for (Method m : interfaceClass.getDeclaredMethods()) {
        String methodName = m.getName();
        Class<?> params[] = m.getParameterTypes();
        try {/* w w w.  j ava 2s.co  m*/
            methods.put(methodName, this.clazz.getMethod(methodName, params));

            Method mtd = params[0].getMethod("newBuilder", new Class[] {});
            builderMethods.put(methodName, mtd);
        } catch (Exception e) {
            e.printStackTrace();
            continue;
        }
    }
}

From source file:com.haulmont.cuba.core.sys.MetadataImpl.java

protected void invokePostConstructMethods(Entity entity)
        throws InvocationTargetException, IllegalAccessException {
    List<Method> postConstructMethods = new ArrayList<>(4);
    List<String> methodNames = new ArrayList<>(4);
    Class clazz = entity.getClass();
    while (clazz != Object.class) {
        Method[] classMethods = clazz.getDeclaredMethods();
        for (Method method : classMethods) {
            if (method.isAnnotationPresent(PostConstruct.class) && !methodNames.contains(method.getName())) {
                postConstructMethods.add(method);
                methodNames.add(method.getName());
            }/* w  ww  .j av  a 2s. c om*/
        }
        clazz = clazz.getSuperclass();
    }

    ListIterator<Method> iterator = postConstructMethods.listIterator(postConstructMethods.size());
    while (iterator.hasPrevious()) {
        Method method = iterator.previous();
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        method.invoke(entity);
    }
}

From source file:py.una.pol.karaku.test.cucumber.TransactionalTestCucumberExecutionListener.java

/**
 * Gets all methods in the supplied {@link Class class} and its superclasses
 * which are annotated with the supplied <code>annotationType</code> but
 * which are not <em>shadowed</em> by methods overridden in subclasses.
 * <p>/*from   ww  w. ja  v a 2s  .c  o  m*/
 * Note: This code has been borrowed from
 * {@link org.junit.internal.runners.TestClass#getAnnotatedMethods(Class)}
 * and adapted.
 * 
 * @param clazz
 *            the class for which to retrieve the annotated methods
 * @param annotationType
 *            the annotation type for which to search
 * @return all annotated methods in the supplied class and its superclasses
 */
private List<Method> getAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationType) {

    List<Method> results = new ArrayList<Method>();
    for (Class<?> eachClass : getSuperClasses(clazz)) {
        Method[] methods = eachClass.getDeclaredMethods();
        for (Method eachMethod : methods) {
            Annotation annotation = eachMethod.getAnnotation(annotationType);
            if (annotation != null && !isShadowed(eachMethod, results)) {
                results.add(eachMethod);
            }
        }
    }
    return results;
}

From source file:cn.webwheel.DefaultMain.java

private void getActions(List<Action> list, Set<Class> set, Class cls, Method method) {
    if (cls == null || !set.add(cls))
        return;/*from w w w  .j  a  v  a2 s  .c  o  m*/
    for (Method m : cls.getDeclaredMethods()) {
        if (!m.getName().equals(method.getName()))
            continue;
        if (!Arrays.equals(m.getParameterTypes(), method.getParameterTypes()))
            continue;
        Action action = m.getAnnotation(Action.class);
        if (action != null) {
            list.add(action);
        }
        break;
    }
    for (Class i : cls.getInterfaces()) {
        getActions(list, set, i, method);
    }
    getActions(list, set, cls.getSuperclass(), method);
}