Skip to content

Commit

Permalink
Merge pull request #42 from Paullo612/micronaut4-support
Browse files Browse the repository at this point in the history
Add Micronaut 4 support
  • Loading branch information
Paullo612 committed Nov 18, 2023
2 parents 81e7477 + 953256e commit 65519a4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ private static ClassElement smashUpGeneric(ClassElement classElement) {
}

public static boolean isAssignable(ClassElement from, ClassElement to) {
// NB: primitives are considered assignable to boxed types since Micronaut 4.0.0
if (from.isPrimitive() && !to.isPrimitive() || !from.isPrimitive() && to.isPrimitive()) {
return false;
}

from = smashUpGeneric(from);
to = smashUpGeneric(to);

Expand All @@ -53,6 +58,11 @@ private static String fixEnclosingClassName(Class<?> enclosing, Class<?> target)
public static boolean isAssignable(ClassElement from, Class<?> to) {
assert !to.isArray();

// NB: primitives are considered assignable to boxed types since Micronaut 4.0.0
if (from.isPrimitive() && !to.isPrimitive() || !from.isPrimitive() && to.isPrimitive()) {
return false;
}

from = smashUpGeneric(from);

Class<?> enclosingClass = to.getEnclosingClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ private PropertyElement getProperty() {

this.property = previousClassElement.getBeanProperties().stream()
.filter(p -> p.getName().equals(getName()))
// Write only properties were introduced in micronaut 4.
.filter(p -> p.getReadMethod().isPresent())
.findFirst()
.map(ElementUtils::fixProperty)
.orElseThrow(() -> context.compileError(
Expand Down Expand Up @@ -97,7 +99,7 @@ public ExpressionContext.RenderCommand run(RenderingAdapter adapter) {
PropertyElement property = getProperty();

MethodElement getter = property.getReadMethod()
// NB: Property must always have read method, or it will not be considered as property by Micronaut.
// NB: Property must always have read method. See property filter.
.orElseThrow(AssertionError::new);

return methodVisitor -> RenderUtils.renderMethodCall(methodVisitor, getter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import io.github.paullo612.mlfx.compiler.CompileFXMLVisitor;
import io.micronaut.context.annotation.Executable;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.AnnotationValueBuilder;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.version.VersionUtils;
import io.micronaut.inject.ast.ClassElement;
import io.micronaut.inject.ast.Element;
import io.micronaut.inject.ast.ElementQuery;
Expand All @@ -29,15 +31,36 @@
import io.micronaut.inject.visitor.TypeElementVisitor;
import io.micronaut.inject.visitor.VisitorContext;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@AutoService(TypeElementVisitor.class)
public class FXMLMicronautVisitor implements TypeElementVisitor<Object, Object> {

private static final boolean IS_MICRONAUT_4_OR_HIGHER = VersionUtils.isAtLeastMicronautVersion("4.0.0");

private static final String FXML_ANNOTATION = "javafx.fxml.FXML";

private static Introspected.Visibility ANY_VISIBILITY;

private AnnotationValueBuilder<Introspected> setVisibility(AnnotationValueBuilder<Introspected> builder) {
if (!IS_MICRONAUT_4_OR_HIGHER) {
return builder;
}

if (ANY_VISIBILITY == null) {
ANY_VISIBILITY = Arrays.stream(Introspected.Visibility.values())
.filter(v -> "ANY".equals(v.name()))
.findFirst()
.orElseThrow(AssertionError::new);
}

return builder
.member("visibility", ANY_VISIBILITY);
}

private void markAsIntrospected(ClassElement classElement, VisitorContext context) {
if (classElement.hasStereotype(Introspected.class)) {
// Already @Introspected. We've already marked this class, or user knows better.
Expand Down Expand Up @@ -66,6 +89,16 @@ private void markAsIntrospected(ClassElement classElement, VisitorContext contex
field
);
}

if (!IS_MICRONAUT_4_OR_HIGHER) {
if (field.isProtected()) {
context.fail(
"Controller field is protected and is annotated by @FXML annotation. Protected fields"
+ " cannot be set to controller by Micronaut version lower than 4.0.0.",
field
);
}
}
}

String[] toInclude = fields.stream()
Expand All @@ -84,11 +117,13 @@ private void markAsIntrospected(ClassElement classElement, VisitorContext contex
toExclude = new String[0];
}

classElement.annotate(AnnotationValue.builder(Introspected.class)
.member("accessKind", Introspected.AccessKind.FIELD)
.member("includes", toInclude)
.member("excludes", toExclude)
.build()
classElement.annotate(
setVisibility(AnnotationValue.builder(Introspected.class)
.member("accessKind", Introspected.AccessKind.FIELD)
.member("includes", toInclude)
.member("excludes", toExclude)
)
.build()
);
}

Expand Down

0 comments on commit 65519a4

Please sign in to comment.