当前位置: 首页>>代码示例>>Java>>正文


Java Dependency.getKey方法代码示例

本文整理汇总了Java中com.google.inject.spi.Dependency.getKey方法的典型用法代码示例。如果您正苦于以下问题:Java Dependency.getKey方法的具体用法?Java Dependency.getKey怎么用?Java Dependency.getKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.inject.spi.Dependency的用法示例。


在下文中一共展示了Dependency.getKey方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: newPassType

import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
public static PassType newPassType(String name, Class<? extends Pass> cls)
{
    ImmutableList.Builder<TypeLiteral<?>> inputs = ImmutableList.builder();
    ImmutableList.Builder<TypeLiteral<?>> outputs = ImmutableList.builder();
    InjectionPoint passCtor = InjectionPoint.forConstructorOf(cls);
    for (Dependency<?> dep : passCtor.getDependencies()) {
        Key key = dep.getKey();
        if (key.getAnnotation() instanceof PassInput) {
            inputs.add(key.getTypeLiteral());
        }
        else if (key.getAnnotation() instanceof PassOutput) {
            checkState(key.getTypeLiteral().getRawType() == Consumer.class);
            ParameterizedType parameterized = (ParameterizedType) key.getTypeLiteral().getType();
            java.lang.reflect.Type outputType = parameterized.getActualTypeArguments()[0];
            outputs.add(TypeLiteral.get(outputType));
        }
    }
    return new PassType(name, cls, inputs.build(), outputs.build());
}
 
开发者ID:wrmsr,项目名称:wava,代码行数:20,代码来源:PassType.java

示例2: analyzeDependencies

import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
private void analyzeDependencies(final Collection<Dependency<?>> dependencies)
{
	for( final Dependency<?> d : dependencies )
	{
		final Key<?> key = d.getKey();
		InjectionPoint injectionPoint = d.getInjectionPoint();
		if( injectionPoint != null && injectionPoint.isOptional() )
		{
			continue;
		}
		if( key.getAnnotationType() == Assisted.class )
		{
			continue;
		}
		TypeLiteral<?> typeLiteral = key.getTypeLiteral();
		Class<?> rawType = typeLiteral.getRawType();
		if( rawType == Injector.class )
		{
			continue;
		}
		if( rawType == MembersInjector.class )
		{
			Key<?> injectedKey = key
				.ofType(((ParameterizedType) typeLiteral.getType()).getActualTypeArguments()[0]);
			dependentKeys.add(injectedKey);
			analyzeImplementation(injectedKey.getTypeLiteral(), true);
		}
		else if( rawType == Provider.class )
		{
			dependentKeys.add(key.ofType(((ParameterizedType) typeLiteral.getType()).getActualTypeArguments()[0]));
		}
		else
		{
			dependentKeys.add(key);
		}
	}
}
 
开发者ID:equella,项目名称:Equella,代码行数:38,代码来源:DependencyAnalyzer.java

示例3: cleanup

import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
/**
 * Iterates through the binding's dependencies to clean up any stray bindings that were leftover
 * from a failed JIT binding. This is required because the bindings are eagerly & optimistically
 * added to allow circular dependency support, so dependencies may pass where they should have
 * failed.
 */
private boolean cleanup(BindingImpl<?> binding, Set<Key> encountered) {
  boolean bindingFailed = false;
  Set<Dependency<?>> deps = getInternalDependencies(binding);
  for (Dependency dep : deps) {
    Key<?> depKey = dep.getKey();
    InjectionPoint ip = dep.getInjectionPoint();
    if (encountered.add(depKey)) { // only check if we haven't looked at this key yet
      BindingImpl depBinding = jitBindings.get(depKey);
      if (depBinding != null) { // if the binding still exists, validate
        boolean failed = cleanup(depBinding, encountered); // if children fail, we fail
        if (depBinding instanceof ConstructorBindingImpl) {
          ConstructorBindingImpl ctorBinding = (ConstructorBindingImpl) depBinding;
          ip = ctorBinding.getInternalConstructor();
          if (!ctorBinding.isInitialized()) {
            failed = true;
          }
        }
        if (failed) {
          removeFailedJitBinding(depBinding, ip);
          bindingFailed = true;
        }
      } else if (state.getExplicitBinding(depKey) == null) {
        // ignore keys if they were explicitly bound, but if neither JIT
        // nor explicit, it's also invalid & should let parent know.
        bindingFailed = true;
      }
    }
  }
  return bindingFailed;
}
 
