Skip to content

Commit

Permalink
Add Kotlin implementation for NullabilityDetector
Browse files Browse the repository at this point in the history
KotlinNullabilityDetector relies on Kotlin Metadata to detect parameter
nullability.
  • Loading branch information
drubanovich-soti committed Jan 21, 2025
1 parent 481a042 commit d365134
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
41 changes: 41 additions & 0 deletions rhino-kotlin/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
plugins {
id 'rhino.library-conventions'
}

dependencies {
implementation project(':rhino-reflection')
implementation 'org.jetbrains.kotlin:kotlin-metadata-jvm:2.1.0'
}

publishing {
publications {
rhinokotlin(MavenPublication) {
from components.java
artifacts = [jar, sourceJar]
pom.withXml {
def root = asNode()

root.appendNode('description', "Rhino reflection interfaces implementations for Kotlin")
root.appendNode("url", "https://mozilla.github.io/rhino/")

def p = root.appendNode("parent")
p.appendNode("groupId", "org.sonatype.oss")
p.appendNode("artifactId", "oss-parent")
p.appendNode("version", "7")

def l = root.appendNode("licenses").appendNode("license")
l.appendNode("name", "Mozilla Public License, Version 2.0")
l.appendNode("url", "http://www.mozilla.org/MPL/2.0/index.txt")

def scm = root.appendNode("scm")
scm.appendNode("connection", "scm:git:[email protected]:mozilla/rhino.git")
scm.appendNode("developerConnection", "scm:git:[email protected]:mozilla/rhino.git")
scm.appendNode("url", "[email protected]:mozilla/rhino.git")

def o = root.appendNode("organization")
o.appendNode("name", "The Mozilla Foundation")
o.appendNode("url", "http://www.mozilla.org")
}
}
}
}
11 changes: 11 additions & 0 deletions rhino-kotlin/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import org.mozilla.javascript.reflection.NullabilityDetector;
import org.mozilla.kotlin.KotlinNullabilityDetector;

module org.mozilla.rhino.kotlin {
requires kotlin.metadata.jvm;
requires kotlin.stdlib;
requires org.mozilla.javascript.reflection;

provides NullabilityDetector with
KotlinNullabilityDetector;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.mozilla.kotlin;

import static kotlin.metadata.Attributes.isNullable;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
import kotlin.Metadata;
import kotlin.metadata.KmClass;
import kotlin.metadata.KmFunction;
import kotlin.metadata.KmValueParameter;
import kotlin.metadata.jvm.KotlinClassMetadata;
import org.mozilla.javascript.reflection.NullabilityDetector;

public class KotlinNullabilityDetector implements NullabilityDetector {
@Override
public boolean[] getParameterNullability(Method method) {
boolean[] result = new boolean[method.getParameterTypes().length];
Metadata metadata = method.getDeclaringClass().getAnnotation(Metadata.class);
if (metadata != null) {
result = getParameterNullabilityFromKotlinMetadata(metadata, method.getName(), result);
}
return result;
}

@Override
public boolean[] getParameterNullability(Constructor<?> constructor) {
boolean[] result = new boolean[constructor.getParameterTypes().length];
Metadata metadata = constructor.getDeclaringClass().getAnnotation(Metadata.class);
if (metadata != null) {
result =
getParameterNullabilityFromKotlinMetadata(
metadata, constructor.getName(), result);
}
return result;
}

private boolean[] getParameterNullabilityFromKotlinMetadata(
Metadata metadata, String methodName, boolean[] fallback) {
KotlinClassMetadata.Class kMetadata =
(KotlinClassMetadata.Class) KotlinClassMetadata.readLenient(metadata);
KmClass clazz = kMetadata.getKmClass();
Optional<KmFunction> function =
clazz.getFunctions().stream()
.filter(f -> f.getName().equals(methodName))
.findFirst();
if (function.isPresent()) {
List<KmValueParameter> params = function.get().getValueParameters();
boolean[] result = new boolean[params.size()];
int index = 0;
for (KmValueParameter parameter : params) {
result[index++] = isNullable(parameter.getType());
}
return result;
} else {
return fallback;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.mozilla.kotlin.KotlinNullabilityDetector
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rootProject.name = 'rhino-root'
include 'rhino', 'rhino-engine', 'rhino-tools', 'rhino-xml', 'rhino-all', 'examples', 'testutils', 'tests', 'benchmarks'
include 'rhino-reflection'
include 'rhino-reflection', 'rhino-kotlin'

0 comments on commit d365134

Please sign in to comment.