List of usage examples for java.lang.reflect Method isBridge
public boolean isBridge()
From source file:org.compass.annotations.config.binding.AnnotationsMappingBinding.java
/** * Recursivly process the class to find all it's annotations. Lower level * class/interfaces with annotations will be added first. *///from w w w. j a va2 s. co m private void processAnnotatedClass(Class<?> clazz) { if (clazz.equals(Class.class)) { return; } Class<?> superClazz = clazz.getSuperclass(); if (superClazz != null && !superClazz.equals(Object.class)) { processAnnotatedClass(superClazz); } Class<?>[] interfaces = clazz.getInterfaces(); for (Class<?> anInterface : interfaces) { processAnnotatedClass(anInterface); } SearchableConstant searchableConstant = clazz.getAnnotation(SearchableConstant.class); if (searchableConstant != null) { bindConstantMetaData(searchableConstant); } SearchableConstants searchableConstants = clazz.getAnnotation(SearchableConstants.class); if (searchableConstants != null) { for (SearchableConstant metaData : searchableConstants.value()) { bindConstantMetaData(metaData); } } SearchableDynamicMetaData searchableDynamicMetaData = clazz.getAnnotation(SearchableDynamicMetaData.class); if (searchableDynamicMetaData != null) { bindDynamicMetaData(searchableDynamicMetaData); } SearchableDynamicMetaDatas searchableDynamicMetaDatas = clazz .getAnnotation(SearchableDynamicMetaDatas.class); if (searchableDynamicMetaDatas != null) { for (SearchableDynamicMetaData metaData : searchableDynamicMetaDatas.value()) { bindDynamicMetaData(metaData); } } // handles recursive extends and the original extend if (clazz.isAnnotationPresent(Searchable.class)) { Searchable searchable = clazz.getAnnotation(Searchable.class); String[] extend = searchable.extend(); if (extend.length != 0) { ArrayList<String> extendedMappings = new ArrayList<String>(); if (classMapping.getExtendedAliases() != null) { extendedMappings.addAll(Arrays.asList(classMapping.getExtendedAliases())); } for (String extendedAlias : extend) { Alias extendedAliasLookup = valueLookup.lookupAlias(extendedAlias); if (extendedAliasLookup == null) { extendedMappings.add(extendedAlias); } else { extendedMappings.add(extendedAliasLookup.getName()); } } classMapping.setExtendedAliases(extendedMappings.toArray(new String[extendedMappings.size()])); } } // if the super class has Searchable annotation as well, add it to the list of extends ArrayList<Class> extendedClasses = new ArrayList<Class>(); if (clazz.getSuperclass() != null) { extendedClasses.add(clazz.getSuperclass()); } extendedClasses.addAll(Arrays.asList(clazz.getInterfaces())); for (Class<?> superClass : extendedClasses) { if (!superClass.isAnnotationPresent(Searchable.class)) { continue; } Searchable superSearchable = superClass.getAnnotation(Searchable.class); String alias = getAliasFromSearchableClass(superClass, superSearchable); HashSet<String> extendedMappings = new HashSet<String>(); if (classMapping.getExtendedAliases() != null) { extendedMappings.addAll(Arrays.asList(classMapping.getExtendedAliases())); } extendedMappings.add(alias); classMapping.setExtendedAliases(extendedMappings.toArray(new String[extendedMappings.size()])); } for (Field field : clazz.getDeclaredFields()) { for (Annotation annotation : field.getAnnotations()) { processsAnnotatedElement(clazz, field.getName(), "field", field.getType(), field.getGenericType(), annotation, field); } } for (Method method : clazz.getDeclaredMethods()) { if (!method.isSynthetic() && !method.isBridge() && !Modifier.isStatic(method.getModifiers()) && method.getParameterTypes().length == 0 && method.getReturnType() != void.class && (method.getName().startsWith("get") || method.getName().startsWith("is"))) { for (Annotation annotation : method.getAnnotations()) { processsAnnotatedElement(clazz, ClassUtils.getShortNameForMethod(method), "property", method.getReturnType(), method.getGenericReturnType(), annotation, method); } } } }
From source file:org.dozer.util.ReflectionUtils.java
/** * There are some nasty bugs for introspection with generics. This method addresses those nasty bugs and tries to find proper methods if available * http://bugs.sun.com/view_bug.do?bug_id=6788525 * http://bugs.sun.com/view_bug.do?bug_id=6528714 * @param descriptor/*w w w. j a v a 2 s . com*/ * @return */ private static PropertyDescriptor fixGenericDescriptor(Class<?> clazz, PropertyDescriptor descriptor) { Method readMethod = descriptor.getReadMethod(); Method writeMethod = descriptor.getWriteMethod(); if (readMethod != null && (readMethod.isBridge() || readMethod.isSynthetic())) { String propertyName = descriptor.getName(); //capitalize the first letter of the string; String baseName = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); String setMethodName = "set" + baseName; String getMethodName = "get" + baseName; Method[] methods = clazz.getMethods(); for (Method method : methods) { if (method.getName().equals(getMethodName) && !method.isBridge() && !method.isSynthetic()) { try { descriptor.setReadMethod(method); } catch (IntrospectionException e) { //move on } } if (method.getName().equals(setMethodName) && !method.isBridge() && !method.isSynthetic()) { try { descriptor.setWriteMethod(method); } catch (IntrospectionException e) { //move on } } } } return descriptor; }
From source file:org.eclipse.gemini.blueprint.compendium.internal.cm.UpdateMethodAdapter.java
/** * Determines the update method.//from w w w .java2 s .c o m * * @param target * @param methodName * @return */ static Map determineUpdateMethod(final Class<?> target, final String methodName) { Assert.notNull(target); Assert.notNull(methodName); final Map methods = new LinkedHashMap(2); final boolean trace = log.isTraceEnabled(); org.springframework.util.ReflectionUtils.doWithMethods(target, new org.springframework.util.ReflectionUtils.MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { // consider filtering bridge non-void and non-public methods as well if (!method.isBridge() && Modifier.isPublic(method.getModifiers()) && (void.class.equals(method.getReturnType())) && methodName.equals(method.getName())) { // check the argument types Class<?>[] args = method.getParameterTypes(); // Properties can be passed as Map or Dictionary if (args != null && args.length == 1) { Class<?> propertiesType = args[0]; if (propertiesType.isAssignableFrom(Map.class) || propertiesType.isAssignableFrom(Dictionary.class)) { if (trace) log.trace("Discovered custom method [" + method.toString() + "] on " + target); // see if there was a method already found Method m = (Method) methods.get(propertiesType); if (m != null) { if (trace) log.trace( "Type " + propertiesType + " already has an associated method [" + m.toString() + "];ignoring " + method); } else methods.put(propertiesType, method); } } } } }); return methods; }
From source file:org.eclipse.gemini.blueprint.config.internal.adapter.CustomListenerAdapterUtils.java
private static Map<Class<?>, List<Method>> doDetermineCustomMethods(final Class<?> target, final String methodName, final Class<?>[] possibleArgumentTypes, final boolean onlyPublic) { final Map<Class<?>, List<Method>> methods = new LinkedHashMap<Class<?>, List<Method>>(3); final boolean trace = log.isTraceEnabled(); org.springframework.util.ReflectionUtils.doWithMethods(target, new org.springframework.util.ReflectionUtils.MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { if (!method.isBridge() && methodName.equals(method.getName())) { if (onlyPublic && !Modifier.isPublic(method.getModifiers())) { if (trace) log.trace("Only public methods are considered; ignoring " + method); return; }//from w ww . j a v a2s . c o m // take a look at the variables Class<?>[] args = method.getParameterTypes(); if (args != null) { // Properties can be ignored if (args.length == 1) { addMethod(args[0], method, methods); } // or passed as Map, Dictionary else if (args.length == 2) { Class<?> propType = args[1]; for (int i = 0; i < possibleArgumentTypes.length; i++) { Class<?> clazz = possibleArgumentTypes[i]; if (clazz.isAssignableFrom(propType)) { addMethod(args[0], method, methods); } } } } } } private void addMethod(Class<?> key, Method mt, Map<Class<?>, List<Method>> methods) { if (trace) log.trace("discovered custom method [" + mt.toString() + "] on " + target); List<Method> mts = methods.get(key); if (mts == null) { mts = new ArrayList<Method>(2); methods.put(key, mts); org.springframework.util.ReflectionUtils.makeAccessible(mt); mts.add(mt); return; } // add a method only if there is still space if (mts.size() == 1) { Method m = mts.get(0); if (m.getParameterTypes().length == mt.getParameterTypes().length) { if (trace) log.trace("Method w/ signature " + methodSignature(m) + " has been already discovered; ignoring it"); } else { org.springframework.util.ReflectionUtils.makeAccessible(mt); mts.add(mt); } } } private String methodSignature(Method m) { StringBuilder sb = new StringBuilder(); int mod = m.getModifiers(); if (mod != 0) { sb.append(Modifier.toString(mod) + " "); } sb.append(m.getReturnType() + " "); sb.append(m.getName() + "("); Class<?>[] params = m.getParameterTypes(); for (int j = 0; j < params.length; j++) { sb.append(params[j]); if (j < (params.length - 1)) sb.append(","); } sb.append(")"); return sb.toString(); } }); return methods; }
From source file:org.eclipse.wb.android.internal.model.property.event.ListenerInfo.java
/** * @return <code>true</code> if given {@link Method} is valid handler for some event in listener. *//*from ww w .ja v a 2 s . c om*/ private boolean isListenerMethod(Method method) { if (method.isBridge()) { return false; } if (ReflectionUtils.isAbstract(method)) { return true; } if (method.getDeclaringClass() == m_interfaceType) { return true; } return false; }
From source file:org.eclipse.wb.internal.core.model.creation.ThisCreationSupport.java
/** * Initializes {@link #m_enhancer} field. *///from ww w .j a v a 2s. c o m private void createEnhancer(Class<?> componentClass, final ExecutionFlowFrameVisitor visitor) { m_enhancer = new Enhancer(); m_enhancer.setClassLoader(getClassLoader()); m_enhancer.setSuperclass(componentClass); Callback interceptor = new MethodInterceptor() { public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { // if not in AST execution, then ignore if (m_interceptOnlyDuringExecution && !m_editorState.isExecuting()) { if (ReflectionUtils.isAbstract(method)) { return returnDefaultValue(method); } return proxy.invokeSuper(obj, args); } // try to find implementation of this method in AST if (!method.isBridge() && !isSuperMethodInvocation()) { String methodSignature = ReflectionUtils.getMethodSignature(method); // handle special SWT methods if (methodSignature.equals("isValidSubclass()")) { return Boolean.TRUE; } if (methodSignature.equals("checkSubclass()")) { return null; } // may be model wants to handle method and provide result { Object result = tryModelMethodInterceptor(method, methodSignature, args); if (result != AstEvaluationEngine.UNKNOWN) { return result; } } // check if we are allowed to intercept method if (!canInterceptMethod(method, methodSignature)) { return proxy.invokeSuper(obj, args); } // try to find MethodDeclaration MethodDeclaration methodDeclaration; { TypeDeclaration typeDeclaration = (TypeDeclaration) m_constructor.getParent(); methodDeclaration = AstNodeUtils.getMethodBySignature(typeDeclaration, methodSignature); } // OK, we have MethodDeclaration, redirect to it if (methodDeclaration != null && !AstNodeUtils.isAbstract(methodDeclaration)) { JavaInfoEvaluationHelper.shouldEvaluateReturnValue(methodDeclaration, true); return visitMethod(obj, method, args, proxy, methodDeclaration, visitor); } } // handle abstract if (ReflectionUtils.isAbstract(method)) { return returnDefaultValue(method); } // invoke super return proxy.invokeSuper(obj, args); } private Object returnDefaultValue(Method method) { Class<?> returnType = method.getReturnType(); return ReflectionUtils.getDefaultValue(returnType); } }; m_enhancer.setCallbacks(new Callback[] { interceptor, NoOp.INSTANCE }); m_enhancer.setCallbackFilter(ENHANCER_FILTER); }
From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java
/** * Appends components for {@link PropertyDescriptor}'s of given {@link Class}, its super class and * implemented interfaces.// ww w .j a v a 2 s . c o m */ private static void appendPropertyComponents(Class<?> currentClass, Set<String> newPropertyNames, Map<String, Method> propertyToGetter, Map<String, Method> propertyToSetter) { for (Method method : currentClass.getDeclaredMethods()) { int methodModifiers = method.getModifiers(); boolean isPublic = Modifier.isPublic(methodModifiers); boolean isProtected = Modifier.isProtected(methodModifiers); boolean isStatic = Modifier.isStatic(methodModifiers); if (method.isBridge()) { continue; } if (!isStatic && (isPublic || isProtected)) { method.setAccessible(true); String methodName = method.getName(); if (methodName.startsWith("set") && method.getParameterTypes().length == 1) { String propertyName = getQualifiedPropertyName(method); if (!propertyToSetter.containsKey(propertyName)) { newPropertyNames.add(propertyName); propertyToSetter.put(propertyName, method); } } if (method.getParameterTypes().length == 0) { if (methodName.startsWith("get")) { String propertyName = getQualifiedPropertyName(method); if (!propertyToGetter.containsKey(propertyName)) { newPropertyNames.add(propertyName); propertyToGetter.put(propertyName, method); } } if (methodName.startsWith("is")) { String propertyName = getQualifiedPropertyName(method); if (!propertyToGetter.containsKey(propertyName)) { newPropertyNames.add(propertyName); propertyToGetter.put(propertyName, method); } } } } } // process interfaces for (Class<?> interfaceClass : currentClass.getInterfaces()) { appendPropertyComponents(interfaceClass, newPropertyNames, propertyToGetter, propertyToSetter); } // process super Class if (currentClass.getSuperclass() != null) { appendPropertyComponents(currentClass.getSuperclass(), newPropertyNames, propertyToGetter, propertyToSetter); } }
From source file:org.evosuite.assertion.InspectorManager.java
private boolean isInspectorMethod(Method method) { if (!Modifier.isPublic(method.getModifiers())) return false; if (!method.getReturnType().isPrimitive() && !method.getReturnType().equals(String.class) && !method.getReturnType().isEnum() && !ClassUtils.isPrimitiveWrapper(method.getReturnType())) { return false; }/*from ww w .ja v a 2 s .c o m*/ if (method.getReturnType().equals(void.class)) return false; if (method.getParameterTypes().length != 0) return false; if (method.getName().equals("hashCode")) return false; if (method.getDeclaringClass().equals(Object.class)) return false; if (method.getDeclaringClass().equals(Enum.class)) return false; if (method.isSynthetic()) return false; if (method.isBridge()) return false; if (method.getName().equals("pop")) return false; if (isBlackListed(method)) return false; if (isImpureJDKMethod(method)) return false; if (isAWTToString(method)) return false; if (Properties.PURE_INSPECTORS) { if (!CheapPurityAnalyzer.getInstance().isPure(method)) { return false; } } return true; }
From source file:org.evosuite.setup.TestClusterGenerator.java
public static boolean canUse(Method m, Class<?> ownerClass) { if (m.isBridge()) { logger.debug("Excluding bridge method: {}", m.toString()); return false; }/*from w w w .j a v a 2 s .c o m*/ if (m.isSynthetic()) { logger.debug("Excluding synthetic method: {}", m.toString()); return false; } if (!Properties.USE_DEPRECATED && m.isAnnotationPresent(Deprecated.class)) { logger.debug("Excluding deprecated method {}", m.getName()); return false; } if (m.isAnnotationPresent(Test.class)) { logger.debug("Excluding 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())) { 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") && !m.getDeclaringClass().equals(Properties.getTargetClass())) 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 class"); 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())) { 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)) { makeAccessible(m); return true; } } return false; }
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; }//from w ww. j av a 2 s. c om 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; }