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

Define a field with initial value #227

Closed
ascrutae opened this issue Dec 5, 2016 · 5 comments
Closed

Define a field with initial value #227

ascrutae opened this issue Dec 5, 2016 · 5 comments
Assignees
Labels
Milestone

Comments

@ascrutae
Copy link

ascrutae commented Dec 5, 2016

I want to define a field with initial value. Is byte-buddy support this?

@raphw
Copy link
Owner

raphw commented Dec 5, 2016

This depends very much on what you want to do. You can set an initial constant value easily using the defineField or field API of the DynamicType.Builder. If you want to set a field from a constructor, have a look at the FieldAccessor API. If you want to define a static field with a value, you can register a LoadedTypeInitializer that does so or intercept the type initializer by ElementMatchers::isTypeInitializer.

@raphw raphw added the question label Dec 5, 2016
@raphw raphw self-assigned this Dec 5, 2016
@raphw raphw added this to the 1.5.8 milestone Dec 5, 2016
@ascrutae
Copy link
Author

ascrutae commented Dec 6, 2016

Thanks for your advise. I set an initial value using the defineField API to set the field. but the type of field is complex type. but I cannot found any API to set the value of complex type. could you show some example?

@raphw
Copy link
Owner

raphw commented Dec 6, 2016

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.

@rajesh-bandarupalli
Copy link

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.

public class Sample {
	private String name = "SOMETHING";
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

How do we generate above piece of code with byte-buddy?

@raphw
Copy link
Owner

raphw commented Jul 24, 2019

The value assignment sets the constant value of the field which is not the runtime value.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants