下面列出了com.google.inject.TypeLiteral#getRawType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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));
}
}
@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));
}
}
@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)
);
}
}
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);
}
@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();
}
}
});
}
}
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);
}
}
@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)));
}
}
}
}
@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));
}
}
}
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();
}
}
@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));
}
}
}
}
}
public static <T> MockProvider<T> of(TypeLiteral<T> type) {
return new MockProvider<>((Class<T>) type.getRawType());
}
@Inject public LenientEnumSetTypeAdapter(TypeLiteral<T> type) {
this.type = (Class<T>) type.getRawType();
}
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);
}