| 1 | package com.reallifedeveloper.common.domain.registry; | |
| 2 | ||
| 3 | import java.util.concurrent.locks.Lock; | |
| 4 | import java.util.concurrent.locks.ReentrantLock; | |
| 5 | ||
| 6 | import org.checkerframework.checker.nullness.qual.Nullable; | |
| 7 | import org.springframework.context.ApplicationContext; | |
| 8 | import org.springframework.context.ApplicationContextAware; | |
| 9 | ||
| 10 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | |
| 11 | ||
| 12 | /** | |
| 13 | * Base class for Spring-based domain registries. A domain registry is conceptually a singleton and should only be used when dependency | |
| 14 | * injection cannot be used, e.g., in entities or value objects. | |
| 15 | * | |
| 16 | * @author RealLifeDeveloper | |
| 17 | */ | |
| 18 | public abstract class AbstractDomainRegistry implements ApplicationContextAware { | |
| 19 | ||
| 20 | private static final Lock CLASS_LOCK = new ReentrantLock(); | |
| 21 | ||
| 22 | private static @Nullable ApplicationContext applicationContext; | |
| 23 | ||
| 24 | /** | |
| 25 | * Looks up the Spring bean of the given type. This method never returns {@code null}; if the bean cannot be found, an exception is | |
| 26 | * thrown. | |
| 27 | * | |
| 28 | * @param <T> the type of the bean to look up | |
| 29 | * @param beanType the class of the bean to look up | |
| 30 | * | |
| 31 | * @return the Spring bean, never {@code null} | |
| 32 | */ | |
| 33 | protected static <T> T getBean(Class<T> beanType) { | |
| 34 |
1
1. getBean : negated conditional → KILLED |
if (applicationContext == null) { |
| 35 | throw new IllegalStateException("DomainRegistry has not been initialized"); | |
| 36 | } | |
| 37 |
1
1. getBean : replaced return value with null for com/reallifedeveloper/common/domain/registry/AbstractDomainRegistry::getBean → KILLED |
return applicationContext.getBean(beanType); |
| 38 | } | |
| 39 | ||
| 40 | @Override | |
| 41 | @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification = "We handle concurrency issues with a lock") | |
| 42 | public void setApplicationContext(ApplicationContext applicationContext) { | |
| 43 |
1
1. setApplicationContext : removed call to java/util/concurrent/locks/Lock::lock → KILLED |
CLASS_LOCK.lock(); |
| 44 | try { | |
| 45 | AbstractDomainRegistry.applicationContext = applicationContext; | |
| 46 | } finally { | |
| 47 |
1
1. setApplicationContext : removed call to java/util/concurrent/locks/Lock::unlock → TIMED_OUT |
CLASS_LOCK.unlock(); |
| 48 | } | |
| 49 | } | |
| 50 | } | |
Mutations | ||
| 34 |
1.1 |
|
| 37 |
1.1 |
|
| 43 |
1.1 |
|
| 47 |
1.1 |