Example usage for java.lang Class getMethods

List of usage examples for java.lang Class getMethods

Introduction

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

Prototype

@CallerSensitive
public Method[] getMethods() throws SecurityException 

Source Link

Document

Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

Usage

From source file:com.braffdev.server.core.module.containerhandler.mapper.clazz.ClassMapper.java

/**
 * @param clazz/*w  w w.  jav a2  s  . com*/
 * @param container
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
private void mapServlet(Class<?> clazz, Container container)
        throws InstantiationException, IllegalAccessException {
    if (this.isServlet(clazz)) {
        Object instance = this.getInstance(clazz, container);

        this.methodMapper.setClassInstance(instance);

        List<Method> methods = Arrays.asList(clazz.getMethods());
        for (Method method : methods) {
            this.mapMethod(method, container);
        }

    }
}

From source file:com.billing.ng.plugin.Parameters.java

/**
 * Returns the field name of javabeans properties annotated with the
 * {@link Parameter} annotation./*ww w  .j  a  va  2s  . co  m*/
 *
 * @param type plugin class with parameter annotations
 * @return map of collected parameter field names and the associated annotation
 */
public Map<String, Parameter> getAnnotatedFields(Class<T> type) {
    Map<String, Parameter> fields = new HashMap<String, Parameter>();

    // collect public member methods of the class, including those defined on the interface
    // or those inherited from a super class or super interface.
    for (Method method : type.getMethods()) {
        Parameter annotation = method.getAnnotation(Parameter.class);
        if (annotation != null) {
            if (method.getName().startsWith("get") || method.getName().startsWith("set")) {
                fields.put(ClassUtils.getFieldName(method.getName()), annotation);
            }
        }
    }

    // collection all field annotations, including private fields that
    // we can to access via a public accessor method
    Class klass = type;
    while (klass != null) {
        for (Field field : klass.getDeclaredFields()) {
            Parameter annotation = field.getAnnotation(Parameter.class);
            if (annotation != null) {
                fields.put(field.getName(), annotation);
            }
        }

        // try the super class
        klass = klass.getSuperclass();
    }

    return fields;
}

From source file:edu.clemson.cs.nestbed.client.gui.MessageMonitorFrame.java

private List<Method> getPropertyMethodList(Class msgClass) {
    List<Method> methodList = new ArrayList<Method>();
    Method[] methods = msgClass.getMethods();

    for (Method i : methods) {
        if (i.getName().startsWith("get_")) {
            methodList.add(i);//from   www .  j ava  2 s .com
        }
    }

    return methodList;
}

From source file:net.mojodna.sprout.SproutAutoLoaderPlugIn.java

/**
 * Finds the method in the target class which corresponds to a registered
 * pathname.//from   www  . ja  v a2s.c  om
 * 
 * @param name Action portion of pathname.
 * @param clazz Target class.
 * @return Corresponding method.
 * @throws NoSuchMethodException when corresponding method cannot be found.
 */
private Method findMethod(final String name, final Class clazz) throws NoSuchMethodException {
    final Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        String methodName = methods[i].getName();
        if (methodName.equals("publick"))
            methodName = "public";
        if (methodName.equalsIgnoreCase(name.replaceAll("_([a-z])", "$1")))
            return methods[i];
    }
    throw new NoSuchMethodException(name);
}

From source file:com.katsu.jpa.dao.JpaDao.java

private void loadCollections(Class c, T e) {
    if (c.getSuperclass() != null) {
        loadCollections(c.getSuperclass(), e);
    }/*  w  w w. j a  va  2  s .  co m*/

    Method[] methods = c.getMethods();
    for (Method m : methods) {
        if (isOneToManyOrManyToManyMethod(m)) {
            try {
                ((Collection) m.invoke(e)).size();
                /*Collection<T> col = (Collection) m.invoke(e);
                for(T entity: col){
                loadCollections(entity.getClass(), entity);
                }*/
            } catch (Exception ex) {
                logger.error(ex);
            }
        }
        //Leo recursivamente las colecciones de la Tes realacionadas con el objeto a escanear
        /*if (isEntity(m)) {
        try {
        loadCollections(m.getReturnType(), (T) m.invoke(e));
        } catch (Exception ex) {
        logger.error(ex);
        }
        }*/
    }
    Field[] fields = c.getDeclaredFields();
    for (Field f : fields) {
        if (isOneToManyOrManyToManyField(f)) {
            try {
                Method m = e.getClass().getMethod(GET + firstCharUpper(f.getName()));
                ((Collection) m.invoke(e)).size();
            } catch (Exception ex) {
                logger.error(ex);
            }
        }
    }
}

