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) 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 Thomas E Enebo <enebo@acm.org> Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de> Copyright (C) 2005 Charles O Nutter <headius@headius.com> Copyright (C) 2007 William N Dortch <bill.dortch@gmail.com> 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;
 
 import java.util.Map;
 import java.util.Set;
 
 import org.jruby.Ruby;
 
 public class JavaSupport {
     private static final Map<String,ClassPRIMITIVE_CLASSES = new HashMap<String,Class>();
     static {
         .put("boolean".);
         .put("byte".);
         .put("char".);
         .put("short".);
         .put("int".);
         .put("long".);
         .put("float".);
         .put("double".);
     }
     
     public static Class getPrimitiveClass(String primitiveType) {
         return .get(primitiveType);
     }
 
     private final Ruby runtime;
     
     private final ObjectProxyCache<IRubyObject,RubyClassobjectProxyCache = 
         // TODO: specifying soft refs, may want to compare memory consumption,
         // behavior with weak refs (specify WEAK in place of SOFT below)
 
         public IRubyObject allocateProxy(Object javaObjectRubyClass clazz) {
             return Java.allocateProxy(javaObjectclazz);
         }
     };
     
     private final ProxyCache proxyCache;
     private static final Constructor<? extends ProxyCachePROXY_CACHE_CONSTRUCTOR;
     
     static {
         Constructor<? extends ProxyCacheconstructor = null;
 
         if (..load()) {
             try {
                 // try to load the ClassValue class. If it succeeds, we can use our
                 // ClassValue-based cache.
                 Class.forName("java.lang.ClassValue");
                 constructor = (Constructor<ProxyCache>)Class.forName("org.jruby.java.proxies.ClassValueProxyCache").getConstructor(Ruby.class);
            }
            catch (Exception ex) {
                // fall through to Map version
            }
        }
        if (constructor == null) {
            try {
                constructor = MapBasedProxyCache.class.getConstructor(Ruby.class);
            } catch (Exception ex2) {
                throw new RuntimeException(ex2);
            }
        }
         = constructor;
    }
    private RubyModule javaModule;
    private RubyClass javaObjectClass;
    private JavaClass objectJavaClass;
    private RubyClass javaClassClass;
    private RubyClass javaArrayClass;
    private RubyClass javaProxyClass;
    private RubyClass javaFieldClass;
    private RubyClass javaMethodClass;
    private RubyClass arrayProxyClass;
    private RubyClass concreteProxyClass;
    private RubyClass mapJavaProxy;
    
    private final Map<StringJavaClassnameClassMap = new HashMap<StringJavaClass>();
    private final Map<ObjectObject[]> javaObjectVariables = new WeakIdentityHashMap();
    // A cache of all JavaProxyClass objects created for this runtime
    private Map<Set<?>, JavaProxyClassjavaProxyClassCache = Collections.synchronizedMap(new HashMap<Set<?>, JavaProxyClass>());
    
    public JavaSupport(Ruby ruby) {
        this. = ruby;
        
        try {
            this. = .newInstance(ruby);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public Class loadJavaClass(String classNamethrows ClassNotFoundException {
        Class primitiveClass;
        if ((primitiveClass = .get(className)) == null) {
            if (!Ruby.isSecurityRestricted()) {
                return Class.forName(classNametrue.getJRubyClassLoader());
            }
            return Class.forName(className);
        }
        return primitiveClass;
    }
    
    public Class loadJavaClassVerbose(String className) {
        try {
            return loadJavaClass(className);
        } catch (ClassNotFoundException cnfExcptn) {
            throw .newNameError("cannot load Java class " + classNameclassNamecnfExcptn);
        } catch (ExceptionInInitializerError eiie) {
            throw .newNameError("cannot initialize Java class " + classNameclassNameeiie);
        } catch (LinkageError le) {
            throw .newNameError("cannot link Java class " + className + ", probable missing dependency: " + le.getLocalizedMessage(), classNamele);
        } catch (SecurityException se) {
            if (.isVerbose()) se.printStackTrace(.getErrorStream());
            throw .newSecurityError(se.getLocalizedMessage());
        }
    }
    
    public Class loadJavaClassQuiet(String className) {
        try {
            return loadJavaClass(className);
        } catch (ClassNotFoundException cnfExcptn) {
            throw .newNameError("cannot load Java class " + classNameclassNamecnfExcptnfalse);
        } catch (ExceptionInInitializerError eiie) {
            throw .newNameError("cannot initialize Java class " + classNameclassNameeiiefalse);
        } catch (LinkageError le) {
            throw .newNameError("cannot link Java class " + classNameclassNamelefalse);
        } catch (SecurityException se) {
            throw .newSecurityError(se.getLocalizedMessage());
        }
    }
    public JavaClass getJavaClassFromCache(Class clazz) {
        return .get(clazz);
    }
    public void handleNativeException(Throwable exceptionMember target) {
        if (exception instanceof RaiseException) {
            // allow RaiseExceptions to propagate
            throw (RaiseExceptionexception;
        } else if (exception instanceof Unrescuable) {
            // allow "unrescuable" flow-control exceptions to propagate
            if (exception instanceof Error) {
                throw (Error)exception;
            } else if (exception instanceof RuntimeException) {
                throw (RuntimeException)exception;
            }
        }
        throw createRaiseException(exceptiontarget);
    }
    private RaiseException createRaiseException(Throwable exceptionMember target) {
        return RaiseException.createNativeRaiseException(exceptiontarget);
    }
        return ;
    }
    // not synchronizing these methods, no harm if these values get set more
    // than once.
    // (also note that there's no chance of getting a partially initialized
    // class/module, as happens-before is guaranteed by volatile write/read
    // of constants table.)
    
    public Map<StringJavaClassgetNameClassMap() {
        return ;
    }
    public void setJavaObjectVariable(Object oint iObject v) {
        synchronized () {
            Object[] vars = .get(o);
            if (vars == null) {
                vars = new Object[i + 1];
                .put(ovars);
            } else if (vars.length <= i) {
                Object[] newVars = new Object[i + 1];
                System.arraycopy(vars, 0, newVars, 0, vars.length);
                .put(onewVars);
                vars = newVars;
            }
            vars[i] = v;
        }
    }
    
    public Object getJavaObjectVariable(Object oint i) {
        if (i == -1) return null;
        
        synchronized () {
            Object[] vars = .get(o);
            if (vars == null || vars.length <= ireturn null;
            return vars[i];
        }
    }
    
    public RubyModule getJavaModule() {
        RubyModule module;
        if ((module = ) != nullreturn module;
        return  = .getModule("Java");
    }
    
        RubyModule module;
        if ((module = ) != nullreturn module;
        return  = .getModule("JavaUtilities");
    }
    
        RubyModule module;
        if ((module = ) != nullreturn module;
        return  = .getModule("JavaArrayUtilities");
    }
    
    public RubyClass getJavaObjectClass() {
        RubyClass clazz;
        if ((clazz = ) != nullreturn clazz;
        return  = getJavaModule().getClass("JavaObject");
    }
    
    public JavaClass getObjectJavaClass() {
        return ;
    }
    
    public void setObjectJavaClass(JavaClass objectJavaClass) {
        this. = objectJavaClass;
    }
    public RubyClass getJavaArrayClass() {
        RubyClass clazz;
        if ((clazz = ) != nullreturn clazz;
        return  = getJavaModule().getClass("JavaArray");
    }
    
    public RubyClass getJavaClassClass() {
        RubyClass clazz;
        if ((clazz = ) != nullreturn clazz;
        return  = getJavaModule().getClass("JavaClass");
    }
        RubyModule module;
        if ((module = ) != nullreturn module;
        return  = .getModule("JavaInterfaceTemplate");
    }
        RubyModule module;
        if ((module = ) != nullreturn module;
        return  = .getModule("JavaPackageModuleTemplate");
    }
    
    public RubyClass getJavaProxyClass() {
        RubyClass clazz;
        if ((clazz = ) != nullreturn clazz;
        return  = .getClass("JavaProxy");
    }
        RubyClass clazz;
        if ((clazz = ) != nullreturn clazz;
        return  = .getClass("ArrayJavaProxyCreator");
    }
    
    public RubyClass getConcreteProxyClass() {
        RubyClass clazz;
        if ((clazz = ) != nullreturn clazz;
        return  = .getClass("ConcreteJavaProxy");
    }
    public RubyClass getMapJavaProxyClass() {
        RubyClass clazz;
        if ((clazz = ) != nullreturn clazz;
        return  = .getClass("MapJavaProxy");
    }
    
    public RubyClass getArrayProxyClass() {
        RubyClass clazz;
        if ((clazz = ) != nullreturn clazz;
        return  = .getClass("ArrayJavaProxy");
    }
    
    public RubyClass getJavaFieldClass() {
        RubyClass clazz;
        if ((clazz = ) != nullreturn clazz;
        return  = getJavaModule().getClass("JavaField");
    }
    public RubyClass getJavaMethodClass() {
        RubyClass clazz;
        if ((clazz = ) != nullreturn clazz;
        return  = getJavaModule().getClass("JavaMethod");
    }
        RubyClass clazz;
        if ((clazz = ) != nullreturn clazz;
        return  = getJavaModule().getClass("JavaConstructor");
    }
    public Map<Set<?>, JavaProxyClassgetJavaProxyClassCache() {
        return this.;
    }
New to GrepCode? Check out our FAQ X