Example usage for java.lang.reflect Method getName

List of usage examples for java.lang.reflect Method getName

Introduction

In this page you can find the example usage for java.lang.reflect Method getName.

Prototype

@Override
public String getName() 

Source Link

Document

Returns the name of the method represented by this Method object, as a String .

Usage

From source file:com.github.jknack.handlebars.internal.BaseTemplate.java

/**
 * Creates a new {@link TypeSafeTemplate}.
 *
 * @param rootType The target type.//from w  w  w .j a  v  a2  s .c  o  m
 * @param template The target template.
 * @return A new {@link TypeSafeTemplate}.
 */
private static Object newTypeSafeTemplate(final Class<?> rootType, final Template template) {
    return Proxy.newProxyInstance(template.getClass().getClassLoader(), new Class[] { rootType },
            new InvocationHandler() {
                private Map<String, Object> attributes = new HashMap<String, Object>();

                @Override
                public Object invoke(final Object proxy, final Method method, final Object[] args)
                        throws IOException {
                    String methodName = method.getName();
                    if ("apply".equals(methodName)) {
                        Context context = Context.newBuilder(args[0]).combine(attributes).build();
                        attributes.clear();
                        if (args.length == 2) {
                            template.apply(context, (Writer) args[1]);
                            return null;
                        }
                        return template.apply(context);
                    }

                    if (Modifier.isPublic(method.getModifiers()) && methodName.startsWith("set")) {
                        String attrName = StringUtils.uncapitalize(methodName.substring("set".length()));
                        if (args != null && args.length == 1 && attrName.length() > 0) {
                            attributes.put(attrName, args[0]);
                            if (TypeSafeTemplate.class.isAssignableFrom(method.getReturnType())) {
                                return proxy;
                            }
                            return null;
                        }
                    }
                    String message = String.format(
                            "No handler method for: '%s(%s)', expected method signature is: 'setXxx(value)'",
                            methodName, args == null ? "" : join(args, ", "));
                    throw new UnsupportedOperationException(message);
                }
            });
}

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

public static String getMethodUniqueIdentifierId(Method method) {
    Class<?>[] parameterTypes = method.getParameterTypes();
    HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
    for (Class parameterType : parameterTypes) {
        hashCodeBuilder.append(parameterType.getCanonicalName());
    }//from w  w w .j a v  a  2  s.c  om
    return method.getName() + "-" + parameterTypes.length + "-" + hashCodeBuilder.toHashCode();
}

From source file:net.mindengine.blogix.BlogixMain.java

private static Method findCommandMethod(String name) {
    Method[] methods = BlogixMain.class.getDeclaredMethods();
    for (Method method : methods) {
        if (Modifier.isStatic(method.getModifiers()) && method.getName().equals("cmd_" + name)) {
            return method;
        }// ww  w  . j ava  2  s . c  o m
    }
    return null;
}

From source file:com.amalto.core.metadata.ClassRepository.java

private static String getName(Method declaredMethod) {
    if (isGetter(declaredMethod)) {
        return format(declaredMethod.getName().substring(GETTER_PREFIX.length()));
    } else if (isBooleanGetter(declaredMethod)) {
        return format(declaredMethod.getName().substring(BOOLEAN_PREFIX.length()));
    } else {/*  ww w  . j ava2 s  .co  m*/
        throw new IllegalArgumentException(
                "Cannot infer field name from method '" + declaredMethod.getName() + "'.");
    }
}

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

/**
 * Determines if the method is a {@link ChromeDevtoolsMethod}, and validates accordingly
 * if it is./*  w w w  .  ja  va  2s .c o  m*/
 *
 * @throws IllegalArgumentException Thrown if it is a {@link ChromeDevtoolsMethod} but
 *     it otherwise fails to satisfy requirements.
 */
private static boolean isDevtoolsMethod(Method method) throws IllegalArgumentException {
    if (!method.isAnnotationPresent(ChromeDevtoolsMethod.class)) {
        return false;
    } else {
        Class<?> args[] = method.getParameterTypes();
        String methodName = method.getDeclaringClass().getSimpleName() + "." + method.getName();
        Util.throwIfNot(args.length == 2, "%s: expected 2 args, got %s", methodName, args.length);
        Util.throwIfNot(args[0].equals(JsonRpcPeer.class), "%s: expected 1st arg of JsonRpcPeer, got %s",
                methodName, args[0].getName());
        Util.throwIfNot(args[1].equals(JSONObject.class), "%s: expected 2nd arg of JSONObject, got %s",
                methodName, args[1].getName());

        Class<?> returnType = method.getReturnType();
        if (!returnType.equals(void.class)) {
            Util.throwIfNot(JsonRpcResult.class.isAssignableFrom(returnType),
                    "%s: expected JsonRpcResult return type, got %s", methodName, returnType.getName());
        }
        return true;
    }
}

From source file:springfox.documentation.spring.web.readers.operation.HandlerMethodResolver.java

private static Predicate<ResolvedMethod> methodNamesAreSame(final Method methodToResolve) {
    return new Predicate<ResolvedMethod>() {
        @Override//from w  w  w  . j  a  v a2  s  . c o  m
        public boolean apply(ResolvedMethod input) {
            return input.getRawMember().getName().equals(methodToResolve.getName());
        }
    };
}

From source file:com.mmj.app.common.notify.NotifyUtils.java

private static void parserAnnotation(Result result, Object obj, Method method,
        Map<EventType, Set<MethodDescriptor>> container) {
    EventConfig eventConfig = method.getAnnotation(EventConfig.class);
    EventType[] events = eventConfig.events();
    if (events == null || events.length == 0) {
        logger.warn("No eventConfig FOUND in ?" + method.getName() + "");
        return;// w w  w  . jav a  2  s  .com
    }
    if (logger.isDebugEnabled()) {
        logger.debug("found ?" + method.getName() + "!");
    }
    for (int i = 0, j = events.length; i < j; i++) {
        Set<MethodDescriptor> methodSet = container.get(events[i]);
        if (methodSet == null) {
            methodSet = new HashSet<MethodDescriptor>();
            container.put(events[i], methodSet);
        }
        MethodDescriptor md = new MethodDescriptor(obj, method);
        methodSet.add(md);
        result.appendMessage(md.toString());
    }
}

From source file:com.zb.app.common.notify.NotifyUtils.java

private static void parserAnnotation(Result result, Object obj, Method method,
        Map<EventType, Set<MethodDescriptor>> container) {
    EventConfig eventConfig = method.getAnnotation(EventConfig.class);
    EventType[] events = eventConfig.events();
    if (events == null || events.length == 0) {
        logger.warn("No eventConfig FOUND in ?" + method.getName() + "");
        return;/*w  w  w  . j a va 2s . com*/
    }
    if (logger.isDebugEnabled()) {
        logger.debug("found ?" + method.getName() + "!");
    }
    for (int i = 0, j = events.length; i < j; i++) {
        Set<MethodDescriptor> methodSet = container.get(events[i]);
        if (methodSet == null) {
            methodSet = new ConcurrentHashSet<MethodDescriptor>();
            container.put(events[i], methodSet);
        }
        MethodDescriptor md = new MethodDescriptor(obj, method);
        methodSet.add(md);
        result.appendMessage(md.toString());
    }
}

From source file:org.zywx.wbpalmstar.plugin.uexiconlist.utils.IconListUtils.java

public static boolean isMethodExist(String className, String methodName) {
    boolean isExist = false;
    try {/*from   w  w w. j  a v a2 s.c o  m*/
        Class<?> mClass = Class.forName(className);
        Method[] methodList = mClass.getMethods();
        for (Method method : methodList) {
            if (methodName.equals(method.getName())) {
                isExist = true;
                break;
            }
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return isExist;
}

From source file:it.unibo.alchemist.language.protelis.util.ReflectionUtils.java

private static Method loadBestMethod(final Class<?> clazz, final String methodName, final Class<?>[] argClass) {
    Objects.requireNonNull(clazz, "The class on which the method will be invoked can not be null.");
    Objects.requireNonNull(methodName, "Method name can not be null.");
    Objects.requireNonNull(argClass, "Method arguments can not be null.");
    /*//  w w w.java  2s . c  o m
     * If there is a matching method, return it
     */
    try {
        return clazz.getMethod(methodName, argClass);
    } catch (NoSuchMethodException | SecurityException e) {
        /*
         * Look it up on the cache
         */
        /*
         * Deal with Java method overloading scoring methods
         */
        final Method[] candidates = clazz.getMethods();
        final List<Pair<Integer, Method>> lm = new ArrayList<>(candidates.length);
        for (final Method m : candidates) {
            // TODO: Workaround for different Method API
            if (m.getParameterTypes().length == argClass.length && methodName.equals(m.getName())) {
                final Class<?>[] params = m.getParameterTypes();
                int p = 0;
                boolean compatible = true;
                for (int i = 0; compatible && i < argClass.length; i++) {
                    final Class<?> expected = params[i];
                    if (expected.isAssignableFrom(argClass[i])) {
                        /*
                         * No downcast required, there is compatibility
                         */
                        p++;
                    } else if (!PrimitiveUtils.classIsNumber(expected)) {
                        compatible = false;
                    }
                }
                if (compatible) {
                    lm.add(new Pair<>(p, m));
                }
            }
        }
        /*
         * Find best
         */
        if (lm.size() > 0) {
            Pair<Integer, Method> max = lm.get(0);
            for (Pair<Integer, Method> cPair : lm) {
                if (cPair.getFirst().compareTo(max.getFirst()) > 0) {
                    max = cPair;
                }
            }
            return max.getSecond();
        }
    }
    List<Class<?>> list = new ArrayList<>();
    for (Class<?> c : argClass) {
        list.add(c);
    }
    final String argType = list.toString();
    throw new NoSuchMethodError(
            methodName + "/" + argClass.length + argType + " does not exist in " + clazz + ".");
}