org.jruby.RubyClass.newClass()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(14.1k)|赞(0)|评价(0)|浏览(75)

本文整理了Java中org.jruby.RubyClass.newClass方法的一些代码示例,展示了RubyClass.newClass的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RubyClass.newClass方法的具体详情如下:
包路径:org.jruby.RubyClass
类名称:RubyClass
方法名:newClass

RubyClass.newClass介绍

[英]Construct a new class with the given name scoped under Object (global) and with Object as its immediate superclass. Corresponds to rb_class_new in MRI.
[中]构造一个新类,其给定名称的作用域位于Object(全局)下,对象作为其直接超类。对应于核磁共振成像中的rb_类_新。

代码示例

代码示例来源:origin: bazelbuild/bazel

private RubyModule buildClassFromDescriptor(ThreadContext context) {
  Ruby runtime = context.runtime;
  ObjectAllocator allocator = new ObjectAllocator() {
    @Override
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new RubyMessage(runtime, klazz, descriptor);
    }
  };
  // rb_define_class_id
  RubyClass klass = RubyClass.newClass(runtime, runtime.getObject());
  klass.setAllocator(allocator);
  klass.makeMetaClass(runtime.getObject().getMetaClass());
  klass.inherit(runtime.getObject());
  RubyModule messageExts = runtime.getClassFromPath("Google::Protobuf::MessageExts");
  klass.include(new IRubyObject[] {messageExts});
  klass.instance_variable_set(runtime.newString(Utils.DESCRIPTOR_INSTANCE_VAR), this);
  klass.defineAnnotatedMethods(RubyMessage.class);
  return klass;
}

代码示例来源:origin: org.jruby/jruby-complete

proxy = RubyClass.newClass(runtime, superClass);

代码示例来源:origin: com.ning.billing/killbill-osgi-bundles-jruby

@JRubyMethod(name = "new", rest = true)
  public static IRubyObject rbNew(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
    Ruby runtime = context.runtime;

    RubyClass implClass = (RubyClass)self.getInstanceVariables().getInstanceVariable("@__implementation");
    if (implClass == null) {
      implClass = RubyClass.newClass(runtime, (RubyClass)runtime.getClass("InterfaceJavaProxy"));
      implClass.include(new IRubyObject[] {self});
      Helpers.setInstanceVariable(implClass, self, "@__implementation");
    }

    return Helpers.invoke(context, implClass, "new", args, block);
  }
}

代码示例来源:origin: org.kill-bill.billing/killbill-osgi-bundles-jruby

@JRubyMethod(name = "new", rest = true)
  public static IRubyObject rbNew(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
    Ruby runtime = context.runtime;

    RubyClass implClass = (RubyClass)self.getInstanceVariables().getInstanceVariable("@__implementation");
    if (implClass == null) {
      implClass = RubyClass.newClass(runtime, (RubyClass)runtime.getClass("InterfaceJavaProxy"));
      implClass.include(new IRubyObject[] {self});
      Helpers.setInstanceVariable(implClass, self, "@__implementation");
    }

    return Helpers.invoke(context, implClass, "new", args, block);
  }
}

代码示例来源:origin: org.jruby/jruby-complete

/**
 * A variation on newClass that allows passing in an array of supplementary
 * call sites to improve dynamic invocation performance.
 */
public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name, ObjectAllocator allocator, RubyModule parent, boolean setParent, CallSite[] extraCallSites) {
  RubyClass clazz = newClass(runtime, superClass, extraCallSites);
  clazz.setBaseName(name);
  clazz.setAllocator(allocator);
  clazz.makeMetaClass(superClass.getMetaClass());
  if (setParent) clazz.setParent(parent);
  parent.setConstant(name, clazz);
  clazz.inherit(superClass);
  return clazz;
}

代码示例来源:origin: com.ning.billing/killbill-osgi-bundles-jruby

/** 
 * A variation on newClass that allows passing in an array of supplementary
 * call sites to improve dynamic invocation performance.
 */
