Implementing Aspect Oriented Programming in Kotlin with Guice

Clavin June
Mar 10 · 4 min read

Today I learn how to implement AOP in Kotlin with Guice, and I want to share with you.

Introduction

Aspect Oriented Programming is just another programming orientation which helps you increase modularity level in your code by grouping the same requirement / behavior / concern into the same module and inject them to the code without modifying any existing code. I think this concept is equivalent with High Order Component, Middleware, or Decorator in other programming language.

Why Guice?

Guice made by google to facilitate dependency injection, service binder, method interceptor, etc. We can use method interceptor feature to easily implementing AOP. But, Guice has several limitations, such as:

  • Classes must be public or package-private.
  • Classes must be non-final
  • Methods must be public, package-private or protected
  • Methods must be non-final
  • Instances must be created by Guice by an @Inject-annotated or no-argument constructor. It is not possible to use method interception on instances that aren't constructed by Guice.

There are at lease 4 terms you need to know before implementing Aspect Oriented Programming, such as:

Cross-cutting concerns

Cross-cutting concerns is referring to what kind of concerns / behaviors / requirements that can be grouped. Let’s say we have some method that need to be logged every time it’s called then we can call it logging concern, or maybe you need to authenticate user credentials on API call, you can call it concern too.

Advice

Advice in aspect oriented programming is the code of the concern that you want to embed to the code.

Pointcut

Pointcut is referring to the point of execution that the advice will be injected. Let’s say we want to inject the logging advice to every method that has annotation @Loggable, or you want to inject the logging advice to any method without arguments. That’s pointcut.

Aspect

Aspect is simple Aspect = Pointcut + Advice .

Code Time

Let’s use the easiest example. We want to create Logging Aspect and inject it to every method with @Loggable annotation.

Creating annotation

Logable annotation

Creating annotation called Loggable with target Function because we want to use this annotation on method only, by default this annotation Retention is RUNTIME so we don’t need to modify it.

Creating advice

Creating the advice by implementing MethodInterceptor interface which provided by Guice, you are force to implement invoke function with MethodInvocation as a parameter. Invocation is your method, by using invocation.proceed() you are invoking your method that using @Loggable annotation. So, any line code before invocation.proceed() is invoked before your method, and any line code after invocation.proceed() is invoked after your method is called. invocation.proceed() will return the result of your method.

Creating Aspect and Defining Pointcut

Aspect is advice + pointcut, so by defining the point cut and defining what advice will injected to that pointcut, you are creating the aspect. By Guice, you just need to create a class that extends AbstractModule so you need to override configure method. Using bindInterceptor, you can define the class pointcut, method pointcut, and defining the advice.

Creating Main Application

  1. Declare Main Interface and its implementation
Declaring Interface and class

Remember the limitation Classes must be non-final, by default Kotlin class is Final, adding open keyword to the class means, it is no longer final class. Don’t forget to add the Loggable annotation.

2. Register Main Interface and its implementation to guice due to this limitation Instances must be created by Guice.

Declaring Module for Main Interface

3. Run the application and create the instance with guice

4. Logging advice will run

logging advice run

Thank you for reading!


If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

Faun

The Must-Read Publication for Aspiring Developers & DevOps…

Clavin June

Written by

a bunch of perceptrons

Faun

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

More From Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade