Package org.jruby.javasupport

Examples of org.jruby.javasupport.JavaClass$InstanceFieldGetterInstaller


    @JRubyMethod(meta = true)
    public static RubyObject get(IRubyObject recv, IRubyObject obj) {
        if (!(obj instanceof JavaClass)) {
            throw recv.getRuntime().newTypeError(obj, recv.getRuntime().getJavaSupport().getJavaClassClass());
        }
        JavaClass type = (JavaClass)obj;
       
        try {
            return getProxyClass(recv.getRuntime(), (Class) type.getValue(), new Class[0]);
        } catch (Error e) {
            RaiseException ex = recv.getRuntime().newArgumentError("unable to create proxy class for " + type.getValue());
            ex.initCause(e);
            throw ex;
        } catch (InvocationTargetException e) {
            RaiseException ex = recv.getRuntime().newArgumentError("unable to create proxy class for " + type.getValue());
            ex.initCause(e);
            throw ex;
        }
    }
View Full Code Here


        // from any other ancestor classes). Methods defined in mixins will
        // be considered intentionally overridden, except those from Kernel,
        // Java, and JavaProxyMethods, as well as Enumerable.
        // TODO: may want to exclude other common mixins?

        JavaClass javaClass = null;
        Set<String> names = new HashSet<String>(); // need names ordered for key generation later
        List<Class<?>> interfaceList = new ArrayList<Class<?>>();

        List<IRubyObject> ancestors = clazz.getAncestorList();
        boolean skipRemainingClasses = false;
        for (IRubyObject ancestorObject: ancestors) {
            RubyModule ancestor = (RubyModule) ancestorObject;
            if (ancestor instanceof RubyClass) {
                if (skipRemainingClasses) continue;
                // we only collect methods and interfaces for
                // user-defined proxy classes.
                if (!ancestor.getInstanceVariables().fastHasInstanceVariable("@java_proxy_class")) {
                    skipRemainingClasses = true;
                    continue;
                }

                // get JavaClass if this is the new proxy class; verify it
                // matches if this is a superclass proxy.
                IRubyObject var = ancestor.getInstanceVariables().fastGetInstanceVariable("@java_class");
                if (var == null) {
                    throw runtime.newTypeError(
                            "no java_class defined for proxy (or ancestor): " + ancestor);
                } else if (!(var instanceof JavaClass)) {
                    throw runtime.newTypeError(
                            "invalid java_class defined for proxy (or ancestor): " +
                            ancestor + ": " + var);
                }
                if (javaClass == null) {
                    javaClass = (JavaClass)var;
                } else if (javaClass != var) {
                    throw runtime.newTypeError(
                            "java_class defined for " + clazz + " (" + javaClass +
                            ") does not match java_class for ancestor " + ancestor +
                            " (" + var + ")");
                }
                // get any included interfaces
                var = ancestor.getInstanceVariables().fastGetInstanceVariable("@java_interfaces");
                if (var != null && !(var instanceof RubyNil)) {
                    if (!(var instanceof RubyArray)) {
                        throw runtime.newTypeError(
                                "invalid java_interfaces defined for proxy (or ancestor): " +
                                ancestor + ": " + var);
                    }
                    RubyArray ifcArray = (RubyArray)var;
                    int size = ifcArray.size();
                    for (int i = size; --i >= 0; ) {
                        IRubyObject ifc = ifcArray.eltInternal(i);
                        if (!(ifc instanceof JavaClass)) {
                            throw runtime.newTypeError(
                                "invalid java interface defined for proxy (or ancestor): " +
                                ancestor + ": " + ifc);
                        }
                        Class interfaceClass = ((JavaClass)ifc).javaClass();
                        if (!interfaceClass.isInterface()) {
                            throw runtime.newTypeError(
                                    "invalid java interface defined for proxy (or ancestor): " +
                                    ancestor + ": " + ifc + " (not an interface)");
                        }
                        if (!interfaceList.contains(interfaceClass)) {
                            interfaceList.add(interfaceClass);
                        }
                    }
                }
                // set this class's method names in var @__java_ovrd_methods if this
                // is the new class; otherwise, get method names from there if this is
                // a proxy superclass.
               
                // FIXME: shouldn't need @__java_ovrd_methods, just query locally defined methods.
               
                var = ancestor.getInstanceVariables().fastGetInstanceVariable("@__java_ovrd_methods");
                if (var == null) {
                    // lock in the overridden methods for the new class, and any as-yet
                    // uninstantiated ancestor class.
                    Map<String, DynamicMethod> methods;
                    RubyArray methodNames;
                    synchronized(methods = ancestor.getMethods()) {
                        methodNames = RubyArray.newArrayLight(runtime,methods.size());
                        for (String methodName: methods.keySet()) {
                            if (!EXCLUDE_METHODS.contains(methodName)) {
                                names.add(methodName);
                                methodNames.append(runtime.newString(methodName));
                            }
                        }
                    }
                    ancestor.fastSetInstanceVariable("@__java_ovrd_methods",methodNames);
                } else {
                    if (!(var instanceof RubyArray)) {
                        throw runtime.newTypeError(
                                "invalid @__java_ovrd_methods defined for proxy: " +
                                ancestor + ": " + var);
                    }
                    RubyArray methodNames = (RubyArray)var;
                    int size = methodNames.size();
                    for (int i = size; --i >= 0; ) {
                        IRubyObject methodName = methodNames.eltInternal(i);
                        if (!(methodName instanceof RubyString)) {
                            throw runtime.newTypeError(
                                    "invalid method name defined for proxy (or ancestor): " +
                                    ancestor + ": " + methodName);
                        }
                        names.add(methodName.asJavaString());
                    }
                }
            } else if (!EXCLUDE_MODULES.contains(ancestor.getName())) {
                Map<String, DynamicMethod> methods;
                synchronized(methods = ancestor.getMethods()) {
                    for (String methodName: methods.keySet()) {
                        if (!EXCLUDE_METHODS.contains(methodName)) {
                            names.add(methodName);
                        }
                    }
                }
            }
        }

        if (javaClass == null) {
            throw runtime.newArgumentError("unable to create proxy class: no java_class defined for " + clazz);
        }
       
        int interfaceCount = interfaceList.size();
        Class<?>[] interfaces = new Class<?>[interfaceCount];
        for (int i = interfaceCount; --i >= 0; ) {
            interfaces[i] = interfaceList.get(i);
        }
      
        try {
            return getProxyClass(recv.getRuntime(), javaClass.javaClass(), interfaces, names);
        } catch (Error e) {
            RaiseException ex = recv.getRuntime().newArgumentError("unable to create proxy class for " + javaClass.getValue() + " : " + e.getMessage());
            //e.printStackTrace();
            ex.initCause(e);
            throw ex;
        } catch (InvocationTargetException e) {
            RaiseException ex = recv.getRuntime().newArgumentError("unable to create proxy class for " + javaClass.getValue() + " : " + e.getMessage());
            //e.printStackTrace();
            ex.initCause(e);
            throw ex;
        }
    }
