Class DefaultReflectionPredicate

  • All Implemented Interfaces:
    ReflectionPredicate

    public class DefaultReflectionPredicate
    extends Object
    implements ReflectionPredicate
    Default implementations of methods that indicate what is "under test": whether a class, method, constructor, or field should be used in Randoop's exploration. Returns true for public members, with some exceptions (see doNotUseSpecialCase(java.lang.reflect.Method) method).

    If a method has the @CheckRep annotation, returns false (the method will be used as a contract checker, not as a method under test).

    • Constructor Detail

      • DefaultReflectionPredicate

        public DefaultReflectionPredicate()
        Create a reflection predicate.
      • DefaultReflectionPredicate

        public DefaultReflectionPredicate​(Collection<String> omitFields)
        DefaultReflectionFilter creates a filter object that uses default criteria for inclusion of reflection objects.
        Parameters:
        omitFields - set of fully-qualified field names to omit
    • Method Detail

      • test

        public boolean test​(Class<?> c)
        Description copied from interface: ReflectionPredicate
        Indicate whether Randoop should use a class.
        Specified by:
        test in interface ReflectionPredicate
        Parameters:
        c - the class to test
        Returns:
        true if the class meets the predicate criteria, false otherwise
      • test

        public boolean test​(Method m)
        Indicate whether Randoop should use a method.

        Does checks for the several cases, including main methods, bridge methods (see discardBridge(Method), non-bridge synthetic methods, non-accessible methods, or methods with non-accessible return types.

        See the code for the full list.

        Specified by:
        test in interface ReflectionPredicate
        Parameters:
        m - the method to test
        Returns:
        true if the method meets the predicate criteria, false otherwise
      • isRandoopInstrumentation

        private boolean isRandoopInstrumentation​(Method m)
      • discardBridge

        private boolean discardBridge​(Method m)
        Determines whether a bridge method should be discarded.

        Bridge methods are synthetic overriding methods that are generated by the compiler to make certain calls type-correct.

        Two of the three known cases involve forcing unchecked casts to allow type narrowing of return types (covariant return types) and instantiation of generic type parameters in methods. Both of these are situations that a programmer could view as overriding, but really aren't. These bridge methods do unchecked type conversions from the general type to the more specific type expected by the local method. As a result, if included for testing, Randoop would generate many tests that would confirm that there is an unchecked type conversion. So, we do not want to include these methods.

        The third known case involves a public class inheriting a public method defined in a private class of the same package. The bridge method in the public class exposes the method outside of the package, and we *do* want to be able to call this method. (This sort of trick is useful in providing a facade to an API where implementation details are only accessible within the package.)

        The only case in which a bridge method should be kept is when it is a visibility bridge.

        Parameters:
        m - the bridge method to test
        Returns:
        true if the bridge method should be discarded, false otherwise
      • isVisibilityBridge

        private boolean isVisibilityBridge​(Method m)
                                    throws Error
        Determines whether a bridge method is a visibility bridge, which allows access to a definition of the method in a non-accessible superclass.

        The method is a visibility bridge if this class is public and some superclass defines the method as non-public.

        Parameters:
        m - the bridge method to test
        Returns:
        true iff m is a visibility bridge
        Throws:
        Error - if a SecurityException is thrown when accessing superclass methods
      • definesNonBridgeMethod

        private boolean definesNonBridgeMethod​(Class<?> c,
                                               Method goalMethod)
        Returns true if the class defines the given method, not as a bridge method. Returns false if the class does not define the given method, or if the class defines the method as a bridge method. Ignores inheritance of methods.
        Parameters:
        c - the possibly-containing class
        goalMethod - the method to search for
        Returns:
        true if the class defines the method
      • isPublic

        private boolean isPublic​(Class<?> c)
        Indicates whether the Class is public.
        Parameters:
        c - the class
        Returns:
        true if c is a public class
      • doNotUseSpecialCase

        private String doNotUseSpecialCase​(Method m)
        Indicates methods for which this predicate should return false. See inline comments for details. This is a main place that Randoop controls which methods are methods under test.
        Parameters:
        m - the method to accept or reject for inclusion in methods under test
        Returns:
        a non-null string giving a reason the method should be skipped, or null to not skip it
      • test

        public boolean test​(Constructor<?> c)
        Indicate whether Randoop should use a constructor.

        Use the constructor unless it is specifically omitted, is synthetic with anonymous parameters, or the class is abstract.

        Specified by:
        test in interface ReflectionPredicate
        Parameters:
        c - the constructor to test
        Returns:
        true if the constructor meets the predicate criteria, false otherwise
      • test

        public boolean test​(Field f)
        Determines whether the name of a field is included among the omitted field names.
        Specified by:
        test in interface ReflectionPredicate
        Parameters:
        f - field to test
        Returns:
        true if field name does not occur in omitFields pattern, and false if it does
      • isRandoopInstrumentation

        private boolean isRandoopInstrumentation​(Field f)
        Indicates that a field is generated by the covered-class instrumentation agent.

        Tests whether the field begins with "randoop_".

        Parameters:
        f - the field
        Returns:
        true if the field name