akshattandon / projectlombok

Automatically exported from code.google.com/p/projectlombok
0 stars 0 forks source link

Add 'useSetters' configuration key to @XArgsConstructor #770

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I kindly request you to add configuration key to @RequiredArgsConstructor and 
@AllArgsConstructor annotations. 

lombok.toString.useSetters = [true | false]
If set to true, lombok should use setters (if available) instead of accessing 
fields directly when generating constructors.

This will make possible the below code:

// Lombok
import android.support.annotation.NonNull;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor(useSetters = true)
public class SimpleRecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerView.ViewHolder> {

    @NonNull private RecyclerView.Adapter mBaseAdapter;
    /* Other fields */

    private boolean mValid = true;

    private void setBaseAdapter(final RecyclerView.Adapter baseAdapter) {
        mBaseAdapter = baseAdapter;
        mBaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                mValid = baseAdapter.getItemCount() > 0;
                notifyDataSetChanged();
            }

            /* Other overridden methods */

        });
    }
}

Instead of:

// Vanilla Java
public class SimpleRecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private RecyclerView.Adapter mBaseAdapter;
    /* Other fields */

    private boolean mValid = true;

    public SimpleRecyclerViewAdapter(/* Required arguments */, RecyclerView.Adapter baseAdapter) {

        /* Simple initialization */
        mBaseAdapter = baseAdapter;
        mBaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                mValid = mBaseAdapter.getItemCount() > 0;
                notifyDataSetChanged();
            }

            /* Other overridden methods */

        });
    }
}

Please, tell what do you think?

Original issue reported on code.google.com by alarmo.l...@gmail.com on 14 Jan 2015 at 1:38

GoogleCodeExporter commented 9 years ago
The idea behind this is to get rid off a simple initialization in constructor 
(like 'mField = field;') and retain only the meaningful parts.

Original comment by alarmo.l...@gmail.com on 14 Jan 2015 at 1:45

GoogleCodeExporter commented 9 years ago
Not quite sure I get what you mean here. For every field where we wold have 
generated 'this.fieldName = argName', we should instead invoke 
'setFieldName(argName);' if the setter exists?

Calling setters from the constructor is not exactly great coding style; the 
object hasn't actually been initialized fully yet. But it's a common enough 
pattern, I suppose.

Original comment by reini...@gmail.com on 31 Jan 2015 at 3:57

GoogleCodeExporter commented 9 years ago
"For every field where we wold have generated 'this.fieldName = argName', we 
should instead invoke 'setFieldName(argName);' if the setter exists?"
Yes, that's what I mean.

There was many fields (in SimpleRecyclerViewAdapter), that could be simply 
initialized in a constructor and a single field (mBaseAdapter), that required a 
complex initialization process. Because of it, I couldn't use @RequiredArgs 
annotation to hide this constructor.

"Calling setters from the constructor is not exactly great coding style"
I understand. To prevent bad things from happening, we could check that the 
setter is 'final', and forbid usage of 'useSetters=true' if it's not.
( 
http://stackoverflow.com/questions/8501735/using-setter-methods-in-constructor-b
ad-practice )

Original comment by alarmo.l...@gmail.com on 2 Feb 2015 at 2:09