Java Code Examples for com.google.inject.spi.TypeEncounter#register()

The following examples show how to use com.google.inject.spi.TypeEncounter#register() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LifecycleModule.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
private <I> void invokePostConstruct(final TypeEncounter<I> encounter, final Method postConstruct) {
    encounter.register(new InjectionListener<I>() {
        @Override
        public void afterInjection(final I injectee) {
            try {
                postConstruct.setAccessible(true);
                postConstruct.invoke(injectee);
            } catch (final IllegalAccessException | InvocationTargetException e) {
                if (e.getCause() instanceof UnrecoverableException) {
                    if (((UnrecoverableException) e.getCause()).isShowException()) {
                        log.error("An unrecoverable Exception occurred. Exiting HiveMQ", e);
                    }
                    System.exit(1);
                }
                throw new ProvisionException("An error occurred while calling @PostConstruct", e);
            }
        }
    });
}
 
Example 2
Source File: LoggingTypeListener.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public <T> void hear(TypeLiteral<T> typeLiteral, TypeEncounter<T> typeEncounter) {
    Set<Field> fields = new HashSet<>();
    for (Class<?> c = typeLiteral.getRawType(); c != Object.class; c = c.getSuperclass()) {
        for (Field field : c.getDeclaredFields()) {
            if (field.isAnnotationPresent(Logging.class)) {
                if (field.getType() == Logger.class) {
                    fields.add(field);
                } else {
                    throw SeedException.createNew(CoreErrorCode.BAD_LOGGER_TYPE)
                            .put("field", format("%s.%s", field.getDeclaringClass().getName(), field.getName()))
                            .put("expectedType", Logger.class.getName())
                            .put("givenType", field.getType().getName());
                }
            }
        }
    }

    if (!fields.isEmpty()) {
        typeEncounter.register(new LoggingMembersInjector<>(fields));
    }
}
 
Example 3
Source File: ConfigurationTypeListener.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public <T> void hear(TypeLiteral<T> typeLiteral, TypeEncounter<T> typeEncounter) {
    Set<ConfigurationMembersInjector.ConfigurableField> fields = new HashSet<>();
    for (Class<?> c = typeLiteral.getRawType(); c != Object.class; c = c.getSuperclass()) {
        for (Field field : c.getDeclaredFields()) {
            Configuration annotation = field.getAnnotation(Configuration.class);
            if (annotation != null) {
                fields.add(new ConfigurationMembersInjector.ConfigurableField(field, annotation));
            }
        }
    }

    if (!fields.isEmpty()) {
        typeEncounter.register(new ConfigurationMembersInjector<>(configuration, fields));
    }
}
 
Example 4
Source File: CliTypeListener.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public <T> void hear(TypeLiteral<T> typeLiteral, TypeEncounter<T> typeEncounter) {
    Set<Field> fields = new HashSet<>();
    CliCommand cliCommand = null;
    for (Class<?> c = typeLiteral.getRawType(); c != Object.class; c = c.getSuperclass()) {
        if (cliCommand == null) {
            cliCommand = c.getAnnotation(CliCommand.class);
        } else if (c.isAnnotationPresent(CliCommand.class)) {
            throw SeedException.createNew(CliErrorCode.CONFLICTING_COMMAND_ANNOTATIONS)
                    .put("class", c.getCanonicalName());
        }
        Arrays.stream(c.getDeclaredFields()).filter(this::isCandidate).forEach(fields::add);
    }

    if (!fields.isEmpty()) {
        typeEncounter.register(new CliMembersInjector<>(
                cliContext,
                cliCommand == null ? typeLiteral.getType().getTypeName() : cliCommand.value(),
                fields)
        );
    }
}
 
Example 5
Source File: LifecycleModule.java    From hivemq-community-edition with Apache License 2.0 5 votes vote down vote up
private <I> void addPreDestroyToRegistry(final TypeEncounter<I> encounter, final Method preDestroy, final LifecycleRegistry registry) {
    encounter.register(new InjectionListener<I>() {
        @Override
        public void afterInjection(final I injectee) {
            registry.addPreDestroyMethod(preDestroy, injectee);

        }
    });
}
 
Example 6
Source File: InjectionLogger.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
    logger.info("Encountered type " + type);
    encounter.register((InjectionListener<I>) injectee -> {
        logger.info("Injected " + injectee);
    });
}
 
Example 7
Source File: Slf4jTypeListener.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public <I> void hear(TypeLiteral<I> iTypeLiteral, TypeEncounter<I> iTypeEncounter) {
    for (Field field : iTypeLiteral.getRawType().getDeclaredFields()) {
        if (field.getType() == Logger.class
            && field.isAnnotationPresent(InjectLogger.class)) {
            iTypeEncounter.register(new Slf4jMembersInjector<I>(field));
        }
    }
}
 
Example 8
Source File: PageObjectTypeListener.java    From bobcat with Apache License 2.0 5 votes vote down vote up
/**
 * Automatically called by Guice during injection of any object. Checks if the injected object's type is
 * annotated as PageObject. If yes, it will register PageObjectInjectorListener in the TypeEncounter, so
 * that PageObjectInjectorListener will be able to perform custom injections.
 */
@Override
public <I> void hear(TypeLiteral<I> typeLiteral, final TypeEncounter<I> typeEncounter) {
  if (typeLiteral.getRawType().isAnnotationPresent(PageObject.class)) {
    typeEncounter.register(new PageObjectInjectorListener(typeEncounter));
  }
}
 
Example 9
Source File: InjectConfigTest.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
public <T> void hear(TypeLiteral<T> typeLiteral, TypeEncounter<T> typeEncounter)
{
  for (Class<?> c = typeLiteral.getRawType(); c != Object.class; c = c.getSuperclass()) {
    LOG.debug("Inspecting fields for " + c);
    for (Field field : c.getDeclaredFields()) {
      if (field.isAnnotationPresent(InjectConfig.class)) {
        typeEncounter.register(new ConfigurationInjector<T>(field, field.getAnnotation(InjectConfig.class)));
      }
    }
  }
}
 
Example 10
Source File: SeleniumClassModule.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
  Class<?> clazz = type.getRawType();
  for (Field field : clazz.getDeclaredFields()) {
    if (field.getType() == TestOrganization.class
        && field.isAnnotationPresent(InjectTestOrganization.class)) {
      encounter.register(
          new TestOrganizationInjector<>(
              field, field.getAnnotation(InjectTestOrganization.class), injectorProvider));
    }
  }
}
 
Example 11
Source File: LogModule.java    From herald with Apache License 2.0 5 votes vote down vote up
private TypeListener createTypeListener() {
    return new TypeListener() {
        @Override
        public <I> void hear(final TypeLiteral<I> typeLiteral, final TypeEncounter<I> typeEncounter) {
            typeEncounter.register((InjectionListener<I>) LoggerInjector::inject);
        }
    };
}
 
Example 12
Source File: IndexedCorpusModule.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
public <T> void hear(TypeLiteral<T> typeLiteral, TypeEncounter<T> typeEncounter) {
     Class<?> clazz = typeLiteral.getRawType();
     while (clazz != null) {
       for (Field field : clazz.getDeclaredFields()) {
         boolean injectAnnoPresent = 
         		field.isAnnotationPresent(fr.univnantes.termsuite.framework.InjectLogger.class);
if (field.getType() == Logger.class &&
           injectAnnoPresent) {
           typeEncounter.register(new Slf4JMembersInjector<T>(field));
         }
       }
       clazz = clazz.getSuperclass();
     }
   }
 
Example 13
Source File: ResourceTypeListener.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public <T> void hear(TypeLiteral<T> typeLiteral, TypeEncounter<T> typeEncounter) {
    for (Class<?> c = typeLiteral.getRawType(); c != Object.class; c = c.getSuperclass()) {
        for (Field field : typeLiteral.getRawType().getDeclaredFields()) {
            Resource resourceAnnotation = field.getAnnotation(Resource.class);
            if (resourceAnnotation != null) {
                String resourceName = resourceAnnotation.name();
                Context contextToLookup = defaultContext;
                String contextName = "default";

                // Check if this injection is from an additional context
                JndiContext jndiContextAnnotation = field.getAnnotation(JndiContext.class);
                if (jndiContextAnnotation != null) {
                    contextName = jndiContextAnnotation.value();
                    contextToLookup = jndiContexts.get(contextName);
                    if (contextToLookup == null) {
                        throw SeedException.createNew(JndiErrorCode.UNKNOWN_JNDI_CONTEXT)
                                .put("field", field)
                                .put("context", contextName);
                    }
                }

                // Register the members injector
                if (!resourceName.isEmpty()) {
                    typeEncounter.register(new ResourceMembersInjector<>(field,
                            contextToLookup,
                            contextName,
                            resourceName));
                }
            }
        }
    }
}
 
Example 14
Source File: AdaptableTypeListener.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
	if (IAdaptable.class.isAssignableFrom(type.getRawType())) {
		// TODO: method check should be moved into members injector,
		// here, only the type + an additional operation should be checked.
		for (final Method method : type.getRawType().getMethods()) {
			// check that AdapterMap annotation is not used to mark
			// injection points (but only in bindings).
			for (int i = 0; i < method
					.getParameterAnnotations().length; i++) {
				AdapterMap adapterMapAnnotation = getAnnotation(
						method.getParameterAnnotations()[i],
						AdapterMap.class);
				if (adapterMapAnnotation != null) {
					encounter.addError(
							"@AdapterMap annotation may only be used in adapter map bindings, not to mark an injection point. Annotate method with @InjectAdapters instead.",
							method);
				}
			}

			// we have a method annotated with AdapterBinding
			if (eligibleForAdapterInjection(method)) {
				// check that no Guice @Inject annotation is present on the
				// method (so no interference occurs).
				Inject injectAnnotation = method
						.getAnnotation(Inject.class);
				if (injectAnnotation != null) {
					encounter.addError(
							"To prevent that Guice member injection interferes with adapter injection, no @Inject annotation may be used on a method that provides an @InjectAdapters annotation.");
				}

				// register member injector on the IAdaptable (and provide
				// the method to it, so it does not have to look it up
				// again).
				AdapterInjector membersInjector = new AdapterInjector(
						method, loggingMode);
				if (injector != null) {
					injector.injectMembers(membersInjector);
				} else {
					nonInjectedMemberInjectors.add(membersInjector);
				}
				// System.out.println("Registering member injector to "
				// + type);
				encounter.register((MembersInjector<I>) membersInjector);
			}
		}
	}
}
 
Example 15
Source File: InternalScheduleModule.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
  encounter.register(new ScheduleInjectionListener<I>(launcher, injector));
}
 
Example 16
Source File: StartablesModule.java    From james-project with Apache License 2.0 4 votes vote down vote up
private <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
    InjectionListener<? super I> injectionListener = ignored -> startables.add(type.getRawType().asSubclass(Startable.class));
    encounter.register(injectionListener);
}