All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dronjax.java.objectshutdownproxy.ObjectShutdownProxyComponent Maven / Gradle / Ivy

Go to download

Create proxy for your java object to synchronously wait for all methods to finish calling.

The newest version!
package com.dronjax.java.objectshutdownproxy;

import java.lang.reflect.Proxy;

public final class ObjectShutdownProxyComponent {

  private final T service;
  private final ObjectShutdownInvocationHandler invocationHandler;

  private ObjectShutdownProxyComponent(
      final T service,
      final Class clazzContract,
      final boolean ignoreAnnotation
  ) {
    this.invocationHandler = new ObjectShutdownInvocationHandler<>(
        service,
        clazzContract,
        ignoreAnnotation
    );
    this.service = createProxy(
        service,
        clazzContract
    );
  }

  public static  ObjectShutdownProxyComponent coverAllPublicClassMethod(
      final T service,
      final Class clazzContract
  ) {
    return new ObjectShutdownProxyComponent<>(
        service,
        clazzContract,
        true
    );
  }

  public static  ObjectShutdownProxyComponent coverAnnotatedClassOrMethod(
      final T service,
      final Class clazzContract
  ) {
    return new ObjectShutdownProxyComponent<>(
        service,
        clazzContract,
        false
    );
  }

  public T getProxiedService() {
    return service;
  }

  public ShutdownHandler getShutdownHandler() {
    return invocationHandler;
  }

  @SuppressWarnings("unchecked")
  private T createProxy(
      final T service,
      final Class clazzContract
  ) {
    return (T) Proxy.newProxyInstance(
        service.getClass().getClassLoader(),
        new Class[]{clazzContract},
        invocationHandler
    );
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy