Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2006 Kresten Krab Thorup <krab@gnu.org> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 
 package org.jruby.javasupport.proxy;
 
 
 import org.jruby.Ruby;
 
 public class JavaProxyConstructor extends JavaProxyReflectionObject implements ParameterTypes {
 
     private final Constructor<?> proxyConstructor;
     private final Class<?>[] apparentParameterTypes;
 
     private final JavaProxyClass declaringProxyClass;
 
     JavaProxyConstructor(Ruby runtimeJavaProxyClass pClass,
             Constructor<?> constructor) {
         super(runtimeruntime.getJavaSupport().getJavaModule().getClass(
                 "JavaProxyConstructor"));
         this. = pClass;
         this. = constructor;
         Class<?>[] parameterTypes = constructor.getParameterTypes();
         int len = parameterTypes.length - 1;
         this. = new Class<?>[len];
         System.arraycopy(parameterTypes, 0, , 0, len);
     }
 
     public Class<?>[] getParameterTypes() {
         return ;
     }
 
     public Class<?>[] getExceptionTypes() {
         return .getExceptionTypes();
     }
     
     public boolean isVarArgs() {
         return .isVarArgs();
     }
 
     @JRubyMethod(name = "declaring_class")
     public JavaProxyClass getDeclaringClass() {
         return ;
     }
 
     public Object newInstance(Object[] argsJavaProxyInvocationHandler handler)
             throws IllegalArgumentExceptionInstantiationException,
         if (args.length != .) {
             throw new IllegalArgumentException("wrong number of parameters");
         }
 
         Object[] realArgs = new Object[args.length + 1];
         System.arraycopy(args, 0, realArgs, 0, args.length);
         realArgs[args.length] = handler;
 
        return .newInstance(realArgs);
    }
    public static RubyClass createJavaProxyConstructorClass(Ruby runtime,
            RubyModule javaProxyModule) {
        RubyClass result = javaProxyModule.defineClassUnder("JavaProxyConstructor",
                runtime.getObject(), .);
        JavaProxyReflectionObject.registerRubyMethods(runtimeresult);
        result.defineAnnotatedMethods(JavaProxyConstructor.class);
        return result;
    }
    public RubyFixnum arity() {
        return getRuntime().newFixnum(getParameterTypes().length);
    }
    
    public boolean equals(Object other) {
        return other instanceof JavaProxyConstructor &&
            this. == ((JavaProxyConstructor)other).;
    }
    
    public int hashCode() {
        return .hashCode();
    }
    protected String nameOnInspection() {
        return getDeclaringClass().nameOnInspection();
    }
    public IRubyObject inspect() {
        StringBuilder result = new StringBuilder();
        result.append(nameOnInspection());
        Class<?>[] parameterTypes = getParameterTypes();
        for (int i = 0; i < parameterTypes.lengthi++) {
            result.append(parameterTypes[i].getName());
            if (i < parameterTypes.length - 1) {
                result.append(',');
            }
        }
        result.append(")>");
        return getRuntime().newString(result.toString());
    }
    public RubyArray argument_types() {
        return buildRubyArray(getParameterTypes());
    }
    
    @JRubyMethod(rest = true)
    public RubyObject new_instance2(IRubyObject[] argsBlock unusedBlock) {
        Arity.checkArgumentCount(getRuntime(), args, 2, 2);
        final IRubyObject self = args[0];
        final Ruby runtime = self.getRuntime();
        RubyArray constructor_args = (RubyArrayargs[1];
        Class<?>[] parameterTypes = getParameterTypes();
        int count = (intconstructor_args.length().getLongValue();
        Object[] converted = new Object[count];
        
        for (int i = 0; i < counti++) {
            // TODO: call ruby method
            IRubyObject ith = constructor_args.aref(getRuntime().newFixnum(i));
            converted[i] = ith.toJava(parameterTypes[i]);
        }
        JavaProxyInvocationHandler handler = new JavaProxyInvocationHandler() {
            public IRubyObject getOrig() {
                return self;
            }
            public Object invoke(Object proxyJavaProxyMethod mObject[] nargsthrows Throwable {
                String name = m.getName();
                DynamicMethod method = self.getMetaClass().searchMethod(name);
                int v = method.getArity().getValue();
                IRubyObject[] newArgs = new IRubyObject[nargs.length];
                for (int i = nargs.length; --i >= 0; ) {
                    newArgs[i] = JavaUtil.convertJavaToUsableRubyObject(runtimenargs[i]);
                }
                
                if (v < 0 || v == (newArgs.length)) {
                    return method.call(runtime.getCurrentContext(), selfself.getMetaClass(), namenewArgs).toJava(m.getReturnType());
                } else if (m.hasSuperImplementation()) {
                    RubyClass superClass = self.getMetaClass().getSuperClass();
                    return RuntimeHelpers.invokeAs(runtime.getCurrentContext(), superClassselfnamenewArgs.).toJava(m.getReturnType());
                } else {
                    throw runtime.newArgumentError(newArgs.lengthv);
                }
            }
        };
        try {
            return JavaObject.wrap(getRuntime(), newInstance(convertedhandler));
        } catch (Exception e) {
            RaiseException ex = getRuntime().newArgumentError(
                    "Constructor invocation failed: " + e.getMessage());
            ex.initCause(e);
            throw ex;
        }
    }
    
    public JavaObject newInstance(final IRubyObject selfObject[] args) {
        final Ruby runtime = self.getRuntime();
        JavaProxyInvocationHandler handler = new JavaProxyInvocationHandler() {
            public IRubyObject getOrig() {
                return self;
            }
            public Object invoke(Object proxyJavaProxyMethod mObject[] nargsthrows Throwable {
                String name = m.getName();
                DynamicMethod method = self.getMetaClass().searchMethod(name);
                int v = method.getArity().getValue();
                IRubyObject[] newArgs = new IRubyObject[nargs.length];
                for (int i = nargs.length; --i >= 0; ) {
                    newArgs[i] = JavaUtil.convertJavaToUsableRubyObject(runtimenargs[i]);
                }
                IRubyObject result = null;
                if (v < 0 || v == (newArgs.length)) {
                    result = method.call(runtime.getCurrentContext(), selfself.getMetaClass(), namenewArgs);
                } else if (m.hasSuperImplementation()) {
                    RubyClass superClass = self.getMetaClass().getSuperClass();
                    result = RuntimeHelpers.invokeAs(runtime.getCurrentContext(), superClassselfnamenewArgs.);
                } else {
                    throw runtime.newArgumentError(newArgs.lengthv);
                }
                if (m.getReturnType() == void.class) {
                    return null;
                } else {
                    return result.toJava(m.getReturnType());
                }
            }
        };
        try {
            return JavaObject.wrap(getRuntime(), newInstance(argshandler));
        } catch (Throwable t) {
            while (t.getCause() != null) {
                t = t.getCause();
            }
            RaiseException ex = getRuntime().newArgumentError(
                    "Constructor invocation failed: " + t.getMessage());
            ex.initCause(t);
            throw ex;
        }
    }
    @JRubyMethod(required = 1, optional = 1)
    public RubyObject new_instance(IRubyObject[] argsBlock block) {
        int size = Arity.checkArgumentCount(getRuntime(), args, 1, 2) - 1;
        final RubyProc proc;
        // Is there a supplied proc argument or do we assume a block was
        // supplied
        if (args[sizeinstanceof RubyProc) {
            proc = (RubyProcargs[size];
        } else {
            proc = getRuntime().newProc(..,block);
            size++;
        }
        RubyArray constructor_args = (RubyArrayargs[0];
        Class<?>[] parameterTypes = getParameterTypes();
        int count = (intconstructor_args.length().getLongValue();
        Object[] converted = new Object[count];
        for (int i = 0; i < counti++) {
            // TODO: call ruby method
            IRubyObject ith = constructor_args.aref(getRuntime().newFixnum(i));
            converted[i] = ith.toJava(parameterTypes[i]);
        }
        final IRubyObject recv = this;
        JavaProxyInvocationHandler handler = new JavaProxyInvocationHandler() {
            public IRubyObject getOrig() {
                return null;
            }
            public Object invoke(Object proxyJavaProxyMethod method,
                    Object[] nargsthrows Throwable {
                int length = nargs == null ? 0 : nargs.length;
                IRubyObject[] rubyArgs = new IRubyObject[length + 2];
                rubyArgs[0] = JavaObject.wrap(recv.getRuntime(), proxy);
                rubyArgs[1] = method;
                for (int i = 0; i < lengthi++) {
                    rubyArgs[i + 2] = JavaUtil.convertJavaToRuby(getRuntime(),
                            nargs[i]);
                }
                IRubyObject call_result = proc.call(getRuntime().getCurrentContext(), rubyArgs);
                Object converted_result = call_result.toJava(method.getReturnType());
                return converted_result;
            }
        };
        Object result;
        try {
            result = newInstance(convertedhandler);
        } catch (Exception e) {
            RaiseException ex = getRuntime().newArgumentError(
                    "Constructor invocation failed: " + e.getMessage());
            ex.initCause(e);
            throw ex;
        }
        return JavaObject.wrap(getRuntime(), result);
    }
New to GrepCode? Check out our FAQ X