Skip to content

Commit

Permalink
Added support for implicit constructors on Java types, including stat…
Browse files Browse the repository at this point in the history
…ic methods as implicit constructors
  • Loading branch information
stanhebben committed Sep 6, 2024
1 parent 6a22089 commit e7278c6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ enum OperatorType {
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.CONSTRUCTOR)
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD})
@interface Constructor {
boolean implicit() default false;
}

@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ public StaticCallable getConstructor() {

@Override
public Optional<StaticCallable> findImplicitConstructor() {
List<StaticCallableMethod> constructors = Stream.concat(
template.getConstructors().stream().filter(c -> c.getModifiers().isImplicit()),
template.getAllMethods().filter(c -> c.getModifiers().isStatic() && c.getModifiers().isImplicit() && c.getHeader().getReturnType().equals(getType()))
)
List<StaticCallableMethod> constructors = template.getConstructors().stream()
.filter(c -> c.getModifiers().isImplicit())
.map(c -> mapper.map(type, c))
.collect(Collectors.toList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,24 @@ public class JavaNativeTypeTemplate {

public List<MethodSymbol> getConstructors() {
if (constructors == null) {
List<MethodSymbol> result = new ArrayList<>();
for (Constructor<?> constructor : class_.cls.getConstructors()) {
if (constructor.isAnnotationPresent(ZenCodeType.Constructor.class)) {
result.add(loadJavaMethod(constructor));
}
}
this.constructors = result;
loadConstructors();
}
return this.constructors;
}

public Stream<MethodSymbol> getAllMethods() {
if (methods == null) {
loadMethods();
private void loadConstructors() {
List<MethodSymbol> result = new ArrayList<>();
for (Constructor<?> constructor : class_.cls.getConstructors()) {
if (constructor.isAnnotationPresent(ZenCodeType.Constructor.class)) {
result.add(loadJavaMethod(constructor, constructor.getAnnotation(ZenCodeType.Constructor.class).implicit()));
}
}
for (Method method : class_.cls.getMethods()) {
if (method.isAnnotationPresent(ZenCodeType.Constructor.class)) {
result.add(loadMethodAsConstructor(method, method.getAnnotation(ZenCodeType.Constructor.class).implicit()));
}
}
return methods.values().stream().flatMap(List::stream);
this.constructors = result;
}

public JavaNativeTypeTemplate(TypeID target, JavaRuntimeClass class_, TypeVariableContext typeVariableContext, boolean expansion) {
Expand Down Expand Up @@ -229,11 +231,20 @@ private boolean isOverridden(Class<?> cls, Method method) {
return !method.getDeclaringClass().equals(cls) || method.isBridge();
}

protected MethodSymbol loadJavaMethod(Constructor<?> constructor) {
protected MethodSymbol loadJavaMethod(Constructor<?> constructor, boolean implicit) {
JavaNativeHeaderConverter headerConverter = class_.module.getHeaderConverter();
FunctionHeader header = headerConverter.getHeader(typeVariableContext, constructor);
header.setReturnType(target); // In ZC, .ctors return the instantiated type
JavaRuntimeMethod method = new JavaRuntimeMethod(class_, target, constructor, header, false);
JavaRuntimeMethod method = new JavaRuntimeMethod(class_, target, constructor, header, implicit);
class_.module.getCompiled().setMethodInfo(method, method);
return method;
}

private MethodSymbol loadMethodAsConstructor(Method javaMethod, boolean implicit) {
JavaNativeHeaderConverter headerConverter = class_.module.getHeaderConverter();
FunctionHeader header = headerConverter.getHeader(typeVariableContext, javaMethod);
header.setReturnType(target); // In ZC, .ctors return the instantiated type
JavaRuntimeMethod method = new JavaRuntimeMethod(class_, target, javaMethod, MethodID.staticMethod(javaMethod.getName()), header, implicit);
class_.module.getCompiled().setMethodInfo(method, method);
return method;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private MyClass(String name) {
this.name = name;
}

@ZenCodeType.Method(implicit = true)
@ZenCodeType.Constructor(implicit = true)
public static MyClass create(String name) {
return new MyClass(name);
}
Expand All @@ -61,7 +61,7 @@ private AnotherType(String value) {
this.value = value;
}

@ZenCodeType.Method(implicit = true)
@ZenCodeType.Constructor(implicit = true)
public static AnotherType create(String value) {
return new AnotherType(value);
}
Expand Down

0 comments on commit e7278c6

Please sign in to comment.