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

Type of filter by reflection #1015

Merged
merged 4 commits into from
Dec 1, 2016

Conversation

pvojtechovsky
Copy link
Collaborator

The type of the Filter does not have to be specified in constructor, but is computed by reflection from the matches parameter type.

This PR is based on #1013, so I have to rebase it after you merge #1013. Just have a look if code like this OK, or we can make it even simpler?

List<CtNewClass<?>> elements = foo.getElements(new AbstractFilter<CtNewClass<?>>() {
	@Override
	public boolean matches(CtNewClass<?> element) {
		return true;
	}
});

@monperrus
Copy link
Collaborator

you can go ahead with rebasing!

@monperrus monperrus changed the title Type of fitler by reflection Type of filter by reflection Nov 29, 2016
@pvojtechovsky
Copy link
Collaborator Author

You can already write it using java 8 lambda like this:

List<CtExpression<?>> expressions2 = foo.getElements((CtExpression<?> e)->true);

expressions2.forEach(it->{
	assertTrue(it instanceof CtExpression);
});

but it will not infer type correctly. It will fail with ClassCastException in call of matches. But it is expected and handled as not matches. I guess this ClassCastException has a performance impact...

May be in Java 9 the parameter type of lambda can be get by reflection.

@monperrus
Copy link
Collaborator

interesting. could you add tests to check that the inferred filteredType is correct?

@pvojtechovsky
Copy link
Collaborator Author

Can I replace all the calls of new AbstractFilter(CLASS), by new AbstractFilter() in all tests and all spoon code? It would be good test that it works. And it would shorten code again...

Should I will keep new AbstractFilter(CLASS) for compatibility?

@pvojtechovsky
Copy link
Collaborator Author

OK, I will add these tests probably tomorrow in the evening. Good night ;)

@pvojtechovsky
Copy link
Collaborator Author

Tests were added

@monperrus monperrus merged commit b83f033 into INRIA:master Dec 1, 2016
@pvojtechovsky pvojtechovsky deleted the typeOfFitlerByReflection branch January 21, 2017 12:01
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