Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mcculls committed Jun 13, 2016
2 parents 256af08 + 7f9a8c8 commit ebd3b0b
Show file tree
Hide file tree
Showing 28 changed files with 732 additions and 250 deletions.
4 changes: 2 additions & 2 deletions build.xml
Expand Up @@ -289,10 +289,10 @@
<replacetoken><![CDATA[<zipfileset src="${common.basedir}/lib/build/asm-5.0.3.jar"/>]]></replacetoken>
</replace>
<replace file="build/no_aop/common.xml" value="">
<replacetoken><![CDATA[<zipfileset src="${common.basedir}/lib/build/cglib-3.2.jar"/>]]></replacetoken>
<replacetoken><![CDATA[<zipfileset src="${common.basedir}/lib/build/cglib-3.2.3.jar"/>]]></replacetoken>
</replace>
<replace file="build/no_aop/common.xml" value="">
<replacetoken><![CDATA[<zipfileset src="${common.basedir}/lib/build/cglib-3.2.jar"><include name="LICENSE"/><include name="NOTICE"/></zipfileset>]]></replacetoken>
<replacetoken><![CDATA[<zipfileset src="${common.basedir}/lib/build/cglib-3.2.3.jar"><include name="LICENSE"/><include name="NOTICE"/></zipfileset>]]></replacetoken>
</replace>
<replace file="build/no_aop/common.xml" value='Bundle-Name" value="$${ant.project.name} (no_aop)'>
<replacetoken><![CDATA[Bundle-Name" value="${ant.project.name}]]></replacetoken>
Expand Down
2 changes: 1 addition & 1 deletion common.xml
Expand Up @@ -148,7 +148,7 @@
classpath="${common.basedir}/lib/build/jarjar-1.1.jar"/>
<jarjar jarfile="${build.dir}/${ant.project.name}-with-deps.jar">
<fileset dir="${build.dir}/classes"/>
<zipfileset src="${common.basedir}/lib/build/cglib-3.2.jar"/>
<zipfileset src="${common.basedir}/lib/build/cglib-3.2.3.jar"/>
<zipfileset src="${common.basedir}/lib/build/asm-5.0.3.jar"/>
<rule pattern="net.sf.cglib.*" result="com.google.inject.internal.cglib.$@1"/>
<rule pattern="net.sf.cglib.**.*" result="com.google.inject.internal.cglib.@1.$@2"/>
Expand Down
34 changes: 12 additions & 22 deletions core/src/com/google/inject/Key.java
Expand Up @@ -21,8 +21,6 @@
import static com.google.inject.internal.Annotations.generateAnnotation;
import static com.google.inject.internal.Annotations.isAllDefaultMethods;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.inject.internal.Annotations;
import com.google.inject.internal.MoreTypes;