public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name, ObjectAllocator allocator, RubyModule parent, boolean setParent, CallSite[] extraCallSites) {
  RubyClass clazz = newClass(runtime, superClass, extraCallSites);
  clazz.setBaseName(name);
  clazz.setAllocator(allocator);
  clazz.makeMetaClass(superClass.getMetaClass());
  if (setParent) clazz.setParent(parent);
  parent.setConstant(name, clazz);
  clazz.inherit(superClass);
  return clazz;
}

代码示例来源:origin: org.kill-bill.billing/killbill-osgi-bundles-jruby

/** 
 * A variation on newClass that allows passing in an array of supplementary
 * call sites to improve dynamic invocation performance.
 */
public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name, ObjectAllocator allocator, RubyModule parent, boolean setParent, CallSite[] extraCallSites) {
  RubyClass clazz = newClass(runtime, superClass, extraCallSites);
  clazz.setBaseName(name);
  clazz.setAllocator(allocator);
  clazz.makeMetaClass(superClass.getMetaClass());
  if (setParent) clazz.setParent(parent);
  parent.setConstant(name, clazz);
  clazz.inherit(superClass);
  return clazz;
}

代码示例来源:origin: org.jruby/jruby-core

/**
 * A variation on newClass that allows passing in an array of supplementary
 * call sites to improve dynamic invocation performance.
 */
public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name, ObjectAllocator allocator, RubyModule parent, boolean setParent, CallSite[] extraCallSites) {
  RubyClass clazz = newClass(runtime, superClass, extraCallSites);
  clazz.setBaseName(name);
  clazz.setAllocator(allocator);
  clazz.makeMetaClass(superClass.getMetaClass());
  if (setParent) clazz.setParent(parent);
  parent.setConstant(name, clazz);
  clazz.inherit(superClass);
  return clazz;
}

代码示例来源:origin: org.jruby/jruby-core

/**
 * Construct a new class with the given name, allocator, parent class,
 * and containing class. If setParent is true, the class's parent will be
 * explicitly set to the provided parent (rather than the new class just
 * being assigned to a constant in that parent).
 * Corresponds to rb_class_new/rb_define_class_id/rb_name_class/rb_set_class_path
 * in MRI.
 */
public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name, ObjectAllocator allocator, RubyModule parent, boolean setParent) {
  RubyClass clazz = newClass(runtime, superClass);
  clazz.setBaseName(name);
  clazz.setAllocator(allocator);
  clazz.makeMetaClass(superClass.getMetaClass());
  if (setParent) clazz.setParent(parent);
  parent.setConstant(name, clazz);
  clazz.inherit(superClass);
  return clazz;
}

代码示例来源:origin: com.ning.billing/killbill-osgi-bundles-jruby

/** 
 * Construct a new class with the given name, allocator, parent class,
 * and containing class. If setParent is true, the class's parent will be
 * explicitly set to the provided parent (rather than the new class just
 * being assigned to a constant in that parent).
 * Corresponds to rb_class_new/rb_define_class_id/rb_name_class/rb_set_class_path
 * in MRI.
 */
public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name, ObjectAllocator allocator, RubyModule parent, boolean setParent) {
  RubyClass clazz = newClass(runtime, superClass);
  clazz.setBaseName(name);
  clazz.setAllocator(allocator);
  clazz.makeMetaClass(superClass.getMetaClass());
  if (setParent) clazz.setParent(parent);
  parent.setConstant(name, clazz);
  clazz.inherit(superClass);
  return clazz;
}

代码示例来源:origin: org.jruby/jruby-complete

/**
 * Construct a new class with the given name, allocator, parent class,
 * and containing class. If setParent is true, the class's parent will be
 * explicitly set to the provided parent (rather than the new class just
 * being assigned to a constant in that parent).
 * Corresponds to rb_class_new/rb_define_class_id/rb_name_class/rb_set_class_path
 * in MRI.
 */
public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name, ObjectAllocator allocator, RubyModule parent, boolean setParent) {
  RubyClass clazz = newClass(runtime, superClass);
  clazz.setBaseName(name);
  clazz.setAllocator(allocator);
  clazz.makeMetaClass(superClass.getMetaClass());
  if (setParent) clazz.setParent(parent);
  parent.setConstant(name, clazz);
  clazz.inherit(superClass);
  return clazz;
}

代码示例来源:origin: org.kill-bill.billing/killbill-osgi-bundles-jruby

