Skip to content

Commit

Permalink
Showing 2 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -37,17 +37,17 @@
/**
*/
public abstract class JavaMethod extends DynamicMethod implements Cloneable, MethodArgs2 {
protected int arityValue;

protected Arity arity = Arity.OPTIONAL;
private String javaName;
private boolean isSingleton;
protected StaticScope staticScope;
private String parameterDesc;
private String[] parameterList;

private static String[] ONE_REQ = new String[] { "q" };
private static String[] TWO_REQ = new String[] { "q", "q" };
private static String[] THREE_REQ = new String[] { "q", "q", "q" };
private static final String[] ONE_REQ = new String[] { "q" };
private static final String[] TWO_REQ = new String[] { "q", "q" };
private static final String[] THREE_REQ = new String[] { "q", "q", "q" };

public static final Class[][] METHODS = {
{JavaMethodZero.class, JavaMethodZeroOrOne.class, JavaMethodZeroOrOneOrTwo.class, JavaMethodZeroOrOneOrTwoOrThree.class},
@@ -94,8 +94,7 @@ protected JavaMethod() {}

public void init(RubyModule implementationClass, Arity arity, Visibility visibility, StaticScope staticScope, CallConfiguration callConfig) {
this.staticScope = staticScope;
this.arity = arity;
this.arityValue = arity.getValue();
setArity(arity);
super.init(implementationClass, visibility, callConfig);
}

@@ -190,7 +189,6 @@ protected final void returnTraceCompiled(ThreadContext context, boolean enabled,

public void setArity(Arity arity) {
this.arity = arity;
this.arityValue = arity.getValue();
}

@Override
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
* rights and limitations under the License.
*
* Copyright (c) 2007 Peter Brant <peter.brant@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"),
@@ -44,38 +44,41 @@

public class ReflectedJavaMethod extends JavaMethod {
private final Method method;

private final boolean needsBlock;
private final boolean isStatic;
private final int required;
private final int optional;
private final boolean rest;
private final int max;

private final boolean argsAsIs;

private final boolean needsThreadContext;

final int arityValue;

public ReflectedJavaMethod(
RubyModule implementationClass, Method method, JRubyMethod annotation) {
super(implementationClass, annotation.visibility());

this.method = method;

Class<?>[] params = method.getParameterTypes();
this.needsBlock = params.length > 0 && params[params.length - 1] == Block.class;
this.isStatic = Modifier.isStatic(method.getModifiers());

Arity arity = Arity.fromAnnotation(annotation, params, this.isStatic);
setArity(arity);
this.arityValue = arity.getValue();

this.required = arity.getValue() >= 0 ? arity.getValue() : Math.abs(arity.getValue())-1;
this.required = arityValue >= 0 ? arityValue : ( - arityValue ) - 1;
this.optional = annotation.optional();
this.rest = annotation.rest();

this.needsThreadContext = params.length > 0 && params[0] == ThreadContext.class;
this.argsAsIs = ! isStatic && optional == 0 && !rest && !needsBlock && !needsThreadContext;

if (rest) {
max = -1;
} else {
@@ -88,31 +91,31 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz
IRubyObject[] args, Block block) {
Ruby runtime = context.runtime;
Arity.checkArgumentCount(runtime, name, args, required, max);

callConfig.pre(context, self, getImplementationClass(), name, block, null);

try {
if (! isStatic && ! method.getDeclaringClass().isAssignableFrom(self.getClass())) {
throw new ClassCastException(
self.getClass().getName() + " cannot be converted to " +
method.getDeclaringClass().getName());
}

if (argsAsIs) {
boolean isTrace = runtime.hasEventHooks();
try {
if (isTrace) {
runtime.callEventHooks(context, RubyEvent.C_CALL, context.getFile(), context.getLine(), name, getImplementationClass());
}
}
return (IRubyObject)method.invoke(self, (Object[])args);
} finally {
if (isTrace) {
runtime.callEventHooks(context, RubyEvent.C_RETURN, context.getFile(), context.getLine(), name, getImplementationClass());
}
}
}
} else {
int argsLength = calcArgsLength();

Object[] params = new Object[argsLength];
int offset = 0;
if (needsThreadContext) {
@@ -135,7 +138,7 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz
if (needsBlock) {
params[offset++] = block;
}

boolean isTrace = runtime.hasEventHooks();
try {
if (isTrace) {
@@ -175,7 +178,7 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz

private int calcArgsLength() {
int argsLength = 0;

if (needsThreadContext) {
argsLength++;
}

0 comments on commit 2445a80

Please sign in to comment.