Skip to content

Instantly share code, notes, and snippets.

@mikebrock
Created March 12, 2013 23:04
Show Gist options
  • Select an option

  • Save mikebrock/5147923 to your computer and use it in GitHub Desktop.

Select an option

Save mikebrock/5147923 to your computer and use it in GitHub Desktop.
@Retention(RetentionPolicy.RUNTIME)
public @interface LogCall {
}
@CodeDecorator
public class LogCallDecorator extends IOCDecoratorExtension<LogCall> {
public LogCallDecorator(Class<LogCall> decoratesWith) {
super(decoratesWith);
}
@Override
public List<? extends Statement> generateDecorator(InjectableInstance<LogCall> ctx) {
ctx.getInjector().addInvokeBefore(ctx.getMethod(),
Stmt.invokeStatic(TestDataCollector.class, "beforeInvoke", Refs.get("a0"), Refs.get("a1")));
ctx.getInjector().addInvokeAfter(ctx.getMethod(),
Stmt.invokeStatic(TestDataCollector.class, "afterInvoke", Refs.get("a0"), Refs.get("a1")));
return Collections.emptyList();
}
}
@Singleton
public class MyDecoratedBean {
private final Map<String, Integer> testMap = new HashMap<String, Integer>();
@LogCall
public void someMethod(final String text, final Integer blah) {
testMap.put(text, blah);
}
public Map<String, Integer> getTestMap() {
return testMap;
}
}
public class TestDataCollector {
private static final Map<String, Integer> beforeInvoke = new HashMap<String, Integer>();
private static final Map<String, Integer> afterInvoke = new HashMap<String, Integer>();
public static void beforeInvoke(String a, Integer b) {
beforeInvoke.put(a, b);
}
public static void afterInvoke(String a, Integer b) {
afterInvoke.put(a, b);
}
public static Map<String, Integer> getBeforeInvoke() {
return beforeInvoke;
}
public static Map<String, Integer> getAfterInvoke() {
return afterInvoke;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment