Move initializers form default ctor to field.
Add feature to spartinizer which removes redundant code from c'tors (such as happens in the default c'tor).
P.S. Happy Hanukkah!
As I understood, you mean we can give up redundant field initializations in constructors:
int a;
A() {
this.a = 0;
}
==>
int a;
A() {
}
It is a good idea- actually, this is exactly what we are doing in on-the-spot field initialization:
int a = 0;
==>
int a;
What make things complicated in the constructors' context are:
- We must initialize
finalfields
- So we must check fields are not final
- But...
- Fields may be declared in parent classes
- Hopa! a problem.
- We even have another issue:
- We should be able to recognize field initializations
- So we can remove the assignment for fields accessed without
thisexpression:
int a;
A() {
a = 0;
}
==>
int a;
A() {
}
- But... we have the meanings of doing this- simply by checking the name is not a variable declared in the constructor's scope (before the assignment)
I now work on completing the environment analysis. When this is done, the changed would be easier. I think the algorithm should be:
- Find all ctors of a class.
- Find all unconditional initialization of fields, i.e., ones that do not depend on theparameters.
- If there is a field initialized/uninitialized for which all such assignments are identical, go.
@orenafek
@orenafek : For your course. @amirsagiv83 , @michalcohen Try forming a rule for this.
Similar to #463