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:org.querybyexample.jpa.JpaUtil.java

public static boolean isEntityIdManuallyAssigned(Class<?> type) {
    for (Method method : type.getMethods()) {
        if (isPrimaryKey(method)) {
            return isManuallyAssigned(method);
        }/* www  .  jav a2s .c  o  m*/
    }
    return false; // no pk found, should not happen
}

From source file:com.opensymphony.xwork2.util.AnnotationUtils.java

/**
 * For the given <code>Class</code> get a collection of the the {@link AnnotatedElement}s 
 * that match the given <code>annotation</code>s or if no <code>annotation</code>s are 
 * specified then return all of the annotated elements of the given <code>Class</code>. 
 * Includes only the method level annotations.
 * //from   w  w  w  .j  a  v a  2  s.  co  m
 * @param clazz The {@link Class} to inspect
 * @param annotation the {@link Annotation}s to find
 * @return A {@link Collection}&lt;{@link AnnotatedElement}&gt; containing all of the
 *  method {@link AnnotatedElement}s matching the specified {@link Annotation}s
 */
public static Collection<Method> getAnnotatedMethods(Class clazz, Class<? extends Annotation>... annotation) {
    Collection<Method> toReturn = new HashSet<>();

    for (Method m : clazz.getMethods()) {
        if (ArrayUtils.isNotEmpty(annotation) && isAnnotatedBy(m, annotation)) {
            toReturn.add(m);
        } else if (ArrayUtils.isEmpty(annotation) && ArrayUtils.isNotEmpty(m.getAnnotations())) {
            toReturn.add(m);
        }
    }

    return toReturn;
}

From source file:com.facebook.stetho.json.ObjectMapper.java

/**
 *
 * @param clazz//from   w  w w. j  a  v  a  2  s.  c  om
 * @return the first method annotated with {@link JsonValue} or null if one does not exist.
 */
@Nullable
private static Method getJsonValueMethod(Class<?> clazz) {
    Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; ++i) {
        Annotation jsonValue = methods[i].getAnnotation(JsonValue.class);
        if (jsonValue != null) {
            return methods[i];
        }
    }
    return null;
}

From source file:Main.java

static Map<String, Method> getGetPropertyMethods(Class<?> beanClass) {
    HashMap<String, Method> props;
    if (getPropsCache.containsKey(beanClass)) {
        props = getPropsCache.get(beanClass);
    } else {// www  .  j  a v  a2 s.c om
        props = new HashMap<String, Method>();
        getPropsCache.put(beanClass, props);
        Method[] ms = beanClass.getMethods();
        for (int i = 0; i < ms.length; i++) {
            Method m = ms[i];
            String name = m.getName();
            if (name.startsWith("get") && name.length() > 3 && Character.isUpperCase(name.charAt(3))) {
                name = name.substring(3);
                props.put(name, m);
            }
        }
    }
    return props;
}

From source file:Main.java

static Map<String, Method> getSetPropertyMethods(Class<?> beanClass) {
    HashMap<String, Method> props;
    if (setPropsCache.containsKey(beanClass)) {
        props = setPropsCache.get(beanClass);
    } else {/*from   w w w .  j  a v  a2  s.c om*/
        props = new HashMap<String, Method>();
        setPropsCache.put(beanClass, props);
        Method[] ms = beanClass.getMethods();
        for (int i = 0; i < ms.length; i++) {
            Method m = ms[i];
            String name = m.getName();
            if (name.startsWith("set") && name.length() > 3 && Character.isUpperCase(name.charAt(3))
                    && m.getParameterTypes().length == 1) {
                name = name.substring(3);
                props.put(name, m);
            }
        }
    }
    return props;
}

From source file:com.weibo.api.motan.protocol.grpc.GrpcUtil.java

public static Method getMethod(String name, Class<?> interfaceClazz) {
    int index = name.lastIndexOf("/");
    if (index > -1) {
        name = name.substring(name.lastIndexOf("/") + 1);
    }/*www .j av a2  s. co m*/

    Method[] methods = interfaceClazz.getMethods();
    for (Method m : methods) {
        if (m.getName().equalsIgnoreCase(name)) {
            return m;
        }
    }
    throw new MotanFrameworkException("not find grpc method");
}

