Example usage for java.lang.reflect Method toString

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

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string describing this Method .

Usage

From source file:org.flite.cach3.aop.CacheBase.java

public static Object validateReturnValueAsKeyObject(final Object returnValue, final Method methodToCache)
        throws Exception {
    if (returnValue == null) {
        throw new InvalidParameterException(String.format(
                "The result of the method [%s] is null, which will not give an appropriate cache key.",
                methodToCache.toString()));
    }//  w w  w.  j  ava  2 s.c o  m
    return returnValue;
}

From source file:org.cruxframework.crux.core.ioc.IocContainerManager.java

/**
 * /* w  w  w  .  jav a 2 s. co m*/
 * @param type
 * @param added
 * @param path
 * @param configurations
 */
private static void bindImplicityInjectionsForMethods(Class<?> type, Set<String> added, Set<String> path,
        Map<String, IocConfig<?>> configurations) {
    for (Method method : type.getDeclaredMethods()) {
        Inject inject = method.getAnnotation(Inject.class);
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (inject != null && !Modifier.isAbstract(method.getModifiers()) && parameterTypes != null
                && parameterTypes.length > 0) {
            if (!added.contains(method.toString())) {
                added.add(method.toString());
                for (int i = 0; i < parameterTypes.length; i++) {
                    Class<?> parameterType = parameterTypes[i];
                    if (isBindable(parameterType, false)) {
                        if (path.contains(parameterType.getCanonicalName())) {
                            throw new IoCException(
                                    "IoC Create Looping Error between classes [" + type.getCanonicalName()
                                            + "] and [" + parameterType.getCanonicalName() + "].");
                        }
                        Set<String> methodPath = new HashSet<String>();
                        methodPath.addAll(path);
                        methodPath.add(parameterType.getCanonicalName());
                        bindTypeImplicitly(parameterType, configurations);
                        bindImplicityInjectcions(parameterType, new HashSet<String>(), methodPath, false,
                                configurations);
                    }
                }
            }
        }
    }
}

From source file:MethodHashing.java

public static long calculateHash(Method method) {
    Map methodHashes = (Map) hashMap.get(method.getDeclaringClass());

    if (methodHashes == null) {
        methodHashes = getInterfaceHashes(method.getDeclaringClass());

        // Copy and add
        WeakHashMap newHashMap = new WeakHashMap();
        newHashMap.putAll(hashMap);//w  ww . java2s.  co m
        newHashMap.put(method.getDeclaringClass(), methodHashes);
        hashMap = newHashMap;
    }

    return ((Long) methodHashes.get(method.toString())).longValue();
}

From source file:org.evosuite.setup.TestUsageChecker.java

