use of java.util.Optional in project ratpack by ratpack.
the class Guice method buildInjector.
static Injector buildInjector(Registry baseRegistry, Action<? super BindingsSpec> bindingsAction, Function<? super Module, ? extends Injector> injectorFactory) throws Exception {
List<Action<? super Binder>> binderActions = Lists.newLinkedList();
List<Module> modules = Lists.newLinkedList();
ServerConfig serverConfig = baseRegistry.get(ServerConfig.class);
BindingsSpec bindings = new DefaultBindingsSpec(serverConfig, binderActions, modules);
bindings.module(new RatpackBaseRegistryModule(baseRegistry));
try {
bindingsAction.execute(bindings);
} catch (Exception e) {
throw uncheck(e);
}
modules.add(new AdHocModule(binderActions));
modules.add(new ConfigModule(serverConfig.getRequiredConfig()));
Optional<BindingsImposition> bindingsImposition = Impositions.current().get(BindingsImposition.class);
if (bindingsImposition.isPresent()) {
BindingsImposition imposition = bindingsImposition.get();
List<Action<? super Binder>> imposedBinderActions = Lists.newLinkedList();
List<Module> imposedModules = Lists.newLinkedList();
BindingsSpec imposedBindings = new DefaultBindingsSpec(serverConfig, imposedBinderActions, imposedModules);
imposition.getBindings().execute(imposedBindings);
imposedModules.add(new AdHocModule(imposedBinderActions));
Module imposedModule = imposedModules.stream().reduce((acc, next) -> Modules.override(acc).with(next)).get();
modules.add(imposedModule);
}
Module masterModule = modules.stream().reduce((acc, next) -> Modules.override(acc).with(next)).get();
return injectorFactory.apply(masterModule);
}
use of java.util.Optional in project requery by requery.
the class EntityType method addAnnotationElement.
@Override
public void addAnnotationElement(TypeElement annotationElement, Element annotatedElement) {
String qualifiedName = annotationElement.getQualifiedName().toString();
Class<? extends Annotation> type;
try {
type = Class.forName(qualifiedName).asSubclass(Annotation.class);
} catch (ClassNotFoundException e) {
return;
}
switch(annotatedElement.getKind()) {
case CLASS:
case INTERFACE:
annotations().put(type, annotatedElement.getAnnotation(type));
break;
case FIELD:
if (annotatedElement.getModifiers().contains(Modifier.STATIC) || annotatedElement.getModifiers().contains(Modifier.FINAL)) {
// check if this a requery annotation
String packageName = Entity.class.getPackage().getName();
if (annotationElement.getQualifiedName().toString().startsWith(packageName)) {
processingEnvironment.getMessager().printMessage(Diagnostic.Kind.ERROR, annotationElement.getQualifiedName() + " not applicable to static or final member", annotatedElement);
}
} else {
VariableElement element = (VariableElement) annotatedElement;
Optional<AttributeMember> attribute = computeAttribute(element);
Annotation annotation = annotatedElement.getAnnotation(type);
attribute.ifPresent(a -> a.annotations().put(type, annotation));
}
break;
case METHOD:
ExecutableElement element = (ExecutableElement) annotatedElement;
Annotation annotation = annotatedElement.getAnnotation(type);
if (ListenerAnnotations.all().anyMatch(a -> a.equals(type))) {
ListenerMethod listener = listeners.computeIfAbsent(element, key -> new ListenerMethod(element));
listener.annotations().put(type, annotation);
} else if (isMethodProcessable(element)) {
Optional<AttributeMember> attribute = computeAttribute(element);
attribute.ifPresent(a -> a.annotations().put(type, annotation));
}
break;
}
}
use of java.util.Optional in project requery by requery.
the class EntityType method builderType.
@Override
public Optional<TypeMirror> builderType() {
Optional<Entity> entityAnnotation = annotationOf(Entity.class);
if (entityAnnotation.isPresent()) {
Entity entity = entityAnnotation.get();
Elements elements = processingEnvironment.getElementUtils();
TypeMirror mirror = null;
try {
// easiest way to get the class TypeMirror
Class<?> builderClass = entity.builder();
if (builderClass != void.class) {
mirror = elements.getTypeElement(builderClass.getName()).asType();
}
} catch (MirroredTypeException typeException) {
mirror = typeException.getTypeMirror();
}
if (mirror != null && mirror.getKind() != TypeKind.VOID) {
return Optional.of(mirror);
}
}
if (builderFactoryMethod().isPresent()) {
return Optional.of(builderFactoryMethod().get().getReturnType());
}
return ElementFilter.typesIn(element().getEnclosedElements()).stream().filter(element -> element.getSimpleName().toString().contains("Builder")).map(Element::asType).filter(Objects::nonNull).filter(type -> type.getKind() != TypeKind.VOID).findFirst();
}
use of java.util.Optional in project requery by requery.
the class EntityType method typeName.
@Override
public QualifiedName typeName() {
String entityName = Stream.of(Mirrors.findAnnotationMirror(element(), Entity.class), Mirrors.findAnnotationMirror(element(), javax.persistence.Entity.class)).filter(Optional::isPresent).map(Optional::get).map(mirror -> Mirrors.findAnnotationValue(mirror, "name")).filter(Optional::isPresent).map(Optional::get).map(value -> value.getValue().toString()).filter(name -> !Names.isEmpty(name)).findAny().orElse("");
Elements elements = processingEnvironment.getElementUtils();
String packageName = elements.getPackageOf(element()).getQualifiedName().toString();
// if set in the annotation just use that
if (!Names.isEmpty(entityName)) {
return new QualifiedName(packageName, entityName);
}
String typeName = element().getSimpleName().toString();
if (element().getKind().isInterface()) {
// maybe I<Something> style
if (typeName.startsWith("I") && Character.isUpperCase(typeName.charAt(1))) {
entityName = typeName.substring(1);
} else {
entityName = typeName + "Entity";
}
} else {
entityName = Names.removeClassPrefixes(typeName);
if (entityName.equals(typeName)) {
entityName = typeName + (isImmutable() || isUnimplementable() ? "Type" : "Entity");
}
}
return new QualifiedName(packageName, entityName);
}
use of java.util.Optional in project spring-framework by spring-projects.
the class ServerSentEventHttpMessageWriter method applyEncoder.
@SuppressWarnings("unchecked")
private <T> Flux<DataBuffer> applyEncoder(Object data, DataBufferFactory bufferFactory, Map<String, Object> hints) {
ResolvableType elementType = ResolvableType.forClass(data.getClass());
Optional<Encoder<?>> encoder = dataEncoders.stream().filter(e -> e.canEncode(elementType, MimeTypeUtils.APPLICATION_JSON)).findFirst();
return ((Encoder<T>) encoder.orElseThrow(() -> new CodecException("No suitable encoder found!"))).encode(Mono.just((T) data), bufferFactory, elementType, MimeTypeUtils.APPLICATION_JSON, hints).concatWith(encodeString("\n", bufferFactory));
}
Aggregations