BaseJpaRepository.java

1
package com.reallifedeveloper.common.infrastructure.persistence;
2
3
import java.lang.reflect.Method;
4
import java.util.Optional;
5
6
import org.springframework.data.jpa.repository.Query;
7
8
/**
9
 * Base class for implementations of repositories that use JPA but that cannot use Spring Data, e.g., because they work with more than one
10
 * entity type.
11
 *
12
 * @author RealLifeDeveloper
13
 */
14
public class BaseJpaRepository {
15
16
    /**
17
     * Creates a new {@code BaseJpaRepository}, intended to be used by sub-classes.
18
     */
19
    protected BaseJpaRepository() {
20
        // The only constructor is protected, to disallow direct instantiation.
21
    }
22
23
    /**
24
     * Gives the value of the {@code @Query} annotation on the given method. The annotation can be on the method in the class, or on the
25
     * method in an implemented interface.
26
     *
27
     * @param methodName     the name of the method with the {@code @Query} annotation
28
     * @param parameterTypes the classes of the method parameters
29
     *
30
     * @return the query string, i.e., the value of the {@code @Query} annotation, or an empty Optional if no such annotation can be found
31
     */
32
    protected Optional<String> getQueryString(String methodName, Class<?>... parameterTypes) {
33
        try {
34 1 1. getQueryString : replaced return value with Optional.empty for com/reallifedeveloper/common/infrastructure/persistence/BaseJpaRepository::getQueryString → KILLED
            return findQueryAnnotation(methodName, parameterTypes).map(Query::value);
35
        } catch (NoSuchMethodException e) {
36
            throw new IllegalStateException("Unexpected error, this should never occur", e);
37
        }
38
    }
39
40
    @SuppressWarnings({ "PMD.EmptyCatchBlock", "PMD.AvoidAccessibilityAlteration" })
41
    private Optional<Query> findQueryAnnotation(String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
42
        Method method = getClass().getDeclaredMethod(methodName, parameterTypes);
43 1 1. findQueryAnnotation : removed call to java/lang/reflect/Method::setAccessible → SURVIVED
        method.setAccessible(true);
44
        Query query = method.getAnnotation(Query.class);
45 1 1. findQueryAnnotation : negated conditional → KILLED
        if (query == null) {
46
            for (Class<?> c : getClass().getInterfaces()) {
47
                try {
48
                    method = c.getDeclaredMethod(methodName, parameterTypes);
49 1 1. findQueryAnnotation : removed call to java/lang/reflect/Method::setAccessible → SURVIVED
                    method.setAccessible(true);
50
                    query = method.getDeclaredAnnotation(Query.class);
51 1 1. findQueryAnnotation : negated conditional → SURVIVED
                    if (query != null) {
52
                        break;
53
                    }
54
                } catch (NoSuchMethodException e) {
55
                    // Ignore, try next interface (if there is one)
56
                }
57
            }
58
        }
59 1 1. findQueryAnnotation : replaced return value with Optional.empty for com/reallifedeveloper/common/infrastructure/persistence/BaseJpaRepository::findQueryAnnotation → KILLED
        return Optional.ofNullable(query);
60
    }
61
}

Mutations

34

1.1
Location : getQueryString
Killed by : com.reallifedeveloper.common.infrastructure.persistence.JpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.infrastructure.persistence.JpaRepositoryTest]/[method:foo()]
replaced return value with Optional.empty for com/reallifedeveloper/common/infrastructure/persistence/BaseJpaRepository::getQueryString → KILLED

43

1.1
Location : findQueryAnnotation
Killed by : none
removed call to java/lang/reflect/Method::setAccessible → SURVIVED
Covering tests

45

1.1
Location : findQueryAnnotation
Killed by : com.reallifedeveloper.common.infrastructure.persistence.JpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.infrastructure.persistence.JpaRepositoryTest]/[method:foo()]
negated conditional → KILLED

49

1.1
Location : findQueryAnnotation
Killed by : none
removed call to java/lang/reflect/Method::setAccessible → SURVIVED
Covering tests

51

1.1
Location : findQueryAnnotation
Killed by : none
negated conditional → SURVIVED
Covering tests

59

1.1
Location : findQueryAnnotation
Killed by : com.reallifedeveloper.common.infrastructure.persistence.JpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.infrastructure.persistence.JpaRepositoryTest]/[method:foo()]
replaced return value with Optional.empty for com/reallifedeveloper/common/infrastructure/persistence/BaseJpaRepository::findQueryAnnotation → KILLED

Active mutators

Tests examined


Report generated by PIT 1.20.2