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

Gizmo2: introduce InstanceExecutableCreator#this_() #224

Merged
merged 1 commit into from
Mar 11, 2025
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.quarkus.gizmo2.creator;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

import io.quarkus.gizmo2.Expr;

Expand All @@ -11,10 +11,15 @@ public sealed interface InstanceExecutableCreator extends ExecutableCreator perm

/**
* Build the body.
* The builder accepts the outermost block, and the {@code this} expression.
* The builder accepts the outermost block.
*
* @param builder the builder (must not be {@code null})
*/
void body(BiConsumer<BlockCreator, Expr> builder);
void body(Consumer<BlockCreator> builder);

/**
* @return the {@code this} expression
*/
Expr this_();

}
13 changes: 9 additions & 4 deletions src/main/java/io/quarkus/gizmo2/impl/ConstructorCreatorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.lang.constant.ConstantDescs;
import java.lang.constant.MethodTypeDesc;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import io.github.dmlloyd.classfile.CodeBuilder;
Expand All @@ -24,7 +23,8 @@ public final class ConstructorCreatorImpl extends ExecutableCreatorImpl implemen
public ConstructorDesc desc() {
ConstructorDesc desc = this.desc;
if (desc == null) {
MethodTypeDesc mtd = MethodTypeDesc.of(ConstantDescs.CD_void, params.stream().map(ParamVarImpl::type).toArray(ClassDesc[]::new));
MethodTypeDesc mtd = MethodTypeDesc.of(ConstantDescs.CD_void,
params.stream().map(ParamVarImpl::type).toArray(ClassDesc[]::new));
this.desc = desc = ConstructorDesc.of(owner(), mtd);
}
return desc;
Expand All @@ -35,8 +35,13 @@ void doCode(final Consumer<BlockCreator> builder, final CodeBuilder cb, final Li
super.doCode(builder, cb, params);
}

public void body(final BiConsumer<BlockCreator, Expr> builder) {
super.body(bc -> builder.accept(bc, new ThisExpr(owner())));
public void body(final Consumer<BlockCreator> builder) {
super.body(bc -> builder.accept(bc));
}

@Override
public Expr this_() {
return new ThisExpr(owner());
}

public void withFlag(final AccessFlag flag) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.quarkus.gizmo2.impl;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import io.github.dmlloyd.classfile.CodeBuilder;
Expand All @@ -20,8 +19,13 @@ void doCode(final Consumer<BlockCreator> builder, final CodeBuilder cb, final Li
super.doCode(builder, cb, params);
}

public void body(final BiConsumer<BlockCreator, Expr> builder) {
super.body(bc -> builder.accept(bc, new ThisExpr(owner())));
public void body(final Consumer<BlockCreator> builder) {
super.body(bc -> builder.accept(bc));
}

@Override
public Expr this_() {
return new ThisExpr(owner());
}

public void withFlag(final AccessFlag flag) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.quarkus.gizmo2.impl;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import io.github.dmlloyd.classfile.CodeBuilder;
Expand All @@ -20,8 +19,13 @@ void doCode(final Consumer<BlockCreator> builder, final CodeBuilder cb, final Li
super.doCode(builder, cb, params);
}

public void body(final BiConsumer<BlockCreator, Expr> builder) {
super.body(bc -> builder.accept(bc, new ThisExpr(owner())));
public void body(final Consumer<BlockCreator> builder) {
super.body(bc -> builder.accept(bc));
}

@Override
public Expr this_() {
return new ThisExpr(owner());
}

public void withFlag(final AccessFlag flag) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.quarkus.gizmo2.impl;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import io.github.dmlloyd.classfile.CodeBuilder;
Expand All @@ -20,8 +19,13 @@ void doCode(final Consumer<BlockCreator> builder, final CodeBuilder cb, final Li
super.doCode(builder, cb, params);
}

public void body(final BiConsumer<BlockCreator, Expr> builder) {
super.body(bc -> builder.accept(bc, new ThisExpr(owner())));
public void body(final Consumer<BlockCreator> builder) {
super.body(bc -> builder.accept(bc));
}

@Override
public Expr this_() {
return new ThisExpr(owner());
}

public void withFlag(final AccessFlag flag) {
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/io/quarkus/gizmo2/InstanceExecutableCreatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.quarkus.gizmo2;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.lang.constant.ClassDesc;
import java.util.function.Function;

import org.junit.jupiter.api.Test;

import io.github.dmlloyd.classfile.Signature.ClassTypeSig;
import io.github.dmlloyd.classfile.Signature.TypeArg;
import io.github.dmlloyd.classfile.extras.reflect.AccessFlag;
import io.quarkus.gizmo2.desc.ConstructorDesc;
import io.quarkus.gizmo2.desc.MethodDesc;

public class InstanceExecutableCreatorTest {

@SuppressWarnings("unchecked")
@Test
public void testThis() {
TestClassMaker tcm = new TestClassMaker();
Gizmo g = Gizmo.create(tcm);
g.class_(ClassDesc.of("io.quarkus.gizmo2.TestFun"), cc -> {
cc.implements_(ClassTypeSig.of(Function.class.getName(), TypeArg.of(ClassTypeSig.of(String.class.getName()))));
cc.withFlag(AccessFlag.PROTECTED);
cc.constructor(con -> {
con.withFlag(AccessFlag.PUBLIC);
con.body(bc -> {
bc.invokeSpecial(ConstructorDesc.of(Object.class), con.this_());
bc.return_();
});
});
MethodDesc convert = cc.method("convert", mc -> {
ParamVar p = mc.parameter("value", String.class);
mc.returning(String.class);
mc.body(bc -> {
// return val.toLowerCase();
Expr ret = bc.invokeVirtual(MethodDesc.of(String.class, "toLowerCase", String.class), p);
bc.return_(ret);
});
});
cc.method("apply", mc -> {
ParamVar p = mc.parameter("t", Object.class);
mc.returning(Object.class);
mc.withFlag(AccessFlag.PUBLIC);
mc.body(bc -> {
// return convert((String)t);
Expr strVal = bc.cast(p, String.class);
bc.return_(bc.invokeVirtual(convert, mc.this_(), strVal));
});
});
});
assertEquals("foo", tcm.noArgsConstructor(Function.class).apply("Foo"));
}

}
12 changes: 12 additions & 0 deletions src/test/java/io/quarkus/gizmo2/TestClassMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ public <T> T constructor(Class<T> asType) {
throw new IllegalAccessError(e.getMessage());
}
}

public <T> T noArgsConstructor(Class<T> asType) {
try {
return (T) lookup.findConstructor(lookup.lookupClass(), MethodType.methodType(void.class)).invoke();
} catch (NoSuchMethodException e) {
throw new NoSuchMethodError(e.getMessage());
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
} catch (Throwable e) {
throw new IllegalStateException(e);
}
}

private static Method findSAMSimple(Class<?> type) {
if (! type.isInterface()) {
Expand Down