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.n52.iceland.service.Service.java

private Set<String> getDeclaredBindingMethods(Class<?> c) {
    if (c.equals(Binding.class)) {
        return Collections.emptySet();
    } else {/*from  w  w w  . ja  va2  s . c  o m*/
        Set<String> parent = getDeclaredBindingMethods(c.getSuperclass());
        for (Method m : c.getDeclaredMethods()) {
            parent.add(m.getName());
        }
        return parent;
    }
}

From source file:org.apache.velocity.util.introspection.ClassMap.java

private void populateMethodCacheWith(MethodCache methodCache, Class classToReflect) {
    if (debugReflection && Logger.isDebugEnabled(this.getClass())) {
        Logger.debug(this, "Reflecting " + classToReflect);
    }//from w w  w . j a  va  2 s .c  om

    try {
        Method[] methods = classToReflect.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            int modifiers = methods[i].getModifiers();
            if (Modifier.isPublic(modifiers)) {
                methodCache.put(methods[i]);
            }
        }
    } catch (SecurityException se) // Everybody feels better with...
    {
        if (Logger.isDebugEnabled(this.getClass())) {
            Logger.debug(this, "While accessing methods of " + classToReflect + ": ", se);
        }
    }
}

From source file:net.minder.config.impl.DefaultConfigurationInjector.java

private void injectClass(Class type, Object target, ConfigurationAdapter config, ConfigurationBinding binding)
        throws ConfigurationException {
    Field[] fields = type.getDeclaredFields();
    for (Field field : fields) {
        injectFieldValue(field, target, config, binding);
    }//  ww  w  . j a v  a2s .co m
    Method[] methods = type.getDeclaredMethods();
    for (Method method : methods) {
        injectMethodValue(method, target, config, binding);
    }
}

From source file:se.crisp.codekvast.agent.daemon.codebase.CodeBaseScanner.java

void findTrackedMethods(CodeBase codeBase, Set<String> packages, Set<String> excludePackages, Class<?> clazz) {
    if (clazz.isInterface()) {
        log.debug("Ignoring interface {}", clazz);
        return;/*from www.j  a va  2 s .  c  o  m*/
    }

    log.debug("Analyzing {}", clazz);
    MethodAnalyzer methodAnalyzer = codeBase.getConfig().getMethodAnalyzer();
    try {
        Method[] declaredMethods = clazz.getDeclaredMethods();
        Method[] methods = clazz.getMethods();
        Method[] allMethods = new Method[declaredMethods.length + methods.length];
        System.arraycopy(declaredMethods, 0, allMethods, 0, declaredMethods.length);
        System.arraycopy(methods, 0, allMethods, declaredMethods.length, methods.length);

        for (Method method : allMethods) {
            SignatureStatus status = methodAnalyzer.apply(method);

            // Some AOP frameworks (e.g., Guice) push methods from a base class down to subclasses created in runtime.
            // We need to map those back to the original declaring signature, or else the original declared method will look unused.

            MethodSignature thisSignature = SignatureUtils.makeMethodSignature(clazz, method);

            MethodSignature declaringSignature = SignatureUtils.makeMethodSignature(
                    findDeclaringClass(method.getDeclaringClass(), method, packages), method);

            if (shouldExcludeSignature(declaringSignature, excludePackages)) {
                status = SignatureStatus.EXCLUDED_BY_PACKAGE_NAME;
            }
            codeBase.addSignature(thisSignature, declaringSignature, status);
        }

        for (Class<?> innerClass : clazz.getDeclaredClasses()) {
            findTrackedMethods(codeBase, packages, excludePackages, innerClass);
        }
    } catch (NoClassDefFoundError e) {
        log.warn("Cannot analyze {}: {}", clazz, e.toString());
    }
}

From source file:com.zht.common.rabc.service.impl.RbacPermissionServiceImpl.java

