Java Code Examples for com.google.inject.TypeLiteral#getRawType()

The following examples show how to use com.google.inject.TypeLiteral#getRawType() . 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: 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 2
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 3
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 4
Source File: ReflectiveJavaTypeWidget.java    From yql-plus with Apache License 2.0 5 votes vote down vote up
public ReflectiveJavaTypeWidget(ProgramValueTypeAdapter adapter, TypeLiteral<?> typeLiteral) {
    super(Type.getType(typeLiteral.getRawType()));
    this.clazz = typeLiteral.getRawType();
    Preconditions.checkArgument(!clazz.isPrimitive(), "ReflectiveTypeWidget used on primitive: %s", clazz.getName());
    Preconditions.checkArgument(!clazz.isArray(), "ReflectiveTypeWidget used on array: %s", clazz.getName());
    Preconditions.checkArgument(TypeUtilities.getPrimitiveType(clazz) == null, "ReflectiveTypeWidget used on boxed primitive: %s", clazz.getName());
    this.adapter = adapter;
    this.type = Type.getType(typeLiteral.getRawType());
    this.typeLiteral = typeLiteral;
    this.dispatcher = new MethodDispatcher(typeLiteral);
}
 
Example 5
Source File: ParameterizedTimedListener.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Override
public <I> void hear(TypeLiteral<I> literal, TypeEncounter<I> encounter) {
    Class<? super I> type = literal.getRawType();
    for (Method method : type.getMethods()) {
        ParameterizedTimed annotation = method.getAnnotation(ParameterizedTimed.class);
        if (annotation == null) {
            continue;
        }

        String metricType = annotation.type();
        if(metricType == null || metricType.isEmpty()) {
            metricType = type.getSimpleName().replaceAll("\\$$", "");
        }
        String metricName = annotation.name();
        if(metricName == null || metricName.isEmpty()) {
            metricName = method.getName();
        }
        String metric = MetricRegistry.name(_group, metricType, metricName);
        final Timer timer = _metricRegistry.timer(metric);

        encounter.bindInterceptor(Matchers.only(method), new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                Timer.Context time = timer.time();
                try {
                    return invocation.proceed();
                } finally {
                    time.stop();
                }
            }
        });
    }
}
 
Example 6
Source File: SettingsBinder.java    From biomedicus with Apache License 2.0 5 votes vote down vote up
private <T> void bindSetting(Key<T> key, Object value) {
  if (value instanceof Key) {
    @SuppressWarnings("unchecked")
    Key<T> valueKey = (Key<T>) value;
    binder.bind(key).to(valueKey);
  } else {
    TypeLiteral<T> typeLiteral = key.getTypeLiteral();
    Class<? super T> rawType = typeLiteral.getRawType();
    @SuppressWarnings("unchecked")
    T cast = (T) rawType.cast(value);
    binder.bind(key).toInstance(cast);
  }
}
 
Example 7
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 8
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 9
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 10
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 11
Source File: MockProvider.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static <T> MockProvider<T> of(TypeLiteral<T> type) {
    return new MockProvider<>((Class<T>) type.getRawType());
}
 
Example 12
Source File: LenientEnumSetTypeAdapter.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Inject public LenientEnumSetTypeAdapter(TypeLiteral<T> type) {
    this.type = (Class<T>) type.getRawType();
}
 
Example 13
Source File: MetaBorgGeneric.java    From spoofax with Apache License 2.0 4 votes vote down vote up
private <K> K instance(TypeLiteral<K> typeLiteral, Type... typeArgs) {
    final Class<? super K> rawType = typeLiteral.getRawType();
    final ParameterizedType type = Types.newParameterizedType(rawType, typeArgs);
    @SuppressWarnings("unchecked") final Key<K> key = (Key<K>) Key.get(type);
    return injector.getInstance(key);
}