Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

CodeGen API Simplification #34

Closed
sirinath opened this issue Nov 3, 2020 · 9 comments
Closed

CodeGen API Simplification #34

sirinath opened this issue Nov 3, 2020 · 9 comments

Comments

@sirinath
Copy link
Contributor

sirinath commented Nov 3, 2020

I believe the codegen API can be simplified. E.g.

.withMethod("setIdAndName", sequence(
				set(property(self(), "id"), arg(0)),
				set(property(self(), "name"), arg(1))))

can become

.method("setIdAndName").args("id", "name").of(int.class, String.class).body(statements(assign("this.id", "this.name").to("id", "name")))

or

.method("setIdAndName").body(assign("this.id", "this.name").to("id", "name"))

as Person defines the arguments and types this is implied.

Similarly

.withField("id", int.class)
...
.withMethod("getId", property(self(), "id"))

can become

.getter("id", int.class)

Also,

.withMethod("hashOfPojo", hash(property(arg(0), "id"), property(arg(0), "name")))

can become

.method("hashOfPojo").args("personPojo").of("ExamplePojo").returning(int.class).body(statements(returning(hash("personPojo.id", "personPojo.name"))))

or

.method("hashOfPojo").body(returning(hash("personPojo.id", "personPojo.name")))

as Person defines the arguments

etc.

Basically,

  • add shortcuts to all JavaBeans specific constructs
  • Use builder pattern and fluent API
  • a simple way to access arguments, fields, etc.
  • a simple way to set fields and return values
@valerialistratova
Copy link
Contributor

Hi @sirinath , thanks for your suggestion!

The main concept behind code generation is implementing an interface that will be used by calling its methods. And ActiveCodegen perfectly supports this use case.

The methods that are generated by ActiveCodegen can be called from a Java class only if the implemented interface declares this method. Thus, you don’t need to generate getter and setter methods.

As for the builder pattern for creating methods and fields, ActiveCodegen has a very flexible and universal DSL that allows for generating any methods and fields with the help of AST expressions composition.

@sirinath
Copy link
Contributor Author

sirinath commented Nov 4, 2020

I think you can make reflection-based calls, MethodHandles, LambdaMetafactory. and using javax.tools.JavaCompiler to generated code.

The AST scheme is difficult to use. If the building is more close to Java code it would be better. E.g. JavaPoet.

@sirinath
Copy link
Contributor Author

sirinath commented Nov 5, 2020

The higher-level API can generate and return the AST representation.

@valerialistratova
Copy link
Contributor

The primary goal of ActiveCodegen is to dynamically construct, generate and manipulate generated code directly in runtime, with respect to runtime parameters. The methods are not just static plaintext code (which could have been written as regular Java code without any code generators at all). That's why AST DSL is an optimal solution for that. In this way, you don’t need to use any additional template engines to generate dynamic plaintext.

@sirinath
Copy link
Contributor Author

sirinath commented Nov 5, 2020

I am not looking to generate any plain text but directly working with the AST is not easy other than for trivial use cases. So that is why I am asking for a helper API which is more high level and easier to use.

@eduard-vasinskyi
Copy link
Contributor

Hello, @sirinath
You may design your own API on top of ActiveCodegen, which will be suitable for your business logic. We plan to leave ActiveCodegen as simple and focused as it is now, without bloating the API.

@sirinath
Copy link
Contributor Author

sirinath commented Dec 15, 2020

Since this is on top of ASM can ActiveCodegen be enhanced to utilise all of ASM capabilities.

simple and focused as it is now, without bloating the API.

This is what I am after also. I feel maybe the API surface and be reduced and simplifies retaining the current functionality.

Also add more API to expose more functionality, but what is added should be simple and focused.

If the the API functionality follows most generalised Java constructs then the API will become minimal and simple.

@eduard-vasinskyi
Copy link
Contributor

Here we have provided some of the goals and design decisions behind ActiveCodegen. It is a tool for dynamic class generation based on runtime parameters and information available in runtime. It supports code generation using functional composition of LISP-like expressions and ASTs. The current API is completely suitable for this task.

@sirinath
Copy link
Contributor Author

It supports code generation using functional composition of LISP-like expressions and ASTs.

Since LISP like expressions and ASTs is supported. Perhaps some additional abilities which might prove to be useful:

  • convert byte-code into and S-expression/AST representation,
  • analyse S-expression/AST representation,
  • search for patterns in the S-expression/AST representation,
  • modify the S-expression/AST representation,
  • write results back modifying the class files.

@activej activej locked and limited conversation to collaborators Feb 17, 2022
@eduard-vasinskyi eduard-vasinskyi converted this issue into discussion #184 Feb 17, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Development

No branches or pull requests

3 participants