Add Ability to Generate Java Bean from an Interface
See original GitHub issueGiven the following interface I would like to be able to generate a bean with minimal effort using ByeBuddy. Ideally, I would like to simply give ByteBuddy an interface with getter and setter pairs and have it take care of doing the analysis on the interface and create a dynamic java bean.
public interface UserConfig {
String getName();
void setName(String name);
int getAge();
void setAge(int age);
}
Issue Analytics
- State:
- Created 9 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How to create a bean as instance of Interface Class
You need to have a Class annotated with @Configuration if using Java configuration for you to create your beans, secondly, the Configuration class...
Read more >Spring Beans With Auto-Generated Implementations - DZone
This tutorial will guide you through the process of building a sample framework that implements the automatic creation of Spring beans from Java...
Read more >Chapter 4. Creating and using bean definitions - Spring
As JavaConfig encounters the VisibilityConfiguration class, it will create 3 beans : publicBean , hiddenBean and secretBean . All of them can see...
Read more >Spring Tutorial - 7 - Coding Beans to an Interface - YouTube
In this episode, I show you how to make an interface for a series of beans that do the same thing and then...
Read more >Commons Configuration – Declaring Beans Howto
A bean factory: This is an object that implements the BeanFactory interface and knows how to create an instance of a bean class....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Implementing the methods is quite easy as you can use the
FileAccessor.ofBeanProperty()
interception. However, you would still need to define the fields. With Byte Buddy, you would therefore define a bean as follows:I take it that you want Byte Buddy to generate the fields for you? While I understand that this is convenient, I feel that it is somewhat out of scope for the library as one can easily write a custom adapter. For example:
This way, one could for example also add annotations, custom visibility, custom names, different field mappings, etc. If Byte Buddy did all this automatically, it would take this freedom away. In Java 8, the above code can even be expressed as a one liner.
I would recommend you to hide this away in an adapter or even by subclassing Byte Buddy and overriding the
subclass
method and everything is done under the covers. Would this be a solution for you?Thanks for the example above, @raphw. You might want to rename toLowerClase to toLowerCase though.