From 7d1b41c80a8e9bb308f85691547d4380af98aebe Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Tue, 6 Sep 2022 02:20:32 +0300 Subject: Improve PropertyTaglet - Now generates a "frame" around it like the other summary sections - Now has the property visibility and type in the table --- .../cottonmc/cotton/gui/jd/PropertyTaglet.java | 54 +++++++++++++++------- .../io/github/cottonmc/cotton/gui/jd/Util.java | 11 +++++ 2 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 javadoc/src/main/java/io/github/cottonmc/cotton/gui/jd/Util.java (limited to 'javadoc/src') diff --git a/javadoc/src/main/java/io/github/cottonmc/cotton/gui/jd/PropertyTaglet.java b/javadoc/src/main/java/io/github/cottonmc/cotton/gui/jd/PropertyTaglet.java index 163f1d6..8b7b1ec 100644 --- a/javadoc/src/main/java/io/github/cottonmc/cotton/gui/jd/PropertyTaglet.java +++ b/javadoc/src/main/java/io/github/cottonmc/cotton/gui/jd/PropertyTaglet.java @@ -12,6 +12,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -19,6 +20,7 @@ import java.util.stream.Collectors; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeVisitor; @@ -28,6 +30,7 @@ import javax.lang.model.util.SimpleTypeVisitor8; public class PropertyTaglet implements Taglet { private static final Pattern PROPERTY_METHOD = Pattern.compile("^(.+)Property$"); private static final String OBSERVABLE_PROPERTY = "io.github.cottonmc.cotton.gui.widget.data.ObservableProperty"; + private static final Set VISIBILITY_MODIFIERS = Set.of(Modifier.PUBLIC, Modifier.PROTECTED); private DocTrees docTrees; @Override @@ -60,21 +63,29 @@ public class PropertyTaglet implements Taglet { StringBuilder builder = new StringBuilder(); if (!myEntries.isEmpty()) { + builder.append("
"); } Map> inheritedProperties = new LinkedHashMap<>(); @@ -103,8 +114,15 @@ public class PropertyTaglet implements Taglet { return cl.getEnclosedElements().stream() .filter(el -> el.getKind() == ElementKind.METHOD) .map(el -> (ExecutableElement) el) - .filter(el -> el.getReturnType().accept(new ObservableTypeFilter(), null)) - .map(el -> { + .map(Util.zip(el -> el.getReturnType().accept(new ObservableTypeProbe(), null))) + .filter(pair -> pair.second().isPresent()) + .map(pair -> { + var el = pair.first(); + Modifier visibility = el.getModifiers().stream() + .filter(VISIBILITY_MODIFIERS::contains) + .findAny() + .orElse(null); + if (visibility == null) return null; // no privates or package-privates Matcher matcher = PROPERTY_METHOD.matcher(el.getSimpleName()); if (matcher.matches()) { @@ -118,7 +136,7 @@ public class PropertyTaglet implements Taglet { .filter(Objects::nonNull) .findAny().orElse(""); - return new PropertyEntry(matcher.group(1), doc); + return new PropertyEntry(visibility, pair.second().get(), matcher.group(1), doc); } return null; @@ -148,24 +166,28 @@ public class PropertyTaglet implements Taglet { cl.getInterfaces().forEach(itf -> itf.accept(typeVisitor, null)); } - private static final class ObservableTypeFilter extends SimpleTypeVisitor8 { - ObservableTypeFilter() { - super(false); + private static final class ObservableTypeProbe extends SimpleTypeVisitor8, Void> { + ObservableTypeProbe() { + super(Optional.empty()); } @Override - public Boolean visitDeclared(DeclaredType t, Void v) { + public Optional visitDeclared(DeclaredType t, Void v) { Element type = t.asElement(); - if (type.getKind() == ElementKind.CLASS) { - return ((TypeElement) type).getQualifiedName().contentEquals(OBSERVABLE_PROPERTY); + if (type.getKind() == ElementKind.CLASS && ((TypeElement) type).getQualifiedName().contentEquals(OBSERVABLE_PROPERTY)) { + if (!t.getTypeArguments().isEmpty()) { + return Optional.of(t.getTypeArguments().get(0).toString()); + } else { + return Optional.of(""); + } } - return false; + return Optional.empty(); } } - private record PropertyEntry(String name, String doc) implements Comparable { + private record PropertyEntry(Modifier visibility, String type, String name, String doc) implements Comparable { @Override public int compareTo(PropertyEntry o) { return name.compareTo(o.name); diff --git a/javadoc/src/main/java/io/github/cottonmc/cotton/gui/jd/Util.java b/javadoc/src/main/java/io/github/cottonmc/cotton/gui/jd/Util.java new file mode 100644 index 0000000..775de4d --- /dev/null +++ b/javadoc/src/main/java/io/github/cottonmc/cotton/gui/jd/Util.java @@ -0,0 +1,11 @@ +package io.github.cottonmc.cotton.gui.jd; + +import java.util.function.Function; + +public final class Util { + public static Function> zip(Function transform) { + return a -> new Pair<>(a, transform.apply(a)); + } + + public record Pair(A first, B second) {} +} -- cgit