felix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Guillaume Sauthier (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (FELIX-4455) Missing default constructor creates invalid class instances
Date Thu, 13 Mar 2014 13:03:42 GMT

    [ https://issues.apache.org/jira/browse/FELIX-4455?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13933211#comment-13933211
] 

Guillaume Sauthier commented on FELIX-4455:
-------------------------------------------

As I said, default field assignment is done in the constructor, not outside of it in the bytecode.
That means that for your component example, the compiled bytecode looks like this:
{code:java}
@Component
public class MyClass {
   private String internal;
   public MyClass() {
      internal = "Default";
   }
}{code}

So, in this case, iPOJO modifies a correct constructor.

What you miss is that, once you defined your own constructor, there is no more the default
one available in the bytecode.

> Missing default constructor creates invalid class instances
> -----------------------------------------------------------
>
>                 Key: FELIX-4455
>                 URL: https://issues.apache.org/jira/browse/FELIX-4455
>             Project: Felix
>          Issue Type: Bug
>          Components: iPOJO
>    Affects Versions: ipojo-manipulator-1.10.1, ipojo-manipulator-1.11.1
>            Reporter: Benjamin Debeerst
>
> Consider the following component definition: 
> @Component
> @Instantiate
> public class ComponentWithoutDefaultConstructor
> {
>     private Object internal = new Object();
>     @Property
>     private String property;
>     /* Constructor for unit tests */
>     public ComponentWithoutDefaultConstructor(String property)
>     {
>         System.out.println("Non-default constructor call!");
>         this.property = property;
>     }
>     @Validate
>     public void activate()
>     {
>         System.out.println("ComponentWithoutDefaultConstructor: activating!");
>         System.out.println(this.internal);
>     }
> }
> As per definition, I would expect every instance of this class to have a non-null member
{{internal}} at class creation. This is all fine for my unit tests in a non-OSGi environment.
> However, having this processes by iPOJO and putting this in an OSGi container, the component
outputs 
> ComponentWithoutDefaultConstructor: activating!
> null
> The existing non-default constructor is not called, while it seems that a separate constructor
has been created that does non instantiate the member variable.
> If I extend the code by an empty default constructor, that one is called and all works
perfectly fine.
> I have no idea how iPOJO creates the object instance anyways, as it should not be possible
to create such an invalid object instance. I don't know much about the reflection APIs or
byte code manipulation though.
> I have put a sample maven project on GitHub[1], which can be built and droppped into
a fresh Karaf container + iPOJO, which shows the problem.
> If there is no constructor iPOJO can use, I would expect iPOJO to either A) throw a warning/error
(and build or runtime) and fail to instantiate the component or B) synthesize the default
constructor properly, including the implicit instantiation of member variables.
> I encountered this problem both with iPOJO 1.10.1 and 1.11.1.
> [1] https://github.com/BenjaminDebeerst/ipojo-component-constructor-sample



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Mime
View raw message