开发者ID:google,项目名称:guice,代码行数:37,代码来源:InjectorImpl.java

示例4: getProviderOrThrow

import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
<T> Provider<T> getProviderOrThrow(final Dependency<T> dependency, Errors errors)
    throws ErrorsException {
  Key<T> key = dependency.getKey();
  BindingImpl<? extends T> binding = getBindingOrThrow(key, errors, JitLimitation.NO_JIT);
  final InternalFactory<? extends T> internalFactory = binding.getInternalFactory();
  final Object source = binding.getSource();

  return new Provider<T>() {
    @Override
    public T get() {
      InternalContext currentContext = enterContext();
      Dependency previous = currentContext.pushDependency(dependency, source);
      try {
        T t = internalFactory.get(currentContext, dependency, false);
        return t;
      } catch (InternalProvisionException e) {
        throw e.addSource(dependency).toProvisionException();
      } finally {
        currentContext.popStateAndSetDependency(previous);
        currentContext.close();
      }
    }

    @Override
    public String toString() {
      return internalFactory.toString();
    }
  };
}
 
开发者ID:google,项目名称:guice,代码行数:30,代码来源:InjectorImpl.java

示例5: testMultibinderDependencies

import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
/** Check that the dependencies are correct. */
public void testMultibinderDependencies() {
  Injector injector =
      Guice.createInjector(
          new AbstractModule() {
            @Override
            protected void configure() {
              MapBinder<Integer, String> mapBinder =
                  MapBinder.newMapBinder(binder(), Integer.class, String.class);
              mapBinder.addBinding(1).toInstance("A");
              mapBinder.addBinding(2).to(Key.get(String.class, Names.named("b")));

              bindConstant().annotatedWith(Names.named("b")).to("B");
            }
          });

  Binding<Map<Integer, String>> binding = injector.getBinding(new Key<Map<Integer, String>>() {});
  HasDependencies withDependencies = (HasDependencies) binding;
  Set<Dependency<?>> actualDependencies = withDependencies.getDependencies();

  // We expect two dependencies, because the dependencies are annotated with
  // Element, which has a uniqueId, it's difficult to directly compare them.
  // Instead we will manually compare all the fields except the uniqueId
  assertEquals(2, actualDependencies.size());
  for (Dependency<?> dependency : actualDependencies) {
    Key<?> key = dependency.getKey();
    assertEquals(new TypeLiteral<String>() {}, key.getTypeLiteral());
    Annotation annotation = dependency.getKey().getAnnotation();
    assertTrue(annotation instanceof Element);
    Element element = (Element) annotation;
    assertEquals("", element.setName());
    assertEquals(Element.Type.MAPBINDER, element.type());
    assertEquals("java.lang.Integer", element.keyType());
  }

  Set<String> elements = Sets.newHashSet();
  elements.addAll(recurseForDependencies(injector, withDependencies));
  assertEquals(ImmutableSet.of("A", "B"), elements);
}
 
开发者ID:google,项目名称:guice,代码行数:40,代码来源:MapBinderTest.java

示例6: testMultibinderDependenciesInToolStage

import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
/** Check that the dependencies are correct in the Tool Stage. */
public void testMultibinderDependenciesInToolStage() {
  Injector injector =
      Guice.createInjector(
          Stage.TOOL,
          new AbstractModule() {
            @Override
            protected void configure() {
              MapBinder<Integer, String> mapBinder =
                  MapBinder.newMapBinder(binder(), Integer.class, String.class);
              mapBinder.addBinding(1).toInstance("A");
              mapBinder.addBinding(2).to(Key.get(String.class, Names.named("b")));

              bindConstant().annotatedWith(Names.named("b")).to("B");
            }
          });

  Binding<Map<Integer, String>> binding = injector.getBinding(new Key<Map<Integer, String>>() {});
  HasDependencies withDependencies = (HasDependencies) binding;
  Set<Dependency<?>> actualDependencies = withDependencies.getDependencies();

  // We expect two dependencies, because the dependencies are annotated with
  // Element, which has a uniqueId, it's difficult to directly compare them.
  // Instead we will manually compare all the fields except the uniqueId
  assertEquals(2, actualDependencies.size());
  for (Dependency<?> dependency : actualDependencies) {
    Key<?> key = dependency.getKey();
    assertEquals(new TypeLiteral<String>() {}, key.getTypeLiteral());
    Annotation annotation = dependency.getKey().getAnnotation();
    assertTrue(annotation instanceof Element);
    Element element = (Element) annotation;
    assertEquals("", element.setName());
    assertEquals(Element.Type.MAPBINDER, element.type());
    assertEquals("java.lang.Integer", element.keyType());
  }
}
 
开发者ID:google,项目名称:guice,代码行数:37,代码来源:MapBinderTest.java

示例7: testMultibindingProviderDependencies

import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
public void testMultibindingProviderDependencies() {
  final Annotation setAnn = Names.named("foo");
  Injector injector =
      Guice.createInjector(
          new AbstractModule() {
            @Override
            protected void configure() {
              Multibinder<String> multibinder =
                  Multibinder.newSetBinder(binder(), String.class, setAnn);
              multibinder.addBinding().toInstance("a");
              multibinder.addBinding().toInstance("b");
            }
          });
  HasDependencies providerBinding =
      (HasDependencies) injector.getBinding(new Key<Collection<Provider<String>>>(setAnn) {});
  HasDependencies setBinding =
      (HasDependencies) injector.getBinding(new Key<Set<String>>(setAnn) {});
  // sanity check the size
  assertEquals(setBinding.getDependencies().toString(), 2, setBinding.getDependencies().size());
  Set<Dependency<?>> expected = Sets.newHashSet();
  for (Dependency<?> dep : setBinding.getDependencies()) {
    Key key = dep.getKey();
    Dependency<?> providerDependency =
        Dependency.get(key.ofType(Types.providerOf(key.getTypeLiteral().getType())));
    expected.add(providerDependency);
  }
  assertEquals(expected, providerBinding.getDependencies());
}
 
开发者ID:google,项目名称:guice,代码行数:29,代码来源:MultibinderTest.java

示例8: cleanup

import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
/**
 * Iterates through the binding's dependencies to clean up any stray bindings that were leftover
 * from a failed JIT binding. This is required because the bindings are eagerly &
 * optimistically added to allow circular dependency support, so dependencies may pass where they
 * should have failed.
 */
private boolean cleanup(BindingImpl<?> binding, Set<Key> encountered) {
  boolean bindingFailed = false;
  Set<Dependency<?>> deps = getInternalDependencies(binding);
  for(Dependency dep : deps) {
    Key<?> depKey = dep.getKey();
    InjectionPoint ip = dep.getInjectionPoint();
    if(encountered.add(depKey)) { // only check if we haven't looked at this key yet
      BindingImpl depBinding = jitBindings.get(depKey);
      if(depBinding != null) { // if the binding still exists, validate
        boolean failed = cleanup(depBinding, encountered); // if children fail, we fail
        if(depBinding instanceof ConstructorBindingImpl) {
          ConstructorBindingImpl ctorBinding = (ConstructorBindingImpl)depBinding;
          ip = ctorBinding.getInternalConstructor();
          if(!ctorBinding.isInitialized()) {
            failed = true;
          }
        }
        if(failed) {
          removeFailedJitBinding(depBinding, ip);
          bindingFailed = true;
        }
      } else if(state.getExplicitBinding(depKey) == null) {
        // ignore keys if they were explicitly bound, but if neither JIT
        // nor explicit, it's also invalid & should let parent know.
        bindingFailed = true;
      }
    }
  }
  return bindingFailed;
}
 
开发者ID:cgruber,项目名称:guice-old,代码行数:37,代码来源:InjectorImpl.java

示例9: apply

import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
@Override
public Key<?> apply(Dependency<?> from) {
  return from.getKey();
}
 
开发者ID:google,项目名称:guice,代码行数:5,代码来源:CheckedProviderTest.java

示例10: apply

import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
public Key<?> apply(Dependency<?> from) {
  return from.getKey();
}
 
开发者ID:cgruber,项目名称:guice-old,代码行数:4,代码来源:CheckedProviderTest.java


注:本文中的com.google.inject.spi.Dependency.getKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。