Main icon indicating copy to clipboard operation
Main copied to clipboard

Move initializers form default ctor to field.

Open AviadCo opened this issue 9 years ago • 5 comments

Add feature to spartinizer which removes redundant code from c'tors (such as happens in the default c'tor).

P.S. Happy Hanukkah!

AviadCo avatar Dec 24 '16 22:12 AviadCo

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:

  1. We must initialize final fields
  • So we must check fields are not final
  • But...
  1. Fields may be declared in parent classes
  • Hopa! a problem.
  • We even have another issue:
  1. We should be able to recognize field initializations
  • So we can remove the assignment for fields accessed without this expression:
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)

OriRoth avatar Dec 24 '16 23:12 OriRoth

I now work on completing the environment analysis. When this is done, the changed would be easier. I think the algorithm should be:

  1. Find all ctors of a class.
  2. Find all unconditional initialization of fields, i.e., ones that do not depend on theparameters.
  3. If there is a field initialized/uninitialized for which all such assignments are identical, go.

yossigil avatar Dec 25 '16 04:12 yossigil

@orenafek

yossigil avatar Jan 03 '17 23:01 yossigil

@orenafek : For your course. @amirsagiv83 , @michalcohen Try forming a rule for this.

yossigil avatar Mar 27 '17 19:03 yossigil

Similar to #463

yossigil avatar Apr 28 '17 05:04 yossigil