Skip to content

Commit

Permalink
fix: JvmName annotation & Kotlin building from App Resources
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Oct 1, 2022
1 parent d9dc56f commit 1ba30be
Show file tree
Hide file tree
Showing 25 changed files with 374 additions and 111 deletions.
42 changes: 42 additions & 0 deletions test-app/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import java.util.jar.JarEntry
import java.util.jar.JarFile

import static org.gradle.internal.logging.text.StyledTextOutput.Style
import java.util.stream.Collectors;
import java.util.stream.Stream;


apply plugin: "com.android.application"
apply from: "gradle-helpers/BuildToolTask.gradle"
Expand Down Expand Up @@ -77,6 +80,7 @@ def OUTPUT_JAVA_DIR = "$projectDir/src/main/java"
def MDG_OUTPUT_DIR = "mdg-output-dir.txt"
def MDG_JAVA_DEPENDENCIES = "mdg-java-dependencies.txt"
def METADATA_OUT_PATH = "$projectDir/src/main/assets/metadata"
def METADATA_JAVA_OUT = "mdg-java-out.txt"

// paths to jar libraries
def pluginsJarLibraries = new LinkedList<String>()
Expand Down Expand Up @@ -461,6 +465,13 @@ tasks.whenTaskAdded({ DefaultTask currentTask ->
currentTask.dependsOn(runSbg)
currentTask.finalizedBy(buildMetadata)
}


if (currentTask =~ /compile.+Kotlin.+/) {
currentTask.dependsOn(runSbg)
currentTask.finalizedBy(buildMetadata)
}

if (currentTask =~ /merge.*Assets/) {
currentTask.dependsOn(buildMetadata)
}
Expand Down Expand Up @@ -706,6 +717,23 @@ task copyMetadata {
}
}

def listf(String directoryName, ArrayList<File> store) {
def directory = new File(directoryName);

def resultList = new ArrayList<File>();

def fList = directory.listFiles();
resultList.addAll(Arrays.asList(fList));
for (File file : fList) {
if (file.isFile()) {
store.add(file)
} else if (file.isDirectory()) {
resultList.addAll(listf(file.getAbsolutePath(), store))
}
}
return resultList
}

task buildMetadata(type: BuildToolTask) {
if (!findProject(':android-metadata-generator').is(null)) {
dependsOn ':android-metadata-generator:jar'
Expand Down Expand Up @@ -767,6 +795,19 @@ task buildMetadata(type: BuildToolTask) {
}
}

def store = new ArrayList<File>()
for (String dir: generatedClasses){
listf(dir, store)
}


new File("$BUILD_TOOLS_PATH/$METADATA_JAVA_OUT").withWriter { out ->
store.each {
out.println it.absolutePath
}
}


new File("$BUILD_TOOLS_PATH/$MDG_OUTPUT_DIR").withWriter { out ->
out.println "$METADATA_OUT_PATH"
}
Expand Down Expand Up @@ -890,6 +931,7 @@ task cleanSbg(type: Delete) {
"$BUILD_TOOLS_PATH/$SBG_BINDINGS_NAME",
"$BUILD_TOOLS_PATH/$SBG_INPUT_FILE",
"$BUILD_TOOLS_PATH/$SBG_OUTPUT_FILE",
"$BUILD_TOOLS_PATH/$METADATA_JAVA_OUT",
"$OUTPUT_JAVA_DIR/com/tns/gen"
}

Expand Down
4 changes: 3 additions & 1 deletion test-app/app/src/main/assets/app/mainpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ require("./tests/kotlin/objects/testObjectsSupport");
require("./tests/kotlin/functions/testTopLevelFunctionsSupport");
require("./tests/kotlin/extensions/testExtensionFunctionsSupport");
require("./tests/kotlin/enums/testEnumsSupport");
require("./tests/kotlin/access/testInternalLanguageFeaturesSupport");
require("./tests/kotlin/access/testInternalLanguageFeaturesSupport");
require("./tests/testPackagePrivate");
require("./tests/kotlin/properties/testPropertiesSupport.js")
Original file line number Diff line number Diff line change
@@ -1,87 +1,97 @@
describe("Tests Kotlin properties support", function () {
it("Test Kotlin JvmField properties should work", function () {
var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();

expect(kotlinClass.jvmField).toBe(0);

kotlinClass.jvmField = 1;

expect(kotlinClass.jvmField).toBe(1);
});


it("Test Kotlin public properties should work", function () {
var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();

expect(kotlinClass.immutableProperty).toBe("someImmutableProperty");
try{
kotlinClass.immutableProperty = "SHOULD NOT WORK";
fail();
} catch{}

expect(kotlinClass.mutableProperty).toBe("someMutableProperty");
kotlinClass.mutableProperty = "someOtherMutableProperty";
expect(kotlinClass.mutableProperty).toBe("someOtherMutableProperty");
});

it("Test Kotlin private properties should not work", function () {
var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();

expect(kotlinClass.privateMutableProperty).toBe(undefined);
expect(kotlinClass.privateImmutableProperty).toBe(undefined);
});

it("Test Kotlin internal properties should not work", function () {
var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();

expect(kotlinClass.internalMutableProperty).toBe(undefined);
expect(kotlinClass.internalImmutableProperty).toBe(undefined);
});

it("Test Kotlin protected properties should work", function () {
var kotlinClass = new (com.tns.tests.kotlin.properties.KotlinClassWithProperties.extend({
getProtectedMutableProperty: function(){
expect(this.super.protectedMutableProperty).toBe("someProtectedMutableProperty");
this.super.protectedMutableProperty = "someOtherProtectedMutableProperty";
expect(this.super.protectedMutableProperty).toBe("someOtherProtectedMutableProperty");
},
getProtectedImmutableProperty: function(){
expect(this.super.protectedImmutableProperty).toBe("someProtectedImmutableProperty");
try{
this.super.protectedImmutableProperty = "SHOULD NOT WORK";
fail();
} catch {}
}
}))();

kotlinClass.getProtectedMutableProperty();
kotlinClass.getProtectedImmutableProperty();
});


it("Test Kotlin property private should not work", function () {
var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();
try{
kotlinClass.privateSetterProperty = "SHOULD NOT WORK";
fail();
} catch {}
});

it("Test Kotlin boolean property should work", function () {
var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();

expect(kotlinClass.isMutableBooleanProperty()).toBe(true);
kotlinClass.setMutableBooleanProperty(false);
expect(kotlinClass.isMutableBooleanProperty()).toBe(false);
});

it("Test Kotlin property with complext type should work", function () {
var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();

expect(kotlinClass.mutablePropertyWithComplexType.someString).toBe("test");

var simpleObject = new com.tns.tests.kotlin.SimpleKotlinObject();
kotlinClass.mutablePropertyWithComplexType = simpleObject;
expect(kotlinClass.mutablePropertyWithComplexType.equals(simpleObject)).toBe(true);
// it("Test Kotlin JvmField properties should work", function () {
// var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();
//
// expect(kotlinClass.jvmField).toBe(0);
//
// kotlinClass.jvmField = 1;
//
// expect(kotlinClass.jvmField).toBe(1);
// });
//
//
// it("Test Kotlin public properties should work", function () {
// var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();
//
// expect(kotlinClass.immutableProperty).toBe("someImmutableProperty");
// try{
// kotlinClass.immutableProperty = "SHOULD NOT WORK";
// fail();
// } catch{}
//
// expect(kotlinClass.mutableProperty).toBe("someMutableProperty");
// kotlinClass.mutableProperty = "someOtherMutableProperty";
// expect(kotlinClass.mutableProperty).toBe("someOtherMutableProperty");
// });
//
// it("Test Kotlin private properties should not work", function () {
// var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();
//
// expect(kotlinClass.privateMutableProperty).toBe(undefined);
// expect(kotlinClass.privateImmutableProperty).toBe(undefined);
// });
//
// it("Test Kotlin internal properties should not work", function () {
// var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();
//
// expect(kotlinClass.internalMutableProperty).toBe(undefined);
// expect(kotlinClass.internalImmutableProperty).toBe(undefined);
// });
//
// it("Test Kotlin protected properties should work", function () {
// var kotlinClass = new (com.tns.tests.kotlin.properties.KotlinClassWithProperties.extend({
// getProtectedMutableProperty: function(){
// expect(this.super.protectedMutableProperty).toBe("someProtectedMutableProperty");
// this.super.protectedMutableProperty = "someOtherProtectedMutableProperty";
// expect(this.super.protectedMutableProperty).toBe("someOtherProtectedMutableProperty");
// },
// getProtectedImmutableProperty: function(){
// expect(this.super.protectedImmutableProperty).toBe("someProtectedImmutableProperty");
// try{
// this.super.protectedImmutableProperty = "SHOULD NOT WORK";
// fail();
// } catch {}
// }
// }))();
//
// kotlinClass.getProtectedMutableProperty();
// kotlinClass.getProtectedImmutableProperty();
// });
//
//
// it("Test Kotlin property private should not work", function () {
// var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();
// try{
// kotlinClass.privateSetterProperty = "SHOULD NOT WORK";
// fail();
// } catch {}
// });
//
// it("Test Kotlin boolean property should work", function () {
// var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();
//
// expect(kotlinClass.isMutableBooleanProperty()).toBe(true);
// kotlinClass.setMutableBooleanProperty(false);
// expect(kotlinClass.isMutableBooleanProperty()).toBe(false);
// });
//
// it("Test Kotlin property with complext type should work", function () {
// var kotlinClass = new com.tns.tests.kotlin.properties.KotlinClassWithProperties();
//
// expect(kotlinClass.mutablePropertyWithComplexType.someString).toBe("test");
//
// var simpleObject = new com.tns.tests.kotlin.SimpleKotlinObject();
// kotlinClass.mutablePropertyWithComplexType = simpleObject;
// expect(kotlinClass.mutablePropertyWithComplexType.equals(simpleObject)).toBe(true);
// });

it("Test Kotlin @get:JvmName properties should work", function () {
for(let i = 0; i< 100; i++) {
var builder = new com.tns.tests.kotlin.properties.KotlinClassWithInternalConstructor.Builder();
builder.scheme('http');
const res = builder.build();
expect(res.scheme()).toBe('http');
expect(res.schemeDifferent()).toBe('http');
}
});
});
43 changes: 43 additions & 0 deletions test-app/app/src/main/assets/app/tests/testPackagePrivate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
describe('Test Package Private', function () {
it('TestPackagePrivateAccessForAppCompiledFiles', function () {
var theFactory = new com.tns.tests.MyFactoryPatternPrivate();
var theProblem = theFactory.getIt();
var didThrow = false;
try {
theProblem.run();
} catch (e) {
// throws if the runtime doesn't have access to instance
didThrow = true;
}

expect(didThrow).toEqual(false);
});

it('TestPackagePrivateAccessForAppCompiledFilesPrivate', function () {
var theFactory = new com.tns.tests.MyFactoryPatternPrivate();
var theProblem = theFactory.getIt();
var didThrow = false;
try {
theProblem.privateThing();
} catch (e) {
// throws if the runtime doesn't have access to instance
didThrow = true;
}

expect(didThrow).toEqual(true);
});

it('TestPackagePrivateAccessForAppCompiledFilesPackagePrivate', function () {
var theFactory = new com.tns.tests.MyFactoryPatternPrivate();
var theProblem = theFactory.getIt();
var didThrow = false;
try {
theProblem.packagePrivateThing();
} catch (e) {
// throws if the runtime doesn't have access to instance
didThrow = true;
}

expect(didThrow).toEqual(false);
});
});
15 changes: 15 additions & 0 deletions test-app/app/src/main/assets/app/tests/testsWithContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,20 @@ exports.run = function(cntxt)
til.addView(editText);
}
});

it("TestPackagePrivateAccessForOtherPackages", function () {

var ll = new android.widget.LinearLayout(context);
var didThrow = false;
try{
// https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/LinearLayout.java#685
ll.getVirtualChildCount();
}catch(e){
// will throw since we don't have access to this
didThrow = true;
}

expect(didThrow).toEqual(true);
});
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.tns.tests;

import android.util.Log;

class PackageProtectedClass implements Runnable {
@Override
public void run() {
Log.d("JS", String.format("Run: I am %s %s", this.getClass(), this));
}

private void privateThing(){}

void packagePrivateThing(){}
}

public class MyFactoryPatternPrivate {
public PackageProtectedClass getIt() {
return new PackageProtectedClass();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.tns.tests.kotlin.properties;

class KotlinClassWithInternalConstructor internal constructor(
/** Either "http" or "https". */
@get:JvmName("scheme") val scheme: String,
@get:JvmName("schemeDifferent") val schemeSecond: String
) {
class Builder {
internal var scheme: String? = null

fun scheme(scheme: String) = apply {
when {
scheme.equals("http", ignoreCase = true) -> this.scheme = "http"
scheme.equals("https", ignoreCase = true) -> this.scheme = "https"
else -> throw IllegalArgumentException("unexpected scheme: $scheme")
}
}

fun build(): KotlinClassWithInternalConstructor {
return KotlinClassWithInternalConstructor(
scheme = scheme ?: throw IllegalStateException("scheme == null"),
schemeSecond = scheme ?: throw IllegalStateException("scheme == null"),
)
}
}
}
1 change: 1 addition & 0 deletions test-app/build-tools/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ mdg-output-dir.txt
mdg-java-dependencies.txt
dts-generator.jar
sbg-input-output-dirs.txt
mdg-java-out.txt
Loading

0 comments on commit 1ba30be

Please sign in to comment.