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

Resolve pre-existing Android Inspection violations #468

Merged
merged 21 commits into from
Apr 16, 2019

Conversation

fractalwrench
Copy link
Contributor

Addresses existing violations for the Android Inspector (all code reviewed in previous PRs).

Marking fields as final guarantees that the reference cannot be modified after initialisation. While
it's still possible to mutate any fields on the object itself, this is a good first step towards
thread safety, and fields should be final by default.
we should prefer a switch statement for longer conditional branches (3+) as it reads better
There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]).

In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation.

This inspection allows to follow the uniform style: either using an empty array (which is recommended in modern Java) or using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).
Prefer using zero-based array when converting collection
refactor: Convert fields to local variables
Remove redundant/verbose code constructs as reported via Android Lint
Add missing nullability annotations in test code
@fractalwrench fractalwrench changed the title Android Inspection fixes Resolve pre-existing Android Inspection violations Apr 15, 2019
@fractalwrench fractalwrench merged commit 6f6c88b into master Apr 16, 2019
@fractalwrench fractalwrench deleted the inspection-fixes branch April 16, 2019 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant