-
Notifications
You must be signed in to change notification settings - Fork 864
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Kotlin implementation for NullabilityDetector
KotlinNullabilityDetector relies on Kotlin Metadata to detect parameter nullability.
- Loading branch information
1 parent
481a042
commit d365134
Showing
5 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
60 changes: 60 additions & 0 deletions
60
rhino-kotlin/src/main/java/org/mozilla/kotlin/KotlinNullabilityDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...rc/main/resources/META-INF/services/org.mozilla.javascript.reflection.NullabilityDetector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.mozilla.kotlin.KotlinNullabilityDetector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|