Skip to content

Commit

Permalink
Added TypeInfo#collectContainedComponentClasses() and getContainedCom…
Browse files Browse the repository at this point in the history
…ponentClasses()
  • Loading branch information
LatvianModder committed Sep 4, 2024
1 parent efee463 commit 8c5f45c
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/dev/latvian/mods/rhino/type/ArrayTypeInfo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.latvian.mods.rhino.type;

import java.util.Collection;
import java.util.Set;

public final class ArrayTypeInfo extends TypeInfoBase {
private final TypeInfo component;
private Class<?> asClass;
Expand Down Expand Up @@ -48,4 +51,14 @@ public String signature() {
public TypeInfo componentType() {
return component;
}

@Override
public void collectContainedComponentClasses(Collection<Class<?>> classes) {
component.collectContainedComponentClasses(classes);
}

@Override
public Set<Class<?>> getContainedComponentClasses() {
return component.getContainedComponentClasses();
}
}
12 changes: 12 additions & 0 deletions src/main/java/dev/latvian/mods/rhino/type/ClassTypeInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package dev.latvian.mods.rhino.type;

import java.util.Set;

public abstract class ClassTypeInfo extends TypeInfoBase {
private final Class<?> type;
private Set<Class<?>> typeSet;

ClassTypeInfo(Class<?> type) {
this.type = type;
Expand Down Expand Up @@ -81,4 +84,13 @@ public boolean isDouble() {
public boolean isCharacter() {
return type == Character.class || type == Character.TYPE;
}

@Override
public Set<Class<?>> getContainedComponentClasses() {
if (typeSet == null) {
typeSet = Set.of(type);
}

return typeSet;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.latvian.mods.rhino.type;

import java.util.Collection;
import java.util.Set;

// null, undefined, true, false
public record JSBasicConstantTypeInfo(String value) implements TypeInfo {
public static final JSBasicConstantTypeInfo NULL = new JSBasicConstantTypeInfo("null");
Expand All @@ -21,4 +24,13 @@ public String toString() {
public void append(TypeStringContext ctx, StringBuilder sb) {
sb.append(value);
}

@Override
public void collectContainedComponentClasses(Collection<Class<?>> classes) {
}

@Override
public Set<Class<?>> getContainedComponentClasses() {
return Set.of();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.rhino.type;

import java.util.Collection;
import java.util.List;

// [string, number]
Expand Down Expand Up @@ -29,4 +30,11 @@ public void append(TypeStringContext ctx, StringBuilder sb) {

sb.append(']');
}

@Override
public void collectContainedComponentClasses(Collection<Class<?>> classes) {
for (var type : types) {
type.type().collectContainedComponentClasses(classes);
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/dev/latvian/mods/rhino/type/JSFunctionTypeInfo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.rhino.type;

import java.util.Collection;
import java.util.List;

// (a: string) => void
Expand Down Expand Up @@ -38,4 +39,13 @@ public void append(TypeStringContext ctx, StringBuilder sb) {
ctx.appendSpace(sb);
ctx.append(sb, returnType);
}

@Override
public void collectContainedComponentClasses(Collection<Class<?>> classes) {
for (var param : params) {
param.type().collectContainedComponentClasses(classes);
}

returnType.collectContainedComponentClasses(classes);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.latvian.mods.rhino.type;

import java.util.Collection;
import java.util.Set;

// 10, -402.01
public record JSNumberConstantTypeInfo(Number number) implements TypeInfo {
@Override
Expand All @@ -16,4 +19,13 @@ public String toString() {
public void append(TypeStringContext ctx, StringBuilder sb) {
sb.append(number);
}

@Override
public void collectContainedComponentClasses(Collection<Class<?>> classes) {
}

@Override
public Set<Class<?>> getContainedComponentClasses() {
return Set.of();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.rhino.type;

import java.util.Collection;
import java.util.List;

// {a: string, b?: number}
Expand Down Expand Up @@ -45,4 +46,11 @@ public void append(TypeStringContext ctx, StringBuilder sb) {

sb.append('}');
}

@Override
public void collectContainedComponentClasses(Collection<Class<?>> classes) {
for (var field : fields) {
field.type().collectContainedComponentClasses(classes);
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/dev/latvian/mods/rhino/type/JSOrTypeInfo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.latvian.mods.rhino.type;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

// string | number
Expand Down Expand Up @@ -42,4 +43,11 @@ public void append(TypeStringContext ctx, StringBuilder sb) {
ctx.append(sb, types.get(i));
}
}

@Override
public void collectContainedComponentClasses(Collection<Class<?>> classes) {
for (var type : types) {
type.collectContainedComponentClasses(classes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import dev.latvian.mods.rhino.ScriptRuntime;

import java.util.Collection;
import java.util.Set;

// "abc"
public record JSStringConstantTypeInfo(String constant) implements TypeInfo {
public static final JSStringConstantTypeInfo EMPTY = new JSStringConstantTypeInfo("");
Expand All @@ -20,4 +23,13 @@ public String toString() {
public void append(TypeStringContext ctx, StringBuilder sb) {
sb.append(ScriptRuntime.escapeAndWrapString(constant));
}

@Override
public void collectContainedComponentClasses(Collection<Class<?>> classes) {
}

@Override
public Set<Class<?>> getContainedComponentClasses() {
return Set.of();
}
}
12 changes: 12 additions & 0 deletions src/main/java/dev/latvian/mods/rhino/type/NoTypeInfo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.latvian.mods.rhino.type;

import java.util.Collection;
import java.util.Set;

final class NoTypeInfo implements TypeInfo {
@Override
public Class<?> asClass() {
Expand Down Expand Up @@ -40,4 +43,13 @@ public TypeInfo asArray() {
public TypeInfo withParams(TypeInfo... params) {
return this;
}

@Override
public void collectContainedComponentClasses(Collection<Class<?>> classes) {
}

@Override
public Set<Class<?>> getContainedComponentClasses() {
return Set.of();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.latvian.mods.rhino.type;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -107,4 +108,13 @@ public Map<String, RecordTypeInfo.Component> recordComponents() {
public List<Object> enumConstants() {
return rawType.enumConstants();
}

@Override
public void collectContainedComponentClasses(Collection<Class<?>> classes) {
rawType.collectContainedComponentClasses(classes);

for (var param : params) {
param.collectContainedComponentClasses(classes);
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/dev/latvian/mods/rhino/type/TypeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Collection;
import java.util.Date;
import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -62,6 +65,7 @@ public interface TypeInfo {
TypeInfo RAW_SET = new InterfaceTypeInfo(Set.class, Boolean.FALSE);
TypeInfo RAW_MAP = new InterfaceTypeInfo(Map.class, Boolean.FALSE);
TypeInfo RAW_OPTIONAL = new BasicClassTypeInfo(Optional.class);
TypeInfo RAW_ENUM_SET = new BasicClassTypeInfo(EnumSet.class);

Class<?> asClass();

Expand Down Expand Up @@ -130,6 +134,8 @@ static TypeInfo of(Class<?> c) {
return DATE;
} else if (c == Optional.class) {
return RAW_OPTIONAL;
} else if (c == EnumSet.class) {
return RAW_ENUM_SET;
} else if (c == Runnable.class) {
return RUNNABLE;
} else if (c == Consumer.class) {
Expand Down Expand Up @@ -294,4 +300,14 @@ default boolean isDouble() {
default boolean isCharacter() {
return false;
}

default void collectContainedComponentClasses(Collection<Class<?>> classes) {
classes.add(asClass());
}

default Set<Class<?>> getContainedComponentClasses() {
var set = new LinkedHashSet<Class<?>>();
collectContainedComponentClasses(set);
return set;
}
}

0 comments on commit 8c5f45c

Please sign in to comment.