Ejemplo n.º 1
1
  protected void configure() {
    final Set<Class<?>> resources = new HashSet<>();
    final Map<Class<?>, Class<?>> resourcesWithContainers = new HashMap<>();
    injector
        .getAllBindings()
        .entrySet()
        .forEach(
            entry -> {
              final Key<?> key = entry.getKey();
              final Class<?> classObject = key.getTypeLiteral().getRawType();
              final Class<?> superClassObject = classObject.getSuperclass();
              if (classObject.isAnnotationPresent(Path.class)) {
                log.info("Loaded Resource " + classObject);
                resources.add(classObject);
              } else if (!Object.class.equals(classObject)
                  && !classObject.isInterface()
                  && superClassObject.isAnnotationPresent(Path.class)) {
                log.info("Loaded Resource " + superClassObject + " with Container " + classObject);
                resourcesWithContainers.put(superClassObject, classObject);
              }
            });

    for (final Class<?> resource : resources) {
      final Object container = injector.getInstance(resource);
      bind((Class) resource).toInstance(container);
      log.info("Binding Resource " + resource + " to Container " + container);
    }

    for (final Map.Entry<Class<?>, Class<?>> entry : resourcesWithContainers.entrySet()) {
      bindResourceToLocalContainer(entry.getKey(), entry.getValue());
      log.info("Binding Resource " + entry.getKey() + " to Container " + entry.getValue());
    }
  }
Ejemplo n.º 2
0
  private static Map<Key<?>, Object> checkForInvalidSeedsAndCopy(
      Map<Key<?>, Object> seededObjects) {
    for (Map.Entry<Key<?>, Object> entry : seededObjects.entrySet()) {
      Key<?> key = entry.getKey();
      Object value = entry.getValue();
      if (value == null) {
        throw new IllegalArgumentException("Seeded objects contains null key. Key: " + key);
      }
      Class<?> rawType = key.getTypeLiteral().getRawType();
      if (!rawType.isInstance(value)) {
        if (!(value instanceof Key)) {
          throw new IllegalArgumentException(
              "Seeded object is not instance of key type. " + "Key: " + key + ". Value: " + value);
        }
        Class<?> valueRawType = ((Key) value).getTypeLiteral().getRawType();
        if (!rawType.isAssignableFrom(valueRawType)) {
          throw new IllegalArgumentException(
              "Chained value key type does not extend the key type. "
                  + "Key Key: "
                  + key
                  + ". Value Key: "
                  + value);
        }
      }
    }

    return seededObjects
        .entrySet()
        .stream()
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  }
  private ImplicitProviderBinding(Key<?> providerKey, Key<?> targetKey) {
    super(Context.format("Implicit provider for %s", providerKey), targetKey);

    this.providerKey = Preconditions.checkNotNull(providerKey);
    this.providerType = (ParameterizedType) providerKey.getTypeLiteral().getType();
    this.targetKey = Preconditions.checkNotNull(targetKey);
  }
Ejemplo n.º 4
0
  private void scanBindings() throws IOException {
    for (Map.Entry<Key<?>, Binding<?>> e : globalInjector.getAllBindings().entrySet()) {
      Key<?> bindingKey = e.getKey();
      Binding<?> binding = e.getValue();
      TypeLiteral boundTypeLiteral = bindingKey.getTypeLiteral();
      Type boundType = boundTypeLiteral.getType();
      if (boundType instanceof Class) {
        final Class boundClass = (Class) boundType;
        for (Method method : boundClass.getMethods()) {
          if ((method.getModifiers() & Modifier.STATIC) == 0) {
            for (Annotation annotation : method.getAnnotations()) {
              if (annotation instanceof Path) {
                Path pathSpec = (Path) annotation;
                RouteSpec routeIn = Routes.parse(pathSpec.value(), true);
                endPointMethods.add(new EndPointMethod(routeIn, bindingKey, boundClass, method));
              }
            }
          }
        }

        if (MessageBodyReader.class.isAssignableFrom(boundClass)) {
          messageBodyReaders.add(0, (MessageBodyReader) globalInjector.getInstance(bindingKey));
        }
        if (MessageBodyWriter.class.isAssignableFrom(boundClass)) {
          messageBodyWriters.add(0, (MessageBodyWriter) globalInjector.getInstance(bindingKey));
        }
      }
    }
  }
 @Override
 public Set<Key<?>> getRootKeys(Injector injector) {
   Set<Key<?>> root = Sets.newHashSet();
   for (Key<?> key : injector.getBindings().keySet()) {
     if (key.getTypeLiteral().getRawType().getPackage() != Guice.class.getPackage()
         && !loggerKey.equals(key)) {
       root.add(key);
     }
   }
   return root;
 }
Ejemplo n.º 6
0
  @Inject
  public RowKeyListenerProvider(Injector injector) {
    Map<Key<?>, Binding<?>> bindings = injector.getAllBindings();

    for (Key<?> key : bindings.keySet()) {
      Class<?> bindingClass = key.getTypeLiteral().getRawType();
      if (RowKeyListener.class.isAssignableFrom(bindingClass)) {
        RowKeyListener listener = (RowKeyListener) injector.getInstance(bindingClass);
        m_listeners.add(listener);
      }
    }
  }
Ejemplo n.º 7
0
 @SuppressWarnings("unchecked")
 @Inject
 void initialize(Injector inj) {
   for (Key<?> k : inj.getBindings().keySet()) {
     if (k.getTypeLiteral().getRawType().isAssignableFrom(Operator.class)) {
       Key<? extends Operator<?>> key = (Key<? extends Operator<?>>) k;
       log.debug("registering operator " + key);
       // registerOperator(inj.getInstance(ko));
     }
     if (k.getTypeLiteral().getRawType().isAssignableFrom(OperatorFactory.class)) {
       Key<? extends OperatorFactory> key = (Key<OperatorFactory>) k;
       ParameterizedType type = (ParameterizedType) key.getTypeLiteral().getType();
       log.debug("got operator with {} on {}.", key, type);
       if (Layer.class.isAssignableFrom((Class<?>) type.getActualTypeArguments()[0])) {
         log.debug("registering layer operator {}.", key);
         layOpFac.add(inj.getInstance(key));
       }
       if (Entity.class.isAssignableFrom((Class<?>) type.getActualTypeArguments()[0])) {
         log.debug("registering entity operator {}.", key);
         entOpFac.add(inj.getInstance(key));
       }
     }
   }
 }
  public SourceSnippet getCreationStatements(
      NameGenerator nameGenerator, List<InjectorMethod> methodsOutput)
      throws NoSourceNameException {
    String providerTypeName = ReflectUtil.getSourceName(providerType);
    String targetKeyName = ReflectUtil.getSourceName(targetKey.getTypeLiteral());

    return new SourceSnippetBuilder()
        .append(providerTypeName)
        .append(" result = new ")
        .append(providerTypeName)
        .append("() { \n")
        .append("  public ")
        .append(targetKeyName)
        .append(" get() { \n")
        .append("    return ")
        .append(SourceSnippets.callGetter(targetKey))
        .append(";\n")
        .append("  }\n")
        .append("};")
        .build();
  }
Ejemplo n.º 9
0
  private static void bindHierarchy(final Binder binder, final Key rootKey) {
    final Class root = rootKey.getTypeLiteral().getRawType();

    Annotation qualifier = rootKey.getAnnotation();
    if (null == qualifier) {
      qualifier = Names.named(root.getName());
    }

    for (Class clazz = root; clazz != Object.class; clazz = clazz.getSuperclass()) {
      if (clazz != root && ExtensionPoint.class.isAssignableFrom(clazz)) {
        binder.bind(clazz).annotatedWith(qualifier).to(rootKey);
      }

      if (Descriptor.class.isAssignableFrom(clazz)) {
        binder.bind(Descriptor.class).annotatedWith(qualifier).to(rootKey);
      }

      for (final Class<?> iface : clazz.getInterfaces()) {
        if (ExtensionPoint.class.isAssignableFrom(iface)) {
          binder.bind(iface).annotatedWith(qualifier).to(rootKey);
        }
      }
    }
  }
Ejemplo n.º 10
0
 public InjectorMatcher(Key<T> typeKey, Matcher<?> instanceMatcher) {
   this.typeKey = typeKey;
   this.instanceMatcher = instanceMatcher;
   this.expectedType = typeKey.getTypeLiteral().getRawType();
 }
Ejemplo n.º 11
0
 public static <T> Matcher<? super Injector> hasInstance(Key<T> instanceType) {
   return new InjectorMatcher(
       instanceType, notNullValue(instanceType.getTypeLiteral().getRawType()));
 }
Ejemplo n.º 12
0
 @Override
 public <T> LinkedBindingBuilder<T> bind(Key<T> key) {
   warnOnSpecialized(key.getTypeLiteral().getRawType());
   return binder.withSource(getBindingLocation()).bind(key);
 }