From source file:hsa.awp.common.util.StaticInitializerBeanFactoryPostProcessor.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) {

    for (Iterator<?> classIterator = classes.keySet().iterator(); classIterator.hasNext();) {
        String className = (String) classIterator.next();
        // System.out.println("Class " + className + ":");
        Map<?, ?> vars = (Map<?, ?>) classes.get(className);
        Class<?> c = null;
        try {//from  w  w w .j  a v  a 2  s.  c o  m
            c = Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new StaticInitializerBeansException("Class not found for " + className, e);
        }
        Method[] methods = c.getMethods();
        for (Iterator<?> fieldIterator = vars.keySet().iterator(); fieldIterator.hasNext();) {
            String fieldName = (String) fieldIterator.next();
            Object value = vars.get(fieldName);
            Method method = findStaticSetter(methods, fieldName);
            if (method == null) {
                throw new StaticInitializerBeansException(
                        "No static setter method found for class " + className + ", field " + fieldName);
            }
            // System.out.println("\tFound method " + method.getName() + " for field " + fieldName + ", value " + value);
            Object newValue = bri.convertIfNecessary(value, getPropertyType(method));
            try {
                method.invoke(null, new Object[] { newValue });
            } catch (Exception e) {
                throw new StaticInitializerBeansException("Invocation of method " + method.getName()
                        + " on class " + className + " with value " + value + " failed.", e);
            }
        }
    }
}

From source file:com.googlecode.jdbcproc.daofactory.impl.block.service.ResultSetConverterBlockServiceImpl.java

private List<OneToOneLink> createOneToOneLinks(String parentTablePrefix,
        ParameterConverterService converterService, Class type, StoredProcedureInfo procedureInfo) {
    List<OneToOneLink> oneToOneLinks = new LinkedList<OneToOneLink>();
    for (Method method : type.getMethods()) {
        if (method.isAnnotationPresent(OneToOne.class) || method.isAnnotationPresent(ManyToOne.class)) {
            String tablePrefix = parentTablePrefix + getTablePrefixFromJoinColumnAnnotation(method);
            Class oneToOneClass = method.getReturnType();
            if (LOG.isDebugEnabled()) {
                LOG.debug("        Finded {}.{}", type.getSimpleName(), oneToOneClass.getSimpleName());
            }/*  w  w w . jav a2s  .c  o m*/
            Method oneToOneSetterMethod = BlockFactoryUtils.findSetterMethod(type, method);
            List<EntityPropertySetter> oneToOnePropertySetters = createEntityPropertySetters(converterService,
                    oneToOneClass, procedureInfo, tablePrefix);
            ResultSetConverterBlockEntity oneToOneBlock = new ResultSetConverterBlockEntity(oneToOneClass,
                    oneToOnePropertySetters,
                    createOneToOneLinks(tablePrefix, converterService, oneToOneClass, procedureInfo));
            oneToOneLinks.add(new OneToOneLink(oneToOneBlock, oneToOneSetterMethod));
        }
    }
    return oneToOneLinks;
}

From source file:com.jhkt.playgroundArena.db.nosql.mongodb.beans.AbstractDocument.java

