Skip to content

Commit

Permalink
Minor memory and performance fixes for Guice.
Browse files Browse the repository at this point in the history
- Use char where available
- Replace usage of LinkedHashMap with ImmutableSet

PiperOrigin-RevId: 398739059
  • Loading branch information
lindner authored and Guice Team committed Sep 24, 2021
1 parent ffd9d01 commit 25fe439
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions core/src/com/google/inject/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public final String toString() {
// method.
String local = toString;
if (local == null) {
local = "Key[type=" + typeLiteral + ", annotation=" + annotationStrategy + "]";
local = "Key[type=" + typeLiteral + ", annotation=" + annotationStrategy + ']';
toString = local;
}
return local;
Expand Down Expand Up @@ -513,7 +513,7 @@ public int hashCode() {

@Override
public String toString() {
return "@" + annotationType.getName();
return '@' + annotationType.getName();
}
}
}
6 changes: 3 additions & 3 deletions core/src/com/google/inject/internal/Annotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private static int annotationHashCode(
/** Implements {@link Annotation#toString}. */
private static String annotationToString(
Class<? extends Annotation> type, Map<String, Object> members) throws Exception {
StringBuilder sb = new StringBuilder().append("@").append(type.getName()).append("(");
StringBuilder sb = new StringBuilder().append('@').append(type.getName()).append('(');
JOINER.appendTo(
sb,
Maps.transformValues(
Expand All @@ -167,7 +167,7 @@ private static String annotationToString(
String s = Arrays.deepToString(new Object[] {arg});
return s.substring(1, s.length() - 1); // cut off brackets
}));
return sb.append(")").toString();
return sb.append(')').toString();
}

/** Returns true if the given annotation is retained at runtime. */
Expand Down Expand Up @@ -413,7 +413,7 @@ public static String nameOf(Key<?> key) {
if (annotation != null && !isMarker(annotationType)) {
return key.getAnnotation().toString();
} else if (key.getAnnotationType() != null) {
return "@" + key.getAnnotationType().getName();
return '@' + key.getAnnotationType().getName();
} else {
return "";
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/com/google/inject/internal/MoreTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,11 @@ public String toString() {
return stringBuilder.toString();
}

stringBuilder.append("<").append(typeToString(typeArguments[0]));
stringBuilder.append('<').append(typeToString(typeArguments[0]));
for (int i = 1; i < typeArguments.length; i++) {
stringBuilder.append(", ").append(typeToString(typeArguments[i]));
}
return stringBuilder.append(">").toString();
return stringBuilder.append('>').toString();
}

private static void ensureOwnerType(Type ownerType, Type rawType) {
Expand Down
6 changes: 3 additions & 3 deletions core/src/com/google/inject/internal/UniqueAnnotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public Class<? extends Annotation> annotationType() {

@Override
public String toString() {
return "@"
return '@'
+ Internal.class.getName()
+ "("
+ '('
+ Annotations.memberValueString("value", value)
+ ")";
+ ')';
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions core/src/com/google/inject/internal/util/Classes.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public static String toString(Member member) {
Class<? extends Member> memberType = Classes.memberType(member);

if (memberType == Method.class) {
return member.getDeclaringClass().getName() + "." + member.getName() + "()";
return member.getDeclaringClass().getName() + '.' + member.getName() + "()";
} else if (memberType == Field.class) {
return member.getDeclaringClass().getName() + "." + member.getName();
return member.getDeclaringClass().getName() + '.' + member.getName();
} else if (memberType == Constructor.class) {
return member.getDeclaringClass().getName() + ".<init>()";
} else {
Expand Down
2 changes: 1 addition & 1 deletion core/src/com/google/inject/name/NamedImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public boolean equals(Object o) {

@Override
public String toString() {
return "@" + Named.class.getName() + "(" + Annotations.memberValueString("value", value) + ")";
return '@' + Named.class.getName() + '(' + Annotations.memberValueString("value", value) + ')';
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions core/src/com/google/inject/spi/Dependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(key);
if (injectionPoint != null) {
builder.append("@").append(injectionPoint);
builder.append('@').append(injectionPoint);
if (parameterIndex != -1) {
builder.append("[").append(parameterIndex).append("]");
builder.append('[').append(parameterIndex).append(']');
}
}
return builder.toString();
Expand Down
3 changes: 1 addition & 2 deletions core/src/com/google/inject/util/Modules.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import com.google.inject.spi.ScopeBinding;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -231,7 +230,7 @@ public void configure() {
}

final Binder binder = baseBinder.skipSources(this.getClass());
final LinkedHashSet<Element> elements = new LinkedHashSet<>(baseElements);
final ImmutableSet<Element> elements = ImmutableSet.copyOf(baseElements);
final Module scannersModule = extractScanners(elements);
final List<Element> overrideElements =
Elements.getElements(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public String toString() {
result.append("@Assisted ");
}
if (bindingAnnotation != null) {
result.append(bindingAnnotation).append(" ");
result.append(bindingAnnotation).append(' ');
}
return result.append(type).toString();
}
Expand Down

0 comments on commit 25fe439

Please sign in to comment.