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.hadoop.yarn.webapp.hamlet.HamletGen.java

void genFactoryMethods(Class<?> cls, int indent) {
    for (Method method : cls.getDeclaredMethods()) {
        String retName = method.getReturnType().getSimpleName();
        String methodName = method.getName();
        if (methodName.charAt(0) == '$')
            continue;
        if (isElement(retName) && method.getParameterTypes().length == 0) {
            genFactoryMethod(retName, methodName, indent);
        }/*from   w  ww . ja  v a  2  s  .  c om*/
    }
}

From source file:com.graphaware.test.integration.GraphAwareIntegrationTest.java

/**
 * Find all classes on classpath (only .class files) that have a method annotated with {@link Procedure}.
 *
 * @return classes with procedures./*from   w  w w .  jav a 2 s  .c  om*/
 */
private Iterable<Class> proceduresOnClassPath() {
    Enumeration<URL> urls;

    try {
        urls = this.getClass().getClassLoader().getResources("");
    } catch (IOException e) {
        throw new RuntimeException();
    }

    List<Class> classes = new ArrayList<>();

    while (urls.hasMoreElements()) {
        Iterator<File> fileIterator;
        File directory;

        try {
            directory = new File(urls.nextElement().toURI());
            fileIterator = FileUtils.iterateFiles(directory, new String[] { "class" }, true);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

        while (fileIterator.hasNext()) {
            File file = fileIterator.next();
            try {
                String path = file.getAbsolutePath();
                Class<?> candidate = Class
                        .forName(path.substring(directory.getAbsolutePath().length() + 1, path.length() - 6)
                                .replaceAll("\\/", "."));

                for (Method m : candidate.getDeclaredMethods()) {
                    if (m.isAnnotationPresent(Procedure.class)) {
                        classes.add(candidate);
                    }
                }

            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }

    return classes;
}

From source file:com.app.services.ExecutorServicesConstruct.java

public void getExecutorServices(Digester serverdigester, Hashtable servicesMap, File exectorServicesXml,
        WebClassLoader customClassLoader) throws Exception {
    ExecutorServices executorServices = (ExecutorServices) serverdigester
            .parse(new InputSource(new FileInputStream(exectorServicesXml)));
    CopyOnWriteArrayList<ExecutorService> executorServicesList = executorServices.getExecutorServices();
    ExecutorServiceAnnot executorServiceAnnot;
    for (ExecutorService executorService : executorServicesList) {
        Class executorServiceClass = customClassLoader
                .loadClass(executorService.getExecutorserviceclass().toString());
        //log.info("executor class in ExecutorServicesConstruct"+executorServiceClass);
        //log.info();
        Method[] methods = executorServiceClass.getDeclaredMethods();
        for (Method method : methods) {
            Annotation[] annotations = method.getDeclaredAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation instanceof ExecutorServiceAnnot) {
                    executorServiceAnnot = (ExecutorServiceAnnot) annotation;
                    ExecutorServiceInfo executorServiceInfo = new ExecutorServiceInfo();
                    executorServiceInfo.setExecutorServicesClass(executorServiceClass);
                    executorServiceInfo.setMethod(method);
                    //log.info(executorServiceAnnot.servicename());
                    //log.info("method.getName()="+method.getName());
                    //log.info("method.getParameterTypes()="+method.getParameterTypes());
                    executorServiceInfo.setMethodParams(method.getParameterTypes());
                    //log.info("method="+executorServiceAnnot.servicename());
                    //log.info("method info="+executorServiceInfo);
                    //if(servicesMap.get(executorServiceAnnot.servicename())==null)throw new Exception();
                    servicesMap.put(executorServiceAnnot.servicename(), executorServiceInfo);
                }//from   w  w  w.  jav a2s .  c  om
            }
        }
    }
}

From source file:com.interface21.transaction.interceptor.MapTransactionAttributeSource.java

public void addTransactionalMethod(String name, TransactionAttribute attr) {
    int lastDotIndex = name.lastIndexOf(".");
    if (lastDotIndex == -1)
        throw new TransactionUsageException(
                "'" + name + "' is not a valid method name: format is FQN.methodName");
    String className = name.substring(0, lastDotIndex);
    String methodName = name.substring(lastDotIndex + 1);

    logger.debug("Transactional method: " + className + "/" + methodName + " with transaction attribute string "
            + attr);// w w w  . ja  va 2  s.c  o m

    try {
        Class clazz = Class.forName(className);

        // TODO address method overloading? At present this will
        // simply match all methods that have the given name.
        // Consider EJB syntax (int, String) etc.?
        Method[] methods = clazz.getDeclaredMethods();
        List matchingMethods = new ArrayList();
        for (int i = 0; i < methods.length; i++) {
            if (isMatch(methods[i].getName(), methodName)) {
                matchingMethods.add(methods[i]);
            }
        }
        if (matchingMethods.isEmpty())
            throw new TransactionUsageException("Couldn't find method '" + methodName + "' on " + clazz);

        // register all matching methods
        for (Iterator it = matchingMethods.iterator(); it.hasNext();) {
            addTransactionalMethod((Method) it.next(), attr);
        }
    } catch (ClassNotFoundException ex) {
        throw new TransactionUsageException("Class '" + className + "' not found");
    }
}

From source file:com.taobao.adfs.util.Utilities.java

/**
 * not support more override method, ASM is a better solution.
 * refer: http://stackoverflow.com/questions/4024587/get-callers-method-not-name
 *//* w w  w.j  ava  2  s. co m*/
public static Method getCaller(boolean staticMethod) throws IOException {
    StackTraceElement caller = Thread.currentThread().getStackTrace()[3];
    Class<?> clazz = ClassCache.getWithIOException(caller.getClassName());
    for (Method method : clazz.getDeclaredMethods()) {
        if (!method.getName().equals(caller.getMethodName()))
            continue;
        if (Modifier.isStatic(method.getModifiers()) != staticMethod)
            continue;
        return method;
    }
    throw new IOException("fail to get caller");
}

From source file:com.incapture.rapgen.persistence.generated.factory.StorableClassesFactory.java

private List<String> createDeserSetters(Class<? extends Storable> storableClass, String typeName) {
    List<String> deserSetters = new LinkedList<>();

    Set<String> methodNames = new HashSet<>();
    for (Method method : storableClass.getDeclaredMethods()) {
        methodNames.add(method.getName());
    }//ww w .  j a  v  a 2s.  co m

    for (Field field : storableClass.getDeclaredFields()) {
        String name = field.getName();
        String capitalizedName = WordUtils.capitalize(name);
        //make sure there's a setter
        if (methodNames.contains("set" + capitalizedName)) {
            deserSetters.add(String.format("%s.set%2$s(extended.get%2s());", WordUtils.uncapitalize(typeName),
                    capitalizedName));
        }
    }
    return deserSetters;
}

From source file:com.dianping.resource.io.util.ClassUtils.java

/**
 * Does the given class or one of its superclasses at least have one or more
 * methods with the supplied name (with any argument types)?
 * Includes non-public methods./*from   ww w. java  2  s.com*/
 * @param clazz   the clazz to check
 * @param methodName the name of the method
 * @return whether there is at least one method with the given name
 */
public static boolean hasAtLeastOneMethodWithName(Class<?> clazz, String methodName) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.notNull(methodName, "Method name must not be null");
    Method[] declaredMethods = clazz.getDeclaredMethods();
    for (Method method : declaredMethods) {
        if (method.getName().equals(methodName)) {
            return true;
        }
    }
    Class<?>[] ifcs = clazz.getInterfaces();
    for (Class<?> ifc : ifcs) {
        if (hasAtLeastOneMethodWithName(ifc, methodName)) {
            return true;
        }
    }
    return (clazz.getSuperclass() != null && hasAtLeastOneMethodWithName(clazz.getSuperclass(), methodName));
}

From source file:com.app.services.ExecutorServicesConstruct.java

/**
 * This method removes the configuration of the War file
 * @param servicesMap/*from w ww  . j a v  a 2  s  .  c  o m*/
 * @param exectorServicesXml
 * @param customClassLoader
 * @throws Exception
 */
public void removeExecutorServices(Hashtable servicesMap, File exectorServicesXml,
        WebClassLoader customClassLoader) throws Exception {
    DigesterLoader serverdigesterLoader = DigesterLoader.newLoader(new FromXmlRulesModule() {

        protected void loadRules() {
            // TODO Auto-generated method stub
            try {
                loadXMLRules(new InputSource(new FileInputStream("./config/executorservices-config.xml")));
            } catch (Exception e) {
                log.error("Could not able to load xml rules ./config/executorservices-config.xml", e);
                // TODO Auto-generated catch block
                //e.printStackTrace();
            }

        }
    });
    Digester serverdigester = serverdigesterLoader.newDigester();
    ExecutorServices executorServices = (ExecutorServices) serverdigester
            .parse(new InputSource(new FileInputStream(exectorServicesXml)));
    CopyOnWriteArrayList<ExecutorService> executorServicesList = executorServices.getExecutorServices();
    ExecutorServiceAnnot executorServiceAnnot;
    for (ExecutorService executorService : executorServicesList) {
        Class executorServiceClass = customClassLoader
                .loadClass(executorService.getExecutorserviceclass().toString());
        //log.info("executor class in ExecutorServicesConstruct"+executorServiceClass);
        //log.info();
        Method[] methods = executorServiceClass.getDeclaredMethods();
        for (Method method : methods) {
            Annotation[] annotations = method.getDeclaredAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation instanceof ExecutorServiceAnnot) {
                    executorServiceAnnot = (ExecutorServiceAnnot) annotation;
                    //if(servicesMap.get(executorServiceAnnot.servicename())==null)throw new Exception();
                    servicesMap.remove(executorServiceAnnot.servicename());
                }
            }
        }
    }
}

From source file:com.freetmp.common.util.ClassUtils.java

public static boolean hasAtLeastOneMethodWithName(Class<?> clazz, String methodName) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.notNull(methodName, "Method name must not be null");
    Method[] declaredMethods = clazz.getDeclaredMethods();
    for (Method method : declaredMethods) {
        if (method.getName().equals(methodName)) {
            return true;
        }//w ww.  ja  v  a  2s . c o  m
    }
    Class<?>[] ifcs = clazz.getInterfaces();
    for (Class<?> ifc : ifcs) {
        if (hasAtLeastOneMethodWithName(ifc, methodName)) {
            return true;
        }
    }
    return (clazz.getSuperclass() != null && hasAtLeastOneMethodWithName(clazz.getSuperclass(), methodName));
}

From source file:com.web.services.ExecutorServicesConstruct.java

/**
 * This method configures the executor services during deployment of the war file
 * @param serverdigester/*from w ww .  j  a  v  a  2s.  co  m*/
 * @param servicesMap
 * @param exectorServicesXml
 * @param customClassLoader
 * @throws Exception
 */
public void getExecutorServices(Digester serverdigester, Hashtable servicesMap, File exectorServicesXml,
        WebClassLoader customClassLoader) throws Exception {
    ExecutorServices executorServices = (ExecutorServices) serverdigester
            .parse(new InputSource(new FileInputStream(exectorServicesXml)));
    CopyOnWriteArrayList<ExecutorService> executorServicesList = executorServices.getExecutorServices();
    ExecutorServiceAnnot executorServiceAnnot;
    for (ExecutorService executorService : executorServicesList) {
        Class executorServiceClass = customClassLoader
                .loadClass(executorService.getExecutorserviceclass().toString());
        //System.out.println("executor class in ExecutorServicesConstruct"+executorServiceClass);
        //System.out.println();
        Method[] methods = executorServiceClass.getDeclaredMethods();
        for (Method method : methods) {
            Annotation[] annotations = method.getDeclaredAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation instanceof ExecutorServiceAnnot) {
                    executorServiceAnnot = (ExecutorServiceAnnot) annotation;
                    ExecutorServiceInfo executorServiceInfo = new ExecutorServiceInfo();
                    executorServiceInfo.setExecutorServicesClass(executorServiceClass);
                    executorServiceInfo.setMethod(method);
                    System.out.println(executorServiceAnnot.servicename());
                    System.out.println("method.getName()=" + method.getName());
                    System.out.println("method.getParameterTypes()=" + method.getParameterTypes());
                    executorServiceInfo.setMethodParams(method.getParameterTypes());
                    //System.out.println("method="+executorServiceAnnot.servicename());
                    //System.out.println("method info="+executorServiceInfo);
                    //if(servicesMap.get(executorServiceAnnot.servicename())==null)throw new Exception();
                    servicesMap.put(executorServiceAnnot.servicename(), executorServiceInfo);
                }
            }
        }
    }
}