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

com.google.inject.EncounterImpl Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
/**
 * Copyright (C) 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.inject;

import com.google.inject.internal.Errors;
import com.google.inject.internal.ImmutableList;
import com.google.inject.internal.Lists;
import static com.google.inject.internal.Preconditions.checkState;
import com.google.inject.matcher.Matcher;
import com.google.inject.matcher.Matchers;
import com.google.inject.spi.InjectionListener;
import com.google.inject.spi.Message;
import com.google.inject.spi.TypeEncounter;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @author [email protected] (Jesse Wilson)
 */
final class EncounterImpl implements TypeEncounter {

  private final Errors errors;
  private final Lookups lookups;
  private List> membersInjectors; // lazy
  private List> injectionListeners; // lazy
  


  private boolean valid = true;

  public EncounterImpl(Errors errors, Lookups lookups) {
    this.errors = errors;
    this.lookups = lookups;
  }

  public void invalidate() {
    valid = false;
  }

  



















  public ImmutableList> getMembersInjectors() {
    return membersInjectors == null
        ? ImmutableList.>of()
        : ImmutableList.copyOf(membersInjectors);
  }

  public ImmutableList> getInjectionListeners() {
    return injectionListeners == null
        ? ImmutableList.>of()
        : ImmutableList.copyOf(injectionListeners);
  }

  public void register(MembersInjector membersInjector) {
    checkState(valid, "Encounters may not be used after hear() returns.");

    if (membersInjectors == null) {
      membersInjectors = Lists.newArrayList();
    }

    membersInjectors.add(membersInjector);
  }

  public void register(InjectionListener injectionListener) {
    checkState(valid, "Encounters may not be used after hear() returns.");

    if (injectionListeners == null) {
      injectionListeners = Lists.newArrayList();
    }

    injectionListeners.add(injectionListener);
  }

  public void addError(String message, Object... arguments) {
    checkState(valid, "Encounters may not be used after hear() returns.");
    errors.addMessage(message, arguments);
  }

  public void addError(Throwable t) {
    checkState(valid, "Encounters may not be used after hear() returns.");
    errors.errorInUserCode(t, "An exception was caught and reported. Message: %s", t.getMessage());
  }

  public void addError(Message message) {
    checkState(valid, "Encounters may not be used after hear() returns.");
    errors.addMessage(message);
  }

  public  Provider getProvider(Key key) {
    checkState(valid, "Encounters may not be used after hear() returns.");
    return lookups.getProvider(key);
  }

  public  Provider getProvider(Class type) {
    return getProvider(Key.get(type));
  }

  public  MembersInjector getMembersInjector(TypeLiteral typeLiteral) {
    checkState(valid, "Encounters may not be used after hear() returns.");
    return lookups.getMembersInjector(typeLiteral);
  }

  public  MembersInjector getMembersInjector(Class type) {
    return getMembersInjector(TypeLiteral.get(type));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy