Example usage for java.lang.reflect Method equals

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

Introduction

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

Prototype

public boolean equals(Object obj) 

Source Link

Document

Compares this Method against the specified object.

Usage

From source file:org.robospring.inject.RoboSpringInjector.java

private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
    LinkedList<AbstractInjectedElement> elements = new LinkedList<AbstractInjectedElement>();
    Class<?> targetClass = clazz;

    do {/*from   w w w  .ja  v  a 2 s.  c  o m*/
        LinkedList<AbstractInjectedElement> currElements = new LinkedList<AbstractInjectedElement>();
        /*************************************************************
         * Fields
         *************************************************************/
        for (Field field : targetClass.getDeclaredFields()) {
            Annotation annotation = findAutowiredAnnotation(field);
            if (annotation != null) {
                if (Modifier.isStatic(field.getModifiers())) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation is not supported on static fields: " + field);
                    }
                    continue;
                }
                boolean required = determineRequiredStatus(annotation);
                currElements.add(new InjectedFieldElement(this, field, required, annotation));
            }
        }
        /*************************************************************
         * Methods
         *************************************************************/
        for (Method method : targetClass.getDeclaredMethods()) {
            Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
            Annotation annotation = BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)
                    ? findAutowiredAnnotation(bridgedMethod)
                    : findAutowiredAnnotation(method);
            if (annotation != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                if (Modifier.isStatic(method.getModifiers())) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation is not supported on static methods: " + method);
                    }
                    continue;
                }
                if (method.getParameterTypes().length == 0) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation should be used on methods with actual parameters: "
                                + method);
                    }
                }
                boolean required = determineRequiredStatus(annotation);
                currElements.add(new InjectedMethodElement(this, method, required, annotation));
            }
        }
        elements.addAll(0, currElements);
        targetClass = targetClass.getSuperclass();
        // skip android super classes - they won't be annotated
    } while (targetClass != null && !targetClass.getName().startsWith("android.")
            && targetClass != Object.class);

    return new InjectionMetadata(clazz, elements);
}

From source file:org.seedstack.seed.core.internal.transaction.AbstractTransactionManager.java

@SuppressWarnings("unchecked")
private Transactional deepGetAnnotation(final Method method, Class<?> targetClass) {
    Transactional transaction = method.getAnnotation(Transactional.class);

    // Fetching annotations from method (we fetch all methods in super and interfaces)
    if (transaction == null) {
        Predicate<? super Method> predicate = (Predicate<Method>) input -> (!method.equals(input))
                && methodIsEqual(method, input);

        Set<Method> methods = ReflectionUtils.getAllMethods(targetClass, predicate);

        for (Method method2 : methods) {
            transaction = method2.getAnnotation(Transactional.class);
            if (transaction != null) {
                break;
            }/*from www. j a va 2  s . co  m*/
        }
    }

    // Fetching annotation from class
    if (transaction == null) {
        transaction = SeedReflectionUtils.getMetaAnnotationFromAncestors(targetClass, Transactional.class);
    }

    return transaction;
}

From source file:org.seedstack.seed.core.utils.SeedReflectionUtils.java

/**
 * Given the class targetClass (not an interface) the method will return all matched methods
 * for method. Methods will be reached in extended super classes or implemented interfaces.
 *
 * @param method from which the search starts.
 * @return a set of methods grabbed from parent classes and interfaces.
 *///w w  w. j av a 2  s  . c  om
public static Set<Method> methodsFromAncestors(final Method method) {
    // Fetching annotations from method (we fetch all methods in super and
    // interfaces)

    Class<?> targetClass = method.getDeclaringClass();

    Predicate<? super Method> predicate = new Predicate<Method>() {
        @Override
        public boolean apply(Method input) {
            return !method.equals(input) && methodsAreEquivalent(method, input);
        }
    };

    Set<Method> methods = new HashSet<Method>();
    methods.add(method);
    methods.addAll(ReflectionUtils.getAllMethods(targetClass, predicate));

    return methods;
}

From source file:org.seedstack.seed.transaction.internal.AbstractTransactionManager.java