/** 
 * Construct a new class with the given name, allocator, parent class,
 * and containing class. If setParent is true, the class's parent will be
 * explicitly set to the provided parent (rather than the new class just
 * being assigned to a constant in that parent).
 * Corresponds to rb_class_new/rb_define_class_id/rb_name_class/rb_set_class_path
 * in MRI.
 */
public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name, ObjectAllocator allocator, RubyModule parent, boolean setParent) {
  RubyClass clazz = newClass(runtime, superClass);
  clazz.setBaseName(name);
  clazz.setAllocator(allocator);
  clazz.makeMetaClass(superClass.getMetaClass());
  if (setParent) clazz.setParent(parent);
  parent.setConstant(name, clazz);
  clazz.inherit(superClass);
  return clazz;
}

代码示例来源:origin: org.kill-bill.billing/killbill-osgi-bundles-jruby

@JRubyMethod(rest = true)
public static IRubyObject impl(ThreadContext context, IRubyObject self, IRubyObject[] args, final Block implBlock) {
  Ruby runtime = context.runtime;
  if (!implBlock.isGiven()) throw runtime.newArgumentError("block required to call #impl on a Java interface");
  final RubyArray methodNames = (args.length > 0) ? runtime.newArray(args) : null;
  RubyClass implClass = RubyClass.newClass(runtime, runtime.getObject());
  implClass.include(new IRubyObject[] {self});
  IRubyObject implObject = implClass.callMethod(context, "new");
  implClass.addMethod("method_missing",
      new org.jruby.internal.runtime.methods.JavaMethod(implClass, Visibility.PUBLIC) {
        @Override
        public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
          Arity.checkArgumentCount(context.runtime, name, args.length, 1, -1);
          if (methodNames == null || methodNames.include_p(context, args[0]).isTrue()) {
            return implBlock.call(context, args);
          } else {
            return clazz.getSuperClass().callMethod(context, "method_missing", args, block);
          }
        }
      });
  return implObject;
}

代码示例来源:origin: com.ning.billing/killbill-osgi-bundles-jruby

@JRubyMethod(rest = true)
public static IRubyObject impl(ThreadContext context, IRubyObject self, IRubyObject[] args, final Block implBlock) {
  Ruby runtime = context.runtime;
  if (!implBlock.isGiven()) throw runtime.newArgumentError("block required to call #impl on a Java interface");
  final RubyArray methodNames = (args.length > 0) ? runtime.newArray(args) : null;
  RubyClass implClass = RubyClass.newClass(runtime, runtime.getObject());
  implClass.include(new IRubyObject[] {self});
  IRubyObject implObject = implClass.callMethod(context, "new");
  implClass.addMethod("method_missing",
      new org.jruby.internal.runtime.methods.JavaMethod(implClass, Visibility.PUBLIC) {
        @Override
        public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
          Arity.checkArgumentCount(context.runtime, name, args.length, 1, -1);
          if (methodNames == null || methodNames.include_p(context, args[0]).isTrue()) {
            return implBlock.call(context, args);
          } else {
            return clazz.getSuperClass().callMethod(context, "method_missing", args, block);
          }
        }
      });
  return implObject;
}

代码示例来源:origin: org.jruby/jruby-complete