Expand Down Expand Up @@ -58,7 +56,10 @@ public class Key<T> {

private final TypeLiteral<T> typeLiteral;
private final int hashCode;
private final Supplier<String> toStringSupplier;
// This field is updated using the 'Data-Race-Ful' lazy intialization pattern
// See http://jeremymanson.blogspot.com/2008/12/benign-data-races-in-java.html for a detailed
// explanation.
private String toString;

/**
* Constructs a new key. Derives the type from this class's type parameter.
Expand All @@ -78,7 +79,6 @@ protected Key(Class<? extends Annotation> annotationType) {
this.typeLiteral = MoreTypes.canonicalizeForKey(
(TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()));
this.hashCode = computeHashCode();
this.toStringSupplier = createToStringSupplier();
}

/**
Expand All @@ -100,7 +100,6 @@ protected Key(Annotation annotation) {
this.typeLiteral = MoreTypes.canonicalizeForKey(
(TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()));
this.hashCode = computeHashCode();
this.toStringSupplier = createToStringSupplier();
}

/**
Expand All @@ -120,7 +119,6 @@ protected Key() {
this.typeLiteral = MoreTypes.canonicalizeForKey(
(TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()));
this.hashCode = computeHashCode();
this.toStringSupplier = createToStringSupplier();
}

/**
Expand All @@ -131,15 +129,13 @@ private Key(Type type, AnnotationStrategy annotationStrategy) {
this.annotationStrategy = annotationStrategy;
this.typeLiteral = MoreTypes.canonicalizeForKey((TypeLiteral<T>) TypeLiteral.get(type));
this.hashCode = computeHashCode();
this.toStringSupplier = createToStringSupplier();
}

/** Constructs a key from a manually specified type. */
private Key(TypeLiteral<T> typeLiteral, AnnotationStrategy annotationStrategy) {
this.annotationStrategy = annotationStrategy;
this.typeLiteral = MoreTypes.canonicalizeForKey(typeLiteral);
this.hashCode = computeHashCode();
this.toStringSupplier = createToStringSupplier();
}

/**
Expand All @@ -149,19 +145,6 @@ private int computeHashCode() {
return typeLiteral.hashCode() * 31 + annotationStrategy.hashCode();
}

/**
* @return a {@link Supplier} which memoizes the value for lazy initialization.
*/
private Supplier<String> createToStringSupplier() {
// The performance hit on access is acceptable since the intended use is for non-performance-
// critical applications such as debugging and logging.
return Suppliers.memoize(new Supplier<String>() {
@Override public String get() {
return "Key[type=" + typeLiteral + ", annotation=" + annotationStrategy + "]";
}
});
}

/**
* Gets the key type.
*/
Expand Down Expand Up @@ -225,7 +208,14 @@ Key<Provider<T>> providerKey() {
}

@Override public final String toString() {
return toStringSupplier.get();
// Note: to not introduce dangerous data races the field should only be read once in this
// method.
String local = toString;
if (local == null) {
local = "Key[type=" + typeLiteral + ", annotation=" + annotationStrategy + "]";
toString = local;
}
return local;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions core/src/com/google/inject/internal/BindingProcessor.java
Expand Up @@ -104,6 +104,14 @@ public Boolean visit(InstanceBinding<? extends T> binding) {
public Boolean visit(ProviderInstanceBinding<? extends T> binding) {
prepareBinding();
javax.inject.Provider<? extends T> provider = binding.getUserSuppliedProvider();
// TODO(lukes): add support for multibinder/mapbinder/optionalbinder here as well
// this may require extracting some generic interface instead of testing for specific
// implementations.
if (provider instanceof ProviderMethod) {
@SuppressWarnings("unchecked")
ProviderMethod<T> asProviderMethod = (ProviderMethod<T>) provider;
return visitProviderMethod(asProviderMethod);
}
Set<InjectionPoint> injectionPoints = binding.getInjectionPoints();
Initializable<? extends javax.inject.Provider<? extends T>> initializable =
initializer.<javax.inject.Provider<? extends T>>requestInjection(
Expand Down Expand Up @@ -153,6 +161,15 @@ public Boolean visit(LinkedKeyBinding<? extends T> binding) {
new LinkedBindingImpl<T>(injector, key, source, scopedFactory, scoping, linkedKey));
return true;
}

/** Handle ProviderMethods specially. */
private Boolean visitProviderMethod(ProviderMethod<T> provider) {
BindingImpl<T> binding =
ProviderMethod.createBinding(injector, key, provider, source, scoping);
scheduleInitialization(binding);
putBinding(binding);
return true;
}

@Override
public Boolean visit(UntargettedBinding<? extends T> untargetted) {
Expand Down
43 changes: 21 additions & 22 deletions core/src/com/google/inject/internal/Errors.java
Expand Up @@ -82,11 +82,10 @@
public final class Errors implements Serializable {

private static final Logger logger = Logger.getLogger(Guice.class.getName());

private static final Set<Dependency<?>> warnedDependencies =
Collections.newSetFromMap(new ConcurrentHashMap<Dependency<?>, Boolean>());


/**
* The root errors object. Used to access the list of error messages.
*/
Expand Down Expand Up @@ -150,7 +149,7 @@ public Errors withSource(Object source) {
public Errors missingImplementation(Key key) {
return addMessage("No implementation for %s was bound.", key);
}

public Errors jitDisabled(Key key) {
return addMessage("Explicit bindings are required and %s is not explicitly bound.", key);
}
Expand Down Expand Up @@ -296,7 +295,7 @@ public Errors duplicateBindingAnnotations(Member member,
return addMessage("%s has more than one annotation annotated with @BindingAnnotation: "
+ "%s and %s", member, a, b);
}

public Errors staticInjectionOnInterface(Class<?> clazz) {
return addMessage("%s is an interface, but interfaces have no static injection points.", clazz);
}
Expand Down Expand Up @@ -329,7 +328,7 @@ public Errors recursiveBinding() {
public Errors bindingAlreadySet(Key<?> key, Object source) {
return addMessage("A binding to %s was already configured at %s.", key, convert(source));
}

public Errors jitBindingAlreadySet(Key<?> key) {
return addMessage("A just-in-time binding to %s was already configured on a parent injector.", key);
}
Expand All @@ -354,7 +353,7 @@ public Errors childBindingAlreadySet(Key<?> key, Set<Object> sources) {

public Errors errorCheckingDuplicateBinding(Key<?> key, Object source, Throwable t) {
return addMessage(
"A binding to %s was already configured at %s and an error was thrown "
"A binding to %s was already configured at %s and an error was thrown "
+ "while checking duplicate bindings. Error: %s",
key, convert(source), t);
}
Expand All @@ -375,8 +374,8 @@ public Errors errorInjectingConstructor(Throwable cause) {
return errorInUserCode(cause, "Error injecting constructor, %s", cause);
}

public Errors errorInProvider(RuntimeException runtimeException) {
Throwable unwrapped = unwrap(runtimeException);
public Errors errorInProvider(Throwable cause) {
Throwable unwrapped = unwrap(cause);
return errorInUserCode(unwrapped, "Error in custom provider, %s", unwrapped);
}

Expand All @@ -395,11 +394,11 @@ public Errors errorNotifyingInjectionListener(
public Errors exposedButNotBound(Key<?> key) {
return addMessage("Could not expose() %s, it must be explicitly bound.", key);
}

public Errors keyNotFullySpecified(TypeLiteral<?> typeLiteral) {
return addMessage("%s cannot be used as a key; It is not fully specified.", typeLiteral);
}

public Errors errorEnhancingClass(Class<?> clazz, Throwable cause) {
return errorInUserCode(cause, "Unable to method intercept: %s", clazz);
}
Expand All @@ -425,8 +424,8 @@ public Errors errorInUserCode(Throwable cause, String messageFormat, Object... a
return addMessage(cause, messageFormat, arguments);
}
}

private Throwable unwrap(RuntimeException runtimeException) {
private Throwable unwrap(Throwable runtimeException) {
if(runtimeException instanceof Exceptions.UnhandledCheckedUserException) {
return runtimeException.getCause();
} else {
Expand Down Expand Up @@ -616,12 +615,12 @@ public static String format(String heading, Collection<Message> errorMessages) {
}

/**
* Returns {@code value} if it is non-null allowed to be null. Otherwise a message is added and
* Returns {@code value} if it is non-null or allowed to be null. Otherwise a message is added and
* an {@code ErrorsException} is thrown.
*/
public <T> T checkForNull(T value, Object source, Dependency<?> dependency)
throws ErrorsException {
if (value != null || dependency.isNullable() ) {
if (value != null || dependency.isNullable()) {
return value;
}

Expand Down Expand Up @@ -674,7 +673,7 @@ public static Throwable getOnlyCause(Collection<Message> messages) {
continue;
}

if (onlyCause != null
if (onlyCause != null
&& !ThrowableEquivalence.INSTANCE.equivalent(onlyCause, messageCause)) {
return null;
}
Expand Down Expand Up @@ -734,11 +733,11 @@ public static Object convert(Object o) {
ElementSource source = null;
if (o instanceof ElementSource) {
source = (ElementSource)o;
o = source.getDeclaringSource();
o = source.getDeclaringSource();
}
return convert(o, source);
}

public static Object convert(Object o, ElementSource source) {
for (Converter<?> converter : converters) {
if (converter.appliesTo(o)) {
Expand All @@ -747,7 +746,7 @@ public static Object convert(Object o, ElementSource source) {
}
return appendModules(o, source);
}

private static Object appendModules(Object source, ElementSource elementSource) {
String modules = moduleSourceString(elementSource);
if (modules.length() == 0) {
Expand All @@ -756,7 +755,7 @@ private static Object appendModules(Object source, ElementSource elementSource)
return source + modules;
}
}

private static String moduleSourceString(ElementSource elementSource) {
// if we only have one module (or don't know what they are), then don't bother
// reporting it, because the source already is going to report exactly that module.
Expand All @@ -772,7 +771,7 @@ private static String moduleSourceString(ElementSource elementSource) {
if (modules.size() <= 1) {
return "";
}

// Ideally we'd do:
// return Joiner.on(" -> ")
// .appendTo(new StringBuilder(" (via modules: "), Lists.reverse(modules))
Expand All @@ -783,7 +782,7 @@ private static String moduleSourceString(ElementSource elementSource) {
builder.append(modules.get(i));
if (i != 0) {
builder.append(" -> ");
}
}
}
builder.append(")");
return builder.toString();
Expand All @@ -797,7 +796,7 @@ public static void formatSource(Formatter formatter, Object source) {
}
formatSource(formatter, source, elementSource);
}

public static void formatSource(Formatter formatter, Object source, ElementSource elementSource) {
String modules = moduleSourceString(elementSource);
if (source instanceof Dependency) {
Expand Down

0 comments on commit ebd3b0b

Please sign in to comment.