Ejemplo n.º 1
0
  @SuppressWarnings({"unchecked"})
  @Override
  public XValue run(Shell shell, List<XValue> args) throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();

    requires(args.size() >= 2, "object( name parent-class [name type ...]");
    String name = args.remove(0).toString();
    XValue xpclass = args.remove(0);
    Class<?> pclass = DataMappingModule.resolveClass(null, xpclass, classLoader);

    DynamicType.Builder<?> b =
        new ByteBuddy().subclass((Class<?>) (pclass != null ? pclass : Object.class)).name(name);

    while (!args.isEmpty()) {
      XValue xv = args.remove(0);
      String f = xv.toString();
      Class<?> cls = String.class;
      if (!args.isEmpty()) cls = DataMappingModule.resolveClass(null, args.remove(0), classLoader);

      b = b.defineField(f, cls, Visibility.PUBLIC);
    }

    Object bean =
        b.make()
            .load(classLoader, ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .newInstance();

    return XValue.newXValue((Object) bean);
  }