jcabi-aspects
repository·master·Indexed 19 days ago
https://github.com/jcabi/jcabi-aspectsA collection of Aspect-Oriented Programming (AOP) aspects for Java that allow developers to add behaviors such as retries and logging via annotations. Key features include the @RetryOnFailure annotation for automatic retry logic and the @UnitedThrow annotation to restrict or encapsulate exceptions during method execution.
What's inside jcabi-aspects
- jcabi-aspects is a collection of Aspect-Oriented Programming (AOP) aspects for Java. It allows you to modify application behavior—such as adding retry logic or logging—by using annotations instead of writing manual boilerplate code. This promotes cleaner code by separating cross-cutting concerns from business logic.
Use @RetryOnFailure to implement retry logic
masterInstead of manually implementing
do/whileloops for error handling, you can annotate a method with@RetryOnFailure. The AOP aspect will automatically handle retrying the method execution in the event of a failure.import com.jcabi.aspects.RetryOnFailure; public class MyResource { @RetryOnFailure public String load(final URL url) { return url.openConnection().getContent(); } }Use the @UnitedThrow annotation to restrict allowed exceptions
masterThe
@UnitedThrowannotation is used to specify which exceptions are permitted to propagate during the execution of an aspect-advised method. Any exception thrown during execution that is not the specified type (or a subclass of it) will be encapsulated.By default, if no value is provided, all exceptions are encapsulated because the default value is
UnitedThrow.None.@UnitedThrow(MyAllowedException.class) public void myMethod() throws MyAllowedException { // ... }Use UnitedThrow.None to encapsulate all exceptions
masterThe
UnitedThrow.Noneclass is a specialThrowableused as the default value for the@UnitedThrowannotation. When@UnitedThrowis applied without arguments, it effectively instructs the aspect to encapsulate all exceptions, as no specific exception type is marked as 'allowed'.@UnitedThrow // Defaults to UnitedThrow.None.class public void myMethod() { // ... }