Skip to content

Commit

Permalink
Make processor throw exception when undoable reducer store does not i…
Browse files Browse the repository at this point in the history
…mplemented UndoableStore interface
  • Loading branch information
izumin5210 committed Nov 23, 2015
1 parent 3344aa1 commit aae1491
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package info.izumin.android.droidux.processor.exception;

/**
* Created by izumin on 11/24/15.
*/
public class InvalidStateClassException extends RuntimeException {
public InvalidStateClassException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;

import info.izumin.android.droidux.UndoableState;
import info.izumin.android.droidux.annotation.Dispatchable;
import info.izumin.android.droidux.annotation.Reducer;
import info.izumin.android.droidux.annotation.Undoable;
import info.izumin.android.droidux.processor.exception.InvalidClassNameException;
import info.izumin.android.droidux.processor.exception.InvalidStateClassException;
import info.izumin.android.droidux.processor.util.StringUtils;

import static info.izumin.android.droidux.processor.util.AnnotationUtils.findMethodsByAnnotation;
Expand Down Expand Up @@ -51,6 +53,16 @@ public ReducerModel(TypeElement element) {

isUndoable = element.getAnnotation(Undoable.class) != null;

if (isUndoable) {
try {
if (!UndoableState.class.isAssignableFrom(Class.forName(state.packageName() + "." + state.simpleName()))) {
throw new InvalidStateClassException("State class for undoable reducer must implement \"UndoableState<T>\".");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

this.qualifiedName = element.getQualifiedName().toString();
this.packageName = StringUtils.getPackageName(qualifiedName);
this.className = StringUtils.getClassName(qualifiedName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,14 @@ public void reducerWithoutSuffix() {
forSourceLines("DroiduxTodoListReduce", Source.EMPTY)
);
}

@Test
public void undoableReducerWithoutUndoableState() {
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("State class for undoable reducer must implement \"UndoableState<T>\".");
assertJavaSource(
forSourceLines("CounterReduce", Source.UndoableReducerWithoutUndoableState.TARGET),
forSourceLines("DroiduxTodoListReduce", Source.EMPTY)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -585,4 +585,23 @@ public static class ReducerWithoutSuffix {
"}"
};
}

public static class UndoableReducerWithoutUndoableState {
public static final String[] TARGET = {
"package info.izumin.android.droidux.sample;",
"import info.izumin.android.droidux.annotation.Dispatchable;",
"import info.izumin.android.droidux.annotation.Reducer;",
"import info.izumin.android.droidux.annotation.Undoable;",
"import info.izumin.android.droidux.processor.fixture.IncrementCountAction;",
"import info.izumin.android.droidux.processor.fixture.Counter;",
"@Undoable",
"@Reducer(Counter.class)",
"public class CounterReduce {",
" @Dispatchable(IncrementCountAction.class)",
" public Counter onIncrement(Counter state, IncrementCountAction action, String extra) {",
" return state;",
" }",
"}"
};
}
}

0 comments on commit aae1491

Please sign in to comment.