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.chukwa.rest.actions.RestController.java

private static String getObjectFields(Object obj) {
    StringBuilder s = new StringBuilder();
    try {//w ww .  ja  v  a 2 s  . com
        Class cls = obj.getClass();

        Method methlist[] = cls.getDeclaredMethods();
        int count = 0;
        for (int i = 0; i < methlist.length; i++) {
            Method m = methlist[i];
            if (m.getName().startsWith("get")) {
                String name = m.getName().substring(3);
                if (count != 0) {
                    s.append(",");
                }
                count += 1;
                s.append("\"" + name + "\"");
            }
        }
        s.append("\n");
    } catch (Throwable e) {
        System.err.println(e);
    }
    return s.toString();

}

From source file:com.facebook.stetho.inspector.MethodDispatcher.java

private static Map<String, MethodDispatchHelper> buildDispatchTable(ObjectMapper objectMapper,
        Iterable<ChromeDevtoolsDomain> domainHandlers) {
    Util.throwIfNull(objectMapper);
    HashMap<String, MethodDispatchHelper> methods = new HashMap<String, MethodDispatchHelper>();
    for (ChromeDevtoolsDomain domainHandler : Util.throwIfNull(domainHandlers)) {
        Class<?> handlerClass = domainHandler.getClass();
        String domainName = handlerClass.getSimpleName();

        for (Method method : handlerClass.getDeclaredMethods()) {
            if (isDevtoolsMethod(method)) {
                MethodDispatchHelper dispatchHelper = new MethodDispatchHelper(objectMapper, domainHandler,
                        method);//from  w w  w .  ja  v  a 2s  .c  o  m
                methods.put(domainName + "." + method.getName(), dispatchHelper);
            }
        }
    }
    return Collections.unmodifiableMap(methods);
}

From source file:com.example.captain_miao.grantap.utils.PermissionUtils.java

public static <A extends Annotation> Method findMethodPermissionDeniedWithRequestCode(Class clazz,
        Class<A> permissionFailClass, int requestCode) {
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isAnnotationPresent(permissionFailClass)) {
            if (requestCode == method.getAnnotation(PermissionDenied.class).requestCode()) {
                return method;
            }//ww  w.  j  ava  2s.  c  o m
        }
    }
    return null;
}

From source file:com.example.captain_miao.grantap.utils.PermissionUtils.java

public static <A extends Annotation> Method findMethodPermissionGrantedWithRequestCode(Class clazz,
        Class<A> permissionFailClass, int requestCode) {
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isAnnotationPresent(permissionFailClass)) {
            if (requestCode == method.getAnnotation(PermissionGranted.class).requestCode()) {
                return method;
            }//from   w w  w .j  a v  a 2  s . com
        }
    }
    return null;
}

From source file:Main.java

public static Map<String, Object> getProperties(Object object, boolean includeSuperClasses, boolean deepCopy) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    if (object == null) {
        return map;
    }//  w  w w .j  a  v a  2s  .co m

    Class<?> objectClass = object.getClass();
    Method[] methods = includeSuperClasses ? objectClass.getMethods() : objectClass.getDeclaredMethods();
    for (Method method : methods) {
        if (method.getParameterTypes().length == 0 && !method.getReturnType().equals(Void.TYPE)) {
            String methodName = method.getName();
            String propertyName = "";
            if (methodName.startsWith("get")) {
                propertyName = methodName.substring(3);
            } else if (methodName.startsWith("is")) {
                propertyName = methodName.substring(2);
            }
            if (propertyName.length() > 0 && Character.isUpperCase(propertyName.charAt(0))) {
                propertyName = Character.toLowerCase(propertyName.charAt(0)) + propertyName.substring(1);

                Object value = null;
                try {
                    value = method.invoke(object);
                } catch (Exception e) {
                }

                Object value2 = value;

                if (!deepCopy) {
                    map.put(propertyName, value);
                } else {
                    if (isSimpleObject(value)) {
                        map.put(propertyName, value);
                    } else if (value instanceof Map) {
                        Map<String, Object> submap = new HashMap<String, Object>();
                        for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
                            submap.put(String.valueOf(entry.getKey()),
                                    convertObject(entry.getValue(), includeSuperClasses));
                        }
                        map.put(propertyName, submap);
                    } else if (value instanceof Iterable) {
                        List<Object> sublist = new ArrayList<Object>();
                        for (Object v : (Iterable<?>) object) {
                            sublist.add(convertObject(v, includeSuperClasses));
                        }
                        map.put(propertyName, sublist);
                    } else if (value.getClass().isArray()) {
                        List<Object> sublist = new ArrayList<Object>();
                        int length = Array.getLength(value);
                        for (int i = 0; i < length; i++) {
                            sublist.add(convertObject(Array.get(value, i), includeSuperClasses));
                        }
                        map.put(propertyName, sublist);
                    } else {
                        map.put(propertyName, getProperties(value, includeSuperClasses, deepCopy));
                    }
                }
            }
        }
    }

    return map;
}

From source file:com.wavemaker.tools.apidocs.tools.parser.util.MethodUtils.java

private static Set<Method> getMethods(Class<?> type, Predicate<? super Method> predicate) {
    final Method[] declaredMethods = type.isInterface() ? type.getMethods() : type.getDeclaredMethods();

    Set<Method> filtered = new LinkedHashSet<>();

    if (declaredMethods != null && declaredMethods.length > 0) {
        for (final Method method : declaredMethods) {
            if (predicate.apply(method)) {
                filtered.add(method);//  w w w . ja v a 2 s. c  om
            }
        }
    }

    return filtered;
}

From source file:com.linecorp.armeria.server.docs.ServiceInfo.java

static ServiceInfo of(Class<?> serviceClass, Iterable<EndpointInfo> endpoints,
        Map<Class<?>, ? extends TBase<?, ?>> sampleRequests) throws ClassNotFoundException {

    requireNonNull(serviceClass, "serviceClass");

    final String name = serviceClass.getName();

    final ClassLoader serviceClassLoader = serviceClass.getClassLoader();
    final Class<?> interfaceClass = Class.forName(name + "$Iface", false, serviceClassLoader);
    final Method[] methods = interfaceClass.getDeclaredMethods();
    final Map<String, String> docStrings = ThriftDocString.getAllDocStrings(serviceClassLoader);

    final List<FunctionInfo> functions = new ArrayList<>(methods.length);
    final Set<ClassInfo> classes = new LinkedHashSet<>();
    for (Method method : methods) {
        final FunctionInfo function = FunctionInfo.of(method, sampleRequests, name, docStrings);
        functions.add(function);/*from  w  w  w  . j a  v  a  2s .co  m*/

        addClassIfPossible(classes, function.returnType());
        function.parameters().stream().forEach(p -> addClassIfPossible(classes, p.type()));
        function.exceptions().stream().forEach(e -> {
            e.fields().stream().forEach(f -> addClassIfPossible(classes, f.type()));
            addClassIfPossible(classes, e);
        });
    }

    return new ServiceInfo(name, functions, classes, endpoints, docStrings.get(name));
}

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());
        }/*from   w w w  . jav  a 2  s  .co m*/
    }

    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:org.apache.hadoop.chukwa.rest.actions.RestController.java

private static String getObjectValues(Object obj) {
    StringBuilder s = new StringBuilder();
    try {//from  www  .ja  va  2s .  c o  m
        Class cls = obj.getClass();

        Method methlist[] = cls.getDeclaredMethods();
        int count = 0;
        for (int i = 0; i < methlist.length; i++) {
            Method m = methlist[i];
            if (m.getName().startsWith("get")) {
                String name = m.getName();
                Object oret = null;
                try {
                    @SuppressWarnings("unchecked")
                    Method meth = cls.getMethod(name);
                    if (meth == null) {
                        continue;
                    }
                    oret = meth.invoke(obj);
                } catch (Exception e) {
                    continue;
                }
                if (count != 0) {
                    s.append(",");
                }

                count += 1;
                if (oret == null) {
                    s.append("\"\"");
                } else if ((oret instanceof Date) || (oret instanceof java.sql.Timestamp)) {
                    long time = 0;
                    if (oret instanceof Date) {
                        Date d = (Date) oret;
                        time = d.getTime();
                    } else if (oret instanceof java.sql.Timestamp) {
                        java.sql.Timestamp d = (java.sql.Timestamp) oret;
                        time = d.getTime();
                    }

                    String date = DateFormatUtils.format(time, "yyyy-MM-dd HH:mm:ss");
                    s.append("\"" + date + "\"");
                } else {
                    s.append("\"" + oret.toString() + "\"");
                }
            }
        }
        s.append("\n");
    } catch (Throwable e) {
        System.err.println(e);
    }
    return s.toString();
}

From source file:com.example.captain_miao.grantap.utils.PermissionUtils.java

public static List<Method> findAnnotationMethods(Class clazz, Class<? extends Annotation> clazz1) {
    List<Method> methods = new ArrayList<>();
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isAnnotationPresent(clazz1)) {
            methods.add(method);/*from   w ww .  j  a v a 2s.co  m*/
        }
    }
    return methods;
}