GithubHelp home page GithubHelp logo

Comments (6)

raphw avatar raphw commented on July 19, 2024

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.

raphw avatar raphw commented on July 19, 2024

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.

jhalterman avatar jhalterman commented on July 19, 2024

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.

raphw avatar raphw commented on July 19, 2024

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.

jhalterman avatar jhalterman commented on July 19, 2024

I think you're right. Thanks for getting me thinking about this!

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

This is implemented from Byte Buddy, version 0.2.

from byte-buddy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.