Example usage for java.lang.reflect Method isBridge

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

Introduction

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

Prototype

public boolean isBridge() 

Source Link

Document

Returns true if this method is a bridge method; returns false otherwise.

Usage

From source file:com.freetmp.common.util.ClassUtils.java

public static boolean isUserLevelMethod(Method method) {
    Assert.notNull(method, "Method must not be null");
    return (method.isBridge() || (!method.isSynthetic() && !isGroovyObjectMethod(method)));
}

From source file:org.crazydog.util.spring.ClassUtils.java

/**
 * Determine whether the given method is declared by the user or at least pointing to
 * a user-declared method./* w ww.ja  v  a  2  s.  c o m*/
 * <p>Checks {@link Method#isSynthetic()} (for implementation methods) as well as the
 * {@code GroovyObject} interface (for interface methods; on an implementation class,
 * implementations of the {@code GroovyObject} methods will be marked as synthetic anyway).
 * Note that, despite being synthetic, bridge methods ({@link Method#isBridge()}) are considered
 * as user-level methods since they are eventually pointing to a user-declared generic method.
 * @param method the method to check
 * @return {@code true} if the method can be considered as user-declared; [@code false} otherwise
 */
public static boolean isUserLevelMethod(Method method) {
    org.springframework.util.Assert.notNull(method, "Method must not be null");
    return (method.isBridge() || (!method.isSynthetic() && !isGroovyObjectMethod(method)));
}

From source file:de.hybris.platform.webservices.util.objectgraphtransformer.impl.AbstractNodeConfig.java

/**
 * Creates a lookup map containing all properties of passed type.
 * <p/>// w w w  .  ja  va 2 s .  c  o m
 * Result maps a property name to a {@link PropertyConfig}.
 * </p>
 * Any property which keeps java bean standard is found and used for {@link PropertyConfig} creation. For finding all
 * properties {@link Introspector} is used which returns general {@link PropertyDescriptor}. But read- and write
 * methods provided by {@link PropertyDescriptor} are only used as "suggestion" here and are getting post-processed
 * to assure following criteria:
 * <p/>
 * - no bridge or synthetic methods are allowed <br/>
 * - co-variant return types are handled correctly <br/>
 * 
 * @param type
 * @return
 */
private Map<String, PropertyConfig> createPropertiesFor(Class<?> type) {
    final Map<String, PropertyConfig> result = new TreeMap<String, PropertyConfig>();
    final Set<String> done = new HashSet<String>();
    while (type != null) {
        // we are only interested in declared methods (no bridge/synthetic ones)
        final Method[] methods = type.getDeclaredMethods();
        for (final Method method : methods) {
            // only public, non-bridged methods are of interest
            if (!method.isBridge() && Modifier.isPublic(method.getModifiers())) {
                // potential bean-getter property?
                if (method.getParameterTypes().length == 0 && method.getReturnType() != void.class) {
                    // not processed yet?
                    final String methodName = method.getName();
                    if (!done.contains(methodName)) {
                        done.add(methodName);

                        final Matcher matcher = BEAN_GETTER.matcher(methodName);
                        String propertyName = null;
                        if (matcher.matches()) {
                            propertyName = matcher.group(1);
                        } else {
                            if (method.getReturnType().equals(boolean.class)) {
                                final Matcher matcher2 = BEAN_BOOLEAN_GETTER.matcher(methodName);
                                if (matcher2.matches()) {
                                    propertyName = matcher2.group(1);
                                }
                            }
                        }

                        if (propertyName != null) {
                            propertyName = normalizePropertyName(propertyName);

                            // get or create a PropertyConfig
                            DefaultPropertyConfig pCfg = (DefaultPropertyConfig) result.get(propertyName);
                            if (pCfg == null) {
                                pCfg = new DefaultPropertyConfig(propertyName, null, null);
                                result.put(propertyName, pCfg);
                            }
                            pCfg.setReadMethod(method);
                        }
                    }
                }

                // potential bean-setter property?
                if (method.getParameterTypes().length == 1 && method.getReturnType() == void.class) {
                    // not processed yet?
                    final String methodName = method.getName();
                    if (!done.contains(methodName)) {
                        done.add(methodName);
                        final Matcher setter = BEAN_SETTER.matcher(methodName);
                        if (setter.matches()) {
                            String propertyName = setter.group(1);
                            propertyName = Character.toLowerCase(propertyName.charAt(0))
                                    + propertyName.substring(1);

                            // get or create a PropertyConfig
                            DefaultPropertyConfig pCfg = (DefaultPropertyConfig) result.get(propertyName);
                            if (pCfg == null) {
                                pCfg = new DefaultPropertyConfig(propertyName, null, null);
                                result.put(propertyName, pCfg);
                            }
                            pCfg.setWriteMethod(method);
                        }
                    }
                }
            }

        }
        type = type.getSuperclass();
    }
    return result;
}

From source file:com.google.code.ssm.aop.CacheBase.java

public Method getMethodToCache(final JoinPoint jp) throws NoSuchMethodException {
    final Signature sig = jp.getSignature();
    if (!(sig instanceof MethodSignature)) {
        throw new InvalidAnnotationException("This annotation is only valid on a method.");
    }//  w  w w .j a  va  2 s. c o  m

    final MethodSignature msig = (MethodSignature) sig;
    final Object target = jp.getTarget();

    // cannot use msig.getMethod() because it can return the method where annotation was declared i.e. method in
    // interface
    String name = msig.getName();
    Class<?>[] parameters = msig.getParameterTypes();

    Method method = findMethodFromTargetGivenNameAndParams(target, name, parameters);

    if (method.isBridge()) {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Method is bridge. Name {}, params: {}", name, parameters);
        }

        parameters = bridgeMethodMappingStore.getTargetParamsTypes(target.getClass(), name, parameters);
        method = findMethodFromTargetGivenNameAndParams(target, name, parameters);
    }

    return method;
}

From source file:com.netflix.hystrix.contrib.javanica.utils.MethodProvider.java

/**
 * Finds generic method for the given bridge method.
 *
 * @param bridgeMethod the bridge method
 * @param aClass       the type where the bridge method is declared
 * @return generic method/*w  w  w .  j av a 2 s  .co  m*/
 * @throws IOException
 * @throws NoSuchMethodException
 * @throws ClassNotFoundException
 */
public Method unbride(final Method bridgeMethod, Class<?> aClass)
        throws IOException, NoSuchMethodException, ClassNotFoundException {
    if (bridgeMethod.isBridge() && bridgeMethod.isSynthetic()) {
        if (cache.containsKey(bridgeMethod)) {
            return cache.get(bridgeMethod);
        }

        ClassReader classReader = new ClassReader(aClass.getName());
        final MethodSignature methodSignature = new MethodSignature();
        classReader.accept(new ClassVisitor(ASM5) {
            @Override
            public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                    String[] exceptions) {
                boolean bridge = (access & ACC_BRIDGE) != 0 && (access & ACC_SYNTHETIC) != 0;
                if (bridge && bridgeMethod.getName().equals(name)
                        && getParameterCount(desc) == bridgeMethod.getParameterTypes().length) {
                    return new MethodFinder(methodSignature);
                }
                return super.visitMethod(access, name, desc, signature, exceptions);
            }
        }, 0);
        Method method = aClass.getDeclaredMethod(methodSignature.name, methodSignature.getParameterTypes());
        cache.put(bridgeMethod, method);
        return method;

    } else {
        return bridgeMethod;
    }
}

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

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

    super(propertyName, null, null);

    if (beanClass == null) {
        throw new IntrospectionException("Bean class must not be null");
    }//  w  ww .  ja  v a2 s . co  m
    this.beanClass = beanClass;

    Method readMethodToUse = BridgeMethodResolver.findBridgedMethod(readMethod);
    Method writeMethodToUse = BridgeMethodResolver.findBridgedMethod(writeMethod);
    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.getParameterTypes().length == 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<Method>();
            for (Method method : beanClass.getMethods()) {
                if (method.getName().equals(writeMethodToUse.getName()) && !method.equals(writeMethodToUse)
                        && !method.isBridge()) {
                    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:GenericClass.java

public List<MethodDef> getMethods() {
    List<MethodDef> founds = new ArrayList<MethodDef>();
    Class<?> current = myclass;
    while (current != null) {
        Method[] methods = current.getDeclaredMethods();
        for (Method method : methods) {
            if (!method.isBridge() && !method.isSynthetic()) {
                MethodDef def = new MethodDef(method);
                if (!founds.contains(def))
                    founds.add(def);/*  www  . j  a  va 2s .c o  m*/
            }
        }
        current = current.getSuperclass();
    }
    return founds;
}

From source file:GenericClass.java

/**
 * Search for all methods having that name, no matter which parameter they
 * take./* w  w w  . jav  a2s .  co  m*/
 * 
 * @param name
 *          The name of the methods
 * @return A list of {@link MethodDef}, ordered with methods from current
 *         class first, then method from superclass and so on.
 */
public List<MethodDef> findAllMethods(String name) {
    List<MethodDef> founds = new ArrayList<MethodDef>();
    Class<?> current = myclass;
    while (current != null) {
        Method[] methods = current.getDeclaredMethods();
        for (Method method : methods) {
            if (!method.isBridge() && !method.isSynthetic() && method.getName().equals(name)) {
                MethodDef def = new MethodDef(method);
                if (!founds.contains(def))
                    founds.add(def);
            }
        }
        current = current.getSuperclass();
    }
    return founds;
}

From source file:GenericClass.java

/**
 * Search for all occurrencies of a specific method.
 * <p>//from ww  w.j av  a 2  s  .co m
 * The type parameters passed in may be Class or null. If they are null, that
 * means that we don't know which class they should be, if they are a class,
 * that means we are searching for that class, and the comparison is made on
 * dereferenced generics.
 * </p>
 * <p>
 * Specifying no parameter types explicitly means "a method without
 * parameters".
 * </p>
 * 
 * @param name
 *          The name of the method
 * @param parameterTypes
 *          The types of the parameters
 * @return A list of {@link MethodDef}, ordered with methods from current
 *         class first, then method from superclass and so on.
 */
public List<MethodDef> findMethods(String name, Class<?>... parameterTypes) {
    List<MethodDef> founds = new ArrayList<MethodDef>();
    Class<?> current = myclass;
    while (current != null) {
        Method[] methods = current.getDeclaredMethods();
        for (Method method : methods) {
            if (!method.isBridge() && !method.isSynthetic() && method.getName().equals(name)) {
                Type[] types = method.getGenericParameterTypes();
                if (types.length == parameterTypes.length) {
                    GenericClass[] genericClasses = getParameterTypes(method);
                    Class<?>[] classes = toRawClasses(genericClasses);
                    boolean good = true;
                    for (int i = 0; i < types.length; i++) {
                        if (parameterTypes[i] != null) {
                            if (!classes[i].equals(parameterTypes[i])) {
                                good = false;
                                break;
                            }
                        }
                    }
                    if (good) {
                        MethodDef def = new MethodDef(method, genericClasses);
                        if (!founds.contains(def))
                            founds.add(def);
                    }
                }
            }
        }
        current = current.getSuperclass();
    }
    return founds;
}

From source file:com.hortonworks.streamline.streams.service.UDFCatalogResource.java

private Method findMethod(Class<?> clazz, String name) {
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.getName().equals(name) && !method.isBridge()) {
            return method;
        }//from   ww  w  . ja  v  a2 s .c  o m
    }
    return null;
}