Comments (6)
As for private constructors, there is no possibility to add them as they could not call their private super constructor equivalent. Private members are not visible even to their sub types. I think, cglib and javassist do not add private constructors to their proxy types.
As for package-private members, it depends. A package-private super constructor could only be called if the defined class was defined in the same package as its super class. This cannot be assured at the point the constructor strategy is applied. I will however add another strategy in version 0.2 that allows for adding package-private members on demand. For now, implementing the strategy is however quite straight-forward:
enum MyConstrutorStrategy implements ConstructorStrategy {
@Override
public MethodList extractConstructors(TypeDescription superType) {
return superType.getDeclaredMethods().filter(not(isPrivate()));
}
@Override
public MethodRegistry inject(MethodRegistry methodRegistry,
MethodAttributeAppender.Factory defaultMethodAttributeAppenderFactory) {
return methodRegistry.append(new MethodRegistry.LatentMethodMatcher.Simple(isConstructor()),
SuperMethodCall.INSTANCE,
defaultMethodAttributeAppenderFactory);
}
}
Are you using code generation for creating proxies of random types? You should really consider using a library such as objenesis for construction and use the NO
. When dealing with package-private classes, you cannot assure that there is a package-private constructor available that you can call. The Java compiler normally adds an additional synthetic constructor to the class that takes an additional synthetic class as its argument to distinguish itself from the normal constructor. You would have to resolve this bridge if you wanted to construct an instance of such a class.
from byte-buddy.
All right, after looking into the matter, I reworked the ConstructorStrategy
to only be applied after the type definition phase is finalized. I ran some benchmarks and it does not create any overhead to delay the application to this later phase such that the constructor strategy can be applied to a fully defined type. This way, ConstructorStrategy.Default.IMITATE_SUPER_TYPE
creates package-private constructors if they are visible. Private constructors are never copied as they are not invokable from their sub type. This resembles the behavior of cglib and javassist. The change will be included in Byte Buddy 0.2 which should be released some time in June 2014. Thanks for your feedback!
from byte-buddy.
Thanks for addressing this.
Right now I'm constructing proxies via reflection which has various pitfalls, mostly around constructors that take parameters, which I'm usually able to deal with, and those which perform side-effects. This hasn't hit me too much in practice, but it does occasionally. Objenesis would be a great alternative, but so far I've been hesitant to rely on something like sun.musc.Unsafe. I noticed your blog post which mentioned it may become part of the JDK API for 9, so I think it's worth considering now.
from byte-buddy.
I would not be too concerned about using the Unsafe
class. As of today, there are too many libraries built around this class that a new JVM implementation would probably always chose to also offer this class. There is a bigger chance of your users encountering issues with side effects of constructor definitions as with objenesis not being compatible to an exotic JVM implementation.
from byte-buddy.
I think you're right. Thanks for getting me thinking about this!
from byte-buddy.
This is implemented from Byte Buddy, version 0.2.
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.