@SuppressWarnings({"rawtypes", "unchecked"})
    public void init() {
      List<LinkField> lfs = new ArrayList<LinkField>();
      lfs.addAll(ones.getAll());
      lfs.addAll(manys.getAll());
      lfs.addAll(manymanys.getAll());
      if (lfs.isEmpty()) return;
      if (log.isDebugEnabled()) log.debug("Found links , enable lazy!! -->" + type);

      Mirror<T> mirror = Mirror.me(type);
      List<InterceptorPair> interceptorPairs = new ArrayList<InterceptorPair>();
      // 准备拦截器
      for (LinkField lf : lfs) {
        String fieldName = lf.getName();
        try {
          Method setter = mirror.getSetter(mirror.getField(fieldName));
          LazyMethodInterceptor lmi = new LazyMethodInterceptor(setter, fieldName);
          interceptorPairs.add(
              new InterceptorPair(
                  lmi,
                  MethodMatcherFactory.matcher(
                      "^(get|set)" + Strings.upperFirst(fieldName) + "$")));
        } catch (Throwable e) {
          if (log.isWarnEnabled()) log.warn("Not setter found for LazyLoading ?!", e);
        }
      }
      // 生成Aop化的类
      ClassAgent agent = new AsmClassAgent();
      for (InterceptorPair interceptorPair : interceptorPairs)
        agent.addInterceptor(
            interceptorPair.getMethodMatcher(), interceptorPair.getMethodInterceptor());
      Class lazyClass = agent.define(DefaultClassDefiner.defaultOne(), type);

      // 检查对象的创建方法
      BornContext<T> bc = Borns.evalByArgTypes(type, ResultSet.class);
      if (null == bc) this.bornByDefault = Mirror.me(lazyClass).getBorningByArgTypes();
      else this.bornByRS = bc.getBorning();
    }
Esempio n. 2
0
  /**
   * 根据构造函数参数,创建一个对象。
   *
   * @param args 构造函数参数
   * @return 新对象
   */
  public T born(Object... args) {
    BornContext<T> bc = Borns.eval(klass, args);
    if (null == bc) throw new BorningException(klass, args);

    return bc.doBorn();
  }
Esempio n. 3
0
 /**
  * 获取对象构建器
  *
  * @param argTypes 构造函数参数类型数组
  * @return 当前对象构建方式
  * @throws BorningException 当没有发现合适的 Borning 时抛出
  */
 public Borning<T> getBorningByArgTypes(Class<?>... argTypes) throws BorningException {
   BornContext<T> bc = Borns.evalByArgTypes(klass, argTypes);
   if (null == bc) throw new BorningException(klass, argTypes);
   return bc.getBorning();
 }
Esempio n. 4
0
  /**
   * 获取对象构建器
   *
   * @param args 构造函数参数
   * @return 当前对象的构建方式。
   * @throws BorningException 当没有发现合适的 Borning 时抛出
   * @see org.nutz.lang.born.Borning
   */
  public Borning<T> getBorning(Object... args) throws BorningException {
    BornContext<T> bc = Borns.eval(klass, args);
    if (null == bc) throw new BorningException(klass, args);

    return bc.getBorning();
  }