static RubyModule createJavaPackageClass(final Ruby runtime, final RubyModule Java) {
  RubyClass superClass = new BlankSlateWrapper(runtime, runtime.getModule(), runtime.getKernel());
  RubyClass JavaPackage = RubyClass.newClass(runtime, superClass);
  JavaPackage.setMetaClass(runtime.getModule());
  JavaPackage.setAllocator(ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
  ((MetaClass) JavaPackage.makeMetaClass(superClass)).setAttached(JavaPackage);
  JavaPackage.setBaseName("JavaPackage");
  JavaPackage.setParent(Java);
  Java.setConstant("JavaPackage", JavaPackage); // Java::JavaPackage
  // JavaPackage.setReifiedClass(JavaPackage.class);
  JavaPackage.defineAnnotatedMethods(JavaPackage.class);
  return JavaPackage;
}

代码示例来源:origin: org.jruby/jruby-core

static RubyModule createJavaPackageClass(final Ruby runtime, final RubyModule Java) {
  RubyClass superClass = new BlankSlateWrapper(runtime, runtime.getModule(), runtime.getKernel());
  RubyClass JavaPackage = RubyClass.newClass(runtime, superClass);
  JavaPackage.setMetaClass(runtime.getModule());
  JavaPackage.setAllocator(ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
  ((MetaClass) JavaPackage.makeMetaClass(superClass)).setAttached(JavaPackage);
  JavaPackage.setBaseName("JavaPackage");
  JavaPackage.setParent(Java);
  Java.setConstant("JavaPackage", JavaPackage); // Java::JavaPackage
  // JavaPackage.setReifiedClass(JavaPackage.class);
  JavaPackage.defineAnnotatedMethods(JavaPackage.class);
  return JavaPackage;
}

代码示例来源:origin: org.kill-bill.billing/killbill-osgi-bundles-jruby

private static RubyClass createProxyClass(Ruby runtime, RubyClass baseType,
    JavaClass javaClass, boolean invokeInherited) {
  // JRUBY-2938 the proxy class might already exist
  RubyClass proxyClass = javaClass.getProxyClass();
  if (proxyClass != null) return proxyClass;
  // this needs to be split, since conditional calling #inherited doesn't fit standard ruby semantics
  RubyClass.checkInheritable(baseType);
  RubyClass superClass = (RubyClass) baseType;
  proxyClass = RubyClass.newClass(runtime, superClass);
  proxyClass.makeMetaClass(superClass.getMetaClass());
  try {
    javaClass.javaClass().asSubclass(java.util.Map.class);
    proxyClass.setAllocator(runtime.getJavaSupport().getMapJavaProxyClass().getAllocator());
    proxyClass.defineAnnotatedMethods(MapJavaProxy.class);
    proxyClass.includeModule(runtime.getEnumerable());
  } catch (ClassCastException e) {
    proxyClass.setAllocator(superClass.getAllocator());
  }
  if (invokeInherited) {
    proxyClass.inherit(superClass);
  }
  proxyClass.callMethod(runtime.getCurrentContext(), "java_class=", javaClass);
  javaClass.setupProxy(proxyClass);
  // add java_method for unbound use
  proxyClass.defineAnnotatedMethods(JavaProxyClassMethods.class);
  return proxyClass;
}

代码示例来源:origin: com.ning.billing/killbill-osgi-bundles-jruby

private static RubyClass createProxyClass(Ruby runtime, RubyClass baseType,
    JavaClass javaClass, boolean invokeInherited) {
  // JRUBY-2938 the proxy class might already exist
  RubyClass proxyClass = javaClass.getProxyClass();
  if (proxyClass != null) return proxyClass;
  // this needs to be split, since conditional calling #inherited doesn't fit standard ruby semantics
  RubyClass.checkInheritable(baseType);
  RubyClass superClass = (RubyClass) baseType;
  proxyClass = RubyClass.newClass(runtime, superClass);
  proxyClass.makeMetaClass(superClass.getMetaClass());
  try {
    javaClass.javaClass().asSubclass(java.util.Map.class);
    proxyClass.setAllocator(runtime.getJavaSupport().getMapJavaProxyClass().getAllocator());
    proxyClass.defineAnnotatedMethods(MapJavaProxy.class);
    proxyClass.includeModule(runtime.getEnumerable());
  } catch (ClassCastException e) {
    proxyClass.setAllocator(superClass.getAllocator());
  }
  if (invokeInherited) {
    proxyClass.inherit(superClass);
  }
  proxyClass.callMethod(runtime.getCurrentContext(), "java_class=", javaClass);
  javaClass.setupProxy(proxyClass);
  // add java_method for unbound use
  proxyClass.defineAnnotatedMethods(JavaProxyClassMethods.class);
  return proxyClass;
}

代码示例来源:origin: org.jruby/jruby-complete

clazz = RubyClass.newClass(runtime, superClazz, name, allocator, this, true);

代码示例来源:origin: org.jruby/jruby-complete

return RubyClass.newClass(this, superClass, id, allocator, parent, !parentIsObject, callSites);

相关文章

微信公众号

最新文章

更多

RubyClass类方法