use of java.util.stream.Stream 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.stream.Stream in project requery by requery.
the class EntityType method process.
@Override
public Set<ElementValidator> process(ProcessingEnvironment processingEnvironment) {
// create attributes for fields that have no annotations
if (element().getKind().isInterface() || isImmutable() || isUnimplementable()) {
ElementFilter.methodsIn(element().getEnclosedElements()).stream().filter(this::isMethodProcessable).forEach(this::computeAttribute);
} else {
// private/static/final members fields are skipped
Set<VariableElement> elements = ElementFilter.fieldsIn(element().getEnclosedElements()).stream().filter(element -> !element.getModifiers().contains(Modifier.PRIVATE) && !element.getModifiers().contains(Modifier.STATIC) && (!element.getModifiers().contains(Modifier.FINAL) || isImmutable())).collect(Collectors.toSet());
if (elements.isEmpty()) {
// if nothing to process try the getters instead
ElementFilter.methodsIn(element().getEnclosedElements()).stream().filter(this::isMethodProcessable).forEach(this::computeAttribute);
} else {
elements.forEach(this::computeAttribute);
}
}
// find listener annotated methods
ElementFilter.methodsIn(element().getEnclosedElements()).forEach(element -> ListenerAnnotations.all().forEach(annotation -> {
if (element.getAnnotation(annotation) != null) {
ListenerMethod listener = listeners.computeIfAbsent(element, key -> new ListenerMethod(element));
listener.annotations().put(annotation, element.getAnnotation(annotation));
}
}));
Set<ProcessableElement<?>> elements = new LinkedHashSet<>();
attributes().values().forEach(attribute -> elements.add((ProcessableElement<?>) attribute));
elements.addAll(listeners.values());
Set<ElementValidator> validations = new LinkedHashSet<>();
elements.forEach(element -> validations.addAll(element.process(processingEnvironment)));
ElementValidator validator = new ElementValidator(element(), processingEnvironment);
Entity entity = annotationOf(Entity.class).orElse(null);
if (entity != null && !Names.isEmpty(entity.name()) && !SourceVersion.isIdentifier(entity.name())) {
validator.error("Invalid class identifier " + entity.name(), Entity.class);
}
if (element().getNestingKind() == NestingKind.ANONYMOUS) {
validator.error("Entity annotation cannot be applied to anonymous class");
}
if (element().getKind() == ElementKind.ENUM) {
validator.error("Entity annotation cannot be applied to an enum class");
}
if (attributes.values().isEmpty()) {
validator.warning("Entity contains no attributes");
}
if (!isReadOnly() && !isEmbedded() && attributes.values().size() == 1 && attributes.values().iterator().next().isGenerated()) {
validator.warning("Entity contains only a single generated attribute may fail to persist");
}
checkReserved(tableName(), validator);
validations.add(validator);
return validations;
}
use of java.util.stream.Stream in project requery by requery.
the class EntityType method factoryArguments.
@Override
public List<String> factoryArguments() {
List<String> names = new ArrayList<>();
ExecutableElement method = factoryMethod().orElseThrow(IllegalStateException::new);
// TODO need more validation here
// now match the builder fields to the parameters...
Map<Element, AttributeDescriptor> map = new LinkedHashMap<>(attributes);
for (VariableElement parameter : method.getParameters()) {
// straight forward case type and name are the same
Element matched = null;
for (Map.Entry<Element, AttributeDescriptor> entry : map.entrySet()) {
AttributeDescriptor attribute = entry.getValue();
String fieldName = attribute.fieldName();
if (fieldName.equalsIgnoreCase(parameter.getSimpleName().toString())) {
names.add(fieldName);
matched = entry.getKey();
}
}
if (matched != null) {
map.remove(matched);
}
}
// didn't work likely because the parameter names are missing
if (names.isEmpty()) {
// for kotlin data classes add processable element field names in order
if (isUnimplementable()) {
ElementFilter.methodsIn(element().getEnclosedElements()).stream().filter(this::isMethodProcessable).forEach(getter -> names.addAll(map.entrySet().stream().filter(entry -> entry.getKey().equals(getter)).map(entry -> entry.getValue().fieldName()).collect(Collectors.toList())));
} else {
for (Map.Entry<Element, AttributeDescriptor> entry : map.entrySet()) {
names.add(0, entry.getValue().fieldName());
}
}
}
return names;
}
use of java.util.stream.Stream in project requery by requery.
the class EntityType method merge.
@Override
public void merge(EntityDescriptor from) {
from.attributes().entrySet().forEach(entry -> {
if (attributes.values().stream().noneMatch(attribute -> attribute.name().equals(entry.getValue().name()))) {
Element element = entry.getValue().element();
attributes.put(entry.getKey(), new AttributeMember(element, this));
}
});
from.listeners().entrySet().stream().filter(entry -> entry.getValue() instanceof ListenerMethod).forEach(entry -> {
ListenerMethod method = (ListenerMethod) entry.getValue();
if (listeners.values().stream().noneMatch(listener -> listener.element().getSimpleName().equals(method.element().getSimpleName()))) {
listeners.put(entry.getKey(), method);
}
});
}
use of java.util.stream.Stream in project spring-framework by spring-projects.
the class StreamConverterTests method convertFromArrayToStream.
@Test
@SuppressWarnings("resource")
public void convertFromArrayToStream() throws NoSuchFieldException {
Integer[] stream = new Integer[] { 1, 0, 1 };
this.conversionService.addConverter(new Converter<Integer, Boolean>() {
@Override
public Boolean convert(Integer source) {
return source == 1;
}
});
TypeDescriptor streamOfBoolean = new TypeDescriptor(Types.class.getField("streamOfBooleans"));
;
Object result = this.conversionService.convert(stream, streamOfBoolean);
assertNotNull("Converted object must not be null", result);
assertTrue("Converted object must be a stream", result instanceof Stream);
@SuppressWarnings("unchecked") Stream<Boolean> content = (Stream<Boolean>) result;
assertEquals(2, content.filter(x -> x).count());
}
Aggregations