NativeScript / NativeScript/android

Enable generation of default (0-arguments) constructor when extending classes

Open
#526 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature severity: low
Dominant language
C++
Stars
563
Forks
144
Avg merge
10h 46m
Merged PRs (30d)
14

Description

If the developer extends a base class with no default constructor, they have no way of explicitly specifying that they want a default constructor code to be generated in Java. This is a certain limitation if the class needs to be declared in the AndroidManifest and is instantiated by the system (attempting to use IntentService is one such case - Android will attempt to create an instance of the implementation of IntentService, but will crash, as it tries to invoke a default constructor, which the developer cannot declare).

Given the following Java class:

package a.b;

public class Base {
    public Base(String a) { ... }       
}

and the following implementation in JavaScript:

a.b.Base.extend("a.b.ExtendedBase", {
    init: function() {
     _this("arbitrary string");      
     // -> will introduce additional logic in the sole ctor block
     // in Java, but will not create an additional constructor
    }  
});

will output

package a.b;

@com.tns.JavaScriptImplementation(javaScriptFile = "./ExtendedBase.js")
public class ExtendedBase extends a.b.Base implements com.tns.NativeScriptHashCodeProvider {
        /* No default constructor */

    public ExtendedBase (java.lang.String param_0){
        super(param_0);
                /* 
                    Additional logic to handle more arguments -> a result of the init function
                */                 
        com.tns.Runtime.initInstance(this);
    }

    public boolean equals__super(java.lang.Object other) {
        return super.equals(other);
    }

    public int hashCode__super() {
        return super.hashCode();
    }
}

Developers have no way to declare a default constructor for a.b.ExtendedBase.

Instead what I need is

package a.b;

@com.tns.JavaScriptImplementation(javaScriptFile = "./ExtendedBase.js")
public class ExtendedBase extends a.b.Base implements com.tns.NativeScriptHashCodeProvider {
        /* default constructor */
        public ExtendedBase () {
        this("Arbitrary String");
    }

    public ExtendedBase (java.lang.String param_0){
        super(param_0);
                /* 
                    Additional logic to handle more arguments -> a result of the init function
                */                 
        com.tns.Runtime.initInstance(this);
    }
}

Generating custom constructors based on the javascript code will require additional analysis, and may not be worth looking into at the moment.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides JavaScript and Java examples but names no source file, test, or entry point. Begin by locating the constructor-generation path for class extensions; done means an extension of a base class without a no-argument constructor can expose a generated zero-argument constructor that delegates as specified, with coverage for the shown case.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, cpp, java, javascript
Domain
mobile, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.