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

br.com.objectos.way.io.TableProxy Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2013 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.way.io;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * @author [email protected] (Marcio Endo)
 */
class TableProxy {

  private final T proxy;

  private final Interceptor interceptor;

  private TableProxy(T proxy, Interceptor interceptor) {
    this.proxy = proxy;
    this.interceptor = interceptor;
  }

  static  TableProxy proxyOf(Class type) {
    Interceptor interceptor = new Interceptor();

    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(type);
    enhancer.setCallback(interceptor);

    @SuppressWarnings("unchecked")
    T proxy = (T) enhancer.create();

    return new TableProxy(proxy, interceptor);
  }

  T getProxy() {
    return proxy;
  }

  T registerCall(TableWriterBuilderPojo.ColBody body) {
    interceptor.registerCall(body);
    return proxy;
  }

  private static class Interceptor implements MethodInterceptor {

    private TableWriterBuilderPojo.ColBody body;

    @Override
    public Object intercept(
        Object obj, Method method, Object[] args, MethodProxy proxy)
        throws Throwable {

      if (body != null) {
        TableMethodWrapper wrapper = TableMethodWrapper.wrapperOf(method, args);
        body.setMethod(wrapper);
      }

      return null;

    }

    void registerCall(TableWriterBuilderPojo.ColBody body) {
      this.body = body;
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy