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

com.google.gwt.inject.rebind.binding.BindProviderBinding Maven / Gradle / Ivy

Go to download

GIN (GWT INjection) brings automatic dependency injection to Google Web Toolkit client-side code. GIN is built on top of Guice and uses (a subset of) Guice's binding language.

There is a newer version: 2.1.2
Show newest version
/*
 * Copyright 2008 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.gwt.inject.rebind.binding;

import com.google.gwt.dev.util.Preconditions;
import com.google.gwt.inject.rebind.reflect.NoSourceNameException;
import com.google.gwt.inject.rebind.reflect.ReflectUtil;
import com.google.gwt.inject.rebind.util.InjectorMethod;
import com.google.gwt.inject.rebind.util.NameGenerator;
import com.google.gwt.inject.rebind.util.SourceSnippet;
import com.google.gwt.inject.rebind.util.SourceSnippetBuilder;
import com.google.gwt.inject.rebind.util.SourceSnippets;
import com.google.inject.Key;

import javax.inject.Provider;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * A binding to call the requested {@link com.google.inject.Provider}.
 */
public class BindProviderBinding extends AbstractBinding implements Binding {

  private final Key> providerKey;
  private final Key sourceKey;

  BindProviderBinding(Key> providerKey, Key sourceKey, Context context) {
    // In which package should we place the code to inject and use a
    // user-supplied provider, given that the provider might be in a different
    // package from the type it provides?
    //
    // This binding needs to be able to create and use a provider, which means
    // that the creator method needs to go in the provider's package.  If the
    // provided type (the source key) is in a different package, it has to be
    // visible in the provider's package; otherwise, the provider couldn't
    // return it from get().
    super(context, providerKey);

    this.providerKey = Preconditions.checkNotNull(providerKey);
    this.sourceKey = Preconditions.checkNotNull(sourceKey);
  }

  public SourceSnippet getCreationStatements(NameGenerator nameGenerator,
      List methodsOutput) throws NoSourceNameException {
    String providedType = ReflectUtil.getSourceName(sourceKey.getTypeLiteral());

    return new SourceSnippetBuilder()
        .append(providedType).append(" result = ").append(SourceSnippets.callGetter(providerKey))
        .append(".get();")
        .build();
  }

  public Collection getDependencies() {
    Context context = getContext();

    Collection dependencies = new ArrayList();
    dependencies.add(new Dependency(Dependency.GINJECTOR, sourceKey, context));
    dependencies.add(new Dependency(sourceKey, providerKey, context));
    return dependencies;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy