-
-
Notifications
You must be signed in to change notification settings - Fork 778
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
Define a field with initial value #227
Comments
This depends very much on what you want to do. You can set an initial constant value easily using the |
Thanks for your advise. I set an initial value using the |
You cannot set a complex value this way, the Java class file format does not support this. You would need to either intercept constructors to do this or define a loaded type initializer. |
Hi @raphw, I would like to initialize a instance field for the dynamically created class, from builder API, with .defineField("name", String.class, Opcodes.ACC_PRIVATE).value("SOMETHING") it's not initializing the field and not generating getter and setter methods. And if i use .defineProperty("name", String.class) doesn't allow me to initialize the field, but it generates getter and setter methods. i should be able to create a class at run time with a field initialized along with getter and setter methods for that particular field something like this.
How do we generate above piece of code with byte-buddy? |
The This might be confusing (and it is) but this regards compiler semantics. If you referred the field in another Java class and compiled it using javac, it would assume this value as the compiler copies such constants. But if this value should be set at runtime, you need to implement or adjust any constructor to set the field's value within the constructor. I will update the javadoc to make this more obvious. |
I want to define a field with initial value. Is byte-buddy support this?
The text was updated successfully, but these errors were encountered: