Skip to content

Commit

Permalink
Fix #33 Revise handler method names
Browse files Browse the repository at this point in the history
- More changes will follow with #48
  • Loading branch information
ljacqu committed Jun 13, 2017
1 parent dc87285 commit 4c29a38
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 63 deletions.
6 changes: 3 additions & 3 deletions injector/src/main/java/ch/jalu/injector/InjectorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void provide(Class<? extends Annotation> clazz, Object object) {
checkNotNull(clazz, "Provided annotation may not be null");
for (Handler handler : config.getHandlers()) {
try {
handler.processProvided(clazz, object);
handler.onAnnotation(clazz, object);
} catch (Exception e) {
rethrowException(e);
}
Expand Down Expand Up @@ -238,7 +238,7 @@ private <T> Instantiation<? extends T> getInstantiation(UnresolvedInstantiationC
private void processPreConstructorHandlers(UnresolvedInstantiationContext<?> unresolvedContext) {
for (Handler handler : config.getHandlers()) {
try {
handler.accept(unresolvedContext);
handler.preProcess(unresolvedContext);
} catch (Exception e) {
rethrowException(e);
}
Expand All @@ -249,7 +249,7 @@ private <T> T runPostConstructHandlers(T instance, ResolvedInstantiationContext<
T object = instance;
for (Handler handler : config.getHandlers()) {
try {
object = firstNotNull(handler.process(object, resolvedContext), object);
object = firstNotNull(handler.postProcess(object, resolvedContext), object);
} catch (Exception e) {
rethrowException(e);
}
Expand Down
6 changes: 3 additions & 3 deletions injector/src/main/java/ch/jalu/injector/handlers/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface Handler {
* @param <T> the class' type
* @throws Exception for failed validation or preconditions
*/
default <T> void accept(UnresolvedInstantiationContext<T> context) throws Exception {
default <T> void preProcess(UnresolvedInstantiationContext<T> context) throws Exception {
}

/**
Expand Down Expand Up @@ -69,7 +69,7 @@ default Object resolveValue(ResolvedInstantiationContext<?> context,
* @throws Exception for validation errors or similar
*/
@Nullable
default <T> T process(T object, ResolvedInstantiationContext<T> context) throws Exception {
default <T> T postProcess(T object, ResolvedInstantiationContext<T> context) throws Exception {
return null;
}

Expand All @@ -80,7 +80,7 @@ default <T> T process(T object, ResolvedInstantiationContext<T> context) throws
* @param object the object
* @throws Exception for failed validations
*/
default void processProvided(Class<? extends Annotation> annotationType, @Nullable Object object) throws Exception {
default void onAnnotation(Class<? extends Annotation> annotationType, @Nullable Object object) throws Exception {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Object resolveValue(ResolvedInstantiationContext context, DependencyDescr
}

@Override
public void processProvided(Class<? extends Annotation> annotation, Object object) {
public void onAnnotation(Class<? extends Annotation> annotation, Object object) {
InjectorUtils.checkNotNull(object, "Object may not be null");
if (storedValues.containsKey(annotation)) {
throw new InjectorException("Value already registered for @" + annotation.getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public <T> Instantiation<? extends T> get(UnresolvedInstantiationContext<T> cont
}

@Override
public <T> T process(T object, ResolvedInstantiationContext<T> context) throws Exception {
public <T> T postProcess(T object, ResolvedInstantiationContext<T> context) {
if (shouldCacheMethod(context) && getInstantiation(context) == null) {
entries.put(context.getMappedClass().getCanonicalName(),
new WeakReference<>(context.getInstantiation()));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class PostConstructMethodInvoker implements Handler {

@Override
public <T> T process(T object, ResolvedInstantiationContext<T> context) {
public <T> T postProcess(T object, ResolvedInstantiationContext<T> context) {
Class<?> clazz = object.getClass();
List<Method> postConstructMethods = getPostConstructMethods(clazz);
for (int i = postConstructMethods.size() - 1; i >= 0; --i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public PreConstructPackageValidator(String rootPackage) {
}

@Override
public <T> void accept(UnresolvedInstantiationContext<T> context) {
public <T> void preProcess(UnresolvedInstantiationContext<T> context) {
final Class<?> clazz = context.getMappedClass();
if (clazz.getPackage() == null) {
String detail = clazz.isPrimitive()
Expand Down
6 changes: 3 additions & 3 deletions injector/src/test/java/ch/jalu/injector/InjectorImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ public void shouldForwardToAnnotationValueHandlers() throws Exception {
injector.provide(Duration.class, object);

// then
verify(annoValHandler1).processProvided(Duration.class, object);
verify(annoValHandler2).processProvided(Duration.class, object);
verify(annoValHandler1).onAnnotation(Duration.class, object);
verify(annoValHandler2).onAnnotation(Duration.class, object);
}

@Test
Expand All @@ -332,7 +332,7 @@ public void shouldForwardException() throws Exception {
Class<? extends Annotation> annotation = Size.class;
Object object = 123;
Handler annoValHandler = mock(Handler.class);
doThrow(Exception.class).when(annoValHandler).processProvided(annotation, object);
doThrow(Exception.class).when(annoValHandler).onAnnotation(annotation, object);
config.addHandlers(Collections.singletonList(annoValHandler));

// expect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SavedAnnotationsHandlerTest {
public void shouldReturnRegisteredValue() {
// given
Object object = "value for @Duration";
savedAnnotationsHandler.processProvided(Duration.class, object);
savedAnnotationsHandler.onAnnotation(Duration.class, object);
Annotation[] annotations = {
newSizeAnnotation("value"), newDurationAnnotation()
};
Expand All @@ -45,7 +45,7 @@ public void shouldReturnNullForUnregisteredAnnotation() {
};
DependencyDescription dependencyDescription = new DependencyDescription(null, annotations);
// register some object under another annotation for the heck of it
savedAnnotationsHandler.processProvided(Test.class, new Object());
savedAnnotationsHandler.onAnnotation(Test.class, new Object());

// when
Object result = savedAnnotationsHandler.resolveValue(null, dependencyDescription);
Expand All @@ -57,18 +57,18 @@ public void shouldReturnNullForUnregisteredAnnotation() {
@Test(expected = InjectorException.class)
public void shouldThrowForSecondAnnotationRegistration() {
// given
savedAnnotationsHandler.processProvided(Size.class, 12);
savedAnnotationsHandler.onAnnotation(Size.class, 12);

// when
savedAnnotationsHandler.processProvided(Size.class, -8);
savedAnnotationsHandler.onAnnotation(Size.class, -8);

// then - exception
}

@Test(expected = InjectorException.class)
public void shouldThrowForNullValueAssociatedToAnnotation() {
// given / when
savedAnnotationsHandler.processProvided(Duration.class, null);
savedAnnotationsHandler.onAnnotation(Duration.class, null);

// then - exception
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void shouldExecutePostConstructMethod() {
PostConstructTestClass testClass = new PostConstructTestClass(123, new ProvidedClass(""));

// when
postConstructInvoker.process(testClass, null);
postConstructInvoker.postProcess(testClass, null);

// then
assertThat(testClass.wasPostConstructCalled(), equalTo(true));
Expand All @@ -46,7 +46,7 @@ public void shouldThrowForInvalidPostConstructMethod() {
exceptionCatcher.expect("@PostConstruct method may not be static or have any parameters");

// when
postConstructInvoker.process(withParams, null);
postConstructInvoker.postProcess(withParams, null);
}

@Test
Expand All @@ -58,7 +58,7 @@ public void shouldThrowForStaticPostConstructMethod() {
exceptionCatcher.expect("@PostConstruct method may not be static or have any parameters");

// when
postConstructInvoker.process(classWithStaticMethod, null);
postConstructInvoker.postProcess(classWithStaticMethod, null);
}

@Test
Expand All @@ -70,7 +70,7 @@ public void shouldForwardExceptionFromPostConstruct() {
exceptionCatcher.expect("Could not invoke method");

// when
postConstructInvoker.process(throwsException, null);
postConstructInvoker.postProcess(throwsException, null);
}

@Test
Expand All @@ -83,7 +83,7 @@ public void shouldThrowForMultiplePostConstructMethods() {
exceptionCatcher.expect("Multiple methods with @PostConstruct");

// when
postConstructInvoker.process(multiplePostConstructs, null);
postConstructInvoker.postProcess(multiplePostConstructs, null);
}

@Test
Expand All @@ -95,7 +95,7 @@ public void shouldThrowForPostConstructNotReturningVoid() {
exceptionCatcher.expect("@PostConstruct method must have return type void");

// when
postConstructInvoker.process(notVoidReturnType, null);
postConstructInvoker.postProcess(notVoidReturnType, null);
}

@Test
Expand All @@ -104,7 +104,7 @@ public void shouldCallPostConstructOnParentsButNotOnNoMethodScanClasses() {
ChildClass childClass = new ChildClass();

// when
postConstructInvoker.process(childClass, null);
postConstructInvoker.postProcess(childClass, null);

// then
assertThat(childClass.wasChildPostConstCalled, equalTo(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import static org.junit.Assert.assertThat;

/**
* Tests that {@link PostConstructHandler} implementations
* can change the object that will be stored.
* Tests that handler implementations can change the object that will be stored on post construct.
*/
public class PostConstructRemappingTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import ch.jalu.injector.TestUtils.ExceptionCatcher;
import ch.jalu.injector.annotations.NoFieldScan;
import ch.jalu.injector.context.UnresolvedInstantiationContext;
import ch.jalu.injector.handlers.postconstruct.PostConstructHandler;
import ch.jalu.injector.handlers.postconstruct.PostConstructMethodInvoker;
import ch.jalu.injector.handlers.provider.ProviderHandlerImpl;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand All @@ -25,9 +26,9 @@ public class PreConstructPackageValidatorTest {
@Test
public void shouldAcceptValidPackage() {
// given / when
validator.accept(buildContext(Injector.class));
validator.accept(buildContext(PostConstructHandler.class));
validator.accept(buildContext(NoFieldScan.class));
validator.preProcess(buildContext(Injector.class));
validator.preProcess(buildContext(PostConstructMethodInvoker.class));
validator.preProcess(buildContext(NoFieldScan.class));

// then - no exception thrown
}
Expand All @@ -38,7 +39,7 @@ public void shouldThrowForPrimitiveClass() {
exceptionCatcher.expect("Primitive types must be provided");

// when
validator.accept(buildContext(boolean.class));
validator.preProcess(buildContext(boolean.class));
}

@Test
Expand All @@ -47,7 +48,7 @@ public void shouldThrowForArrayClass() {
exceptionCatcher.expect("Unknown how to inject array classes");

// when
validator.accept(buildContext(PostConstructHandler[].class));
validator.preProcess(buildContext(ProviderHandlerImpl[].class));
}

@Test
Expand All @@ -56,7 +57,7 @@ public void shouldRejectClassWithInvalidPackage() {
exceptionCatcher.expect("outside of the allowed packages");

// when
validator.accept(buildContext(Test.class));
validator.preProcess(buildContext(Test.class));
}

private static <T> UnresolvedInstantiationContext<T> buildContext(Class<T> clz) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public <T> void register(Class<T> parent, Class<? extends T> child) {
}

@Override
public <T> void accept(UnresolvedInstantiationContext<T> context) {
public <T> void preProcess(UnresolvedInstantiationContext<T> context) {
increment();
Class<? extends T> implClass = getImplClass(context.getMappedClass());
if (implClass != null) {
Expand All @@ -45,7 +45,7 @@ private <T> Class<? extends T> getImplClass(Class<T> clazz) {
}

@Override
public <T> T process(T object, ResolvedInstantiationContext<T> context) {
public <T> T postProcess(T object, ResolvedInstantiationContext<T> context) {
// Injector doesn't register the object with the mapped class by default. In this test case, this is desirable.
Injector injector = context.getInjector();
if (context.getMappedClass() != context.getOriginalClass()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ProfilePostConstructHandler(InjectorImpl injector) {
}

@Override
public <T> T process(final T object, ResolvedInstantiationContext<T> context) throws ReflectiveOperationException {
public <T> T postProcess(final T object, ResolvedInstantiationContext<T> context) throws ReflectiveOperationException {
final Class<?> clazz = object.getClass();
if (!hasProfileMethod(clazz)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ThrowingPostConstructHandler(Class<?>... throwForClasses) {
}

@Override
public <T> T process(T object, ResolvedInstantiationContext<T> context) {
public <T> T postProcess(T object, ResolvedInstantiationContext<T> context) {
increment();
for (Class<?> clazz : throwForClasses) {
if (clazz.isInstance(object)) {
Expand Down

0 comments on commit 4c29a38

Please sign in to comment.