Package org.jruby

Examples of org.jruby.RubyModule


    }
   
    @Override
    public String definition(Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) {
        //RubyModule module = context.getRubyClass();
        RubyModule module = context.getCurrentScope().getStaticScope().getModule();
       
        if (module == null && self.getMetaClass().fastIsClassVarDefined(name)) {
            return "class variable";
        } else if (module.fastIsClassVarDefined(name)) {
            return "class variable";
        }

        IRubyObject attached = module.isSingleton() ? ((MetaClass)module).getAttached() : null;
       
        if (attached instanceof RubyModule && ((RubyModule) attached).fastIsClassVarDefined(name)) {
            return "class variable";
        }
View Full Code Here


        runtime.getErr().print("Warning: minijava is experimental and subject to change\n");
       
        runtime.getKernel().defineAnnotatedMethods(MiniJava.class);

        // load up object and add a few useful methods
        RubyModule javaObject = getMirrorForClass(runtime, Object.class);

        javaObject.addMethod("to_s", new JavaMethod.JavaMethodZero(javaObject, Visibility.PUBLIC) {
            @Override
            public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
                return context.getRuntime().newString(((JavaObjectWrapper) self).object.toString());
            }
        });

        javaObject.addMethod("hash", new JavaMethod.JavaMethodZero(javaObject, Visibility.PUBLIC) {
            @Override
            public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
                return self.getRuntime().newFixnum(((JavaObjectWrapper) self).object.hashCode());
            }
        });

        javaObject.addMethod("==", new JavaMethod.JavaMethodOne(javaObject, Visibility.PUBLIC) {
            @Override
            public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg) {
                if (arg instanceof JavaObjectWrapper) {
                    return context.getRuntime().newBoolean(((JavaObjectWrapper) self).object.equals(((JavaObjectWrapper) arg).object));
                } else {
                    return context.getRuntime().getFalse();
                }
            }
        });

        // open up the 'to_java' and 'as' coercion methods on Ruby Objects, via Kernel
        RubyModule rubyKernel = runtime.getKernel();
        rubyKernel.addModuleFunction("to_java", new JavaMethod.JavaMethodZeroOrOne(rubyKernel, Visibility.PUBLIC) {
            @Override
            public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
                return ((RubyObject) self).to_java();
            }
View Full Code Here

    public static IRubyObject rb_import(ThreadContext context, IRubyObject self, IRubyObject name) {
        String className = name.toString();
        try {
            Class cls = findClass(context.getRuntime().getJRubyClassLoader(), className);

            RubyModule namespace;
            if (self instanceof RubyModule) {
                namespace = (RubyModule) self;
            } else {
                namespace = self.getMetaClass().getRealClass();
            }

            namespace.defineConstant(cls.getSimpleName(), getMirrorForClass(context.getRuntime(), cls));

            return context.getRuntime().getNil();
        } catch (Exception e) {
            if (context.getRuntime().getDebug().isTrue()) {
                e.printStackTrace();
View Full Code Here

    public static IRubyObject rb_import(ThreadContext context, IRubyObject self, IRubyObject name, IRubyObject as) {
        String className = name.toString();
        try {
            Class cls = findClass(context.getRuntime().getJRubyClassLoader(), className);

            RubyModule namespace;
            if (self instanceof RubyModule) {
                namespace = (RubyModule) self;
            } else {
                namespace = self.getMetaClass().getRealClass();
            }

            namespace.defineConstant(as.toString(), getMirrorForClass(context.getRuntime(), cls));

            return context.getRuntime().getNil();
        } catch (Exception e) {
            if (context.getRuntime().getDebug().isTrue()) {
                e.printStackTrace();
View Full Code Here

    public static RubyModule getMirrorForClass(Ruby ruby, Class cls) {
        if (cls == null) {
            return ruby.getObject();
        }

        RubyModule rubyCls = classMap.get(cls);

        if (rubyCls == null) {
            rubyCls = createMirrorForClass(ruby, cls);

            classMap.put(cls, rubyCls);
View Full Code Here

    }

    protected static RubyModule createMirrorForClass(Ruby ruby, Class cls) {
        if (cls.isInterface()) {
            // interfaces are handled as modules
            RubyModule rubyMod = RubyModule.newModule(ruby);
            return rubyMod;
        } else {
            // construct the mirror class and parent classes
            RubyClass rubyCls = RubyClass.newClass(ruby, (RubyClass) getMirrorForClass(ruby, cls.getSuperclass()));
            return rubyCls;
View Full Code Here

        // skip population of declared elements
        if (cls.getEnclosingClass() != null && !Modifier.isPublic(cls.getModifiers())) {
            return;
        }

        RubyModule rubySing = rubyMod.getSingletonClass();

        // if it's an array, only add methods for aref, aset, and length
        if (cls.isArray()) {
            populateMirrorForArrayClass(rubyMod, cls);
        } else {
View Full Code Here

    private static void populateDeclaredMethods(RubyModule rubyMod, final Class cls, boolean includeStatic) throws SecurityException {
        // add all instance and static methods
        Method[] methods = cls.getDeclaredMethods();
        for (final Method method : methods) {
            String name = method.getName();
            RubyModule target;

            // only public methods
            if (!Modifier.isPublic(method.getModifiers())) {
                continue;
            }

            if (Modifier.isStatic(method.getModifiers())) {
                if (!includeStatic) continue; // only include static methods if specified
               
                target = rubyMod.getSingletonClass();
            } else {
                target = rubyMod;
            }

            JavaMethodFactory factory = getMethodFactory(method.getReturnType());
            DynamicMethod dynMethod = factory.createMethod(target, method);

            // if not overloaded, we add a method that guesses at which signature to use
            // TODO: just adding first one right now...add in signature-guessing logic
            if (target.getMethods().get(name) == null) {
                target.addMethod(name, dynMethod);
            }

            // add method with full signature, so it's guaranteed to be directly accessible
            // TODO: no need for this to be a full, formal JVM signature
            name = name + prettyParams(method.getParameterTypes());
            target.addMethod(name, dynMethod);
        }
    }
View Full Code Here

     */
    public static final String MODULE_NAME = "JRuby::FFI";
    public static final String CLASS_NAME = "Provider";
   
    public static RubyClass createProviderClass(Ruby runtime) {
        RubyModule module = FFIProvider.getModule(runtime);
        RubyClass result = module.defineClassUnder(CLASS_NAME,
                runtime.getObject(),
                ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
        result.defineAnnotatedMethods(FFIProvider.class);
        result.defineAnnotatedConstants(FFIProvider.class);

View Full Code Here

* @author <a href="mailto:ola.bini@gmail.com">Ola Bini</a>
*/
@JRubyClass(name="Net::BufferedIO")
public class NetProtocolBufferedIO {
    public static void create(Ruby runtime) {
        RubyModule mNet = runtime.getModule("Net");

        RubyClass cBufferedIO = (RubyClass)mNet.getConstant("BufferedIO");
        cBufferedIO.defineAnnotatedMethods(NetProtocolBufferedIO.class);

        RubyModule mNativeImpl = cBufferedIO.defineModuleUnder("NativeImplementation");

        mNativeImpl.defineAnnotatedMethods(NativeImpl.class);
    }   
View Full Code Here

TOP

Related Classes of org.jruby.RubyModule

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.