-
-
Notifications
You must be signed in to change notification settings - Fork 352
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
test for thisAccess like this.field and not SomeClass.this.field #991
Conversation
And I have added another test, then one which should automatically use thisAccess even if the field was implicit before the refactoring. Idea of the test. Let's have method public class Pizza {
int size;
void addSize(int plus) {
size = size + plus;
}
} The field void addSize(int size) {
size = size + size;
} but it should produce `this.size = this.size + size;' So I agree with @leventov, that printer should not care about CtFieldAccess.isImplicit, but should print optimized value. |
Hi, I wrote a possible solution here: tdurieux@52d75a7 |
I tried your solution on my project and it produce compilation errors in following situation public abstract class A {
public void methodOnA() {}
}
public class B extends A {
public void methodOnB() {
methodOnA();
}
} because it produced public void methodOnB() {
A.this.methodOnA();
} |
Anyway thank You for investigating this problem! I will need solution for that probably next year - in the case, when all pass and I let spoon refactor sources of my project. More I understand spoon more I see how much work is in it! ... and how complicated task it is to analyze/refactor sources. |
Yes I know, it does not work at all. It's much more complex that anticipated. |
Looking at If I would have time to do it, I would try opposite approach. To make PrintingContext more clever then it is now. The PrintingContext is called (push/pop) with each CtElement. So it knows the context the best. I guess the algorithm, which would resolve accessible variables, should be based on the PrintingContext and helper data collected there. But it is just my feeling, I have not tried it yet or thought about that deeper. |
It does not work when you extends classes from external libraries. Example // class from external library
public External() {
int a = 0;
}
public class Parent () {
int a = 0;
private class Child() extends External {
public void m () {
Parent.this.a++; // with your technique the qualified name will be removed and java will access a from External not from Parent
}
}
} I update my code, now I succeed to compile Spoon tdurieux@64a6bcb |
It is really a challenge :-) |
my code is not compatible with the no-classpath mode. |
I just checked the ClassNotFoundExceptions and the problem is not in the class path. I am not using noClassPath mode. That new algorithm fails on each nested class like package a.b.c;
public class A {
protected static class B {}
} because it tries to load class with name "B", but it should load class "a.b.c.A$B" |
If I have some time I will try to fix to fix. By the way I updated my branch and now it supports invocation. |
I did not succeed to reproduce your issue. The qualified name of your example is correct. |
cedc8c9
to
d248eab
Compare
I have committed here the test which reproduces the ClassNotFoundException on invalid qualified name of nested type. Run the FieldAccessTest.testFieldAccessDeclaredInADefaultClass() in debug and put breakpoint into The type.getQualifiedName() is "Fails", but should be spoon.test.fieldaccesses.testclasses.internal.Foo$Fails |
Ok, thanks. With my current code I cannot reproduce the bug because I remove the isSubtypeOf call from the jdttreebuilder. |
fixed by #1031? |
Yes, #1031 is fixing this issue |
closed by #1031 |
I guess the issue #826 created by @leventov is about the problem, which is reproducable by 2 new tests of this PR
The problem, which I feel too is that in the source class like
The spoon produces field access
this.size
in these two wrong formsif
factory.getEnvironment().setAutoImports(true)
then it isif
factory.getEnvironment().setAutoImports(false)
then it isBut I would prefer to print
this.size
in all cases.