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

review: fix issue related with overridden methods retrieved from getAllMethods #1375

Merged
merged 11 commits into from
Jun 22, 2017

Conversation

surli
Copy link
Collaborator

@surli surli commented Jun 7, 2017

getAllMethods should not retrieve a parent method if an overridden method is defined, even if the signature is different.

For example, if a class implements the method public int compareTo(T o); with object it will have the following signature: public int compareTo(Object o) and getAllMethod should only retrieve this one.

@surli
Copy link
Collaborator Author

surli commented Jun 7, 2017

I may use your help on this one @pvojtechovsky : I'm trying to use ClassTypingContext but I get a lot of errors that I do not fully understand...

@pvojtechovsky
Copy link
Collaborator

I will have a look at it today evening

@pvojtechovsky
Copy link
Collaborator

There were bugs in ClassTypingContext. I have fixed them in #1379.

Rebase this PR after #1379 is merged, then the tests should pass.

@@ -894,6 +895,14 @@ public String getQualifiedName() {
map(new AllTypeMembersFunction(CtMethod.class)).forEach(new CtConsumer<CtMethod<?>>() {
@Override
public void accept(CtMethod<?> method) {
ClassTypingContext ctc = new ClassTypingContext(CtTypeImpl.this);

Copy link
Collaborator

Choose a reason for hiding this comment

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

I suggest to move creation of ClassTypingContext above (out of the cycle). It will perform faster, because creation of ClassTypingContext is expensive and it is better to use cached information about super type hierarchy, then to create it always again.

Like this:

	@Override
	public Set<CtMethod<?>> getAllMethods() {
		final Set<String> distinctSignatures = new HashSet<>();
		final Set<CtMethod<?>> l = new SignatureBasedSortedSet<>();
		final ClassTypingContext ctc = new ClassTypingContext(CtTypeImpl.this);
		map(new AllTypeMembersFunction(CtMethod.class)).forEach(new CtConsumer<CtMethod<?>>() {
			@Override
			public void accept(CtMethod<?> method) {

				for (CtMethod<?> methods : l) {
					if (ctc.isSameSignature(methods, method)) {
						return;
					}
				}

				if (distinctSignatures.add(method.getSignature())) {
					l.add(method);
				}
			}
		});
		return Collections.unmodifiableSet(l);
	}

I would commit into this branch, but I have no permission.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I suggest to move creation of ClassTypingContext above (out of the cycle).

Yes, this was my first move but as I got a lot of errors in tests, I tried inside the loop to be sure of the state of ClassTypingContext I will change that :)

@surli
Copy link
Collaborator Author

surli commented Jun 8, 2017

There were bugs in ClassTypingContext. I have fixed them in #1379.

Ok, I wasn't sure but I had this feeling indeed. I'll look at your PR right now.

@surli surli changed the title WiP: fix issue related with overridden methods retrieved from getAllMethods review: fix issue related with overridden methods retrieved from getAllMethods Jun 8, 2017
@@ -643,7 +646,7 @@ private boolean isSubTypeArg(CtTypeReference<?> subArg, CtTypeReference<?> super
if (typeRef instanceof CtTypeParameterReference) {
CtTypeParameterReference typeParamRef = (CtTypeParameterReference) typeRef;
CtTypeParameter typeParam = typeParamRef.getDeclaration();
if (typeParam.getTypeParameterDeclarer() instanceof CtExecutable) {
if (typeParam != null && typeParam.getTypeParameterDeclarer() instanceof CtExecutable) {
//the parameter is declared in scope of Method or Constructor
Copy link
Collaborator

Choose a reason for hiding this comment

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

If typeParamRef.getDeclaration(); returns null then something is wrong ... may be it should throw SpoonException?

In what situtation it happens? Is it really correct to ignore that null?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was happening in LambdaTest.testCompileLambdaGeneratedBySpoon and MainTest.testTest. Apparently the problem is it tries to resolve R in public abstract R apply(T arg0); but it never reach the interface Function where it's defined.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So it sounds like another bug of CtTypeParameterReference#getDeclaration()
And then I do not like the idea to ignore null here. It should throw SpoonExpeception instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So it sounds like another bug of CtTypeParameterReference#getDeclaration()

Ok I'm working on it, it seems to be related with my recent changes with CtTypeParameter on method parameters.

And then I do not like the idea to ignore null here. It should throw SpoonExpeception instead.

I'll change that.

@surli surli changed the title review: fix issue related with overridden methods retrieved from getAllMethods WiP: fix issue related with overridden methods retrieved from getAllMethods Jun 8, 2017
@@ -891,9 +892,16 @@ public String getQualifiedName() {
public Set<CtMethod<?>> getAllMethods() {
final Set<String> distinctSignatures = new HashSet<>();
final Set<CtMethod<?>> l = new SignatureBasedSortedSet<>();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This set should not be needed anymore.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You are right! Please remove it too

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually I may have found an issue which prevent me from removing the set, see #1407. I propose to keep it as it is (it works well now) and to remove it later if needed following the change made for #1407

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would first merge this, with a clean implementation, and maybe a temporary behavioral change highlighted in the test diff. And then, we can concentrate on #1407.

@surli surli changed the title WiP: fix issue related with overridden methods retrieved from getAllMethods review: fix issue related with overridden methods retrieved from getAllMethods Jun 21, 2017
@monperrus monperrus merged commit 521f230 into INRIA:master Jun 22, 2017
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.

3 participants