Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: spoon.support.reflect.declaration.CtTypeImpl.hasSameParameters(...) lacks an if condition #6072

Open
CoreRasurae opened this issue Nov 12, 2024 · 1 comment
Labels

Comments

@CoreRasurae
Copy link
Contributor

Describe the bug

spoon.support.reflect.declaration.CtTypeImpl.hasSameParameters(CtExecutable<?> candidate, CtTypeReference<?>... parameterTypes) lacks an if condition without which a comparison between a non-array type parameter and a candidate executable with an array parameter can take place, leading to a wrong return value.

Source code you are trying to analyze/transform

No response

Source code for your Spoon processing

protected boolean hasSameParameters(CtExecutable<?> candidate, CtTypeReference<?>... parameterTypes) {
		if (candidate.getParameters().size() != parameterTypes.length) {
			return false;
		}
		for (int i = 0; (i < candidate.getParameters().size()) && (i < parameterTypes.length); i++) {
			final CtTypeReference<?> ctParameterType = candidate.getParameters().get(i).getType();
			final CtTypeReference<?> parameterType = parameterTypes[i];
			if (parameterType instanceof CtArrayTypeReference) {
				if (ctParameterType instanceof CtArrayTypeReference) {
					if (!isSameParameter(candidate, ((CtArrayTypeReference) ctParameterType).getComponentType(), ((CtArrayTypeReference) parameterType).getComponentType())) {
						return false;
					} else {
						if (((CtArrayTypeReference) ctParameterType).getDimensionCount() != ((CtArrayTypeReference) parameterType).getDimensionCount()) {
							return false;
						}
					}
				} else {
					return false;
				}
                        <-- Condition is lacking here, if ctParameterType is an Array Type Ref.
			} else if (!isSameParameter(candidate, ctParameterType, parameterType)) {
				return false;
			}
		}
		return true;
	}

Actual output

No response

Expected output

protected boolean hasSameParameters(
            CtExecutable<?> candidate, CtTypeReference<?>... parameterTypes) {
        if (candidate.getParameters().size() != parameterTypes.length) {
            return false;
        }
        for (int i = 0; (i < candidate.getParameters().size()) && (i < parameterTypes.length); 
                i++) {
            final CtTypeReference<?> ctParameterType = 
                    candidate.getParameters().get(i).getType();
            final CtTypeReference<?> parameterType = parameterTypes[i];
            if (parameterType instanceof CtArrayTypeReference) {
                if (ctParameterType instanceof CtArrayTypeReference) {
                    if (!isSameParameter(candidate, 
                            ((CtArrayTypeReference) ctParameterType).getComponentType(), 
                            ((CtArrayTypeReference) parameterType).getComponentType())) {
                        return false;
                    } else {
                        if (((CtArrayTypeReference) ctParameterType).getDimensionCount() 
                                != ((CtArrayTypeReference) parameterType).getDimensionCount()) {
                            return false;
                        }
                    }
                } else {
                    return false;
                }
            //The following else if condition must be appended to fix the logic
            } else if (ctParameterType instanceof CtArrayTypeReference) {
                return false;
            } else if (!isSameParameter(candidate, ctParameterType, parameterType)) {
                return false;
            }
        }
        return true;
    }

Spoon Version

11.0.1

JVM Version

Not a JVM version issue

What operating system are you using?

NixOS Vicuna, but not relevant for this bug

@I-Al-Istannen
Copy link
Collaborator

I don't understand the problem. If you pass in a non-array parameter the type erasure will differ (Foo[] vs Foo) and the isSame check will fail. Can you provide a failing example? i.e. some source code using spoon where this produces wrong results?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants