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

fix IndexOutOfBoundsException in getType() #1932

Merged
merged 3 commits into from
Apr 1, 2018

Conversation

Egor18
Copy link
Contributor

@Egor18 Egor18 commented Mar 29, 2018

Hi! I found out that IndexOutOfBoundsException is occurred in cases when getType() is called on parameter of a catch with multiple exception types, some of which is unknown (noclasspath mode):

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at spoon.support.reflect.code.CtCatchVariableImpl.getType(CtCatchVariableImpl.java:109)
        ....

I think it would be better to return null from getType() in such cases instead, so I provided this PR.


List<CtCatch> catches = model.getElements(new TypeFilter<CtCatch>(CtCatch.class));
catches.get(0).getParameter().getType(); // catch with single UnknownException
catches.get(1).getParameter().getType(); // multicatch with UnknownException
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add assert, which checks the returned value too?
assertNull(...)

@pvojtechovsky
Copy link
Collaborator

Hi!
It looks good to me. I agree that null is the best returned value for that case.
Optionally we might return Exception.class, but it doesn't warn the client that value is unknown ... So I like null

@pvojtechovsky
Copy link
Collaborator

It would be nice to have it documented in javaDoc of CtCatchVariable#getType() too.

@pvojtechovsky pvojtechovsky merged commit 25ff47f into INRIA:master Apr 1, 2018
@pvojtechovsky
Copy link
Collaborator

Thank You!

@surli surli mentioned this pull request Jun 25, 2018
@Egor18 Egor18 deleted the fix_out_of_bounds_exception branch October 5, 2018 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants