Skip to content

Commit

Permalink
Varargs method implementation.
Browse files Browse the repository at this point in the history
 * added ELLIPSIS to the grammar and let FormalParameter receive isVarArgs token.
 * implemented Types.isSignatureVarargsAssignable for method signature comparison.
 * make type parameter an array of Type if isVarArgs
 * implement method invocation and appropriate type casting for isVarArgs
  • Loading branch information
nickl- committed May 29, 2018
1 parent 377268c commit c1adf5d
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 78 deletions.
10 changes: 8 additions & 2 deletions src/main/java/bsh/BSHFormalParameter.java
Expand Up @@ -27,6 +27,8 @@

package bsh;

import java.lang.reflect.Array;

/**
A formal parameter declaration.
For loose variable declaration type is null.
Expand All @@ -38,18 +40,19 @@ class BSHFormalParameter extends SimpleNode
// unsafe caching of type here
public Class type;
boolean isFinal = false;
boolean isVarArgs = false;

BSHFormalParameter(int id) { super(id); }

public String getTypeDescriptor(
CallStack callstack, Interpreter interpreter, String defaultPackage )
{
if ( jjtGetNumChildren() > 0 )
return ((BSHType)jjtGetChild(0)).getTypeDescriptor(
return (isVarArgs ? "[" : "") + ((BSHType)jjtGetChild(0)).getTypeDescriptor(
callstack, interpreter, defaultPackage );
else
// this will probably not get used
return "Ljava/lang/Object;"; // Object type
return (isVarArgs ? "[" : "") +"Ljava/lang/Object;"; // Object type
}

/**
Expand All @@ -63,6 +66,9 @@ public Object eval( CallStack callstack, Interpreter interpreter)
else
type = UNTYPED;

if (isVarArgs)
type = Array.newInstance(type, 0).getClass();

return type;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/bsh/BSHFormalParameters.java
Expand Up @@ -38,6 +38,7 @@ class BSHFormalParameters extends SimpleNode
Class [] paramTypes;
int numArgs;
String [] typeDescriptors;
boolean isVarArgs;

BSHFormalParameters(int id) { super(id); }

Expand All @@ -53,6 +54,7 @@ void insureParsed()
for(int i=0; i<numArgs; i++)
{
BSHFormalParameter param = (BSHFormalParameter)jjtGetChild(i);
isVarArgs = param.isVarArgs;
paramNames[i] = param.name;
paramModifiers[i] = new Modifiers(Modifiers.FIELD);
if (param.isFinal)
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/bsh/BSHMethodDeclaration.java
Expand Up @@ -46,6 +46,7 @@ class BSHMethodDeclaration extends SimpleNode
// Unsafe caching of type here.
Class returnType; // null (none), Void.TYPE, or a Class
int numThrows = 0;
boolean isVarArgs;

BSHMethodDeclaration(int id) { super(id); }

Expand Down Expand Up @@ -73,6 +74,8 @@ synchronized void insureNodesParsed()
paramsNode = (BSHFormalParameters)jjtGetChild(0);
blockNode = (BSHBlock)jjtGetChild(1+numThrows); // skip throws
}
paramsNode.insureParsed();
isVarArgs = paramsNode.isVarArgs;
}

/**
Expand Down Expand Up @@ -125,11 +128,8 @@ public Object eval( CallStack callstack, Interpreter interpreter )

NameSpace namespace = callstack.top();
BshMethod bshMethod = new BshMethod( this, namespace, modifiers );
try {
namespace.setMethod( bshMethod );
} catch ( UtilEvalError e ) {
throw e.toEvalError(this,callstack);
}

namespace.setMethod( bshMethod );

return Primitive.VOID;
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/bsh/BlockNameSpace.java
Expand Up @@ -178,9 +178,7 @@ public void importPackage(String name) {
getParent().importPackage( name );
}

public void setMethod(BshMethod method)
throws UtilEvalError
{
public void setMethod(BshMethod method) {
getParent().setMethod( method );
}
}
Expand Down

0 comments on commit c1adf5d

Please sign in to comment.