View Full Code Here

            return isExceptionHandled(((RaiseException)currentException).getException(), exceptions, runtime, context, self);
        } else {
            for (int i = 0; i < exceptions.length; i++) {
                if (exceptions[i] instanceof RubyClass) {
                    RubyClass rubyClass = (RubyClass)exceptions[i];
                    JavaClass javaClass = (JavaClass)rubyClass.fastGetInstanceVariable("@java_class");
                    if (javaClass != null) {
                        Class cls = javaClass.javaClass();
                        if (cls.isInstance(currentException)) {
                            return runtime.getTrue();
                        }
                    }
                }
View Full Code Here

            public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
                assert self instanceof RubyClass : "InterfaceJavaProxy.new defined on non-class ";
                RubyClass rubyClass = (RubyClass)self;

                IRubyObject proxy = rubyClass.allocate();
                JavaClass javaClass = (JavaClass)RuntimeHelpers.invoke(context, proxy.getMetaClass(), "java_class");
                IRubyObject proxyInstance = Java.new_proxy_instance2(
                        self,
                        self,
                        RubyArray.newArray(context.getRuntime(), javaClass),
                        block);
View Full Code Here

        return getJavaArray().at(index);
    }
   
    @JRubyMethod(name = "+", backtrace = true)
    public IRubyObject op_plus(ThreadContext context, IRubyObject other) {
        JavaClass arrayClass = JavaClass.get(context.getRuntime(), getJavaArray().getComponentType());
        if (other instanceof ArrayJavaProxy) {
            JavaArray otherArray = ((ArrayJavaProxy)other).getJavaArray();
           
            if (getJavaArray().getComponentType().isAssignableFrom(otherArray.getComponentType())) {
                return arrayClass.concatArrays(context, getJavaArray(), otherArray);
            }
        }
        return arrayClass.concatArrays(context, getJavaArray(), other);
    }
View Full Code Here

        }
    }
   
    public IRubyObject getRange(ThreadContext context, IRubyObject arg0) {
        int length = (int)getJavaArray().length().getLongValue();
        JavaClass arrayClass = JavaClass.get(context.getRuntime(), getJavaArray().getComponentType());
       
        if (arg0 instanceof RubyRange) {
            RubyRange range = (RubyRange)arg0;
            if (range.first() instanceof RubyFixnum && range.last() instanceof RubyFixnum) {
                int first = (int)((RubyFixnum)range.first()).getLongValue();
                int last = (int)((RubyFixnum)range.last()).getLongValue();
               
                first = first >= 0 ? first : length + first;
                last = last >= 0 ? last : length + last;
                int newLength = last - first;
                if (range.exclude_end_p().isFalse()) newLength += 1;
               
                if (newLength <= 0) {
                    return arrayClass.emptyJavaArray(context);
                }
       
                return arrayClass.javaArraySubarray(context, getJavaArray(), first, newLength);
            } else {
                throw context.getRuntime().newTypeError("only Fixnum ranges supported");
            }
        } else {
            throw context.getRuntime().newTypeError(arg0, context.getRuntime().getRange());
View Full Code Here

            throw context.getRuntime().newTypeError(arg0, context.getRuntime().getRange());
        }
    }
   
    public IRubyObject getRange(ThreadContext context, IRubyObject firstObj, IRubyObject lengthObj) {
        JavaClass arrayClass = JavaClass.get(context.getRuntime(), getJavaArray().getComponentType());
       
        if (firstObj instanceof RubyFixnum && lengthObj instanceof RubyFixnum) {
            int first = (int)((RubyFixnum)firstObj).getLongValue();
            int length = (int)((RubyFixnum)lengthObj).getLongValue();

            if (length > getJavaArray().length().getLongValue()) {
                throw context.getRuntime().newIndexError("length specifed is longer than array");
            }

            first = first >= 0 ? first : (int)getJavaArray().length().getLongValue() + first;

            if (length <= 0) {
                return arrayClass.emptyJavaArray(context);
            }

            return arrayClass.javaArraySubarray(context, getJavaArray(), first, length);
        } else {
            throw context.getRuntime().newTypeError("only Fixnum ranges supported");
        }
    }
View Full Code Here

            return to_java(context, fromArray);
        }
       
        Ruby runtime = context.getRuntime();
       
        JavaClass targetType = getTargetType(context, runtime, type);
       
        return targetType.javaArrayFromRubyArray(context, fromArray);
    }
View Full Code Here

       
        return targetType.javaArrayFromRubyArray(context, fromArray);
    }
   
    private static JavaClass getTargetType(ThreadContext context, Ruby runtime, IRubyObject type) {
        JavaClass targetType;

        if (type instanceof RubyString || type instanceof RubySymbol) {
            targetType = runtime.getJavaSupport().getNameClassMap().get(type.asJavaString());
            if (targetType == null) targetType = JavaClass.forNameVerbose(runtime, type.asJavaString());
        } else if (type instanceof RubyModule && type.respondsTo("java_class")) {
View Full Code Here

            // let Ruby exceptions decide if they handle it
            return isExceptionHandled(JavaUtil.convertJavaToUsableRubyObject(context.getRuntime(), throwable), catchable, context).isTrue();
        }
        if (catchable instanceof RubyClass && catchable.getInstanceVariables().hasInstanceVariable("@java_class")) {
            RubyClass rubyClass = (RubyClass)catchable;
            JavaClass javaClass = (JavaClass)rubyClass.fastGetInstanceVariable("@java_class");
            if (javaClass != null) {
                Class cls = javaClass.javaClass();
                if (cls.isInstance(throwable)) {
                    return true;
                }
            }
        }
View Full Code Here

TOP

Related Classes of org.jruby.javasupport.JavaClass$InstanceFieldGetterInstaller

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.