-
Notifications
You must be signed in to change notification settings - Fork 4
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
Byte code generator #167
base: 1.4.x
Are you sure you want to change the base?
Byte code generator #167
Conversation
sourcegen-generator-bytecode/src/main/java/io/micronaut/sourcegen/ByteCodeGenerator.java
Outdated
Show resolved
Hide resolved
sourcegen-generator-bytecode/src/main/java/io/micronaut/sourcegen/ByteCodeGenerator.java
Outdated
Show resolved
Hide resolved
sourcegen-generator-bytecode/src/main/java/io/micronaut/sourcegen/ByteCodeGenerator.java
Outdated
Show resolved
Hide resolved
sourcegen-generator-bytecode/src/main/java/io/micronaut/sourcegen/ByteCodeGenerator.java
Outdated
Show resolved
Hide resolved
sourcegen-generator-bytecode/src/main/java/io/micronaut/sourcegen/ByteCodeGenerator.java
Outdated
Show resolved
Hide resolved
sourcegen-generator-bytecode/src/main/java/io/micronaut/sourcegen/ByteCodeGenerator.java
Outdated
Show resolved
Hide resolved
throw new IllegalStateException("Field " + field.name() + " is not available in [" + classDef + "]:" + classDef.getFields()); | ||
} | ||
} else { | ||
throw new IllegalStateException("Field access no supported on the object definition: " + context.objectDef); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw new IllegalStateException("Field access no supported on the object definition: " + context.objectDef); | |
throw new IllegalStateException("Field access not supported on the object definition: " + context.objectDef); |
sourcegen-generator-bytecode/src/main/java/io/micronaut/sourcegen/ByteCodeGenerator.java
Outdated
Show resolved
Hide resolved
sourcegen-generator-bytecode/src/main/java/io/micronaut/sourcegen/ByteCodeGenerator.java
Outdated
Show resolved
Hide resolved
sourcegen-generator-bytecode/src/main/java/io/micronaut/sourcegen/SignatureWriterUtils.java
Outdated
Show resolved
Hide resolved
sourcegen-generator-bytecode/src/main/java/io/micronaut/sourcegen/ByteCodeGenerator.java
Outdated
Show resolved
Hide resolved
sourcegen-generator-bytecode/src/main/java/io/micronaut/sourcegen/ByteCodeGenerator.java
Outdated
Show resolved
Hide resolved
if (context.objectDef == null) { | ||
throw new IllegalStateException("Accessing 'super' is not available"); | ||
} | ||
generatorAdapter.loadThis(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it loadThis
for super?
ExpressionDef left, | ||
ExpressionDef right) implements ExpressionDef { | ||
@Override | ||
public TypeDef type() { | ||
return TypeDef.Primitive.BOOLEAN; | ||
return left.type(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this break something because 1.0f + 2.0d = 3.0d
? I suppose users could explicitly cast to double if needed though.
* @author Denis Stepanov | ||
* @since 1.4 | ||
*/ | ||
sealed interface ConditionExpressionDef extends ExpressionDef { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call this BooleanExpressionDef
?
Please review it again.
Let's merge it and move from there |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, but I would refactor ByteCodeWriter
to make it more maintainable since it is a monster now
gradle.properties
Outdated
@@ -1,4 +1,4 @@ | |||
projectVersion=1.4.2-SNAPSHOT | |||
projectVersion=1.4.900-SNAPSHOT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might want to revert this not sure what its for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I change it to publish a local version, which will not likely conflict with an existing one
sourcegen-model/src/main/java/io/micronaut/sourcegen/model/FieldDef.java
Outdated
Show resolved
Hide resolved
sourcegen-model/src/main/java/io/micronaut/sourcegen/model/MethodDef.java
Show resolved
Hide resolved
sourcegen-model/src/main/java/io/micronaut/sourcegen/model/MethodDef.java
Outdated
Show resolved
Hide resolved
sourcegen-model/src/main/java/io/micronaut/sourcegen/model/MethodDef.java
Outdated
Show resolved
Hide resolved
* @param objectDef The object definition | ||
*/ | ||
public void writeObject(ClassVisitor classVisitor, ObjectDef objectDef) { | ||
if (objectDef instanceof ClassDef classDef) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like here instead of all this if/else it would be good to use inheritance and have different implementations for records, classes, interfaces etc.
* @param statementDef The statement definition | ||
* @param finallyBlock The runnable that should be invoked before any returning operation - return/throw | ||
*/ | ||
public void pushStatement(GeneratorAdapter generatorAdapter, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is another case that could do with refactoring. Something like:
StatementWriter.for(statementDef).write(generator, context, finallyBlock)
// if (context.objectDef instanceof ClassDef classDef) { | ||
// if (!classDef.hasField(field.name()) && classDef.getProperties().stream().noneMatch(prop -> prop.getName().equals(field.name()))) { | ||
// throw new IllegalStateException("Field '" + field.name() + "' is not available in [" + classDef + "]:" + classDef.getFields()); | ||
// } | ||
// } else { | ||
// throw new IllegalStateException("Field access not supported on the object definition: " + context.objectDef); | ||
// } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this code is dead delete it
// if (context.objectDef == null) { | ||
// throw new IllegalStateException("Accessing 'this' is not available"); | ||
// } | ||
// if (context.objectDef instanceof ClassDef classDef) { | ||
// if (!classDef.hasField(field.name()) && classDef.getProperties().stream().noneMatch(prop -> prop.getName().equals(field.name()))) { | ||
// throw new IllegalStateException("Field '" + field.name() + "' is not available in [" + classDef + "]:" + classDef.getFields()); | ||
// } | ||
// } else { | ||
// throw new IllegalStateException("Field access not supported on the object definition: " + context.objectDef); | ||
// } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this code is dead delete it
throw new UnsupportedOperationException("Unrecognized variable: " + variableDef); | ||
} | ||
|
||
private void pushConstant(GeneratorAdapter generatorAdapter, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like:
ConstantWriter.for(constant).write(generator)
would be good
@graemerocher Refactored out statement/expression writers. Do you want to make the |
Quality Gate failedFailed conditions See analysis details on SonarQube Cloud Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE |
Added a way to generate the bytecode directly; currently, if added, it will generate bytecode instead of JAVA sources.
I have added the bytecode checker by default to find bugs directly; it might be an optional feature in the future.
I have copied existing tests for Java generator. The missing features:
Includes a lot of expression improvement and cleanup. Builders adjusted to invoke methods based on the original signature if generic.
I will experiment next in Core to see what is missing / not working.