Package javassist.util.proxy

Examples of javassist.util.proxy.ProxyFactory$CacheKey

Then, the following method call will be forwarded to MethodHandler mi and prints a message before executing the originally called method bar() in Foo.

The last three lines of the code shown above can be replaced with a call to the helper method create, which generates a proxy class, instantiates it, and sets the method handler of the instance:

To change the method handler during runtime, execute the following code:

You can also specify the default method handler:

The default handler is implicitly attached to an instance of the generated class c2. Calling setHandler on the instance is not necessary unless another method handler must be attached to the instance.

The following code is an example of method handler. It does not execute anything except invoking the original method:

A proxy object generated by ProxyFactory is serializable if its super class or interfaces implement a java.io.Serializable. However, a serialized proxy object will not be compatible with future releases. The serialization support should be used for short-term storage or RMI. @see MethodHandler @since 3.1


  private JavassistDataObjectInstantiator() {
  }

  public <T> T newInstance(Class<T> classToInstantiate) {
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setSuperclass(classToInstantiate);
    Class newClass = proxyFactory.createClass();
    Object instance;
    try {
      instance = newClass.newInstance();
    } catch (Exception e) {
      throw new MappingException(e);
View Full Code Here


         socket.setEnabledProtocols(protocols);
      if (cipherSuites != null)
         socket.setEnabledCipherSuites(cipherSuites);

      DomainServerSocket handler = new DomainServerSocket(socket);
      ProxyFactory pf = new ProxyFactory();
      pf.setHandler(handler);
      pf.setSuperclass(SSLServerSocket.class);
      Class[] sig = {};
      Object[] args = {};

      SSLServerSocket proxy = null;
      try
      {
         proxy = (SSLServerSocket) pf.create(sig, args);
      }
      catch (Exception e)
      {
         IOException ioe = new IOException("Failed to create SSLServerSocket proxy");
         ioe.initCause(e);
View Full Code Here

         socket.setEnabledProtocols(protocols);
      if( cipherSuites != null )
         socket.setEnabledCipherSuites(cipherSuites);

      DomainServerSocket handler = new DomainServerSocket(socket);
      ProxyFactory pf = new ProxyFactory();
      pf.setHandler(handler);
      pf.setSuperclass(SSLServerSocket.class);
      Class[] sig = {};
      Object[] args = {};

      SSLServerSocket proxy = null;
      try
      {
         proxy = (SSLServerSocket) pf.create(sig, args);
      }
      catch (Exception e)
      {
         IOException ioe = new IOException("Failed to create SSLServerSocket proxy");
         ioe.initCause(e);
View Full Code Here

  @Override
  @SuppressWarnings("unchecked")
  public <G extends GlobalContext<?, ?>> G createGlobalContext(Class<? extends G> globalContextImplType,
      final Class<? extends EntityContext<?, ?>> entityContextImplType, Class<? extends PropertyContext<?, ?>> propertyContextImplType) {

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setSuperclass( globalContextImplType );
    proxyFactory.setFilter( new EntityMethodFilter() );

    try {
      return (G) proxyFactory.create(
          new Class<?>[] { ConfigurationContext.class },
          new Object[] { this },
          new EntityOrPropertyMethodHandler( entityContextImplType, propertyContextImplType ) );
    }
    catch (Exception e) {
View Full Code Here

  @SuppressWarnings("unchecked")
  private <E extends EntityContext<?, ?>> E createEntityMappingContext(Class<? extends E> entityContextImplType,
      Class<? extends PropertyContext<?, ?>> propertyContextImplType) {

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setSuperclass( entityContextImplType );
    proxyFactory.setFilter( new EntityOrPropertyMethodFilter() );

    try {
      return (E) proxyFactory.create(
          new Class<?>[] { ConfigurationContext.class },
          new Object[] { this },
          new EntityOrPropertyMethodHandler( entityContextImplType, propertyContextImplType ) );
    }
    catch (Exception e) {
View Full Code Here

  @SuppressWarnings("unchecked")
  private <P extends PropertyContext<?, ?>> P createPropertyMappingContext(Class<? extends EntityContext<?, ?>> entityContextImplType,
      Class<? extends P> propertyContextImplType) {

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setSuperclass( propertyContextImplType );
    proxyFactory.setFilter( new EntityOrPropertyMethodFilter() );

    try {
      return (P) proxyFactory.create(
          new Class<?>[] { ConfigurationContext.class },
          new Object[] { this },
          new EntityOrPropertyMethodHandler( entityContextImplType, propertyContextImplType ) );
    }
    catch (Exception e) {
View Full Code Here

   */
  @SuppressWarnings("unchecked")
  public <G extends GlobalContext<?, ?>> G createGlobalContext(Class<? extends G> globalContextImplType,
      final Class<? extends EntityContext<?, ?>> entityContextImplType, Class<? extends PropertyContext<?, ?>> propertyContextImplType) {

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setSuperclass( globalContextImplType );
    proxyFactory.setFilter( new EntityMethodFilter() );

    try {
      return (G) proxyFactory.create(
          new Class<?>[] { ConfigurationContext.class },
          new Object[] { this },
          new EntityOrPropertyMethodHandler( entityContextImplType, propertyContextImplType ) );
    }
    catch (Exception e) {
View Full Code Here

  @SuppressWarnings("unchecked")
  private <E extends EntityContext<?, ?>> E createEntityMappingContext(Class<? extends E> entityContextImplType,
      Class<? extends PropertyContext<?, ?>> propertyContextImplType) {

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setSuperclass( entityContextImplType );
    proxyFactory.setFilter( new EntityOrPropertyMethodFilter() );

    try {
      return (E) proxyFactory.create(
          new Class<?>[] { ConfigurationContext.class },
          new Object[] { this },
          new EntityOrPropertyMethodHandler( entityContextImplType, propertyContextImplType ) );
    }
    catch (Exception e) {
View Full Code Here

  @SuppressWarnings("unchecked")
  private <P extends PropertyContext<?, ?>> P createPropertyMappingContext(Class<? extends EntityContext<?, ?>> entityContextImplType,
      Class<? extends P> propertyContextImplType) {

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setSuperclass( propertyContextImplType );
    proxyFactory.setFilter( new EntityOrPropertyMethodFilter() );

    try {
      return (P) proxyFactory.create(
          new Class<?>[] { ConfigurationContext.class },
          new Object[] { this },
          new EntityOrPropertyMethodHandler( entityContextImplType, propertyContextImplType ) );
    }
    catch (Exception e) {
View Full Code Here

  }

  public static TaskInputOutputContext getTaskIOContext(
      final Broadcast<Configuration> conf,
      final Accumulator<Map<String, Long>> counters) {
    ProxyFactory factory = new ProxyFactory();
    Class<TaskInputOutputContext> superType = TaskInputOutputContext.class;
    Class[] types = new Class[0];
    Object[] args = new Object[0];
    final TaskAttemptID taskAttemptId = new TaskAttemptID();
    if (superType.isInterface()) {
      factory.setInterfaces(new Class[] { superType });
    } else {
      types = new Class[] { Configuration.class, TaskAttemptID.class, RecordWriter.class, OutputCommitter.class,
          StatusReporter.class };
      args = new Object[] { conf.value(), taskAttemptId, null, null, null };
      factory.setSuperclass(superType);
    }

    final Set<String> handledMethods = ImmutableSet.of("getConfiguration", "getCounter",
        "progress", "getTaskAttemptID");
    factory.setFilter(new MethodFilter() {
      @Override
      public boolean isHandled(Method m) {
        return handledMethods.contains(m.getName());
      }
    });
    MethodHandler handler = new MethodHandler() {
      @Override
      public Object invoke(Object arg0, Method m, Method arg2, Object[] args) throws Throwable {
        String name = m.getName();
        if ("getConfiguration".equals(name)) {
          return conf.value();
        } else if ("progress".equals(name)) {
          // no-op
          return null;
        } else if ("getTaskAttemptID".equals(name)) {
          return taskAttemptId;
        } else if ("getCounter".equals(name)){ // getCounter
          if (args.length == 1) {
            return getCounter(counters, args[0].getClass().getName(), ((Enum) args[0]).name());
          } else {
            return getCounter(counters, (String) args[0], (String) args[1]);
          }
        } else {
          throw new IllegalStateException("Unhandled method " + name);
        }
      }
    };

    try {
      Object newInstance = factory.create(types, args, handler);
      return (TaskInputOutputContext<?, ?, ?, ?>) newInstance;
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
View Full Code Here

TOP

Related Classes of javassist.util.proxy.ProxyFactory$CacheKey

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.