question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Ability to Generate Java Bean from an Interface

See original GitHub issue

Given 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:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
raphwcommented, Jan 1, 2018

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:

new ByteBuddy()
  .subclass(UserConfig.class)
  .method(isGetter().or(isSetter())).intercept(FieldAccessor.ofBeanProperty())
  .defineField("name", String.class, Visibility.PRIVATE)
  .defineField("age", int.class, Visibility.PRIVATE)
  .make()

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:

<T> DynamicType.Builder<T> fields(DynamicType.Builder<T> builder, Class<?> type) {
  for(Method method : type.getDeclaredMethods()) {
    if(method.getName().startsWith("set") {
      String name = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);
      builder = builder.defineField(name, method.getParameterTypes()[0], Visibility.PRIVATE);
    }
  }
  return builder;
}

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?

0reactions
Flamencocommented, Dec 25, 2017

Thanks for the example above, @raphw. You might want to rename toLowerClase to toLowerCase though.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found