Skip to content

Commit

Permalink
🔀 Merge origin/alpha into origin/beta (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
lengors authored Sep 28, 2024
2 parents 84329f5 + bee78b3 commit f606ee9
Show file tree
Hide file tree
Showing 20 changed files with 997 additions and 156 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# [v1.1.1-alpha.1](https://github.com/lengors/js2pets/compare/v1.1.0...v1.1.1-alpha.1) (2024-09-28)

## 🐛 Bug Fixes
- [`9aec43c`](https://github.com/lengors/js2pets/commit/9aec43c) Fields, related parameters and return types now annotated with `Nullable`

# [v1.1.1-dev.1](https://github.com/lengors/js2pets/compare/v1.1.0...v1.1.1-dev.1) (2024-09-28)

## 🐛 Bug Fixes
- [`9aec43c`](https://github.com/lengors/js2pets/commit/9aec43c) Fields, related parameters and return types now annotated with `Nullable`

# [v1.1.0](https://github.com/lengors/js2pets/compare/v1.0.0...v1.1.0) (2024-09-22)

## ✨ New Features
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.lengors.js2pets.annotators;

import java.util.function.BiConsumer;
import java.util.stream.Stream;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.jsonschema2pojo.Annotator;
Expand Down Expand Up @@ -29,21 +30,34 @@ public void constructor(final Annotator annotator, final JMethod constructor) {
}

private <T> void dispatch(final Annotator annotator, final T value, final BiConsumer<EnhancedAnnotator, T> action) {
streamAnnotators(annotator)
.filter(EnhancedAnnotator.class::isInstance)
.forEach(leafAnnotator -> action.accept((EnhancedAnnotator) leafAnnotator, value));
}

/**
* Returns a flat stream of annotators by traversing the given annotator if it's a composite annotator.
*
* @param annotator The source annotator.
* @return The stream of annotators obtained.
*/
public Stream<Annotator> streamAnnotators(final Annotator annotator) {
if (annotator instanceof CompositeAnnotator compositeAnnotator) {
final var annotatorsField = FieldUtils.getField(compositeAnnotator.getClass(), "annotators", true);
final var annotatorsField = FieldUtils.getField(annotator.getClass(), "annotators", true);
final Annotator[] annotators;
try {
annotators = (Annotator[]) annotatorsField.get(compositeAnnotator);
} catch (final IllegalAccessException exception) {
return;
return Stream.empty();
}
if (annotators != null) {
for (final var childAnnotator : annotators) {
dispatch(childAnnotator, value, action);
}
if (annotators == null) {
return Stream.empty();
}
} else if (annotator instanceof EnhancedAnnotator enhancedAnnotator) {
action.accept(enhancedAnnotator, value);
return Stream
.of(annotators)
.flatMap(AnnotatorUtils::streamAnnotators);
} else {
return Stream.of(annotator);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package io.github.lengors.js2pets.annotators;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.SequencedCollection;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.apache.commons.collections4.IteratorUtils;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jsonschema2pojo.NoopAnnotator;

import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JType;

/**
Expand All @@ -18,7 +27,8 @@
public class CheckerableAnnotator extends NoopAnnotator implements EnhancedAnnotator {

/**
* Annotator callback that annotates equals methods' parameter with Checkerframework's {@link Nullable}.
* Annotator callback that annotates equals methods' parameter with Checkerframework's {@link Nullable} as well as any
* parameters for the respective field already annotated with {@link Nullable}.
*
* @param type The generated class to apply the annotation to.
*/
Expand All @@ -28,6 +38,36 @@ public void type(final JType type) {
return;
}

final var nullableFields = clazz
.fields()
.values()
.stream()
.filter(field -> field
.annotations()
.stream()
.map(JAnnotationUse::getAnnotationClass)
.map(JClass::fullName)
.anyMatch(Nullable.class.getName()::equals))
.map(JFieldVar::name)
.collect(Collectors.toUnmodifiableSet());

final var classList = classes(clazz)
.toList();

Stream
.concat(classList
.stream()
.flatMap(classFromSet -> classFromSet
.methods()
.stream()),
classList
.stream()
.flatMap(classFromSet -> stream(classFromSet.constructors())))
.map(JMethod::params)
.flatMap(List::stream)
.filter(param -> nullableFields.contains(param.name()))
.forEach(param -> param.annotate(Nullable.class));

final var objectRef = clazz
.owner()
._ref(Object.class);
Expand All @@ -40,4 +80,19 @@ public void type(final JType type) {
.map(SequencedCollection::getFirst)
.ifPresent(parameter -> parameter.annotate(Nullable.class));
}

private static <T> Stream<T> stream(final Iterator<T> iterator) {
return stream(IteratorUtils.asIterable(iterator));
}

private static <T> Stream<T> stream(final Iterable<T> iterable) {
return StreamSupport.stream(iterable.spliterator(), false);
}

private static Stream<JDefinedClass> classes(final JDefinedClass definedClass) {
return Stream.concat(
Stream.of(definedClass),
stream(definedClass.classes())
.flatMap(CheckerableAnnotator::classes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ default void constructor(JMethod constructor) {
/**
* Listener to type generation finished for annotations.
*
* @param type
* @param type The generated type.
*/
default void type(JType type) {

Expand Down
Loading

0 comments on commit f606ee9

Please sign in to comment.