Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #5

Merged
merged 9 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/org/comroid/annotations/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.comroid.annotations;

import org.comroid.annotations.internal.Inherit;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@Inherit(Inherit.Type.FromBoth)
public @interface Category {
String value() default "";

Description[] desc() default {};
}
15 changes: 15 additions & 0 deletions src/main/java/org/comroid/annotations/Default.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.comroid.annotations;

import org.intellij.lang.annotations.Language;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Default {
@Language("JShellLanguage")
String value() default "null";
}
12 changes: 12 additions & 0 deletions src/main/java/org/comroid/annotations/Description.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.comroid.annotations;

import org.comroid.annotations.internal.Inherit;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@Inherit(Inherit.Type.FromSupertype)
public @interface Description {
String[] value() default {};
}
12 changes: 12 additions & 0 deletions src/main/java/org/comroid/annotations/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.comroid.annotations;

import org.comroid.annotations.internal.Inherit;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@Inherit(Inherit.Type.FromSupertype)
public @interface Order {
int value();
}
54 changes: 51 additions & 3 deletions src/main/java/org/comroid/annotations/internal/Annotations.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.comroid.annotations.internal;

import jdk.jshell.JShell;
import jdk.jshell.JShellException;
import jdk.jshell.SnippetEvent;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.Value;
import lombok.experimental.UtilityClass;
import lombok.extern.java.Log;
import org.comroid.annotations.Alias;
import org.comroid.annotations.Convert;
import org.comroid.annotations.Ignore;
import org.comroid.annotations.*;
import org.comroid.api.Polyfill;
import org.comroid.api.data.seri.DataStructure;
import org.comroid.api.data.seri.StandardValueType;
import org.comroid.api.func.ext.Wrap;
import org.comroid.api.info.Constraint;
import org.comroid.api.java.ReflectionHelper;
Expand All @@ -20,6 +24,7 @@
import java.lang.annotation.ElementType;
import java.lang.reflect.*;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -34,6 +39,12 @@
@ApiStatus.Internal
public class Annotations {
public static final Class<?>[] SystemFilters = new Class<?>[]{Object.class, Class.class, Annotation.class};
public static final Comparator<AnnotatedElement> OrderComparator = Comparator.comparingInt(it ->
findAnnotations(Order.class, it)
.findAny()
.map(Result::getAnnotation)
.map(Order::value)
.orElse(0));

@ApiStatus.Experimental
@Convert(identifyVia = "annotationType")
Expand All @@ -48,6 +59,41 @@ public Set<String> aliases(@NotNull AnnotatedElement of) {
.collect(Collectors.toUnmodifiableSet());
}

public Stream<String> description(@NotNull AnnotatedElement of) {
return findAnnotations(Description.class, of)
.flatMap(it -> stream(it.annotation.value()));
}

public Wrap<Result<Category>> category(@NotNull AnnotatedElement of) {
return Wrap.ofStream(findAnnotations(Category.class, of));
}

public <R> @Nullable R defaultValue(@NotNull AnnotatedElement of) {
final var silent = new Object(){
@SneakyThrows
public void throwIfExcPresent(SnippetEvent e) {
var exc = e.exception();
if (exc != null)
throw exc;
}
};
try (final var jShell = JShell.create()) {
return findAnnotations(Default.class, of)
.map(Result::getAnnotation)
.flatMap(expr -> jShell.eval(expr.value()).stream())
.peek(silent::throwIfExcPresent)
.map(SnippetEvent::value)
.filter(Objects::nonNull)
.findAny()
.map(StandardValueType::findGoodType)
.map(Polyfill::<R>uncheckedCast)
.orElse(null);
} catch (Throwable t) {
log.log(Level.WARNING, "Failed to evaluate default expression of " + of, t);
return null;
}
}

public Optional<? extends AnnotatedElement> ignore(@NotNull AnnotatedElement it) {
return ignore(it, null);
}
Expand Down Expand Up @@ -112,6 +158,8 @@ public <A extends Annotation> Stream<Result<A>> findAnnotations(final Class<A> t
break;
case FromSupertype, FromBoth:
sources = sources.collect(append(findAncestor(member, type).stream()));
if (inherit != Inherit.Type.FromBoth)
break;
case FromParent:
sources = sources.collect(append(decl));
break;
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/comroid/api/data/seri/DataNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ static Stream<Entry> properties(final java.lang.Object it) {
if (it instanceof DataNode.Base)
return ((DataNode) it).properties();
return DataStructure.of(it.getClass(), java.lang.Object.class)
.getProperties().values().stream()
.getDeclaredProperties().values().stream()
.map(Polyfill::<DataStructure<java.lang.Object>.Property<Object>>uncheckedCast)
.map(prop -> new Entry(prop.getName(), of(prop.getFrom(it))));
}
Expand Down Expand Up @@ -309,7 +309,12 @@ public Stream<Entry> properties() {
@AllArgsConstructor
@Ignore(Convertible.class)
class Value<T> extends Base implements ValueBox<T> {
public static final DataNode NULL = new Value<>(null);
public static final DataNode NULL = new Value<>(null){
@Override
public ValueType<?> getHeldType() {
return VOID;
}
};
protected @Nullable T value;

@Override
Expand Down
Loading