@Override
public void scanPacgeToloadPermis(String packagz) {
    Set<Class<?>> calssSet = PermissionUtil.scanAllController(packagz);
    if (calssSet == null || calssSet.size() == 0) {
        throw new ServiceLogicalException("??");
    }//from ww w.  j  a  va  2 s.  c  o m
    List<RbacPermission> pList = new ArrayList<RbacPermission>();
    for (Class<?> calzz : calssSet) {
        Method[] methods = calzz.getDeclaredMethods();
        String clazzUrl = "";
        if (calzz.isAnnotationPresent(RequestMapping.class)) {
            RequestMapping rm = calzz.getAnnotation(RequestMapping.class);
            String[] clazzUrlArray = rm.value();
            clazzUrl = clazzUrlArray[0];
        }
        if (methods == null || methods.length == 0) {
            continue;
        }
        for (Method method : methods) {
            String url = "";
            if (method.isAnnotationPresent(RequestMapping.class)) {
                RequestMapping mrp = method.getAnnotation(RequestMapping.class);
                String[] methodUrl = mrp.value();
                System.out.println(clazzUrl + methodUrl[0]);//permission--URL
                url = clazzUrl + methodUrl[0];
            }
            if (method.isAnnotationPresent(RequiresPermissions.class)) {
                Annotation rp = method.getAnnotation(RequiresPermissions.class);
                String[] methodRPerms = ((RequiresPermissions) rp).value();//permission--Code
                if (methodRPerms != null && methodRPerms.length > 1) {// pemis
                    for (String str : methodRPerms) {
                        RbacPermission perms = new RbacPermission();
                        perms.setUrl(url);
                        perms.setCode(str);
                        perms.setName("SCAN");
                        perms.setEnabled(true);
                        perms.setType("P");
                        pList.add(perms);
                    }
                } else if (methodRPerms != null && methodRPerms.length == 1) {
                    RbacPermission perms = new RbacPermission();
                    perms.setUrl(url);
                    perms.setCode(methodRPerms[0]);
                    pList.add(perms);
                    perms.setName("SCAN");
                    perms.setEnabled(true);
                    perms.setType("P");
                    pList.add(perms);
                } else if (methodRPerms == null || methodRPerms.length == 0) {
                    RbacPermission perms = new RbacPermission();
                    perms.setUrl(url);
                    perms.setCode("");
                    perms.setName("SCAN");
                    perms.setEnabled(true);
                    perms.setType("P");
                    pList.add(perms);
                    pList.add(perms);
                }
            }

        }
        for (RbacPermission p : pList) {
            $base_save(p);
        }
    }
}

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

private List<String> getInterfaceProperties(Class<?> intf) {
    List<String> result = new ArrayList<>();
    for (Method method : intf.getDeclaredMethods()) {
        if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
            result.add(StringUtils.uncapitalize(method.getName().substring(3)));
        }//  w w  w .  j a v  a 2  s.  c om
    }
    return result;
}

From source file:net.ymate.platform.webmvc.WebMVC.java

public boolean registerController(Class<? extends Controller> targetClass) throws Exception {
    boolean _isValid = false;
    for (Method _method : targetClass.getDeclaredMethods()) {
        if (_method.isAnnotationPresent(RequestMapping.class)) {
            RequestMeta _meta = new RequestMeta(this, targetClass, _method);
            __mappingParser.registerRequestMeta(_meta);
            ////from w  ww.  ja  v  a  2  s. c o m
            if (__owner.getConfig().isDevelopMode()) {
                _LOG.debug("--> " + _meta.getAllowMethods() + ": " + _meta.getMapping() + " : "
                        + _meta.getTargetClass().getName());
            }
            //
            _isValid = true;
        }
    }
    //
    if (_isValid) {
        if (targetClass.getAnnotation(Controller.class).singleton()) {
            __owner.registerBean(BeanMeta.create(targetClass.newInstance(), targetClass));
        } else {
            __owner.registerBean(BeanMeta.create(targetClass));
        }
    }
    return _isValid;
}

From source file:dk.statsbiblioteket.util.qa.PackageScanner.java

/**
 * <b>Beware:</b> This class may throw exotic exceptions since it deals with
 * binary class data./*from  www .  j  ava  2s .co  m*/
 *
 * @param classTarget The class to analyze.
 * @param filename    The file containing the class.
 * @return An array of ReportElements extracted from the members of the
 *         class and the class it self.
 */
