Skip to content

Commit

Permalink
Changes related to issue #731 method lookup regression. This is only …
Browse files Browse the repository at this point in the history
…a partial fix because each change uncovered another subtle issue. Still work to be done.
  • Loading branch information
tmoore committed Mar 14, 2023
1 parent da31f7e commit 82e50e5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/main/java/bsh/BSHArrayInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ public Object eval( Class<?> baseType, int dimensions, CallStack callstack,
inferType = inferCommonType(null, this, callstack, interpreter);

// force MapEntry to Map output
if ( MapEntry.class == inferType && Void.TYPE == baseType
|| MapEntry.class == baseType )
baseType = Map.class;
if (dimensions < 2)
if ( MapEntry.class == inferType && Void.TYPE == baseType
|| MapEntry.class == baseType )
baseType = Map.class;

// no common type was inferred
if ( null == inferType ) {
Expand Down Expand Up @@ -145,6 +146,7 @@ private Object buildArray(int dimensions, Class<?> baseType,
// the values are set.
dims[0] = jjtGetNumChildren();
Object array = Array.newInstance( baseType, dims );
Class<?> entryType = array.getClass().getComponentType();

// Evaluate the child nodes
for ( int i = 0; i < jjtGetNumChildren(); i++ ) {
Expand Down Expand Up @@ -180,7 +182,7 @@ private Object buildArray(int dimensions, Class<?> baseType,
try {
// store the value in the array
Array.set(array, i,
normalizeEntry(entry, baseType, dimensions, callstack));
normalizeEntry(entry, entryType, dimensions, callstack));
} catch( IllegalArgumentException e ) {
Interpreter.debug("illegal arg", e);
throwTypeError( baseType, entry, i, callstack );
Expand Down Expand Up @@ -249,7 +251,11 @@ private Object normalizeEntry(Object value, Class<?> baseType, int dimensions,
* @throws EvalError thrown on cast exceptions */
private Object toCollection(Object value, Class<?> type, CallStack callstack)
throws EvalError {
if ( Types.isCollectionType(type) ) try {
Class valClaz = value.getClass();
if ( Types.isCollectionType(type) &&
!(valClaz.isArray() &&
type.isAssignableFrom(Types.arrayElementType(valClaz))))
try {
return Types.castObject(value, type, Types.CAST);
} catch ( UtilEvalError e ) {
e.toEvalError(this, callstack);
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/bsh/BSHFormalParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,24 @@ public String getTypeDescriptor(
public Object eval( CallStack callstack, Interpreter interpreter)
throws EvalError
{
if ( jjtGetNumChildren() > 0 )
if ( jjtGetNumChildren() > 0 ) {
type = ((BSHType)jjtGetChild(0)).getType( callstack, interpreter );
else
/*
* Check if dimensions have been recorded for this parameter.
* If so,
* - If no array dimensions on the type then add them now.
* - If there are already array dimensions on the type then
* throw an error.
*/
if (dimensions > 0) {
if (!type.isArray())
type = Array.newInstance(type, new int[dimensions]).getClass();
else
throw new EvalError(
"Array dimensions not allowed on both type and name: "
+ name, this, null );
}
} else
type = UNTYPED;

if (isVarArgs)
Expand All @@ -78,4 +93,3 @@ public String toString() {
return super.toString() + ": " + name + ", final=" + isFinal + ", varargs=" + isVarArgs;
}
}

2 changes: 1 addition & 1 deletion src/main/java/bsh/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ If fromValue is (or would be) Primitive.NULL then fromType should be null.
public static Object castObject( Class<?> toType, Class<?> fromType, Object fromValue,
int operation, boolean checkOnly ) throws UtilEvalError {
// assignment to loose type, void type, or exactly same type
if ( toType == null || arrayElementType(toType) == arrayElementType(fromType) )
if ( toType == null || toType == fromType )
return checkOnly ? VALID_CAST :
fromValue;

Expand Down

0 comments on commit 82e50e5

Please sign in to comment.