Comments (5)
Java is a quite tricky language when it comes to package-private access. Package-private access is also rather an invention for being used by compilers rather then by users since they have strange semantics.
You might have heard before that two classes are only consider equal by the Java run time if they were loaded with the same class loader even if they otherwise represent an identical class. The same is true for Java packages. If a class is loaded with a different class loader, it cannot get private-package access to any classes (or class members) that belong to another class loader.
The injection strategy uses reflection to load a class into a given class loader. The wrapper class loader on the other hand, creates a new child class loader for loading a given class. The former strategy is more efficient since a class loader is a rather expensive object to Java (takes about 70 nanoseconds to create it in my benchmarks). However, it does not need to call internal methods. Also, the wrapper class loader allows the unloading of the generated class if none of its instances and its class and class loader are not longer reachable. This is handy when creating classes that are only needed for a limited amount of time such as in bootstrap procedures. One specific use case of this strategy would be mocking for example as for creating type safe DSLs. This allows to keep the perm gen requirements of an application down as the injector strategy often targets the system class loader what makes loaded classes invincible for the application's life time.
The injector strategy is equivalent to Javassist and cglib's approaches.
from byte-buddy.
Hi Jonathan,
you are defining a package-private class org.modelmapper.FooTest.Foo
for which you define a default-package subclass FooBar.Foo
. This latter class cannot access its super class. Therefore, you need to either define the subclass to be in the same package as its super class or you need to change the visibility of the super class to public
. The naming strategy is supposed to create a fully qualified name.
Thank you for using Byte Buddy! You are somewhat an early adopter and I hope to improve this behavior in later versions. A future version of Byte Buddy will definitely come with an (optional) validator that will throw an appropriate exception. Today's version does not yet catch all validation errors with a detailed error message because this validation generates some run time overhead.
Also note that I am planing to release a version 0.2 of Byte Buddy in only a couple of weeks. It comes with several bug fixes, some new functionality to better support Java 8 and improved support for creating proxy classes.
Best regards, Rafael
from byte-buddy.
Ah, I assumed that the package was always the same as the superclass and that the naming strategy was just for the class name. Good to know.
So using a fully qualified subclass name, things work well, but only if I use ClassLoadingStrategy.Default.INJECTION
. Why is this?
from byte-buddy.
Good info. I recalled that Guice uses a separate class loader for instrumenting/proxying classes/methods that are not private/package-private and the user classloader for everything else. A hybrid strategy like that is something I might need to look into.
from byte-buddy.
I thought about predefining such a strategy for Byte Buddy 0.2. However, this would require you to resolve any invokable method of a class in order to check if any of those methods is package-private relatively to the generated class in order to determine its necessity. Such reflective look-ups are unfortunately expensive. I might still just add the strategy and add an explicit warning of its costs.
from byte-buddy.
Related Issues (20)
- Not able to intercept methods of java.net.HttpURLConnection class in Java 17 HOT 1
- Strange interaction with method implementations for methods from composite interfaces HOT 1
- 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
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.