Skip to content

Instantly share code, notes, and snippets.

@gissuebot
Created July 7, 2014 19:13
Show Gist options
  • Save gissuebot/14538301a056de033f4b to your computer and use it in GitHub Desktop.
Save gissuebot/14538301a056de033f4b to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 776, comment 1
Description: Avoid unnecessary object creation if .withSource doesn't change the source
Author: Stuart McCulloch <mcculls@gmail.com>
Bug-Google: http://code.google.com/p/google-guice/issues/detail?id=776
Last-Update: 2013-10-22
diff --git a/core/src/com/google/inject/internal/Errors.java b/core/src/com/google/inject/internal/Errors.java
index 25e73ca..d8dd43c 100644
--- a/core/src/com/google/inject/internal/Errors.java
+++ b/core/src/com/google/inject/internal/Errors.java
@@ -115,7 +115,7 @@ public final class Errors implements Serializable {
* Returns an instance that uses {@code source} as a reference point for newly added errors.
*/
public Errors withSource(Object source) {
- return source == SourceProvider.UNKNOWN_SOURCE
+ return source == this.source || source == SourceProvider.UNKNOWN_SOURCE
? this
: new Errors(this, source);
}
diff --git a/core/src/com/google/inject/spi/Elements.java b/core/src/com/google/inject/spi/Elements.java
index 44d49ff..6129255 100644
--- a/core/src/com/google/inject/spi/Elements.java
+++ b/core/src/com/google/inject/spi/Elements.java
@@ -325,7 +325,7 @@ public final class Elements {
}
public RecordingBinder withSource(final Object source) {
- return new RecordingBinder(this, source, null);
+ return source == this.source ? this : new RecordingBinder(this, source, null);
}
public RecordingBinder skipSources(Class... classesToSkip) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment