Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion docs/mutation-framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ package.
| `@WithUtf8Length` | `java.lang.String` | Specifies the length of the generated string in UTF-8 bytes, see annotation Javadoc for further information |
| `@WithSize` | `java.util.List`, `java.util.Map` | Specifies the size of the generated collection |
| `@UrlSegment` | `java.lang.String` | `String` should only contain valid URL segment characters |
| `@IgnoreProperties` | `@FuzzTest` methods, JavaBeans, or containers | Excludes JavaBean properties by property name (`value`) or assignable type (`types`) from setter-based mutation |
| `@IgnoreGetters` | `@FuzzTest` methods, JavaBeans, or containers | Excludes JavaBean properties whose getter method name matches any entry in `value` |
| `@IgnoreSetters` | `@FuzzTest` methods, JavaBeans, or containers | Excludes JavaBean setter methods whose name matches any entry in `value` |

The example below shows how Fuzz Test parameters can be annotated to provide
additional information to the mutation framework.
Expand Down Expand Up @@ -163,7 +166,24 @@ no arguments. The corresponding methods are looked up by name and must adhere to
the JavaBeans Spec naming convention, meaning `setXX` and `getXX`/`isXX` methods
for property `XX`. A JavaBean can have additional getters corresponding to
computed properties, but it is required that all setters have a corresponding
getter.
getter. To exclude properties from setter-based mutation without annotating the
JavaBean class under test (for example, write-only setters or properties with
unsupported types), annotate the `@FuzzTest` method or parameter type with
`@IgnoreProperties`, `@IgnoreGetters`, or `@IgnoreSetters`:

```java title="Example" showLineNumbers
@IgnoreProperties(value = {"internalId"}, types = {UnsupportedService.class})
@IgnoreSetters({"setAuditCallback"})
@FuzzTest
public void testOnboardingBean(@NotNull OnboardingBean onboarding) {
// ...
}
```

By default, these annotations use `PropertyConstraint.RECURSIVE`, so rules
(including `types = {UnsupportedService.class}`) apply across nested JavaBeans
and collections. Set `constraint = PropertyConstraint.DECLARATION` on a
parameter type to limit an annotation to that type alone.

```java title="Example" showLineNumbers
public static class FooBean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;

import com.code_intelligence.jazzer.mutation.annotation.IgnoreGetters;
import com.code_intelligence.jazzer.mutation.annotation.IgnoreProperties;
import com.code_intelligence.jazzer.mutation.annotation.IgnoreSetters;
import com.code_intelligence.jazzer.mutation.annotation.ValuePool;
import com.code_intelligence.jazzer.mutation.api.ExtendedMutatorFactory;
import com.code_intelligence.jazzer.mutation.api.PseudoRandom;
Expand All @@ -40,10 +43,13 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;

public final class ArgumentsMutator {
private final ExtendedMutatorFactory mutatorFactory;
Expand Down Expand Up @@ -100,18 +106,21 @@ public static Optional<ArgumentsMutator> forMethod(
throw validationError;
}

ValuePool[] valuePools = method.getAnnotationsByType(ValuePool.class);
Annotation[] methodAnnotations =
Stream.of(
method.getAnnotationsByType(ValuePool.class),
method.getAnnotationsByType(IgnoreProperties.class),
method.getAnnotationsByType(IgnoreGetters.class),
method.getAnnotationsByType(IgnoreSetters.class))
.flatMap(Arrays::stream)
.toArray(Annotation[]::new);

return toArrayOrEmpty(
stream(method.getAnnotatedParameterTypes())
.map(
type -> {
// Forward all @ValuePool annotations of the fuzz test method to each
// arg.
AnnotatedType t = type;
for (ValuePool pool : valuePools) {
t = TypeSupport.withExtraAnnotations(t, pool);
}
// Forward method-level annotations of the fuzz test method to each arg.
AnnotatedType t = TypeSupport.withExtraAnnotations(type, methodAnnotations);
Optional<SerializingMutator<?>> mutator = mutatorFactory.tryCreate(t);
if (!mutator.isPresent()) {
Log.error(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.code_intelligence.jazzer.mutation.annotation;

import static com.code_intelligence.jazzer.mutation.utils.PropertyConstraint.RECURSIVE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import com.code_intelligence.jazzer.mutation.utils.AppliesTo;
import com.code_intelligence.jazzer.mutation.utils.IgnoreRecursiveConflicts;
import com.code_intelligence.jazzer.mutation.utils.PropertyConstraint;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/** Excludes JavaBean properties whose getter method name matches any entry in {@link #value()}. */
@Target({METHOD, TYPE_USE})
@Retention(RUNTIME)
@AppliesTo(subClassesOf = Object.class)
@IgnoreRecursiveConflicts
@PropertyConstraint
public @interface IgnoreGetters {
/** Getter method names to ignore (e.g., {@code "getFoo"}, {@code "isBar"}). */
String[] value();

/**
* Defines the scope of the annotation. Possible values are defined in {@link
* com.code_intelligence.jazzer.mutation.utils.PropertyConstraint}. Defaults to {@code RECURSIVE}.
*/
String constraint() default RECURSIVE;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.code_intelligence.jazzer.mutation.annotation;

import static com.code_intelligence.jazzer.mutation.utils.PropertyConstraint.RECURSIVE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import com.code_intelligence.jazzer.mutation.utils.AppliesTo;
import com.code_intelligence.jazzer.mutation.utils.IgnoreRecursiveConflicts;
import com.code_intelligence.jazzer.mutation.utils.PropertyConstraint;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
* Excludes matching JavaBean properties from setter-based bean mutation.
*
* <p>Can be applied to {@code @FuzzTest} methods or parameter types. By default, rules propagate
* recursively through nested beans and containers ({@link PropertyConstraint#RECURSIVE}).
*/
@Target({METHOD, TYPE_USE})
@Retention(RUNTIME)
@AppliesTo(subClassesOf = Object.class)
@IgnoreRecursiveConflicts
@PropertyConstraint
public @interface IgnoreProperties {
/**
* JavaBean property names to ignore (e.g., {@code "foo"} matches {@code setFoo}/{@code getFoo}).
*/
String[] value() default {};

/**
* Property types to ignore (matches assignable setter parameter types, including generic type
* arguments and array component types).
*/
Class<?>[] types() default {};

/**
* Defines the scope of the annotation. Possible values are defined in {@link
* com.code_intelligence.jazzer.mutation.utils.PropertyConstraint}. Defaults to {@code RECURSIVE}.
*/
String constraint() default RECURSIVE;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.code_intelligence.jazzer.mutation.annotation;

import static com.code_intelligence.jazzer.mutation.utils.PropertyConstraint.RECURSIVE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import com.code_intelligence.jazzer.mutation.utils.AppliesTo;
import com.code_intelligence.jazzer.mutation.utils.IgnoreRecursiveConflicts;
import com.code_intelligence.jazzer.mutation.utils.PropertyConstraint;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/** Excludes JavaBean setter methods whose name matches any entry in {@link #value()}. */
@Target({METHOD, TYPE_USE})
@Retention(RUNTIME)
@AppliesTo(subClassesOf = Object.class)
@IgnoreRecursiveConflicts
@PropertyConstraint
public @interface IgnoreSetters {
/** Setter method names to ignore (e.g., {@code "setFoo"}, {@code "withBar"}). */
String[] value();

/**
* Defines the scope of the annotation. Possible values are defined in {@link
* com.code_intelligence.jazzer.mutation.utils.PropertyConstraint}. Defaults to {@code RECURSIVE}.
*/
String constraint() default RECURSIVE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ java_library(
}),
deps = [
":aggregates_helper",
"//src/main/java/com/code_intelligence/jazzer/mutation/annotation",
"//src/main/java/com/code_intelligence/jazzer/mutation/api",
"//src/main/java/com/code_intelligence/jazzer/mutation/combinator",
"//src/main/java/com/code_intelligence/jazzer/mutation/support",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,14 @@ static Optional<Constructor<?>> findDefaultConstructor(Class<?> clazz) {
}
}

static Map<String, Method> findGettersByPropertyName(Class<?> clazz) {
return findMethods(clazz, BeanSupport::isGetter)
.collect(toMap(BeanSupport::toPropertyName, method -> method, (a, b) -> a));
}

static Optional<Method[]> findGettersByPropertyNames(
Class<?> clazz, Stream<String> propertyNames) {
Map<String, Method> gettersByPropertyName =
findMethods(clazz, BeanSupport::isGetter)
.collect(toMap(BeanSupport::toPropertyName, method -> method, (a, b) -> a));
Map<String, Method> gettersByPropertyName = findGettersByPropertyName(clazz);
return toArrayOrEmpty(
propertyNames.map(gettersByPropertyName::get).map(Optional::ofNullable), Method[]::new);
}
Expand Down
Loading