Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/main/java/org/mybatis/cdi/MyBatisBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public MyBatisBean(String id, Class<Type> type, Set<Annotation> qualifiers, Stri
this.type = type;
this.sqlSessionFactoryName = sqlSessionFactoryName;
if (qualifiers == null || qualifiers.isEmpty()) {
this.qualifiers = new HashSet<Annotation>();
this.qualifiers = new HashSet<>();
this.qualifiers.add(new CDIUtils.SerializableDefaultAnnotationLiteral());
this.qualifiers.add(new CDIUtils.SerializableAnyAnnotationLiteral());
} else {
Expand All @@ -77,7 +77,7 @@ public MyBatisBean(String id, Class<Type> type, Set<Annotation> qualifiers, Stri

@Override
public Set<Type> getTypes() {
Set<Type> types = new HashSet<Type>();
Set<Type> types = new HashSet<>();
types.add(this.type);
return types;
}
Expand Down
54 changes: 22 additions & 32 deletions src/main/java/org/mybatis/cdi/MybatisExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -54,11 +53,11 @@ public class MybatisExtension implements Extension {

private static final Logger LOGGER = Logger.getLogger(MybatisExtension.class.getName());

private final Set<BeanKey> sessionProducers = new HashSet<BeanKey>();
private final Set<BeanKey> sessionProducers = new HashSet<>();

private final Set<Type> mapperTypes = new HashSet<Type>();
private final Set<Type> mapperTypes = new HashSet<>();

private final Set<InjectionPoint> injectionPoints = new HashSet<InjectionPoint>();
private final Set<InjectionPoint> injectionPoints = new HashSet<>();

/**
* Collect types of all mappers annotated with Mapper.
Expand Down Expand Up @@ -95,15 +94,17 @@ protected <T, X> void processProducer(@Observes final ProcessProducer<T, X> pp)
final boolean isAnnotated = am.isAnnotationPresent(SessionFactoryProvider.class);
final boolean isSqlSessionFactory = am.getBaseType().equals(SqlSessionFactory.class);
final Object[] logData = { am.getJavaMember().getDeclaringClass().getSimpleName(), am.getJavaMember().getName() };
if (isAnnotated && isSqlSessionFactory) {
LOGGER.log(Level.INFO, "MyBatis CDI Module - SqlSessionFactory producer {0}.{1}", logData);
this.sessionProducers.add(new BeanKey((Class<Type>) (Type) SqlSession.class, am.getAnnotations()));
} else if (isAnnotated && !isSqlSessionFactory) {
LOGGER.log(Level.SEVERE, "MyBatis CDI Module - Invalid return type (Must be SqlSessionFactory): {0}.{1}",
logData);
pp.addDefinitionError(new MybatisCdiConfigurationException(String
.format("SessionFactoryProvider producers must return SqlSessionFactory (%s.%s)", logData[0], logData[1])));
} else if (!isAnnotated && isSqlSessionFactory) {
if (isAnnotated) {
if (isSqlSessionFactory) {
LOGGER.log(Level.INFO, "MyBatis CDI Module - SqlSessionFactory producer {0}.{1}", logData);
this.sessionProducers.add(new BeanKey((Class<Type>) (Type) SqlSession.class, am.getAnnotations()));
} else {
LOGGER.log(Level.SEVERE, "MyBatis CDI Module - Invalid return type (Must be SqlSessionFactory): {0}.{1}",
logData);
pp.addDefinitionError(new MybatisCdiConfigurationException(String
.format("SessionFactoryProvider producers must return SqlSessionFactory (%s.%s)", logData[0], logData[1])));
}
} else if (isSqlSessionFactory) {
LOGGER.log(Level.WARNING,
"MyBatis CDI Module - Ignored SqlSessionFactory producer because it is not annotated with @SessionFactoryProvider: {0}.{1}",
logData);
Expand All @@ -120,9 +121,7 @@ protected <T, X> void processProducer(@Observes final ProcessProducer<T, X> pp)
*/
protected <X> void processInjectionTarget(@Observes ProcessInjectionTarget<X> event) {
final InjectionTarget<X> it = event.getInjectionTarget();
for (final InjectionPoint ip : it.getInjectionPoints()) {
injectionPoints.add(ip);
}
this.injectionPoints.addAll(it.getInjectionPoints());
}

/**
Expand All @@ -136,8 +135,8 @@ protected <X> void processInjectionTarget(@Observes ProcessInjectionTarget<X> ev
protected void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) {
LOGGER.log(Level.INFO, "MyBatis CDI Module - Activated");

Set<BeanKey> mappers = new HashSet<BeanKey>();
Set<BeanKey> sessionTargets = new HashSet<BeanKey>();
Set<BeanKey> mappers = new HashSet<>();
Set<BeanKey> sessionTargets = new HashSet<>();

for (InjectionPoint ip : injectionPoints) {
if (this.mapperTypes.contains(ip.getAnnotated().getBaseType())) {
Expand Down Expand Up @@ -212,7 +211,7 @@ public BeanKey(Class<Type> type, Set<Annotation> annotations) {
}

private Set<Annotation> filterQualifiers(Set<Annotation> annotations) {
final Set<Annotation> set = new HashSet<Annotation>();
final Set<Annotation> set = new HashSet<>();
for (Annotation a : annotations) {
if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
set.add(a);
Expand All @@ -222,13 +221,8 @@ private Set<Annotation> filterQualifiers(Set<Annotation> annotations) {
}

private List<Annotation> sort(Set<Annotation> annotations) {
final List<Annotation> list = new ArrayList<Annotation>(annotations);
Collections.sort(list, new Comparator<Annotation>() {
@Override
public int compare(Annotation a, Annotation b) {
return a.getClass().getName().compareTo(b.getClass().getName());
}
});
final List<Annotation> list = new ArrayList<>(annotations);
Collections.sort(list, (a, b) -> a.getClass().getName().compareTo(b.getClass().getName()));
return list;
}

Expand All @@ -240,16 +234,12 @@ public int compareTo(BeanKey o) {
@Override
public int hashCode() {
int hash = 3;
hash = 43 * hash + (this.key != null ? this.key.hashCode() : 0);
return hash;
return 43 * hash + (this.key != null ? this.key.hashCode() : 0);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (obj == null || this.getClass() != obj.getClass()) {
return false;
}
final BeanKey other = (BeanKey) obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void init() {
if (this.factories.isUnsatisfied()) {
throw new MybatisCdiConfigurationException("There are no SqlSessionFactory producers properly configured.");
}
Map<SqlSessionFactory, SqlSessionManager> m = new HashMap<SqlSessionFactory, SqlSessionManager>();
Map<SqlSessionFactory, SqlSessionManager> m = new HashMap<>();
for (SqlSessionFactory factory : this.factories) {
SqlSessionManager manager = SqlSessionManager.newInstance(factory);
m.put(factory, manager);
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/org/mybatis/cdi/FooServiceJTATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// @EnableWeld
// TODO Remove the following once we drop cdi 1.1 support
@ExtendWith(WeldJunit5Extension.class)
public class FooServiceJTATest {
class FooServiceJTATest {

// TODO Add the following once we drop cdi 1.1 support
// @WeldSetup
Expand All @@ -42,14 +42,14 @@ public class FooServiceJTATest {
private UserTransaction userTransaction;

@Test
public void jtaShouldGetAUserWithNoTX() throws Exception {
void jtaShouldGetAUserWithNoTX() throws Exception {
this.userTransaction.begin();
Assertions.assertEquals("1-User1", this.fooServiceJTA.getUserWithNoTransaction(1).getName());
this.userTransaction.commit();
}

@Test
public void jtaShouldInsertAUserAndCommit() {
void jtaShouldInsertAUserAndCommit() {
User user = new User();
user.setId(20);
user.setName("User20");
Expand All @@ -58,7 +58,7 @@ public void jtaShouldInsertAUserAndCommit() {
}

@Test
public void jtaShouldInsertAUserAndRollItBack() {
void jtaShouldInsertAUserAndRollItBack() {
User user = new User();
user.setId(30);
user.setName("User30");
Expand All @@ -71,7 +71,7 @@ public void jtaShouldInsertAUserAndRollItBack() {
}

@Test
public void jtaShouldInsertAUserWithExistingJtaTxAndCommit() throws Exception {
void jtaShouldInsertAUserWithExistingJtaTxAndCommit() throws Exception {
User user = new User();
user.setId(40);
user.setName("User40");
Expand All @@ -82,7 +82,7 @@ public void jtaShouldInsertAUserWithExistingJtaTxAndCommit() throws Exception {
}

@Test
public void jtaShouldInsertAUserWithExistingJtaTxAndRollItBack() throws Exception {
void jtaShouldInsertAUserWithExistingJtaTxAndRollItBack() throws Exception {
User user = new User();
user.setId(50);
user.setName("User50");
Expand All @@ -92,4 +92,4 @@ public void jtaShouldInsertAUserWithExistingJtaTxAndRollItBack() throws Exceptio
Assertions.assertNull(this.fooServiceJTA.getUserWithNoTransaction(user.getId()));
}

}
}
18 changes: 9 additions & 9 deletions src/test/java/org/mybatis/cdi/FooServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// @EnableWeld
// TODO Remove the following once we drop cdi 1.1 support
@ExtendWith(WeldJunit5Extension.class)
public class FooServiceTest {
class FooServiceTest {

// TODO Add the following once we drop cdi 1.1 support
// @WeldSetup
Expand All @@ -46,7 +46,7 @@ public class FooServiceTest {
private SerializableFooService serFooService;

@Test
public void shouldGetAUser() {
void shouldGetAUser() {
Assertions.assertEquals("1-User1", this.fooService.getUserFromSqlSession(1).getName());
Assertions.assertEquals("1-User1", this.fooService.getUser(1).getName());
Assertions.assertEquals("2-User2", this.fooService.getUser2(2).getName());
Expand All @@ -55,14 +55,14 @@ public void shouldGetAUser() {
}

@Test
public void shouldInjectTheSameMapper() {
void shouldInjectTheSameMapper() {
Assertions.assertEquals(this.fooService.getUser2(1).getName(), this.fooService.getUserDummy(1).getName());
Assertions.assertEquals(this.fooService.getUser2(2).getName(), this.fooService.getUserDummy(2).getName());
Assertions.assertEquals(this.fooService.getUser2(3).getName(), this.fooService.getUserDummy(3).getName());
}

@Test
public void shouldInsertAUserAndCommit() {
void shouldInsertAUserAndCommit() {
User user = new User();
user.setId(20);
user.setName("User20");
Expand All @@ -71,7 +71,7 @@ public void shouldInsertAUserAndCommit() {
}

@Test
public void shouldInsertAUserThatFailsWithRuntimeAndRollItBack() {
void shouldInsertAUserThatFailsWithRuntimeAndRollItBack() {
User user = new User();
user.setId(30);
user.setName("User40");
Expand All @@ -84,7 +84,7 @@ public void shouldInsertAUserThatFailsWithRuntimeAndRollItBack() {
}

@Test
public void shouldInsertAUserThatFailsWithACheckedAndCommit() {
void shouldInsertAUserThatFailsWithACheckedAndCommit() {
User user = new User();
user.setId(30);
user.setName("User30");
Expand All @@ -97,7 +97,7 @@ public void shouldInsertAUserThatFailsWithACheckedAndCommit() {
}

@Test
public void shouldInsertAUserThatFailsWithACustomExceptionMarkedToRollbackAndRollItBack() {
void shouldInsertAUserThatFailsWithACustomExceptionMarkedToRollbackAndRollItBack() {
User user = new User();
user.setId(30);
user.setName("User30");
Expand All @@ -110,7 +110,7 @@ public void shouldInsertAUserThatFailsWithACustomExceptionMarkedToRollbackAndRol
}

@Test
public void injectedMappersAreSerializable() throws Exception {
void injectedMappersAreSerializable() throws Exception {
ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream("target/mapper.ser"));
oout.writeObject(this.serFooService);
oout.close();
Expand All @@ -120,4 +120,4 @@ public void injectedMappersAreSerializable() throws Exception {
Assertions.assertEquals(this.serFooService.getUser(1).getName(), unserialized.getUser(1).getName());
}

}
}
6 changes: 3 additions & 3 deletions src/test/java/org/mybatis/cdi/MybatisExtensionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class MybatisExtensionTest {
class MybatisExtensionTest {

@Test
public <T> void mappersFoundAfterTheBeanUsingTheMapperInAnInjectionPointHasBeenScannedShouldBeInstantiated()
<T> void mappersFoundAfterTheBeanUsingTheMapperInAnInjectionPointHasBeenScannedShouldBeInstantiated()
throws Exception {

MybatisExtension extension = new MybatisExtension();
Expand Down Expand Up @@ -94,4 +94,4 @@ private <T> void processAnnotatedType(MybatisExtension extension, Type type) {
extension.processAnnotatedType(pat);
}

}
}