From source file:com.taobao.weex.devtools.json.ObjectMapper.java

@Nullable
private static Method getJsonValueMethodImpl(Class<?> clazz) {
    Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; ++i) {
        Annotation jsonValue = methods[i].getAnnotation(JsonValue.class);
        if (jsonValue != null) {
            return methods[i];
        }//from w w  w.  j  a va2 s.  c o m
    }
    return null;
}

From source file:com.revolsys.util.JavaBeanUtil.java

public static List<Method> getMethods(final Class<?> clazz) {
    final Method[] methods = clazz.getMethods();
    Arrays.sort(methods, new Comparator<Method>() {
        @Override// w  w  w.  j  a va2 s .  c om
        public int compare(final Method method1, final Method method2) {
            final String name1 = method1.getName().replaceAll("^(set|get|is)", "").toLowerCase();
            final String name2 = method2.getName().replaceAll("^(set|get|is)", "").toLowerCase();
            final int nameCompare = name1.compareTo(name2);
            return nameCompare;
        }
    });
    return Arrays.asList(methods);
}

From source file:io.rhiot.cloudplatform.service.binding.OperationBinding.java

static OperationBinding operationBinding(PayloadEncoding payloadEncoding, String channel,
        byte[] incomingPayload, Map<String, Object> headers, Registry registry) {
    String rawChannel = channel.substring(channel.lastIndexOf('/') + 1);
    String[] channelParts = rawChannel.split("\\.");
    String service = channelParts[0];
    String operation = channelParts[1];

    List<Object> arguments = new LinkedList<>(asList(channelParts).subList(2, channelParts.length));

    for (Map.Entry<String, Object> header : headers.entrySet()) {
        if (header.getKey().startsWith("RHIOT_ARG")) {
            arguments.add(header.getValue());
        }//  w  ww.  j  a  va 2s . c om
    }

    Object bean = registry.lookupByName(service);
    Validate.notNull(bean, "Cannot find service with name '%s'.", service);
    Class beanType = bean.getClass();

    LOG.debug("Detected service bean type {} for operation: {}", beanType, operation);
    List<Method> beanMethods = new ArrayList<>(asList(beanType.getDeclaredMethods()));
    beanMethods.addAll(asList(beanType.getMethods()));
    Method operationMethod = beanMethods.stream().filter(method -> method.getName().equals(operation)).findAny()
            .get();

    if (incomingPayload != null && incomingPayload.length > 0) {
        Object payload = incomingPayload;
        if (operationMethod.getParameterTypes()[operationMethod.getParameterTypes().length
                - 1] != byte[].class) {
            payload = payloadEncoding.decode(incomingPayload);
        }
        arguments.add(payload);
    }
    return new OperationBinding(service, operation, arguments, operationMethod);
}

From source file:io.neba.core.resourcemodels.factory.ModelInstantiator.java

/**
 * @return all public methods annotated with @Inject. Fails if a public method
 * annotated with @Inject does not take exactly one argument.
 *//*from ww  w .j a  v  a 2 s  .c  o  m*/
@Nonnull
private static ModelServiceSetter[] resolveServiceSetters(@Nonnull Class<?> modelType) {
    List<ModelServiceSetter> serviceSetters = new ArrayList<>();
    for (Method method : modelType.getMethods()) {
        if (isStatic(method.getModifiers())) {
            continue;
        }
        if (!annotations(method).containsName(INJECT_ANNOTATION_NAME)) {
            continue;
        }

        if (method.getParameterCount() != 1) {
            throw new InvalidModelException("The method " + method
                    + " is annotated with @Inject and must thus take exactly one argument.");
        }

        Filter filter = findFilterAnnotation(method.getParameterAnnotations()[0]);
        Type serviceType = method.getGenericParameterTypes()[0];
        ServiceDependency serviceDependency = new ServiceDependency(serviceType, modelType, filter);
        serviceSetters.add(new ModelServiceSetter(serviceDependency, method));
    }
    return serviceSetters.toArray(new ModelServiceSetter[0]);
}