Skip to content

Java implementation of an Aspect-Oriented Programming (AOP) system using the Dynamic Proxy API. Features a centralized Factory and Aspect Weaver to apply before, after, and around advices to target interfaces at runtime.

Notifications You must be signed in to change notification settings

evankost/aop-proxy-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AOP Proxy Implementation in Java

Java implementation of an Aspect-Oriented Programming (AOP) mechanism using the Dynamic Proxy API. This project allows for method interception at runtime to apply cross-cutting concerns—such as logging, security, and performance monitoring—without modifying the original source code.


Features

  • Dynamic Method Interception: Use of java.lang.reflect.Proxy to wrap target objects and intercept method calls.

  • Advice Support:

    • Before Advice: Logic executed before the target method.

    • After Advice: Logic executed after the target method finishes.

    • Around Advice: Logic that intercepts the execution entirely, allowing for return value modification or exception handling.

  • Builder API: An AspectBuilder to easily define target classes and associate specific advices with method signatures.

  • Automated Weaving: An AspectWeaver that dynamically applies registered aspects to target instances.

  • Factory Pattern: A centralized Factory for creating builders and weavers while managing a shared registry of aspects.


Technical Implementation

To handle method interception and runtime logic, this implementation:

  • Uses Dynamic Proxies: Leverages Proxy.newProxyInstance to wrap any object that implements at least one interface.
  • State Management: Uses an InvocationHandler to coordinate the execution flow of before, around, and after advices.
  • Decoupled Architecture: Advice logic is stored as Runnable tasks mapped to specific Method objects.

Getting Started

Prerequisites

  • Java Development Kit (JDK) 17 or higher.
  • Basic understanding of Reflection and Dynamic Proxies.

Installation

Clone the repository:

git clone https://github.com/evankost/aop-proxy-java.git
cd aop-proxy-java

Usage

1. Build an Aspect

Use the factory to define which methods should trigger specific logic.

Factory factory = new Factory();
AspectBuilder builder = factory.newAspectBuilder()
    .withTargets(new Class<?>[]{ TargetService.class })
    .withBeforeAdviceFor(() -> System.out.println("Method starting..."), targetMethod)
    .withAfterAdviceFor(() -> System.out.println("Method finished."), targetMethod)
    .build(); // Automatically registered in the factory

2. Weave and Execute

Apply the aspect to a concrete implementation to get a proxied object.

AspectWeaver weaver = factory.newAspectWeaver();
TargetService service = new TargetServiceImpl();

// The weaver returns a proxied version of your object
TargetService proxied = (TargetService) weaver.weave(service);

// Method calls now trigger the registered advices automatically
proxied.performAction("test"); 

Running Tests and Building with Gradle

The project uses Gradle for build automation and the Spock Framework for its comprehensive test suite.

  1. Navigate to the project directory:
cd aop-proxy-java

2.Build the Project:

./gradlew build
  • Compiles the source code and packages the project.

3.Run Tests:

./gradlew test
  • Executes all unit and integration tests.

  • Verifies security controls, performance monitoring, and exception handling.

4.View Test Results:

  • Open lib/build/reports/tests/test/index.html in a browser for detailed reports on execution order and interception success.

5.Clean and Rebuild:

./gradlew clean build

License & Citation

Academic use. Implementation of the (original repository) specification.

About

Java implementation of an Aspect-Oriented Programming (AOP) system using the Dynamic Proxy API. Features a centralized Factory and Aspect Weaver to apply before, after, and around advices to target interfaces at runtime.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published