Service injector - implement stricter requirements for PostConstruct methods
- Implement similar restrictions as prescribed by the PostConstruct documentation: - Class may have at most one method annotated with PostConstruct - PostConstruct method must return void - Javadoc: replace mentions of injector construction where any injection method was meant
This commit is contained in:
@@ -166,7 +166,7 @@ public class AuthMeServiceInitializerTest {
|
||||
PostConstructTestClass testClass = initializer.get(PostConstructTestClass.class);
|
||||
|
||||
// then
|
||||
assertThat(testClass.werePostConstructsCalled(), equalTo(true));
|
||||
assertThat(testClass.wasPostConstructCalled(), equalTo(true));
|
||||
assertThat(testClass.getBetaManager(), not(nullValue()));
|
||||
}
|
||||
|
||||
@@ -188,6 +188,18 @@ public class AuthMeServiceInitializerTest {
|
||||
initializer.get(InvalidPostConstruct.ThrowsException.class);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldThrowForMultiplePostConstructMethods() {
|
||||
// given / when / then
|
||||
initializer.get(InvalidPostConstruct.MultiplePostConstructs.class);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldThrowForPostConstructNotReturningVoid() {
|
||||
// given / when / then
|
||||
initializer.get(InvalidPostConstruct.NotVoidReturnType.class);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldThrowForAbstractNonRegisteredDependency() {
|
||||
// given / when / then
|
||||
|
||||
@@ -9,10 +9,8 @@ import javax.inject.Inject;
|
||||
public abstract class InvalidPostConstruct {
|
||||
|
||||
public static final class WithParams {
|
||||
@SuppressWarnings("unused")
|
||||
@Inject
|
||||
private AlphaService alphaService;
|
||||
@SuppressWarnings("unused")
|
||||
@Inject
|
||||
private ProvidedClass providedClass;
|
||||
|
||||
@@ -41,4 +39,28 @@ public abstract class InvalidPostConstruct {
|
||||
throw new IllegalStateException("Exception in post construct");
|
||||
}
|
||||
}
|
||||
|
||||
public static final class NotVoidReturnType {
|
||||
@Inject
|
||||
private ProvidedClass providedClass;
|
||||
|
||||
@PostConstruct
|
||||
public int returnsInt() {
|
||||
return 42;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class MultiplePostConstructs {
|
||||
@Inject
|
||||
private ProvidedClass providedClass;
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct1() {
|
||||
// --
|
||||
}
|
||||
@PostConstruct
|
||||
public void postConstruct2() {
|
||||
// --
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,22 +17,15 @@ public class PostConstructTestClass implements SettingsDependent {
|
||||
@Inject
|
||||
private BetaManager betaManager;
|
||||
private boolean wasPostConstructCalled = false;
|
||||
private boolean wasSecondPostConstructCalled = false;
|
||||
private boolean wasReloaded = false;
|
||||
|
||||
@PostConstruct
|
||||
protected void setFieldToTrue() {
|
||||
public void postConstructMethod() {
|
||||
wasPostConstructCalled = true;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public int otherPostConstructMethod() {
|
||||
wasSecondPostConstructCalled = true;
|
||||
return 42;
|
||||
}
|
||||
|
||||
public boolean werePostConstructsCalled() {
|
||||
return wasPostConstructCalled && wasSecondPostConstructCalled;
|
||||
public boolean wasPostConstructCalled() {
|
||||
return wasPostConstructCalled;
|
||||
}
|
||||
|
||||
public BetaManager getBetaManager() {
|
||||
|
||||
Reference in New Issue
Block a user