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

private Fields which should not be reassigned, but cannot be marked with final keyword #31

Closed
cushon opened this issue Oct 31, 2014 · 3 comments

Comments

@cushon
Copy link
Collaborator

cushon commented Oct 31, 2014

Original issue created by alexeagle@google.com on 2012-07-30 at 05:15 PM


Suggested by David Mankin:

In Guice, we have @Inject fields, which should only be set by the injector when creating the class. Since they can't be marked final, we often prefer to use constructor injection instead, even though it's quite bulky.

Similarly in GWT, fields cannot be marked final because the serialization mechanism needs to be able to set the values in the server when populating a data object.

We should be able to enforce that such fields are never re-assigned in code within the class.

We might want an annotation for this, maybe in JSR305, or maybe JSR330 (which would be nice since other DI frameworks should take advantage). For Guice, you could imagine @Inject(finalish=true) but if we do GWT, may as well make an annotation.

Probably not practical to do this with non-private fields since we'd have to scan outside the enclosing class.

@cushon
Copy link
Collaborator Author

cushon commented Oct 31, 2014

Original comment posted by eaftan@google.com on 2013-02-11 at 09:41 PM


Would this be problematic for non-reflection-based DI frameworks like Dagger? They generate code which would then be compiled by javac, and if we add a check that fields with a certain annotation should be treated as final, then the generated code would also have to abide by that rule.


Labels: -Priority-Medium, Priority-Low

@cushon
Copy link
Collaborator Author

cushon commented Oct 31, 2014

Original comment posted by kevinb@google.com on 2013-02-11 at 10:31 PM


I don't see a problem wrt Guice -- just use constructor injection.

I don't really like the idea of a "think of this like it's final" annotation, because you won't get the actual memory visibility semantics at runtime.

@cushon
Copy link
Collaborator Author

cushon commented Oct 31, 2014

Original comment posted by eaftan@google.com on 2014-03-04 at 12:35 AM


I agree with Kevin that this is probably not a high enough priority to get done. Marking WillNotFix, but if you want to work on it please reopen and send us your code.


Status: WontFix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant