Skip to content

Commit

Permalink
🦄 refactor(Android): Add support to Optional, Stream, `ZonedDateT…
Browse files Browse the repository at this point in the history
…ime` for Android
  • Loading branch information
caoccao committed Feb 27, 2022
1 parent 9112ecb commit 8d47fb6
Show file tree
Hide file tree
Showing 12 changed files with 7 additions and 36 deletions.
7 changes: 4 additions & 3 deletions android/javet-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021. caoccao.com Sam Cao
* Copyright 2021-2022. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,8 +55,9 @@ android {
}

dependencies {
implementation("androidx.appcompat:appcompat:1.3.1")
implementation("org.threeten:threetenbp:1.5.1")
// https://developer.android.com/studio/write/java8-support
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5")
implementation("androidx.appcompat:appcompat:1.4.1")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
Expand Down
1 change: 1 addition & 0 deletions docs/release_notes/release_notes_1_1.html
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ <h1>Release Notes 1.1.x<a class="headerlink" href="#release-notes-1-1-x" title="
<h2>1.1.1<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><p>Enhanced createV8ValueArrayBuffer with ByteBuffer</p></li>
<li><p>Added support to <cite>Optional</cite>, <cite>Stream</cite>, <cite>ZonedDateTime</cite> for Android</p></li>
</ul>
</div>
<div class="section" id="v8-v9-8">
Expand Down
1 change: 1 addition & 0 deletions docs/release_notes/release_notes_1_1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Release Notes 1.1.x
-------------

* Enhanced createV8ValueArrayBuffer with ByteBuffer
* Added support to `Optional`, `Stream`, `ZonedDateTime` for Android

1.1.0 V8 v9.8
-------------
Expand Down
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions docs/tutorial/basic/installation.html
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,6 @@ <h3>Android<a class="headerlink" href="#android" title="Permalink to this headli
<ul class="simple">
<li><p>Only V8 mode is supported for Android. Supporting Node.js mode implies huge amount of work, but is not mission impossible. Please contact the maintainer for detail.</p></li>
<li><p>If you need Node.js features on Android, please refer to project <a class="reference external" href="https://github.com/caoccao/Javenode">Javenode</a>.</p></li>
<li><p><a class="reference external" href="https://developer.android.com/reference/java/time/ZonedDateTime">ZonedDateTime</a> (since ABI 26) is polyfilled by <code class="docutils literal notranslate"><span class="pre">org.threeten.bp.ZonedDateTime</span></code> so that Javet can work well with low ABI version.</p></li>
<li><p><cite>Stream</cite> and <cite>Optional</cite> are not supported because they are not available with ABI version 23-24.</p></li>
<li><p>If you need ABI version 21/22, please use Javet v1.0.x or contact the maintainer for details.</p></li>
</ul>
</div>
Expand Down
2 changes: 0 additions & 2 deletions docs/tutorial/basic/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,4 @@ Android x86_64 Yes (ABI >= 23)

* Only V8 mode is supported for Android. Supporting Node.js mode implies huge amount of work, but is not mission impossible. Please contact the maintainer for detail.
* If you need Node.js features on Android, please refer to project `Javenode <https://github.com/caoccao/Javenode>`_.
* `ZonedDateTime <https://developer.android.com/reference/java/time/ZonedDateTime>`_ (since ABI 26) is polyfilled by ``org.threeten.bp.ZonedDateTime`` so that Javet can work well with low ABI version.
* `Stream` and `Optional` are not supported because they are not available with ABI version 23-24.
* If you need ABI version 21/22, please use Javet v1.0.x or contact the maintainer for details.
10 changes: 0 additions & 10 deletions scripts/python/patch_android_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,9 @@ class PatchAndroidBuild(object):
def __init__(self) -> None:
self._line_separator = '\n'
self._patch_dict = {
'java.time.': 'org.threeten.bp.',
'.getParameterCount()': '.getParameterTypes().length',
}
self._comment_list = [
'import java.util.Optional;',
'import java.util.OptionalDouble;',
'import java.util.OptionalInt;',
'import java.util.OptionalLong;',
'import java.util.stream.BaseStream;',
'import java.util.stream.DoubleStream;',
'import java.util.stream.IntStream;',
'import java.util.stream.LongStream;',
'import java.util.stream.Stream;',
'import javax.management.ListenerNotFoundException;',
'import javax.management.Notification;',
'import javax.management.NotificationEmitter;',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ private static Object convert(IJavetConverter converter, Class<?> expectedClass,
} else if (expectedClass == char.class) {
return config.getDefaultChar();
}
/* if not defined ANDROID */
} else if (expectedClass == Optional.class) {
return Optional.empty();
} else if (expectedClass == OptionalInt.class) {
Expand All @@ -92,7 +91,6 @@ private static Object convert(IJavetConverter converter, Class<?> expectedClass,
return DoubleStream.empty();
} else if (expectedClass == LongStream.class) {
return LongStream.empty();
/* end if */
}
} else if (expectedClass.isAssignableFrom(v8Value.getClass())) {
// Skip assignable
Expand Down Expand Up @@ -259,7 +257,6 @@ private static Object convert(IJavetConverter converter, Class<?> expectedClass,
return convertedString.length() > 0 ?
convertedString.charAt(0) : converter.getConfig().getDefaultChar();
}
/* if not defined ANDROID */
} else if (expectedClass == Optional.class) {
return Optional.of(convertedObject);
} else if (expectedClass == OptionalInt.class) {
Expand Down Expand Up @@ -294,7 +291,6 @@ private static Object convert(IJavetConverter converter, Class<?> expectedClass,
if (doubleStream != null) {
return doubleStream;
}
/* end if */
}
}
} catch (Throwable ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ protected <T extends V8Value> T toV8Value(
v8Value = v8ValueArray;
v8Scope.setEscapable();
}
/* if not defined ANDROID */
} else if (object instanceof BaseStream) {
try (V8Scope v8Scope = v8Runtime.getV8Scope()) {
V8ValueArray v8ValueArray = v8Scope.createV8ValueArray();
Expand All @@ -444,7 +443,6 @@ protected <T extends V8Value> T toV8Value(
v8Value = v8ValueArray;
v8Scope.setEscapable();
}
/* end if */
} else if (object instanceof IJavetEntityFunction) {
final IJavetEntityFunction javetEntityFunction = (IJavetEntityFunction) object;
String sourceCode = javetEntityFunction.getJSFunctionType().isUserDefined() ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ protected <T extends V8Value> T toV8Value(
v8Value = v8Runtime.createV8ValueInteger((Byte) object);
} else if (object instanceof Character) {
v8Value = v8Runtime.createV8ValueString(((Character) object).toString());
/* if not defined ANDROID */
} else if (object instanceof Optional) {
Optional<?> optional = (Optional<?>) object;
v8Value = optional.isPresent() ? toV8Value(v8Runtime, optional.get(), depth) : v8Runtime.createV8ValueNull();
Expand All @@ -126,7 +125,6 @@ protected <T extends V8Value> T toV8Value(
} else if (object instanceof OptionalLong) {
OptionalLong optional = (OptionalLong) object;
v8Value = optional.isPresent() ? toV8Value(v8Runtime, optional.getAsLong(), depth) : v8Runtime.createV8ValueNull();
/* end if */
} else {
v8Value = v8Runtime.createV8ValueUndefined();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ public static String getMethodNameFromLambda(Serializable lambda) {
* @return the method name set
* @since 0.9.13
*/
/* if not defined ANDROID */
public static Set<String> getMethodNameSetFromLambdas(Serializable... lambdas) {
return Stream.of(Objects.requireNonNull(lambdas))
.filter(Objects::nonNull)
.map(JavetReflectionUtils::getMethodNameFromLambda)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}
/* end if */

/**
* Safe set accessible.
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/caoccao/javet/utils/JavetTypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public static Object toApproximatePrimitive(Class<?> expectedClass, Object objec
* @return the double stream
* @since 0.9.13
*/
/* if not defined ANDROID */
public static DoubleStream toDoubleStream(Object object) {
if (object instanceof double[]) {
return DoubleStream.of((double[]) object);
Expand All @@ -150,7 +149,6 @@ public static DoubleStream toDoubleStream(Object object) {
}
return null;
}
/* end if */

/**
* To exact primitive object.
Expand Down Expand Up @@ -195,7 +193,6 @@ public static Object toExactPrimitive(Class<?> expectedClass, Object object) {
* @return the int stream
* @since 0.9.13
*/
/* if not defined ANDROID */
public static IntStream toIntStream(Object object) {
if (object instanceof int[]) {
return IntStream.of((int[]) object);
Expand All @@ -207,7 +204,6 @@ public static IntStream toIntStream(Object object) {
}
return null;
}
/* end if */

/**
* Convert object to long stream.
Expand All @@ -216,7 +212,6 @@ public static IntStream toIntStream(Object object) {
* @return the long stream
* @since 0.9.13
*/
/* if not defined ANDROID */
public static LongStream toLongStream(Object object) {
if (object instanceof long[]) {
return LongStream.of((long[]) object);
Expand All @@ -228,7 +223,6 @@ public static LongStream toLongStream(Object object) {
}
return null;
}
/* end if */

/**
* Convert object to stream.
Expand All @@ -237,7 +231,6 @@ public static LongStream toLongStream(Object object) {
* @return the stream
* @since 0.9.13
*/
/* if not defined ANDROID */
public static Stream<?> toStream(Object object) {
if (object.getClass().isArray()) {
if (object instanceof int[]) {
Expand Down Expand Up @@ -289,5 +282,4 @@ public static Stream<?> toStream(Object object) {
}
return null;
}
/* end if */
}

0 comments on commit 8d47fb6

Please sign in to comment.