Skip to content

Commit

Permalink
Enable autovalue readable tests for J2KT
Browse files Browse the repository at this point in the history
For now I need to remove methods that use `@Memoized` as those will cause J2KT
to fail.

PiperOrigin-RevId: 708326385
  • Loading branch information
kevinoconnor7 authored and copybara-github committed Dec 20, 2024
1 parent a2ac36b commit 091a9d7
Show file tree
Hide file tree
Showing 42 changed files with 1,869 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ abstract class AutoValueWithExtensions {
public abstract String getStringField();

@Memoized
@J2ktIncompatible // TODO(b/385167941): Remove when @Memoized can be used with J2KT.
int getMemoizedNative() {
return getIntField() * 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract class AutoValueWithFields extends Parent implements Serializable {
public abstract int getIntField();

@Memoized
@J2ktIncompatible // TODO(b/385167941): Remove when @Memoized can be used with J2KT.
int getMemoizedNative() {
return getIntField() * 2;
}
Expand Down
14 changes: 11 additions & 3 deletions transpiler/javatests/com/google/j2cl/readable/java/autovalue/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//build_defs:rules.bzl", "j2cl_library")
load(
"//transpiler/javatests/com/google/j2cl/readable:readable_example.bzl",
"readable_example",
Expand All @@ -8,12 +9,19 @@ package(
licenses = ["notice"],
)

j2cl_library(
name = "j2kt_incompatible",
srcs = ["J2ktIncompatible.java"],
)

# TODO(b/317166154): Port this test to Kotlin when AutoValue is supported.
readable_example(
srcs = glob(["*.java"]),
# TODO(b/202513859): Add support for experimental_optimize_autovalue in J2KT.
generate_kt_readables = False,
srcs = glob(
["*.java"],
exclude = ["J2ktIncompatible.java"],
),
deps = [
":j2kt_incompatible",
"//third_party:auto_value_generates_api-j2cl",
"//third_party:jsinterop-annotations-j2cl",
"//third_party:jsr305_annotations-j2cl",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2024 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package autovalue;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.CLASS)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})
public @interface J2ktIncompatible {}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ static SimpleAutoValue create() {
return new AutoValue_SimpleAutoValue(42, true, "text", 43.0, 44.0, new int[] {45});
}

private AutoValue_SimpleAutoValue field1;
@JsNonNull private AutoValue_SimpleAutoValue field2;
private EmptyAutoValue field1;
// Demonstrate a mismatch of nullability in the type descriptors still works.
@JsNonNull private EmptyAutoValue field2 = new AutoValue_EmptyAutoValue.Builder().build();

static AutoValue_SimpleAutoValue[] castAndInstanceOf(Object o) {
return o instanceof AutoValue_SimpleAutoValue[] ? (AutoValue_SimpleAutoValue[]) o : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const reflect = goog.require('goog.reflect');
const ValueType = goog.require('javaemul.internal.ValueType$impl');
const $Util = goog.require('nativebootstrap.Util$impl');

let Builder = goog.forwardDeclare('autovalue.AutoValue_EmptyAutoValue.Builder$impl');
let EmptyAutoValue = goog.forwardDeclare('autovalue.EmptyAutoValue$impl');
let Objects = goog.forwardDeclare('java.util.Objects$impl');
let $Arrays = goog.forwardDeclare('vmbootstrap.Arrays$impl');
let $$int = goog.forwardDeclare('vmbootstrap.primitives.$int$impl');
Expand All @@ -14,9 +16,9 @@ class SimpleAutoValue extends ValueType {
super();
/**@type {number} @nodts*/
this.f_intField__autovalue_SimpleAutoValue_ = 0;
/**@type {SimpleAutoValue} @nodts*/
/**@type {EmptyAutoValue} @nodts*/
this.f_field1__autovalue_SimpleAutoValue_;
/**@type {!SimpleAutoValue} @nodts*/
/**@type {!EmptyAutoValue} @nodts*/
this.f_field2__autovalue_SimpleAutoValue_;
/**@type {number} @nodts*/
this.f_intField__autovalue_AutoValue_SimpleAutoValue_ = 0;
Expand Down Expand Up @@ -51,6 +53,7 @@ class SimpleAutoValue extends ValueType {
/** @nodts */
$ctor__autovalue_SimpleAutoValue__int__boolean__java_lang_String__java_lang_Double__java_lang_Double__arrayOf_int__void(/** number */ intField, /** boolean */ booleanField, /** ?string */ stringField, /** ?number */ doubleField, /** ?number */ nullableField, /** Array<number> */ arrayField) {
this.$ctor__javaemul_internal_ValueType__void();
this.$init__void_$p_autovalue_SimpleAutoValue();
this.f_intField__autovalue_AutoValue_SimpleAutoValue_ = intField;
this.f_booleanField__autovalue_AutoValue_SimpleAutoValue_ = booleanField;
Objects.m_requireNonNull__java_lang_Object__java_lang_Object(stringField);
Expand All @@ -60,7 +63,7 @@ class SimpleAutoValue extends ValueType {
this.f_nullableField__autovalue_AutoValue_SimpleAutoValue_ = nullableField;
Objects.m_requireNonNull__java_lang_Object__java_lang_Object(arrayField);
this.f_arrayField__autovalue_AutoValue_SimpleAutoValue_ = arrayField;
$J2CL_PRESERVE$(this.f_field1__autovalue_SimpleAutoValue_, this.f_field2__autovalue_SimpleAutoValue_, this.f_intField__autovalue_AutoValue_SimpleAutoValue_, this.f_booleanField__autovalue_AutoValue_SimpleAutoValue_, this.f_stringField__autovalue_AutoValue_SimpleAutoValue_, this.f_doubleField__autovalue_AutoValue_SimpleAutoValue_, this.f_nullableField__autovalue_AutoValue_SimpleAutoValue_, this.f_arrayField__autovalue_AutoValue_SimpleAutoValue_);
$J2CL_PRESERVE$(this.f_intField__autovalue_AutoValue_SimpleAutoValue_, this.f_booleanField__autovalue_AutoValue_SimpleAutoValue_, this.f_stringField__autovalue_AutoValue_SimpleAutoValue_, this.f_doubleField__autovalue_AutoValue_SimpleAutoValue_, this.f_nullableField__autovalue_AutoValue_SimpleAutoValue_, this.f_arrayField__autovalue_AutoValue_SimpleAutoValue_);
}
/** @nodts @return {number} */
m_getIntField__int() {
Expand All @@ -86,6 +89,10 @@ class SimpleAutoValue extends ValueType {
m_getArrayField__arrayOf_int() {
return this.f_arrayField__autovalue_AutoValue_SimpleAutoValue_;
}
/** @private @nodts */
$init__void_$p_autovalue_SimpleAutoValue() {
this.f_field2__autovalue_SimpleAutoValue_ = Builder.$create__().m_build__autovalue_EmptyAutoValue();
}
/** @nodts */
static $clinit() {
SimpleAutoValue.$clinit = () =>{};
Expand All @@ -99,6 +106,7 @@ class SimpleAutoValue extends ValueType {

/** @nodts */
static $loadModules() {
Builder = goog.module.get('autovalue.AutoValue_EmptyAutoValue.Builder$impl');
Objects = goog.module.get('java.util.Objects$impl');
$Arrays = goog.module.get('vmbootstrap.Arrays$impl');
$$int = goog.module.get('vmbootstrap.primitives.$int$impl');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
goog.module('autovalue.SimpleAutoValue');

goog.require('autovalue.AutoValue_EmptyAutoValue.Builder');
goog.require('autovalue.EmptyAutoValue');
goog.require('goog.reflect');
goog.require('java.util.Objects');
goog.require('javaemul.internal.ValueType');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Generated from "autovalue/$AutoValue_AutoValueWithExtensions.java"
@file:OptIn(ExperimentalObjCName::class)
@file:Suppress(
"ALWAYS_NULL",
"PARAMETER_NAME_CHANGED_ON_OVERRIDE",
"SENSELESS_COMPARISON",
"UNCHECKED_CAST",
"UNNECESSARY_LATEINIT",
"UNNECESSARY_NOT_NULL_ASSERTION",
"UNREACHABLE_CODE",
"UNUSED_ANONYMOUS_PARAMETER",
"UNUSED_PARAMETER",
"UNUSED_VARIABLE",
"USELESS_CAST",
"VARIABLE_IN_SINGLETON_WITHOUT_THREAD_LOCAL",
"VARIABLE_WITH_REDUNDANT_INITIALIZER",
"REDUNDANT_ELSE_IN_WHEN")

package autovalue

import javaemul.lang.*
import autovalue.AutoValueWithExtensions
import java.lang.NullPointerException
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.OptIn
import kotlin.String
import kotlin.Suppress
import kotlin.experimental.ExperimentalObjCName
import kotlin.native.ObjCName

@ObjCName("J2ktAutovalue_AutoValue_AutoValueWithExtensions", exact = true)
abstract class ___AutoValue_AutoValueWithExtensions: AutoValueWithExtensions {
private val intField: Int

private val stringField: String?

internal constructor(intField: Int, stringField: String?) {
this.intField = intField
if (stringField == null) {
throw NullPointerException("Null stringField")
}
this.stringField = stringField
}

override fun getIntField(): Int {
return this.intField
}

override fun getStringField(): String? {
return this.stringField
}

override fun toString(): String {
return "AutoValueWithExtensions{" + "intField=" + this.intField + ", " + "stringField=" + this.stringField + "}"
}

override fun equals(o: Any?): Boolean {
if (o === this) {
return true
}
if (o is AutoValueWithExtensions) {
val that: AutoValueWithExtensions? = o as AutoValueWithExtensions?
return this.intField == that!!.getIntField() && this.stringField!!.equals(that!!.getStringField())
}
return false
}

override fun hashCode(): Int {
var h___: Int = 1
h___ = h___ * 1000003
h___ = h___.xor(this.intField)
h___ = h___ * 1000003
h___ = h___.xor(this.stringField!!.hashCode())
return h___
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Generated by J2KT from "autovalue/AutoValueJsType.java"

#import <Foundation/NSObjCRuntime.h>

@class J2ktAutovalueAutoValueJsType;

NS_ASSUME_NONNULL_BEGIN

@compatibility_alias AutovalueAutoValueJsType J2ktAutovalueAutoValueJsType;

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Generated from "autovalue/AutoValueJsType.java"
@file:OptIn(ExperimentalObjCName::class)
@file:Suppress(
"ALWAYS_NULL",
"PARAMETER_NAME_CHANGED_ON_OVERRIDE",
"SENSELESS_COMPARISON",
"UNCHECKED_CAST",
"UNNECESSARY_LATEINIT",
"UNNECESSARY_NOT_NULL_ASSERTION",
"UNREACHABLE_CODE",
"UNUSED_ANONYMOUS_PARAMETER",
"UNUSED_PARAMETER",
"UNUSED_VARIABLE",
"USELESS_CAST",
"VARIABLE_IN_SINGLETON_WITHOUT_THREAD_LOCAL",
"VARIABLE_WITH_REDUNDANT_INITIALIZER",
"REDUNDANT_ELSE_IN_WHEN")

package autovalue

import javaemul.lang.*
import jsinterop.annotations.JsIgnore
import jsinterop.annotations.JsMethod
import jsinterop.annotations.JsType
import kotlin.Int
import kotlin.OptIn
import kotlin.Suppress
import kotlin.experimental.ExperimentalObjCName
import kotlin.native.ObjCName

@ObjCName("J2ktAutovalueAutoValueJsType", exact = true)
@JsType
abstract class AutoValueJsType {
@JsIgnore
constructor()

@ObjCName("getField")
abstract fun getField(): Int

@JsMethod(name = "getField2")
internal abstract fun getWithJsMethod_pp_autovalue(): Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Generated by J2KT from "autovalue/AutoValueWithBuilder.java"

#import <Foundation/NSObjCRuntime.h>

@class J2ktAutovalueAutoValueWithBuilder;
@class J2ktAutovalueAutoValueWithBuilderCompanion;

NS_ASSUME_NONNULL_BEGIN

@compatibility_alias AutovalueAutoValueWithBuilder J2ktAutovalueAutoValueWithBuilder;

@compatibility_alias AutovalueAutoValueWithBuilderCompanion J2ktAutovalueAutoValueWithBuilderCompanion;

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Generated from "autovalue/AutoValueWithBuilder.java"
@file:OptIn(ExperimentalObjCName::class)
@file:Suppress(
"ALWAYS_NULL",
"PARAMETER_NAME_CHANGED_ON_OVERRIDE",
"SENSELESS_COMPARISON",
"UNCHECKED_CAST",
"UNNECESSARY_LATEINIT",
"UNNECESSARY_NOT_NULL_ASSERTION",
"UNREACHABLE_CODE",
"UNUSED_ANONYMOUS_PARAMETER",
"UNUSED_PARAMETER",
"UNUSED_VARIABLE",
"USELESS_CAST",
"VARIABLE_IN_SINGLETON_WITHOUT_THREAD_LOCAL",
"VARIABLE_WITH_REDUNDANT_INITIALIZER",
"REDUNDANT_ELSE_IN_WHEN")

package autovalue

import javaemul.lang.*
import autovalue.AutoValue_AutoValueWithBuilder
import kotlin.Boolean
import kotlin.Double
import kotlin.OptIn
import kotlin.Suppress
import kotlin.experimental.ExperimentalObjCName
import kotlin.jvm.JvmStatic
import kotlin.native.ObjCName

@ObjCName("J2ktAutovalueAutoValueWithBuilder", exact = true)
abstract class AutoValueWithBuilder {
@ObjCName("getBooleanField")
abstract fun getBooleanField(): Boolean

@ObjCName("getNullableField")
abstract fun getNullableField(): Double?

@ObjCName("toBuilder")
abstract fun toBuilder(): AutoValueWithBuilder.Builder?

@ObjCName("J2ktAutovalueAutoValueWithBuilderCompanion", exact = true)
companion object {
@JvmStatic
internal fun create_pp_autovalue(): AutoValueWithBuilder? {
return AutoValue_AutoValueWithBuilder.Builder().setBooleanField(true)!!.build()
}
}

@ObjCName("J2ktAutovalueAutoValueWithBuilder_Builder", exact = true)
abstract class Builder internal constructor() {
@ObjCName("setBooleanField")
abstract fun setBooleanField(
@ObjCName("withBoolean") x: Boolean
): AutoValueWithBuilder.Builder?

@ObjCName("setNullableField")
abstract fun setNullableField(
@ObjCName("withJavaLangDouble") x: Double?
): AutoValueWithBuilder.Builder?

@ObjCName("build")
abstract fun build(): AutoValueWithBuilder?
}
}
Loading

0 comments on commit 091a9d7

Please sign in to comment.