private Transactional deepGetAnnotation(final Method method, Class<?> targetClass) {
    Transactional transaction = method.getAnnotation(Transactional.class);

    // Fetching annotations from method (we fetch all methods in super and interfaces)
    if (transaction == null) {
        Predicate<? super Method> predicate = new Predicate<Method>() {
            @Override/*  w w  w  . j a v  a 2 s . co  m*/
            public boolean apply(Method input) {
                return (!method.equals(input)) && methodIsEqual(method, input);
            }
        };

        Set<Method> methods = ReflectionUtils.getAllMethods(targetClass, predicate);

        for (Method method2 : methods) {
            transaction = method2.getAnnotation(Transactional.class);
            if (transaction != null) {
                break;
            }
        }
    }

    // Fetching annotation from class
    if (transaction == null) {
        transaction = SeedReflectionUtils.getMetaAnnotationFromAncestors(targetClass, Transactional.class);
    }

    return transaction;
}

From source file:org.shaman.rpg.editor.dbviewer.TreeTableModelFactory.java

private static String getFieldName(Method method) {
    try {// w  w w .  j  a v  a2 s .  c o m
        Class<?> clazz = method.getDeclaringClass();
        BeanInfo info = Introspector.getBeanInfo(clazz);
        PropertyDescriptor[] props = info.getPropertyDescriptors();
        for (PropertyDescriptor pd : props) {
            if (method.equals(pd.getWriteMethod()) || method.equals(pd.getReadMethod())) {
                //               System.out.println(pd.getDisplayName());
                return pd.getName();
            }
        }
    } catch (Exception e) {
        LOG.log(Level.FINE, "unable to get field name for method " + method, e);
    }

    return method.getName();
}

From source file:org.springframework.batch.core.jsr.configuration.support.SpringAutowiredAnnotationBeanPostProcessor.java

protected InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
    LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
    Class<?> targetClass = clazz;

    do {//from w ww  . j  a  va  2  s .com
        LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>();
        for (Field field : targetClass.getDeclaredFields()) {
            Annotation annotation = findAutowiredAnnotation(field);
            if (annotation != null) {
                if (Modifier.isStatic(field.getModifiers())) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation is not supported on static fields: " + field);
                    }
                    continue;
                }
                boolean required = determineRequiredStatus(annotation);
                currElements.add(new AutowiredFieldElement(field, required));
            }
        }
        for (Method method : targetClass.getDeclaredMethods()) {
            Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
            Annotation annotation = BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)
                    ? findAutowiredAnnotation(bridgedMethod)
                    : findAutowiredAnnotation(method);
            if (annotation != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                if (Modifier.isStatic(method.getModifiers())) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation is not supported on static methods: " + method);
                    }
                    continue;
                }
                if (method.getParameterTypes().length == 0) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation should be used on methods with actual parameters: "
                                + method);
                    }
                }
                boolean required = determineRequiredStatus(annotation);
                PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
                currElements.add(new AutowiredMethodElement(method, required, pd));
            }
        }
        elements.addAll(0, currElements);
        targetClass = targetClass.getSuperclass();
    } while (targetClass != null && targetClass != Object.class);

    return new InjectionMetadata(clazz, elements);
}

From source file:org.springframework.beans.BeanUtils.java

/**
 * Find a JavaBeans {@code PropertyDescriptor} for the given method,
 * with the method either being the read method or the write method for
 * that bean property./*from   w  ww . j a v  a 2s .  co  m*/
 * @param method the method to find a corresponding PropertyDescriptor for
 * @param clazz the (most specific) class to introspect for descriptors
 * @return the corresponding PropertyDescriptor, or {@code null} if none
 * @throws BeansException if PropertyDescriptor lookup fails
 * @since 3.2.13
 */
@Nullable
public static PropertyDescriptor findPropertyForMethod(Method method, Class<?> clazz) throws BeansException {
    Assert.notNull(method, "Method must not be null");
    PropertyDescriptor[] pds = getPropertyDescriptors(clazz);
    for (PropertyDescriptor pd : pds) {
        if (method.equals(pd.getReadMethod()) || method.equals(pd.getWriteMethod())) {
            return pd;
        }
    }
    return null;
}

From source file:org.springframework.beans.GenericTypeAwarePropertyDescriptor.java