public static boolean canUse(Method m, Class<?> ownerClass) {

    if (m.isBridge()) {
        logger.debug("Excluding bridge method: " + m.toString());
        return false;
    }/* w  w w. ja v a2s.  co  m*/

    if (m.isSynthetic()) {
        logger.debug("Excluding synthetic method: " + m.toString());
        return false;
    }

    if (!Properties.USE_DEPRECATED && m.isAnnotationPresent(Deprecated.class)) {
        final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();

        if (Properties.hasTargetClassBeenLoaded() && !m.getDeclaringClass().equals(targetClass)) {
            logger.debug("Excluding deprecated method " + m.getName());
            return false;
        }
    }

    if (m.isAnnotationPresent(Test.class) || m.isAnnotationPresent(Before.class)
            || m.isAnnotationPresent(BeforeClass.class) || m.isAnnotationPresent(After.class)
            || m.isAnnotationPresent(AfterClass.class)) {
        logger.debug("Excluding test method " + m.getName());
        return false;
    }

    if (m.isAnnotationPresent(EvoSuiteTest.class)) {
        logger.debug("Excluding EvoSuite test method " + m.getName());
        return false;
    }

    if (m.isAnnotationPresent(EvoSuiteExclude.class)) {
        logger.debug("Excluding method with exclusion annotation " + m.getName());
        return false;
    }

    if (m.getDeclaringClass().equals(java.lang.Object.class)) {
        return false;
    }

    if (!m.getReturnType().equals(String.class)
            && (!canUse(m.getReturnType()) || !canUse(m.getGenericReturnType()))) {
        return false;
    }

    for (java.lang.reflect.Type paramType : m.getGenericParameterTypes()) {
        if (!canUse(paramType))
            return false;
    }

    if (m.getDeclaringClass().equals(Enum.class)) {
        return false;
        /*
        if (m.getName().equals("valueOf") || m.getName().equals("values")
         || m.getName().equals("ordinal")) {
           logger.debug("Excluding valueOf for Enum " + m.toString());
           return false;
        }
        // Skip compareTo on enums (like Randoop)
        if (m.getName().equals("compareTo") && m.getParameterTypes().length == 1
         && m.getParameterTypes()[0].equals(Enum.class))
           return false;
           */
    }

    if (m.getDeclaringClass().equals(java.lang.Thread.class))
        return false;

    // Hashcode only if we need to cover it
    if (m.getName().equals("hashCode")) {
        final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();

        if (!m.getDeclaringClass().equals(targetClass))
            return false;
        else {
            if (GraphPool.getInstance(ownerClass.getClassLoader()).getActualCFG(Properties.TARGET_CLASS,
                    m.getName() + Type.getMethodDescriptor(m)) == null) {
                // Don't cover generated hashCode
                // TODO: This should work via annotations
                return false;
            }
        }
    }

    // Randoop special case: just clumps together a bunch of hashCodes, so skip it
    if (m.getName().equals("deepHashCode") && m.getDeclaringClass().equals(Arrays.class))
        return false;

    // Randoop special case: differs too much between JDK installations
    if (m.getName().equals("getAvailableLocales"))
        return false;

    if (m.getName().equals(ClassResetter.STATIC_RESET)) {
        logger.debug("Ignoring static reset method");
        return false;
    }

    if (isForbiddenNonDeterministicCall(m)) {
        return false;
    }

    if (!Properties.CONSIDER_MAIN_METHODS && m.getName().equals("main") && Modifier.isStatic(m.getModifiers())
            && Modifier.isPublic(m.getModifiers())) {
        logger.debug("Ignoring static main method ");
        return false;
    }

    /*
    if(m.getTypeParameters().length > 0) {
       logger.debug("Cannot handle generic methods at this point");
       if(m.getDeclaringClass().equals(Properties.getTargetClass())) {
    LoggingUtils.getEvoLogger().info("* Skipping method "+m.getName()+": generic methods are not handled yet");
       }
       return false;
    }
    */

    // If default or
    if (Modifier.isPublic(m.getModifiers())) {
        TestClusterUtils.makeAccessible(m);
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(m.getModifiers())) {
        //              && !Modifier.isProtected(m.getModifiers())) {
        String packageName = ClassUtils.getPackageName(ownerClass);
        String declaredPackageName = ClassUtils.getPackageName(m.getDeclaringClass());
        if (packageName.equals(Properties.CLASS_PREFIX) && packageName.equals(declaredPackageName)) {
            TestClusterUtils.makeAccessible(m);
            return true;
        }
    }

    return false;
}

From source file:org.flite.cach3.aop.CacheBase.java

public static Method getKeyMethod(final Object keyObject, final CacheKeyMethodStore methodStore)
        throws NoSuchMethodException {
    final Method storedMethod = methodStore.find(keyObject.getClass());
    if (storedMethod != null) {
        return storedMethod;
    }/*from  www  .ja  v  a 2s .  co  m*/
    final Method[] methods = keyObject.getClass().getDeclaredMethods();
    Method targetMethod = null;
    for (final Method method : methods) {
        if (method != null && method.getAnnotation(CacheKeyMethod.class) != null) {
            if (method.getParameterTypes().length > 0) {
                throw new InvalidAnnotationException(
                        String.format("Method [%s] must have 0 arguments to be annotated with [%s]",
                                method.toString(), CacheKeyMethod.class.getName()));
            }
            if (!String.class.equals(method.getReturnType())) {
                throw new InvalidAnnotationException(
                        String.format("Method [%s] must return a String to be annotated with [%s]",
                                method.toString(), CacheKeyMethod.class.getName()));
            }
            if (targetMethod != null) {
                throw new InvalidAnnotationException(String.format(
                        "Class [%s] should have only one method annotated with [%s]. See [%s] and [%s]",
                        keyObject.getClass().getName(), CacheKeyMethod.class.getName(), targetMethod.getName(),
                        method.getName()));
            }
            targetMethod = method;
        }
    }

    if (targetMethod == null) {
        targetMethod = keyObject.getClass().getMethod("toString", null);
    }

    methodStore.add(keyObject.getClass(), targetMethod);

    return targetMethod;
}

From source file:org.flite.cach3.aop.CacheBase.java

