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) 2006 Ola Bini <ola@ologix.com> Copyright (c) 2007 Peter Brant <peter.brant@gmail.com> Copyright (C) 2008 The JRuby Community <www.headius.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 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.internal.runtime.methods;
 
 
 import java.util.List;
This MethodFactory uses reflection to provide method handles. Reflection is typically slower than code-generated handles, but it does provide a simple mechanism for binding in environments where code-generation isn't supported.

See also:
org.jruby.internal.runtime.methods.MethodFactory
 
 public class ReflectionMethodFactory extends MethodFactory {
     private static final Logger LOG = LoggerFactory.getLogger("ReflectionMethodFactory");

    
Use reflection to provide a method handle for a compiled Ruby method.

See also:
org.jruby.internal.runtime.methods.MethodFactory.getCompiledMethod
 
     public DynamicMethod getCompiledMethodLazily(RubyModule implementationClass,
             String rubyNameString javaNameArity arityVisibility visibility
             StaticScope scopeObject scriptObjectCallConfiguration callConfig,
             ISourcePosition positionString parameterDesc) {
 
         return getCompiledMethod(
                 implementationClass,
                 rubyName,
                 javaName,
                 arity,
                 visibility,
                 scope,
                 scriptObject,
                 callConfig,
                 position,
                 parameterDesc);
     }
    
    
Use reflection to provide a method handle for a compiled Ruby method.

See also:
org.jruby.internal.runtime.methods.MethodFactory.getCompiledMethod
 
     public DynamicMethod getCompiledMethod(RubyModule implementationClass,
             String rubyNameString javaNameArity arityVisibility visibility
             StaticScope scopeObject scriptObjectCallConfiguration callConfig,
             ISourcePosition positionString parameterDesc) {
         try {
             Class scriptClass = scriptObject.getClass();
             Method method = scriptClass.getMethod(javaNamescriptClassThreadContext.classIRubyObject.classIRubyObject[].classBlock.class);
            return new ReflectedCompiledMethod(
                    implementationClass,
                    arity,
                    visibility,
                    scope,
                    scriptObject,
                    method,
                    callConfig,
                    position,
                    parameterDesc);
        } catch (NoSuchMethodException nsme) {
            throw new RuntimeException("No method with name " + javaName + " found in " + scriptObject.getClass());
        }
    }
    
    
Use reflection to provide a method handle based on an annotated Java method.

See also:
org.jruby.internal.runtime.methods.MethodFactory.getAnnotatedMethod
    public DynamicMethod getAnnotatedMethod(RubyModule implementationClassJavaMethodDescriptor desc) {
        try {
            if (!Modifier.isPublic(desc.getDeclaringClass().getModifiers())) {
                .warn("warning: binding non-public class {}; reflected handles won't work"desc.declaringClassName);
            }
            Method method = desc.getDeclaringClass().getDeclaredMethod(desc.namedesc.getParameterClasses());
            JavaMethod ic = new ReflectedJavaMethod(implementationClassmethoddesc.anno);
            TypePopulator.populateMethod(
                    ic,
                    ic.getArity().getValue(),
                    method.getName(),
                    Modifier.isStatic(method.getModifiers()),
                    CallConfiguration.getCallConfigByAnno(desc.anno),
                    desc.anno.notImplemented());
                
            return ic;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    
Use reflection to provide a method handle based on an annotated Java method.

See also:
org.jruby.internal.runtime.methods.MethodFactory.getAnnotatedMethod
    public DynamicMethod getAnnotatedMethod(RubyModule implementationClassList<JavaMethodDescriptordescs) {
        try {
            if (!Modifier.isPublic(descs.get(0).getDeclaringClass().getModifiers())) {
                .warn("warning: binding non-public class {}; reflected handles won't work"descs.get(0).);
            }
            
            List<Methodmethods = new ArrayList();
            List<JRubyMethodannotations = new ArrayList();
            
            for (JavaMethodDescriptor descdescs) {
                methods.add(desc.getDeclaringClass().getDeclaredMethod(desc.namedesc.getParameterClasses()));
                annotations.add(desc.anno);
            }
            Method method0 = methods.get(0);
            JRubyMethod anno0 = annotations.get(0);
            
            JavaMethod ic = new ReflectedJavaMultiMethod(implementationClassmethodsannotations);
            TypePopulator.populateMethod(
                    ic,
                    ic.getArity().getValue(),
                    method0.getName(),
                    Modifier.isStatic(method0.getModifiers()),
                    CallConfiguration.getCallConfigByAnno(anno0),
                    anno0.notImplemented());
            return ic;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public CompiledBlockCallback getBlockCallback(String methodfinal String filefinal int linefinal Object scriptObject) {
        try {
            Class scriptClass = scriptObject.getClass();
            final Method blockMethod = scriptClass.getMethod(methodscriptClassThreadContext.classIRubyObject.classIRubyObject.classBlock.class);
            return new CompiledBlockCallback() {
                public IRubyObject call(ThreadContext contextIRubyObject selfIRubyObject argsBlock block) {
                    try {
                        return (IRubyObject)blockMethod.invoke(nullscriptObjectcontextselfargsblock);
                    } catch (IllegalAccessException ex) {
                        throw new RuntimeException(ex);
                    } catch (IllegalArgumentException ex) {
                        throw new RuntimeException(ex);
                    } catch (InvocationTargetException ex) {
                        Throwable cause = ex.getCause();
                        if (cause instanceof RuntimeException) {
                            throw (RuntimeExceptioncause;
                        } else if (cause instanceof Error) {
                            throw (Errorcause;
                        } else {
                            throw new RuntimeException(ex);
                        }
                    }
                }
                public String getFile() {
                    return file;
                }
                public int getLine() {
                    return line;
                }
            };
        } catch (NoSuchMethodException nsme) {
            throw new RuntimeException(nsme);
        }
    }
    public CompiledBlockCallback19 getBlockCallback19(String methodfinal String filefinal int linefinal Object scriptObject) {
        try {
            Class scriptClass = scriptObject.getClass();
            final Method blockMethod = scriptClass.getMethod(methodscriptClassThreadContext.classIRubyObject.classIRubyObject[].classBlock.class);
            return new CompiledBlockCallback19() {
                public IRubyObject call(ThreadContext contextIRubyObject selfIRubyObject[] argsBlock block) {
                    try {
                        return (IRubyObject)blockMethod.invoke(nullscriptObjectcontextselfargsblock);
                    } catch (IllegalAccessException ex) {
                        throw new RuntimeException(ex);
                    } catch (IllegalArgumentException ex) {
                        throw new RuntimeException(ex);
                    } catch (InvocationTargetException ex) {
                        Throwable cause = ex.getCause();
                        if (cause instanceof RuntimeException) {
                            throw (RuntimeExceptioncause;
                        } else if (cause instanceof Error) {
                            throw (Errorcause;
                        } else {
                            throw new RuntimeException(ex);
                        }
                    }
                }
                public String getFile() {
                    return file;
                }
                public int getLine() {
                    return line;
                }
            };
        } catch (NoSuchMethodException nsme) {
            throw new RuntimeException(nsme);
        }
    }
New to GrepCode? Check out our FAQ X