Comments (6)
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?
from byte-buddy.
That's pretty much what I am doing. I only ask that bytebuddy provide such functionally because it seems like a common usecase. Perhaps bytebuddy should provide utility classes for common usescases so people don't have have to reinvent the wheel.
from byte-buddy.
I will give it some thought. I do not want to bloat the library with too many common use cases as this caused quite some trouble with cglib where something similar was attempted. But I see the use of it.
from byte-buddy.
I appreciate that.
from byte-buddy.
I decided against an explicit API. From my experience with cglib, this can be better solved by an external library which is specialized in handling exceptions. However, I decided to add some convenience methods to make adding methods from reflection results more easy. I hope you can live with this compromise.
Thank you again for using Byte Buddy and your feedback!
from byte-buddy.
Thanks for the example above, @raphw. You might want to rename toLowerClase to toLowerCase though.
from byte-buddy.
Related Issues (20)
- Replace Method Code HOT 3
- Can I replace the new byte[] in the onTransformation method? HOT 1
- I want to use bytebuddy to advice java.time.LocalDateTime HOT 3
- BUG: instrumentation error HOT 3
- No classes have been predefined during the image build to load from bytecodes at runtime HOT 8
- Verify Error on Byte Buddy generated class HOT 1
- Question: copy class hierarchy in an environment without Java Agent HOT 1
- Can 'private final method' in 'public final class' be redefined? HOT 4
- Method matchers works by chance HOT 2
- Why does ClassNotFoundException appear in Interceptor when running in SpringBoot? HOT 1
- Compilation error after adding lombok to classpath HOT 1
- why is the byte-buudy AgentBuilder source code so long ? HOT 2
- Invalid module descriptor of byte-buddy `jar` when `JAVA_HOME` is set to version 21 HOT 16
- matched class wasn't transformered and the issue appears just twice, and i didn't reproducing the issue HOT 10
- Fail to read property due to `IllegalArgumentException` when using it on withX methods HOT 4
- Improve diagnostics/troubleshooting features for agent installation HOT 1
- ByteBudy agent required by Mockito fails to install on GraalVM HOT 1
- Error when trying to modify the response of a method assigned to a public static final config variable HOT 9
- How to change the loaded class in javaagent? HOT 5
- [Question] AgentBuilder.InitializationStrategy.SelfInjection's behaviour in detail HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from byte-buddy.