public static void verifyReturnTypeIsList(final Method method, final Class annotationClass) {
    if (verifyTypeIsList(method.getReturnType())) {
        return;//from www.j  a  va 2s  .  c  o m
    }
    throw new InvalidAnnotationException(String.format(
            "The annotation [%s] is only valid on a method that returns a [%s]. "
                    + "[%s] does not fulfill this requirement by returning [%s].",
            annotationClass.getName(), List.class.getName(), method.toString(),
            method.getReturnType().getName()));
}

From source file:org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovyCallSiteSelector.java

private static boolean isMoreSpecific(Method more, Method less) {
    Class<?>[] moreParams = more.getParameterTypes();
    Class<?>[] lessParams = less.getParameterTypes();
    assert moreParams.length == lessParams.length;
    for (int i = 0; i < moreParams.length; i++) {
        Class<?> moreParam = Primitives.wrap(moreParams[i]);
        Class<?> lessParam = Primitives.wrap(lessParams[i]);
        if (moreParam.isAssignableFrom(lessParam)) {
            return false;
        } else if (lessParam.isAssignableFrom(moreParam)) {
            return true;
        }//from   w  w  w .ja v  a  2s  .  c o m
    }
    // Incomparable. Arbitrarily pick one of them.
    return more.toString().compareTo(less.toString()) > 0;
}

From source file:org.wallride.autoconfigure.CacheKeyGenerator.java

@Override
public Object generate(Object target, Method method, Object... params) {
    return method.toString() + " [" + StringUtils.arrayToCommaDelimitedString(params) + "]";
}

From source file:net.sourceforge.stripes.integration.spring.SpringHelper.java

/**
 * Looks for all methods and fields annotated with {@code @SpringBean} and attempts
 * to lookup and inject a managed bean into the field/property. If any annotated
 * element cannot be injected an exception is thrown.
 *
 * @param bean the bean into which to inject spring beans
 * @param ctx the Spring application context
 */// ww  w  .j a va 2 s . co m
public static void injectBeans(Object bean, ApplicationContext ctx) {
    // First inject any values using annotated methods
    for (Method m : getMethods(bean.getClass())) {
        try {
            SpringBean springBean = m.getAnnotation(SpringBean.class);
            boolean nameSupplied = !"".equals(springBean.value());
            String name = nameSupplied ? springBean.value() : methodToPropertyName(m);
            Class<?> beanType = m.getParameterTypes()[0];
            Object managedBean = findSpringBean(ctx, name, beanType, !nameSupplied);
            m.invoke(bean, managedBean);
        } catch (Exception e) {
            throw new StripesRuntimeException(
                    "Exception while trying to lookup and inject " + "a Spring bean into a bean of type "
                            + bean.getClass().getSimpleName() + " using method " + m.toString(),
                    e);
        }
    }

    // And then inject any properties that are annotated
    for (Field f : getFields(bean.getClass())) {
        try {
            SpringBean springBean = f.getAnnotation(SpringBean.class);
            boolean nameSupplied = !"".equals(springBean.value());
            String name = nameSupplied ? springBean.value() : f.getName();
            Object managedBean = findSpringBean(ctx, name, f.getType(), !nameSupplied);
            f.set(bean, managedBean);
        } catch (Exception e) {
            throw new StripesRuntimeException(
                    "Exception while trying to lookup and inject " + "a Spring bean into a bean of type "
                            + bean.getClass().getSimpleName() + " using field access on field " + f.toString(),
                    e);
        }
    }
}

From source file:cc.kune.core.server.rack.filters.rest.DefaultRESTMethodFinder.java

@Override
public RESTMethod findMethod(final String methodName, final Parameters parameters, final Class<?> serviceType) {
    final RESTServiceDefinition serviceDefinition = getServiceDefinition(serviceType);
    final Method[] serviceMethods = serviceDefinition.getMethods();
    LOG.debug("SERVICE METHODS: " + Arrays.toString(serviceMethods));
    for (final Method method : serviceMethods) {
        LOG.debug("CHECKING: " + method.toString());
        if (method.getName().equals(methodName)) {
            final REST methodAnnotation = method.getAnnotation(REST.class);
            if (checkParams(methodAnnotation, parameters)) {
                return new RESTMethod(method, methodAnnotation.params(), parameters, methodAnnotation.format());
            }//from   w  ww  . j  a  v a  2  s. co  m
        }
    }
    return null;
}