public GenericTypeAwarePropertyDescriptor(Class<?> beanClass, String propertyName, @Nullable Method readMethod,
        @Nullable Method writeMethod, Class<?> propertyEditorClass) throws IntrospectionException {

    super(propertyName, null, null);
    this.beanClass = beanClass;

    Method readMethodToUse = (readMethod != null ? BridgeMethodResolver.findBridgedMethod(readMethod) : null);
    Method writeMethodToUse = (writeMethod != null ? BridgeMethodResolver.findBridgedMethod(writeMethod)
            : null);/* w w w . j  a  va 2 s  . co m*/
    if (writeMethodToUse == null && readMethodToUse != null) {
        // Fallback: Original JavaBeans introspection might not have found matching setter
        // method due to lack of bridge method resolution, in case of the getter using a
        // covariant return type whereas the setter is defined for the concrete property type.
        Method candidate = ClassUtils.getMethodIfAvailable(this.beanClass,
                "set" + StringUtils.capitalize(getName()), (Class<?>[]) null);
        if (candidate != null && candidate.getParameterCount() == 1) {
            writeMethodToUse = candidate;
        }
    }
    this.readMethod = readMethodToUse;
    this.writeMethod = writeMethodToUse;

    if (this.writeMethod != null) {
        if (this.readMethod == null) {
            // Write method not matched against read method: potentially ambiguous through
            // several overloaded variants, in which case an arbitrary winner has been chosen
            // by the JDK's JavaBeans Introspector...
            Set<Method> ambiguousCandidates = new HashSet<>();
            for (Method method : beanClass.getMethods()) {
                if (method.getName().equals(writeMethodToUse.getName()) && !method.equals(writeMethodToUse)
                        && !method.isBridge()
                        && method.getParameterCount() == writeMethodToUse.getParameterCount()) {
                    ambiguousCandidates.add(method);
                }
            }
            if (!ambiguousCandidates.isEmpty()) {
                this.ambiguousWriteMethods = ambiguousCandidates;
            }
        }
        this.writeMethodParameter = new MethodParameter(this.writeMethod, 0);
        GenericTypeResolver.resolveParameterType(this.writeMethodParameter, this.beanClass);
    }

    if (this.readMethod != null) {
        this.propertyType = GenericTypeResolver.resolveReturnType(this.readMethod, this.beanClass);
    } else if (this.writeMethodParameter != null) {
        this.propertyType = this.writeMethodParameter.getParameterType();
    }

    this.propertyEditorClass = propertyEditorClass;
}

From source file:org.springframework.cloud.sleuth.annotation.SpanTagAnnotationHandler.java

private void mergeAnnotatedMethodsIfNecessary(MethodInvocation pjp, Method method, Method mostSpecificMethod,
        List<SleuthAnnotatedParameter> annotatedParameters) {
    // that can happen if we have an abstraction and a concrete class that is
    // annotated with @NewSpan annotation
    if (!method.equals(mostSpecificMethod)) {
        List<SleuthAnnotatedParameter> annotatedParametersForActualMethod = SleuthAnnotationUtils
                .findAnnotatedParameters(method, pjp.getArguments());
        mergeAnnotatedParameters(annotatedParameters, annotatedParametersForActualMethod);
    }/*from   w  w  w. j av a 2  s  .  co m*/
}

From source file:org.springframework.orm.hibernate3.SessionFactoryBuilderSupport.java

/**
 * Wrap the given {@code SessionFactory} with a proxy, if demanded.
 * <p>The default implementation wraps the given {@code SessionFactory} as a Spring
 * {@link DisposableBean} proxy in order to call {@link SessionFactory#close()} on
 * {@code ApplicationContext} {@linkplain ConfigurableApplicationContext#close() shutdown}.
 * <p>Subclasses may override this to implement transaction awareness through
 * a {@code SessionFactory} proxy for example, or even to avoid creation of the
 * {@code DisposableBean} proxy altogether.
 * @param rawSf the raw {@code SessionFactory} as built by {@link #buildSessionFactory()}
 * @return the {@code SessionFactory} reference to expose
 * @see #buildSessionFactory()/*www  . j  a va  2  s  .c o  m*/
 */
protected SessionFactory wrapSessionFactoryIfNecessary(final SessionFactory rawSf) {
    return (SessionFactory) Proxy.newProxyInstance(this.beanClassLoader,
            new Class<?>[] { SessionFactory.class, DisposableBean.class }, new InvocationHandler() {
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if (ReflectionUtils.isToStringMethod(method)) {
                        return String.format("DisposableBean proxy for SessionFactory [%s]", rawSf.toString());
                    }
                    if (method.equals(DisposableBean.class.getMethod("destroy"))) {
                        closeHibernateSessionFactory(SessionFactoryBuilderSupport.this, rawSf);
                        rawSf.close();
                        return null;
                    }
                    return method.invoke(rawSf, args);
                }
            });
}