Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Factorize surround with try/catch generation
Extract the surround-with-try-catch part of the anonymous Runnable
generation, in order to reuse it for generating other anonymous classes.
  • Loading branch information
rom1v committed Apr 22, 2013
commit 1ab7373fecc389a165580d9b74b6a94cb8bc8faf
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ public String getIdStringFromIdFieldRef(JFieldRef idRef) {
throw new IllegalStateException("Unable to extract target name from JFieldRef");
}

public JTryBlock surroundWithTryCatch(EBeanHolder holder, JBlock block, JBlock content, String exceptionMessage) {
Classes classes = holder.classes();
JTryBlock tryBlock = block._try();
tryBlock.body().add(content);
JCatchBlock catchBlock = tryBlock._catch(classes.RUNTIME_EXCEPTION);
JVar exceptionParam = catchBlock.param("e");
JInvocation errorInvoke = classes.LOG.staticInvoke("e");
errorInvoke.arg(holder.generatedClass.name());
errorInvoke.arg(exceptionMessage);
errorInvoke.arg(exceptionParam);
catchBlock.body().add(errorInvoke);
return tryBlock;
}

public JDefinedClass createDelegatingAnonymousRunnableClass(EBeanHolder holder, JMethod delegatedMethod) {

JCodeModel codeModel = holder.codeModel();
Expand All @@ -242,20 +256,9 @@ public JDefinedClass createDelegatingAnonymousRunnableClass(EBeanHolder holder,
runMethod.annotate(Override.class);

JBlock runMethodBody = runMethod.body();
JTryBlock runTry = runMethodBody._try();

runTry.body().add(previousMethodBody);

JCatchBlock runCatch = runTry._catch(classes.RUNTIME_EXCEPTION);
JVar exceptionParam = runCatch.param("e");

JInvocation errorInvoke = classes.LOG.staticInvoke("e");

errorInvoke.arg(holder.generatedClass.name());
errorInvoke.arg("A runtime exception was thrown while executing code in a runnable");
errorInvoke.arg(exceptionParam);
surroundWithTryCatch(holder, runMethodBody, previousMethodBody, "A runtime exception was thrown while executing code in a runnable");

runCatch.body().add(errorInvoke);
return anonymousRunnableClass;
}

Expand Down