NativeScript / NativeScript/android

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

未关闭
#526 5 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

feature severity: low
主要语言
C++
星标
563
派生
144
平均合并
10 小时 46 分钟
30 天内合并 PR
14

描述

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.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

该 issue 提供了 JavaScript 和 Java 示例,但没有指明源文件、测试或入口点。首先定位类扩展的构造函数生成路径;完成的标准是:一个没有无参构造函数的基类的扩展能够提供一个按指定方式委托的生成的零参数构造函数,并覆盖所示案例。

由索引模型根据 Issue 内容生成。

评估

技术栈
android, cpp, java, javascript
领域
mobile, tooling
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。