Java Code Examples for org.jruby.RubyClass#defineAnnotatedMethods()

The following examples show how to use org.jruby.RubyClass#defineAnnotatedMethods() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RubySchema.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * This method registers the class with the given runtime.
 *
 * @param runtime an instance of the Ruby runtime
 * @return        a RubyClass object with metadata about the registered class
 */
public static RubyClass define(Ruby runtime) {
    RubyClass result = runtime.defineClass("Schema",runtime.getObject(), ALLOCATOR);

    result.kindOf = new RubyModule.KindOf() {
        public boolean isKindOf(IRubyObject obj, RubyModule type) {
            return obj instanceof RubySchema;
        }
    };

    result.includeModule(runtime.getEnumerable());

    result.defineAnnotatedMethods(RubySchema.class);

    return result;
}
 
Example 2
Source File: RubyDataBag.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * This method registers the class with the given runtime. It is not necessary to do this here,
 * but it is simpler to associate the methods necessary to register the class with the class
 * itself, so on the Library side it is possible to just specify "RubyDataBag.define(runtime)".
 *
 * @param runtime an instance of the Ruby runtime
 * @return        a RubyClass object with metadata about the registered class
 */
public static RubyClass define(Ruby runtime) {
    // This generates the class object associated with DataBag, and registers it with the
    // runtime. The RubyClass object has all the metadata associated with a Class itself.
    RubyClass result = runtime.defineClass("DataBag", runtime.getObject(), ALLOCATOR);

    // This registers a method which can be used to know whether a module is an
    // instance of the class.
    result.kindOf = new RubyModule.KindOf() {
        public boolean isKindOf(IRubyObject obj, RubyModule type) {
            return obj instanceof RubyDataBag;
        }
    };

    // This includes the Enumerable module that we specified.
    result.includeModule(runtime.getEnumerable());

    // This method actually reads the annotations we placed and registers
    // all of the methods.
    result.defineAnnotatedMethods(RubyDataBag.class);

    // This returns the RubyClass object with all the new metadata.
    return result;
}
 
Example 3
Source File: RubyOutputStreamWrapper.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
public static RubyClass getOrCreateOutputStreamWrapperClass(final Ruby rubyRuntime) {
  RubyModule asciidoctorModule = rubyRuntime.getModule("AsciidoctorJ");
  RubyClass outputStreamWrapperClass = asciidoctorModule.getClass(RUBY_CLASS_NAME);
  if (outputStreamWrapperClass != null) {
    return outputStreamWrapperClass;
  }

  final RubyClass rubyClass = asciidoctorModule.defineClassUnder(RUBY_CLASS_NAME, rubyRuntime.getObject(), new ObjectAllocator() {
    @Override
    public IRubyObject allocate(final Ruby runtime, final RubyClass klazz) {
      return new RubyOutputStreamWrapper(runtime, klazz);
    }
  });

  rubyClass.defineAnnotatedMethods(RubyOutputStreamWrapper.class);

  return rubyClass;
}
 
Example 4
Source File: JavaDissectorLibrary.java    From logstash-filter-dissect with Apache License 2.0 5 votes vote down vote up
@Override
public final void load(final Ruby runtime, final boolean wrap) {
    final RubyModule module = runtime.defineModule("LogStash");

    final RubyClass clazz = runtime.defineClassUnder("Dissector", runtime.getObject(), JavaDissectorLibrary.RubyDissect::new, module);
    clazz.defineAnnotatedMethods(JavaDissectorLibrary.RubyDissect.class);

    final RubyClass runtimeError = runtime.getRuntimeError();
    module.defineClassUnder("FieldFormatError", runtimeError, runtimeError.getAllocator());
    module.defineClassUnder("ConvertDatatypeFormatError", runtimeError, runtimeError.getAllocator());
}
 
Example 5
Source File: RubyDataByteArray.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the DataByteArray class with the Ruby runtime.
 *
 * @param runtime an instance of the Ruby runtime
 * @return a RubyClass object with metadata about the registered class
 */
public static RubyClass define(Ruby runtime) {
    RubyClass result = runtime.defineClass("DataByteArray", runtime.getObject(), ALLOCATOR);

    result.kindOf = new RubyModule.KindOf() {
        public boolean isKindOf(IRubyObject obj, RubyModule type) {
            return obj instanceof RubyDataByteArray;
        }
    };

    result.defineAnnotatedMethods(RubyDataByteArray.class);

    return result;
}
 
Example 6
Source File: ProcessorProxyUtil.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
/**
 * Defines the annotated methods of the given class and all super classes as
 * {@link org.jruby.RubyClass#defineAnnotatedMethods(Class)} does not handle inherited methods.
 * @param rubyClass
 * @param proxyClass
 */
public static void defineAnnotatedMethods(RubyClass rubyClass, Class<?> proxyClass) {
    Class<?> currentClass = proxyClass;
    while (currentClass != RubyObject.class) {
        rubyClass.defineAnnotatedMethods(currentClass);
        currentClass = currentClass.getSuperclass();
    }
}
 
Example 7
Source File: TreeprocessorProxy.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public static RubyClass register(final JRubyAsciidoctor asciidoctor, final Class<? extends Treeprocessor> treeProcessor) {
    RubyClass rubyClass = ProcessorProxyUtil.defineProcessorClass(asciidoctor.getRubyRuntime(), "Treeprocessor", new JRubyAsciidoctorObjectAllocator(asciidoctor) {
        @Override
        public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
            return new TreeprocessorProxy(asciidoctor, klazz, treeProcessor);
        }
    });

    applyAnnotations(treeProcessor, rubyClass);

    rubyClass.defineAnnotatedMethods(TreeprocessorProxy.class);
    return rubyClass;
}
 
Example 8
Source File: TreeprocessorProxy.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public static RubyClass register(final JRubyAsciidoctor asciidoctor, final Treeprocessor treeProcessor) {
    RubyClass rubyClass = ProcessorProxyUtil.defineProcessorClass(asciidoctor.getRubyRuntime(), "Treeprocessor", new JRubyAsciidoctorObjectAllocator(asciidoctor) {
        @Override
        public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
            return new TreeprocessorProxy(this.asciidoctor, klazz, treeProcessor);
        }
    });

    applyAnnotations(treeProcessor.getClass(), rubyClass);

    rubyClass.defineAnnotatedMethods(TreeprocessorProxy.class);
    return rubyClass;
}
 
Example 9
Source File: ExtensionGroupImpl.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
static RubyClass createExtensionGroupClass(final Ruby rubyRuntime) {
  final RubyClass extensionGroupClass = rubyRuntime.getModule("AsciidoctorModule")
      .defineClassUnder("ExtensionGroupImpl", rubyRuntime.getObject(), new ObjectAllocator() {
        @Override
        public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
          return new ExtensionGroupRegistrationCallback(runtime, klazz);
        }
      });
  extensionGroupClass.defineAnnotatedMethods(ExtensionGroupRegistrationCallback.class);
  return extensionGroupClass;
}
 
Example 10
Source File: JRubyRackInput.java    From rack-servlet with Apache License 2.0 5 votes vote down vote up
private static RubyClass getRackInputClass(Ruby runtime) {
  RubyModule module = runtime.getOrCreateModule("RackServlet");
  RubyClass klass = module.getClass("RackInput");
  if (klass == null) {
    klass = module.defineClassUnder("RackInput", runtime.getObject(), ALLOCATOR);
    klass.defineAnnotatedMethods(JRubyRackInput.class);
  }
  return klass;
}