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


Java DefaultBindingTargetVisitor类代码示例

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


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

示例1: testGetInstanceName_providerMethod

import com.google.inject.spi.DefaultBindingTargetVisitor; //导入依赖的package包/类
public void testGetInstanceName_providerMethod() throws Exception {
  final ProviderMethod<?>[] methodHolder = new ProviderMethod[1];

  Injector injector = Guice.createInjector(new ProvidingModule());
  injector
      .getBinding(Integer.class)
      .acceptTargetVisitor(
          new DefaultBindingTargetVisitor<Object, Void>() {
            @SuppressWarnings("unchecked")
            @Override
            public Void visit(ProviderInstanceBinding<?> binding) {
              methodHolder[0] = (ProviderMethod) binding.getUserSuppliedProvider();
              return null;
            }
          });

  assertEquals(
      "Method provider should pretty print as the method signature",
      "#provideInteger(String)",
      nameFactory.getInstanceName(methodHolder[0]));
}
 
开发者ID:google,项目名称:guice,代码行数:22,代码来源:ShortNameFactoryTest.java

示例2: testGetInstanceName_providerMethod

import com.google.inject.spi.DefaultBindingTargetVisitor; //导入依赖的package包/类
public void testGetInstanceName_providerMethod() throws Exception {
  final ProviderMethod<?>[] methodHolder = new ProviderMethod[1];
  
  Injector injector = Guice.createInjector(new ProvidingModule());
  injector.getBinding(Integer.class).acceptTargetVisitor(
      new DefaultBindingTargetVisitor<Object, Void>() {
        @SuppressWarnings("unchecked") @Override
        public Void visit(ProviderInstanceBinding<?> binding) {
          methodHolder[0] = (ProviderMethod) binding.getUserSuppliedProvider();
          return null;
        }
      });
  
  assertEquals("Method provider should pretty print as the method signature",
      "#provideInteger(String)", nameFactory.getInstanceName(methodHolder[0]));
}
 
开发者ID:cgruber,项目名称:guice-old,代码行数:17,代码来源:ShortNameFactoryTest.java

示例3: testGetInstanceName_providerMethod

import com.google.inject.spi.DefaultBindingTargetVisitor; //导入依赖的package包/类
public void testGetInstanceName_providerMethod() throws Exception {
  final ProviderMethod<?>[] methodHolder = new ProviderMethod[1];
  
  Injector injector = Guice.createInjector(new ProvidingModule());
  injector.getBinding(Integer.class).acceptTargetVisitor(
      new DefaultBindingTargetVisitor<Object, Void>() {
        @SuppressWarnings("unchecked") @Override
        public Void visit(ProviderInstanceBinding<?> binding) {
          methodHolder[0] = (ProviderMethod) binding.getProviderInstance();
          return null;
        }
      });
  
  assertEquals("Method provider should pretty print as the method signature",
      "#provideInteger(String)", nameFactory.getInstanceName(methodHolder[0]));
}
 
开发者ID:utopiazh,项目名称:google-guice,代码行数:17,代码来源:ShortNameFactoryTest.java

示例4: visit

import com.google.inject.spi.DefaultBindingTargetVisitor; //导入依赖的package包/类
@Override
public <T> Void visit(Binding<T> binding) {
  final Key<T> key = binding.getKey();

  if (!keysToIntercept.contains(key)) {
    return super.visit(binding);
  }

  binding.acceptTargetVisitor(new DefaultBindingTargetVisitor<T, Void>() {
    @Override
    public Void visit(UntargettedBinding<? extends T> untargettedBinding) {
      binder.addError("Cannot intercept bare binding of %s. " +
          		"You may only intercept bindings that bind a class to something.", key);
      return null;
    }
  });

  Key<T> anonymousKey = Key.get(key.getTypeLiteral(), UniqueAnnotations.create());
  binder.bind(key).toProvider(new InterceptingProvider<T>(key, binder.getProvider(anonymousKey)));

  ScopedBindingBuilder scopedBindingBuilder = bindKeyToTarget(binding, binder, anonymousKey);

  // we scope the user's provider, not the interceptor. This is dangerous,
  // but convenient. It means that although the user's provider will live
  // in its proper scope, the intereptor gets invoked without a scope
  applyScoping(binding, scopedBindingBuilder);

  keysIntercepted.add(key);
  return null;
}
 
开发者ID:zorzella,项目名称:guiceberry,代码行数:31,代码来源:InterceptingBindingsBuilder.java

示例5: visit

import com.google.inject.spi.DefaultBindingTargetVisitor; //导入依赖的package包/类
@Override
public <T> Void visit(Binding<T> binding) {
  final Key<T> key = binding.getKey();

  if (!keysToIntercept.contains(key)) {
    return super.visit(binding);
  }

  binding.acceptTargetVisitor(new DefaultBindingTargetVisitor<T, Void>() {
    @Override
    public Void visit(UntargettedBinding<? extends T> untargettedBinding) {
      binder.addError("Cannot intercept bare binding of %s. " +
          		"You may only intercept bindings that bind a class to something.", key);
      return null;
    }
  });

  Key<T> anonymousKey = Key.get(key.getTypeLiteral(), UniqueAnnotations.create());
  binder.bind(key).toProvider(new InterceptingProvider<T>(key, binder.getProvider(anonymousKey)));

  ScopedBindingBuilder scopedBindingBuilder = bindKeyToTarget(binding, binder, anonymousKey);

  // we scope the user's provider, not the interceptor. This is dangerous,
  // but convenient. It means that although the user's provider will live
  // in its proper scope, the interceptor gets invoked without a scope
  applyScoping(binding, scopedBindingBuilder);

  keysIntercepted.add(key);
  return null;
}
 
开发者ID:zorzella,项目名称:guiceberry,代码行数:31,代码来源:InterceptingBindingsBuilder.java

示例6: getBindings

import com.google.inject.spi.DefaultBindingTargetVisitor; //导入依赖的package包/类
private Module getBindings(LogInject<_Logger_> logInject, Class<_Logger_> loggerClass)
{
    TypeLiteral<_Logger_> loggerType = TypeLiteral.get(loggerClass);
    GuiceLoggerProvider<_Logger_> provider = new GuiceLoggerProvider<>();
    Predicate<Dependency<?>> matchesLogger = dependency -> loggerType.equals(dependency.getKey().getTypeLiteral());
    return new AbstractModule()
    {
        @Override
        protected void configure()
        {
            ProvisionListener provisionListener = new ProvisionListener()
            {
                @Override
                public <_Target_> void onProvision(ProvisionInvocation<_Target_> provision)
                {
                    Binding<_Target_> binding = provision.getBinding();
                    if (loggerType.equals(binding.getKey().getTypeLiteral()))
                    {
                        Dependency<?> loggerDependency;
                        Stream<Dependency<?>> stream;
                        stream = provision.getDependencyChain().stream().map(DependencyAndSource::getDependency);
                        Iterator<Dependency<?>> dependencies = reverse(stream.collect(toList())).iterator();
                        if (dependencies.hasNext() && matchesLogger.test(loggerDependency = dependencies.next()))
                        {
                            InjectionPoint injectionPoint = loggerDependency.getInjectionPoint();
                            TypeLiteral<?> declaringType = injectionPoint.getDeclaringType();
                            TypeLiteral<?> targetType = null;
                            if (dependencies.hasNext())
                            {
                                TypeLiteral<?> typeLiteral = dependencies.next().getKey().getTypeLiteral();
                                if (declaringType.getRawType().isAssignableFrom(typeLiteral.getRawType()))
                                {
                                    targetType = typeLiteral;
                                }
                            }
                            Class<?> logger = (targetType != null? targetType:declaringType).getRawType();
                            BindingTargetVisitor<_Target_, Void> bindingTargetVisitor;
                            bindingTargetVisitor = new DefaultBindingTargetVisitor<_Target_, Void>()
                            {
                                @Override
                                public Void visit(ProviderInstanceBinding<? extends _Target_> instanceBinding)
                                {
                                    if (provider.equals(instanceBinding.getUserSuppliedProvider()))
                                    {
                                        provider.setLogger(logInject.createLogger(logger));
                                    }
                                    return null;
                                }
                            };
                            binding.acceptTargetVisitor(bindingTargetVisitor);
                        }
                    }
                }
            };
            bind(loggerClass).toProvider(provider);
            bindListener(Matchers.any(), provisionListener);
        }
    };
}
 
开发者ID:raner,项目名称:loginject,代码行数:60,代码来源:GuiceLogInjectionService.java


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