Code optimization suggestions
- Dominant language
- Java
- Stars
- 12.7k
- Forks
- 1.7k
- Avg merge
- 11m
- Merged PRs (30d)
- 2
Description
Hi,
I find that the private field hasAnnotations at Line 287 in the file 'guice/core/src/com/google/inject/internal/Annotations.java ' on the master branch is only assigned and used in the field cache. Therefore, this field can be removed from the class, and become an input parameter in the field cache. This transformation will normally reduce memory usage and improve readability of your code.I will be happy if this transformation is helpful.
```java
// line 287 this field can be removed from the class
private CacheLoader, Boolean> hasAnnotations =
new CacheLoader, Boolean>() {
@Override
public Boolean load(Class annotationType) {
for (Annotation annotation : annotationType.getAnnotations()) {
if (annotationTypes.contains(annotation.annotationType())) {
return true;
}
}
return false;
}
};
// start
final LoadingCache, Boolean> cache =
CacheBuilder.newBuilder().weakKeys().build(new CacheLoader, Boolean>() {
@Override
public Boolean load(Class annotationType) {
for (Annotation annotation : annotationType.getAnnotations()) {
if (annotationTypes.contains(annotation.annotationType())) {
return true;
}
}
return false;
}
});
// end
final LoadingCache, Boolean> cache =
CacheBuilder.newBuilder().weakKeys().build(hasAnnotations);
```
Contributor guide
Assessment
This issue has not been assessed yet.