Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Dynamic Proxies in Java with JDK Proxy

Tech May 18 2

Proxy patetrns allow indirect access to target objects, enabling additional functionality like access control and auditing. A real-world analogy would be a property agent representing a homeowner.

Static Proxy Limitations

Static proxies require both the proxy and target classes to implement the same interface. This creates several constraints:

  • Each proxy class can only handle one target class
  • Target classes must be known in advance
  • Code reuse becomes difficult with multiple target classes
interface LegalProcess {
    void execute();
}

class Defendant implements LegalProcess {
    private String name;
    
    public Defendant(String name) {
        this.name = name;
    }
    
    @Override
    public void execute() {
        System.out.println(name + " states: I'm innocent");
    }
}

class Attorney implements LegalProcess {
    private Defendant client;
    
    public Attorney(Defendant client) {
        this.client = client;
    }
    
    @Override
    public void execute() {
        System.out.println("Attorney presents evidence: Video shows client was traveling");
        System.out.println("Attorney concludes: Client couldn't have committed the crime");
        client.execute();
    }
}

Dynamic Proxy Advantages

JDK dynamic proxies overcome static proxy limitations by:

  • Creating proxy objects at runtime
  • Working with any interface implementation
  • Allowing flexible method interception
interface MealActions {
    void consumeFood(String item);
    void consumeDrink();
}

class Individual implements MealActions {
    private String name;
    
    public Individual(String name) {
        this.name = name;
    }
    
    @Override
    public void consumeFood(String item) {
        System.out.println(name + " is eating " + item);
    }
    
    @Override
    public void consumeDrink() {
        System.out.println(name + " is drinking tea");
    }
}

public class ProxyDemo {
    public static void main(String[] args) {
        MealActions person = new Individual("John");
        
        MealActions proxy = (MealActions) Proxy.newProxyInstance(
            person.getClass().getClassLoader(),
            person.getClass().getInterfaces(),
            (p, method, params) -> {
                if (method.getName().equals("consumeFood")) {
                    System.out.println("Washing hands before meal");
                    Object result = method.invoke(person, params);
                    System.out.println("Cleaning dishes after meal");
                    return result;
                }
                return method.invoke(person, params);
            }
        );
        
        proxy.consumeFood("sandwich");
    }
}

Key characteristics of JDK dynamic proxies:

  1. Operate at interface level - require both interface and implementation
  2. Proxy objects can only be cast to interfaces, not concrete classes
  3. Only interface-defined methods can be intercepted
  4. Annotations are only readable from interface methods
Tags: Java

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.