All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.inject.spi.Message Maven / Gradle / Ivy

package com.google.inject.spi;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.inject.Binder;
import com.google.inject.internal.Messages;
import com.google.inject.internal.util.SourceProvider;

import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * An error message and the context in which it occured. Messages are usually created internally by
 * Guice and its extensions. Messages can be created explicitly in a module using {@link
 * com.google.inject.Binder#addError(Throwable) addError()} statements:
 * 
 *     try {
 *       bindPropertiesFromFile();
 *     } catch (IOException e) {
 *       addError(e);
 *     }
* */ public final class Message implements Element { private final String message; private final Throwable cause; private final List sources; public Message(List sources, String message, Throwable cause) { this.sources = ImmutableList.copyOf(sources); this.message = checkNotNull(message, "message"); this.cause = cause; } public Message(String message, Throwable cause) { this(ImmutableList.of(), message, cause); } public Message(Object source, String message) { this(ImmutableList.of(source), message, null); } public Message(String message) { this(ImmutableList.of(), message, null); } @Override public String getSource() { return sources.isEmpty() ? SourceProvider.UNKNOWN_SOURCE.toString() : Messages.convert(sources.get(sources.size() - 1)).toString(); } public List getSources() { return sources; } /** * Gets the error message text. */ public String getMessage() { return message; } @Override public T acceptVisitor(ElementVisitor visitor) { return visitor.visit(this); } /** * Returns the throwable that caused this message, or {@code null} if this * message was not caused by a throwable. */ public Throwable getCause() { return cause; } @Override public String toString() { return message; } @Override public int hashCode() { return Objects.hashCode(message, cause, sources); } @Override public boolean equals(Object o) { if (!(o instanceof Message)) { return false; } Message e = (Message) o; return Objects.equal(message, e.message) && Objects.equal(cause, e.cause) && Objects.equal(sources, e.sources); } @Override public void applyTo(Binder binder) { binder.withSource(getSource()).addError(this); } }