Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse 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/epl-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) 2001 Chad Fowler <chadfowler@chadfowler.com> Copyright (C) 2001 Alan Moore <alan_moore@gmx.net> Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de> Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr> Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se> Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de> Copyright (C) 2004 Thomas E Enebo <enebo@acm.org> Copyright (C) 2004 David Corbin <dcorbin@users.sourceforge.net> 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 EPL, 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 EPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.javasupport;
 
 
 import org.jruby.Ruby;
 
 @JRubyClass(name="Java::JavaConstructor")
 public class JavaConstructor extends JavaCallable {
     private final Constructor<?> constructor;
     private final JavaUtil.JavaConverter objectConverter;
 
     public Object getValue() {
         return ;
     }
 
     public static RubyClass createJavaConstructorClass(Ruby runtimeRubyModule javaModule) {
         // TODO: NOT_ALLOCATABLE_ALLOCATOR is probably ok here, since we don't intend for people to monkey with
         // this type and it can't be marshalled. Confirm. JRUBY-415
         RubyClass result =
                 javaModule.defineClassUnder("JavaConstructor"runtime.getObject(), .);
 
         JavaAccessibleObject.registerRubyMethods(runtimeresult);
         JavaCallable.registerRubyMethods(runtimeresult);
         
         result.defineAnnotatedMethods(JavaConstructor.class);
         
         return result;
     }
 
     public JavaConstructor(Ruby runtimeConstructor<?> constructor) {
         super(runtimeruntime.getJavaSupport().getJavaConstructorClass(), constructor.getParameterTypes());
         this. = constructor;
         
         this. = JavaUtil.getJavaConverter(constructor.getDeclaringClass());
     }
 
     public static JavaConstructor create(Ruby runtimeConstructor<?> constructor) {
         return new JavaConstructor(runtimeconstructor);
     }
     
     public static JavaConstructor getMatchingConstructor(Ruby runtimeClass<?> javaClassClass<?>[] argumentTypes) {
         try {
             return create(runtimejavaClass.getConstructor(argumentTypes));
         } catch (NoSuchMethodException e) {
             // Java reflection does not allow retrieving constructors like methods
             CtorSearch: for (Constructor<?> ctor : javaClass.getConstructors()) {
                 Class<?>[] targetTypes = ctor.getParameterTypes();
                 
                 // for zero args case we can stop searching
                 if (targetTypes.length != argumentTypes.length) {
                     continue CtorSearch;
                 } else if (targetTypes.length == 0 && argumentTypes.length == 0) {
                     return create(runtimector);
                 } else {
                     boolean found = true;
                    
                    TypeScan: for (int i = 0; i < argumentTypes.lengthi++) {
                        if (i >= targetTypes.lengthfound = false;
                        
                        if (targetTypes[i].isAssignableFrom(argumentTypes[i])) {
                            found = true;
                            continue TypeScan;
                        } else {
                            found = false;
                            continue CtorSearch;
                        }
                    }
                    // if we get here, we found a matching method, use it
                    // TODO: choose narrowest method by continuing to search
                    if (found) {
                        return create(runtimector);
                    }
                }
            }
        }
        // no matching ctor found
        return null;
    }
    public boolean equals(Object other) {
        return other instanceof JavaConstructor &&
            this. == ((JavaConstructor)other).;
    }
    
    public int hashCode() {
        return .hashCode();
    }
    public int getArity() {
        return .;
    }
    
    protected String nameOnInspection() {
        return getType().toString();
    }
    public Class<?>[] getParameterTypes() {
        return ;
    }
    public Class<?>[] getExceptionTypes() {
        return .getExceptionTypes();
    }
    public Type[] getGenericParameterTypes() {
        return .getGenericParameterTypes();
    }
    public Type[] getGenericExceptionTypes() {
        return .getGenericExceptionTypes();
    }
    public Annotation[][] getParameterAnnotations() {
        return .getParameterAnnotations();
    }
    
    public boolean isVarArgs() {
        return .isVarArgs();
    }
    public int getModifiers() {
        return .getModifiers();
    }
    
    public String toGenericString() {
        return .toGenericString();
    }
        return ;
    }
    
    public IRubyObject type_parameters() {
        return Java.getInstance(getRuntime(), .getTypeParameters());
    }
    public IRubyObject return_type() {
        return getRuntime().getNil();
    }
    @JRubyMethod(rest = true)
    public IRubyObject new_instance(IRubyObject[] args) {
        int length = args.length;
        Class<?>[] types = ;
        if (length != types.length) {
            throw getRuntime().newArgumentError(lengthtypes.length);
        }
        Object[] constructorArguments = new Object[length];
        for (int i = length; --i >= 0; ) {
            constructorArguments[i] = args[i].toJava(types[i]);
        }
        try {
            Object result = .newInstance(constructorArguments);
            return JavaObject.wrap(getRuntime(), result);
        } catch (IllegalArgumentException iae) {
            throw getRuntime().newTypeError("expected " + argument_types().inspect() +
                                              ", got [" + constructorArguments[0].getClass().getName() + ", ...]");
        } catch (IllegalAccessException iae) {
            throw getRuntime().newTypeError("illegal access");
        } catch (InvocationTargetException ite) {
            // not reached
            assert false;
            return null;
        } catch (InstantiationException ie) {
            throw getRuntime().newTypeError("can't make instance of " + .getDeclaringClass().getName());
        }
    }
    public IRubyObject new_instance(Object[] arguments) {
        checkArity(arguments.length);
        try {
            Object result = .newInstance(arguments);
            return JavaObject.wrap(getRuntime(), result);
        } catch (IllegalArgumentException iae) {
            throw getRuntime().newTypeError("expected " + argument_types().inspect() +
                                              ", got [" + arguments[0].getClass().getName() + ", ...]");
        } catch (IllegalAccessException iae) {
            throw getRuntime().newTypeError("illegal access");
        } catch (InvocationTargetException ite) {
            // not reached
            assert false;
            return null;
        } catch (InstantiationException ie) {
            throw getRuntime().newTypeError("can't make instance of " + .getDeclaringClass().getName());
        }
    }
    public Object newInstanceDirect(Object... arguments) {
        checkArity(arguments.length);
        try {
            return .newInstance(arguments);
        } catch (IllegalArgumentException iae) {
            return handlelIllegalArgumentEx(iaearguments);
        } catch (IllegalAccessException iae) {
            return handleIllegalAccessEx(iae);
        } catch (InvocationTargetException ite) {
            return handleInvocationTargetEx(ite);
        } catch (Throwable t) {
            return handleThrowable(t);
        }
    }
    public Object newInstanceDirect() {
        checkArity(0);
        try {
            return .newInstance();
        } catch (IllegalArgumentException iae) {
            return handlelIllegalArgumentEx(iae);
        } catch (IllegalAccessException iae) {
            return handleIllegalAccessEx(iae);
        } catch (InvocationTargetException ite) {
            return handleInvocationTargetEx(ite);
        } catch (Throwable t) {
            return handleThrowable(t);
        }
    }
    public Object newInstanceDirect(Object arg0) {
        checkArity(1);
        try {
            return .newInstance(arg0);
        } catch (IllegalArgumentException iae) {
            return handlelIllegalArgumentEx(iaearg0);
        } catch (IllegalAccessException iae) {
            return handleIllegalAccessEx(iae);
        } catch (InvocationTargetException ite) {
            return handleInvocationTargetEx(ite);
        } catch (Throwable t) {
            return handleThrowable(t);
        }
    }
    public Object newInstanceDirect(Object arg0Object arg1) {
        checkArity(2);
        try {
            return .newInstance(arg0arg1);
        } catch (IllegalArgumentException iae) {
            return handlelIllegalArgumentEx(iaearg0arg1);
        } catch (IllegalAccessException iae) {
            return handleIllegalAccessEx(iae);
        } catch (InvocationTargetException ite) {
            return handleInvocationTargetEx(ite);
        } catch (Throwable t) {
            return handleThrowable(t);
        }
    }
    public Object newInstanceDirect(Object arg0Object arg1Object arg2) {
        checkArity(3);
        try {
            return .newInstance(arg0arg1arg2);
        } catch (IllegalArgumentException iae) {
            return handlelIllegalArgumentEx(iaearg0arg1arg2);
        } catch (IllegalAccessException iae) {
            return handleIllegalAccessEx(iae);
        } catch (InvocationTargetException ite) {
            return handleInvocationTargetEx(ite);
        } catch (Throwable t) {
            return handleThrowable(t);
        }
    }
    public Object newInstanceDirect(Object arg0Object arg1Object arg2Object arg3) {
        checkArity(4);
        try {
            return .newInstance(arg0arg1arg2arg3);
        } catch (IllegalArgumentException iae) {
            return handlelIllegalArgumentEx(iaearg0arg1arg2arg3);
        } catch (IllegalAccessException iae) {
            return handleIllegalAccessEx(iae);
        } catch (InvocationTargetException ite) {
            return handleInvocationTargetEx(ite);
        } catch (Throwable t) {
            return handleThrowable(t);
        }
    }
        throw getRuntime().newTypeError("illegal access on constructor for type " + .getDeclaringClass().getSimpleName() + ": " + iae.getMessage());
    }
    private IRubyObject handlelIllegalArgumentEx(IllegalArgumentException iaeObject... arguments) {
        throw getRuntime().newTypeError(
                "for constructor of type " +
                .getDeclaringClass().getSimpleName() +
                " expected " +
                argument_types().inspect() +
                "; got: " +
                dumpArgTypes(arguments) +
                "; error: " +
                iae.getMessage());
    }
New to GrepCode? Check out our FAQ X