Search in sources :

Example 26 with Stream

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();
}
Also used : MirroredTypeException(javax.lang.model.type.MirroredTypeException) Transient(io.requery.Transient) Table(io.requery.Table) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Embedded(io.requery.Embedded) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) MirroredTypeException(javax.lang.model.type.MirroredTypeException) PropertyNameStyle(io.requery.PropertyNameStyle) NestingKind(javax.lang.model.element.NestingKind) ElementFilter(javax.lang.model.util.ElementFilter) LinkedHashSet(java.util.LinkedHashSet) Name(javax.lang.model.element.Name) View(io.requery.View) Entity(io.requery.Entity) ElementKind(javax.lang.model.element.ElementKind) ExecutableElement(javax.lang.model.element.ExecutableElement) Cacheable(javax.persistence.Cacheable) Set(java.util.Set) ReadOnly(io.requery.ReadOnly) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Stream(java.util.stream.Stream) Index(javax.persistence.Index) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Annotation(java.lang.annotation.Annotation) Factory(io.requery.Factory) Optional(java.util.Optional) Embeddable(javax.persistence.Embeddable) Entity(io.requery.Entity) TypeMirror(javax.lang.model.type.TypeMirror) Objects(java.util.Objects) Elements(javax.lang.model.util.Elements)

Example 27 with Stream

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;
}
Also used : Transient(io.requery.Transient) Table(io.requery.Table) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Embedded(io.requery.Embedded) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) MirroredTypeException(javax.lang.model.type.MirroredTypeException) PropertyNameStyle(io.requery.PropertyNameStyle) NestingKind(javax.lang.model.element.NestingKind) ElementFilter(javax.lang.model.util.ElementFilter) LinkedHashSet(java.util.LinkedHashSet) Name(javax.lang.model.element.Name) View(io.requery.View) Entity(io.requery.Entity) ElementKind(javax.lang.model.element.ElementKind) ExecutableElement(javax.lang.model.element.ExecutableElement) Cacheable(javax.persistence.Cacheable) Set(java.util.Set) ReadOnly(io.requery.ReadOnly) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Stream(java.util.stream.Stream) Index(javax.persistence.Index) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Annotation(java.lang.annotation.Annotation) Factory(io.requery.Factory) Optional(java.util.Optional) Embeddable(javax.persistence.Embeddable) LinkedHashSet(java.util.LinkedHashSet) Entity(io.requery.Entity) VariableElement(javax.lang.model.element.VariableElement)

Example 28 with Stream

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;
}
Also used : Transient(io.requery.Transient) Table(io.requery.Table) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Embedded(io.requery.Embedded) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) MirroredTypeException(javax.lang.model.type.MirroredTypeException) PropertyNameStyle(io.requery.PropertyNameStyle) NestingKind(javax.lang.model.element.NestingKind) ElementFilter(javax.lang.model.util.ElementFilter) LinkedHashSet(java.util.LinkedHashSet) Name(javax.lang.model.element.Name) View(io.requery.View) Entity(io.requery.Entity) ElementKind(javax.lang.model.element.ElementKind) ExecutableElement(javax.lang.model.element.ExecutableElement) Cacheable(javax.persistence.Cacheable) Set(java.util.Set) ReadOnly(io.requery.ReadOnly) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Stream(java.util.stream.Stream) Index(javax.persistence.Index) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Annotation(java.lang.annotation.Annotation) Factory(io.requery.Factory) Optional(java.util.Optional) Embeddable(javax.persistence.Embeddable) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 29 with Stream

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);
        }
    });
}
Also used : Transient(io.requery.Transient) Table(io.requery.Table) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Embedded(io.requery.Embedded) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) MirroredTypeException(javax.lang.model.type.MirroredTypeException) PropertyNameStyle(io.requery.PropertyNameStyle) NestingKind(javax.lang.model.element.NestingKind) ElementFilter(javax.lang.model.util.ElementFilter) LinkedHashSet(java.util.LinkedHashSet) Name(javax.lang.model.element.Name) View(io.requery.View) Entity(io.requery.Entity) ElementKind(javax.lang.model.element.ElementKind) ExecutableElement(javax.lang.model.element.ExecutableElement) Cacheable(javax.persistence.Cacheable) Set(java.util.Set) ReadOnly(io.requery.ReadOnly) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Stream(java.util.stream.Stream) Index(javax.persistence.Index) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Annotation(java.lang.annotation.Annotation) Factory(io.requery.Factory) Optional(java.util.Optional) Embeddable(javax.persistence.Embeddable) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element)

Example 30 with Stream

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());
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Stream(java.util.stream.Stream) Test(org.junit.Test)

Aggregations

Stream (java.util.stream.Stream)161 Collectors (java.util.stream.Collectors)98 List (java.util.List)89 ArrayList (java.util.ArrayList)66 Map (java.util.Map)66 Set (java.util.Set)59 IOException (java.io.IOException)58 Optional (java.util.Optional)45 Collections (java.util.Collections)43 HashMap (java.util.HashMap)43 Arrays (java.util.Arrays)33 HashSet (java.util.HashSet)33 File (java.io.File)32 Path (java.nio.file.Path)32 Function (java.util.function.Function)28 Logger (org.slf4j.Logger)26 LoggerFactory (org.slf4j.LoggerFactory)26 java.util (java.util)25 Predicate (java.util.function.Predicate)23 Objects (java.util.Objects)22