@SuppressWarnings({ "unchecked" })
public final ReportElement[] analyzeClass(Class classTarget, String filename) {
    //FIXME: Filenames for internal classes does not refer to correct .java
    //file (it uses Foo$Bar.java)
    Class[] classes = classTarget.getDeclaredClasses();
    Constructor[] constructors = classTarget.getDeclaredConstructors();
    Method[] methods = classTarget.getDeclaredMethods();
    Field[] fields = classTarget.getDeclaredFields();

    List<ReportElement> elements = new ArrayList<ReportElement>();

    // Add the top level class
    ReportElement topLevel = new ReportElement(ReportElement.ElementType.CLASS, classTarget.getName(), null,
            baseSource.toString(), filename, (QAInfo) classTarget.getAnnotation(QAInfo.class));
    elements.add(topLevel);

    for (Class c : classes) {
        ReportElement classInfo = new ReportElement(ReportElement.ElementType.CLASS, c.getName(),
                classTarget.getName(), baseSource.toString(), filename, (QAInfo) c.getAnnotation(QAInfo.class));
        elements.add(classInfo);
    }

    for (Constructor c : constructors) {
        ReportElement conInfo = new ReportElement(ReportElement.ElementType.METHOD,
                c.getName().substring(c.getName().lastIndexOf(".") + 1), classTarget.getName(),
                baseSource.toString(), filename, (QAInfo) c.getAnnotation(QAInfo.class));
        elements.add(conInfo);
    }

    for (Method m : methods) {
        ReportElement metInfo = new ReportElement(ReportElement.ElementType.METHOD, m.getName(),
                classTarget.getName(), baseSource.toString(), filename, (QAInfo) m.getAnnotation(QAInfo.class));
        elements.add(metInfo);
    }

    for (Field f : fields) {
        ReportElement fInfo = new ReportElement(ReportElement.ElementType.FIELD, f.getName(),
                classTarget.getName(), baseSource.toString(), filename, (QAInfo) f.getAnnotation(QAInfo.class));
        elements.add(fInfo);
    }
    return elements.toArray(new ReportElement[elements.size()]);
}

From source file:ch.systemsx.cisd.openbis.generic.shared.RegressionTestCase.java

protected void assertMandatoryMethodAnnotations(Class<?> clazz, String exceptions) {
    List<Class<? extends Annotation>> mandatoryAnnotations = new ArrayList<Class<? extends Annotation>>();
    mandatoryAnnotations.add(RolesAllowed.class);
    mandatoryAnnotations.add(Transactional.class);

    final String noMissingAnnotationsMsg = "Missing annotations in class " + clazz.getCanonicalName() + ":\n";
    StringBuilder problems = new StringBuilder(noMissingAnnotationsMsg);
    for (Method m : clazz.getDeclaredMethods()) {
        List<String> missingAnnotations = new ArrayList<String>();
        for (Class<? extends Annotation> c : mandatoryAnnotations) {
            if (m.getAnnotation(c) == null) {
                missingAnnotations.add(c.getSimpleName());
            }//from www .ja va  2 s.c om
        }
        if (missingAnnotations.size() > 0) {
            problems.append(String.format("%s: %s\n", m.getName(), StringUtils.join(missingAnnotations, ", ")));
        }
    }
    assertEquals(noMissingAnnotationsMsg + exceptions, problems.toString());
}

From source file:org.synyx.hades.dao.orm.GenericDaoFactory.java

/**
 * Returns all/*from w  ww. j a  v  a  2 s . c o m*/
 * 
 * @param daoInterface
 * @return
 */
private Iterable<Method> getFinderMethods(Class<?> daoInterface) {

    Set<Method> result = new HashSet<Method>();

    for (Method method : daoInterface.getDeclaredMethods()) {
        if (!isCustomMethod(method, daoInterface) && !isBaseClassMethod(method, daoInterface)) {
            result.add(method);
        }
    }

    return result;
}