public final BasicDBObject resolveToBasicDBObject() {

    BasicDBObject bDBObject = new BasicDBObject();
    try {//ww w .  j a  v a  2 s  .c  o m
        Class<? extends AbstractDocument> currClass = getClass();
        while (currClass != null) {

            for (Method method : currClass.getMethods()) {
                IDocumentKeyValue dkv = method.getAnnotation(IDocumentKeyValue.class);
                String mName = method.getName();
                if (dkv != null && mName.startsWith(JPAConstants.GETTER_PREFIX)) {

                    try {
                        Object returnValue = method.invoke(this, JPAConstants.EMPTY_OBJECT_ARGUMENT_LIST);
                        char[] propertyChars = mName.substring(3).toCharArray();
                        String property = String.valueOf(propertyChars[0]).toLowerCase()
                                + String.valueOf(propertyChars, 1, propertyChars.length - 1);

                        if (returnValue == null) {
                            continue;
                        }

                        if (returnValue instanceof AbstractDocument) {

                            Object subReturnValue = RESOLVE_TO_BASIC_DB_OBJECT_METHOD.invoke(returnValue,
                                    JPAConstants.EMPTY_OBJECT_ARGUMENT_LIST);
                            bDBObject.put(property, subReturnValue);

                        } else if (returnValue instanceof Enum) {

                            Enum<?> enumClass = Enum.class.cast(returnValue);
                            BasicDBObject enumObject = new BasicDBObject();

                            enumObject.put(JPAConstants.CONVERTER_CLASS.CLASS.name(),
                                    enumClass.getClass().getName());
                            enumObject.put(JPAConstants.CONVERTER_CLASS.CONTENT.name(), enumClass.name());

                            bDBObject.put(property, enumObject);

                        } else if (returnValue instanceof Collection) {

                            Collection<?> collectionContent = (Collection<?>) returnValue;
                            BasicDBObject collectionObject = new BasicDBObject();
                            collectionObject.put(JPAConstants.CONVERTER_CLASS.CLASS.name(),
                                    collectionContent.getClass().getName());

                            BasicDBList bDBList = new BasicDBList();
                            if (collectionContent.iterator().next() instanceof AbstractDocument) {
                                for (Object content : collectionContent) {
                                    if (content instanceof AbstractDocument) {
                                        Object subReturnValue = RESOLVE_TO_BASIC_DB_OBJECT_METHOD
                                                .invoke(returnValue, JPAConstants.EMPTY_OBJECT_ARGUMENT_LIST);
                                        bDBList.add(subReturnValue);
                                    }
                                }
                            } else {
                                bDBList.addAll(collectionContent);
                            }

                            collectionObject.put(JPAConstants.CONVERTER_CLASS.CONTENT.name(), bDBList);
                            bDBObject.put(property, collectionObject);

                        } else if (returnValue instanceof Map) {

                            Map<?, ?> mapContent = (Map<?, ?>) returnValue;
                            BasicDBObject mapObject = new BasicDBObject();
                            mapObject.put(JPAConstants.CONVERTER_CLASS.CLASS.name(),
                                    mapContent.getClass().getName());

                            Set<?> keys = mapContent.keySet();
                            if (keys.iterator().next() instanceof AbstractDocument) {

                                Map<Object, Object> convertedMap = new HashMap<Object, Object>();
                                for (Object key : keys) {
                                    Object value = mapContent.get(key);
                                    Object subReturnValue = RESOLVE_TO_BASIC_DB_OBJECT_METHOD.invoke(value,
                                            JPAConstants.EMPTY_OBJECT_ARGUMENT_LIST);

                                    convertedMap.put(key, subReturnValue);
                                }

                                mapContent = convertedMap;
                            }

                            mapObject.put(JPAConstants.CONVERTER_CLASS.CONTENT.name(), mapContent);
                            bDBObject.put(property, mapObject);

                        } else {
                            bDBObject.put(property, returnValue);
                        }

                    } catch (Exception e) {

                    }

                }
            }

            currClass = currClass.getSuperclass().asSubclass(AbstractDocument.class);
        }

    } catch (ClassCastException castException) {

    }

    bDBObject.put(JPAConstants.CONVERTER_CLASS.CLASS.name(), getClass().getName());
    _log.info("BdBObject " + bDBObject);
    return bDBObject;
}

From source file:net.sf.ehcache.config.BeanHandler.java

/**
 * Finds a setter method.//from w  w w  .j av  a 2 s  .  co m
 */
private Method findSetMethod(final Class objClass, final String prefix, final String name) throws Exception {
    final String methodName = makeMethodName(prefix, name);
    final Method[] methods = objClass.getMethods();
    Method candidate = null;
    for (int i = 0; i < methods.length; i++) {
        final Method method = methods[i];
        if (!method.getName().equals(methodName)) {
            continue;
        }
        if (Modifier.isStatic(method.getModifiers())) {
            continue;
        }
        if (method.getParameterTypes().length != 1) {
            continue;
        }
        if (!method.getReturnType().equals(Void.TYPE)) {
            continue;
        }
        if (candidate != null) {
            throw new Exception("Multiple " + methodName + "() methods in class " + objClass.getName() + ".");
        }
        candidate = method;
    }

    return candidate;
}