Skip to content

Commit

Permalink
Added support for Collections (java only)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanhebben committed Oct 25, 2024
1 parent fc48652 commit 30770bc
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
import org.openzen.zenscript.codemodel.compilation.ResolvingType;
import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol;
import org.openzen.zenscript.codemodel.identifiers.TypeSymbol;
import org.openzen.zenscript.codemodel.member.IDefinitionMember;
import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
import org.openzen.zenscript.codemodel.type.TypeID;
import org.openzen.zenscript.codemodel.type.member.InterfaceResolvingType;
import org.openzen.zenscript.codemodel.type.member.MemberSet;

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

public class InterfaceDefinition extends HighLevelDefinition {
Expand All @@ -39,6 +43,19 @@ public <C, R> R accept(C context, DefinitionVisitorWithContext<C, R> visitor) {

@Override
public ResolvingType resolve(TypeID[] typeArguments) {
return InterfaceResolvingType.of(super.resolve(typeArguments), baseInterfaces);
if (baseInterfaces.isEmpty()) {
return InterfaceResolvingType.of(super.resolve(typeArguments), Collections.emptyList());
} else {
TypeID type = DefinitionTypeID.create(this, typeArguments);

MemberSet.Builder members = MemberSet.create(type);
GenericMapper mapper = GenericMapper.create(typeParameters, typeArguments);
for (IDefinitionMember member : this.members) {
member.registerTo(type, members, mapper);
}

TypeID[] baseInterfaces = this.baseInterfaces.toArray(TypeID.NONE);
return InterfaceResolvingType.of(super.resolve(typeArguments), Arrays.asList(mapper.map(baseInterfaces)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Optional<TypeSymbol> resolveJavaType(Class<?> javaType) {

private void fillJavaClassToZenCodeClassName() {
fillMapping(List.class, "stdlib.List");
fillMapping(Collection.class, "stdlib.List");
fillMapping(Collection.class, "javalib.Collection");
fillMapping(Comparable.class, "stdlib.Comparable");
fillMapping(Exception.class, "stdlib.Exception");
fillMapping(IllegalArgumentException.class, "stdlib.IllegalArgumentException");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class JavaNativeTypeTemplate {
protected final JavaRuntimeClass class_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public class JavaPrepareDefinitionVisitor implements DefinitionVisitor<JavaClass
nativeClasses.put("collections::Set", cls);
}

{
JavaNativeClass cls = new JavaNativeClass(new JavaClass("java.util", "Collection", JavaClass.Kind.INTERFACE));
cls.addInstanceMethod("add", "add", "(Ljava/lang/Object;)Z");
cls.addInstanceMethod("remove", "remove", "(Ljava/lang/Object;)Z");
cls.addInstanceMethod("contains", "contains", "(Ljava/lang/Object;)Z");
cls.addMethod("toArray", JavaSpecialMethod.COLLECTION_TO_ARRAY);
cls.addInstanceMethod("length", "size", "()I");
cls.addInstanceMethod("isEmpty", "isEmpty", "()Z");
cls.addInstanceMethod("iterate", "iterator", "()Ljava/util/Iterator;");
nativeClasses.put("javalib::Collection", cls);
}

{
JavaNativeClass cls = new JavaNativeClass(new JavaClass("java.util", "HashSet", JavaClass.Kind.CLASS));
cls.addConstructor("constructor", "()V");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
import org.openzen.zenscript.scriptingexample.tests.helpers.ScriptBuilder;
import org.openzen.zenscript.scriptingexample.tests.helpers.ZenCodeTest;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.*;


class JavaNativeSpecialTypeTests extends ZenCodeTest {

@Override
public List<String> getRequiredStdLibModules() {
return Collections.singletonList("stdlib");
return Arrays.asList("stdlib", "javalib");
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion StdLibs

0 comments on commit 30770bc

Please sign in to comment.