google / google/guice

Code optimization suggestions

Open
#1,634 0 comments 0 reactions 0 assignees View on GitHub
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.