Skip to content

Commit 8b11e39

Browse files
Googlercopybara-github
authored andcommitted
[javac] Add readable test for sealed classes.
PiperOrigin-RevId: 777667014
1 parent c8ea395 commit 8b11e39

44 files changed

Lines changed: 3185 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load(
2+
"//transpiler/javatests/com/google/j2cl/readable:readable_example.bzl",
3+
"readable_example",
4+
)
5+
6+
package(
7+
default_applicable_licenses = ["//:j2cl_license"],
8+
licenses = ["notice"],
9+
)
10+
11+
readable_example(
12+
srcs = glob(["**/*.java"]),
13+
experimental_java_frontend = "javac",
14+
javacopts = [
15+
"-source 21",
16+
"-target 21",
17+
],
18+
)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2025 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sealedclasses;
18+
19+
import sealedclasses.sealedclassinanotherpackage.SealedClassInAnotherPackage;
20+
21+
public class SealedClasses {
22+
public static final class SubClass extends SealedClassInAnotherPackage {}
23+
24+
public sealed interface Animal {}
25+
26+
public abstract static sealed class Mammal implements Animal {
27+
public Mammal() {}
28+
29+
private Mammal(String name) {
30+
this();
31+
}
32+
33+
public String retrieveName() {
34+
return "Animal";
35+
}
36+
37+
public abstract void changeName(String updatedName);
38+
39+
// Subclasses can have any visibility
40+
protected static final class Dolphin extends Mammal {
41+
@Override
42+
public void changeName(String updatedName) {}
43+
}
44+
}
45+
46+
public static final class Dog extends Mammal {
47+
@Override
48+
public void changeName(String updatedName) {}
49+
}
50+
51+
static final class Cat extends Mammal {
52+
@Override
53+
public void changeName(String updatedName) {}
54+
}
55+
56+
private static final class Whale extends Mammal {
57+
@Override
58+
public void changeName(String updatedName) {}
59+
}
60+
61+
public static final class Deer implements Animal {
62+
private Deer() {}
63+
}
64+
65+
// Enum classes cannot extend a sealed class, but they can implement sealed interfaces.
66+
enum EnumImplementsSealedInterface implements Animal {
67+
V1,
68+
V2
69+
}
70+
71+
public abstract static sealed class Shape {
72+
public static final class Circle extends Shape {}
73+
74+
public static final class Rectangle extends Shape {
75+
private Rectangle() {}
76+
}
77+
}
78+
79+
// The else branch is not mandatory in when expression if when has a subject of sealed type
80+
// TODO(b/286448526): Implement pattern matching switch.
81+
// private static String checkShape(Shape e) {
82+
// return switch (e) {
83+
// case Shape.Circle c -> "Circle";
84+
// case Shape.Rectangle r -> "Rectangle";
85+
// };
86+
// }
87+
88+
// Unlike Kotlin, sealed classes in Java can be concrete and subtypes can be inner classes.
89+
public static sealed class SealedClassWithInnerClass {
90+
public final class A extends SealedClassWithInnerClass {}
91+
92+
public final class B extends SealedClassWithInnerClass {}
93+
}
94+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright 2025 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
// Java allows subtypes of sealed classes to be defined in different packages, but they must be in
15+
// the same module and NOT the unnamed module.
16+
module sealedclasses {}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
goog.module('sealedclasses.SealedClasses.Animal$impl');
2+
3+
const $Util = goog.require('nativebootstrap.Util$impl');
4+
5+
/**
6+
* @interface
7+
*/
8+
class Animal {
9+
/** @nodts */
10+
static $clinit() {
11+
Animal.$clinit = () =>{};
12+
Animal.$loadModules();
13+
}
14+
15+
static $markImplementor(/** Function */ ctor) {
16+
ctor.prototype.$implements__sealedclasses_SealedClasses_Animal = true;
17+
}
18+
/** @nodts @return {boolean} */
19+
static $isInstance(/** ? */ instance) {
20+
return instance != null && !!instance.$implements__sealedclasses_SealedClasses_Animal;
21+
}
22+
23+
/** @nodts */
24+
static $loadModules() {}
25+
}
26+
Animal.$markImplementor(/**@type {Function}*/ (Animal));
27+
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (Animal), 'sealedclasses.SealedClasses$Animal');
28+
29+
exports = Animal;
30+
31+
//# sourceMappingURL=SealedClasses$Animal.js.map
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
goog.module('sealedclasses.SealedClasses.Animal');
2+
3+
goog.require('nativebootstrap.Util');
4+
5+
const Animal = goog.require('sealedclasses.SealedClasses.Animal$impl');
6+
exports = Animal;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
goog.module('sealedclasses.SealedClasses.Cat$impl');
2+
3+
const $Util = goog.require('nativebootstrap.Util$impl');
4+
const Mammal = goog.require('sealedclasses.SealedClasses.Mammal$impl');
5+
6+
/**
7+
* @final
8+
*/
9+
class Cat extends Mammal {
10+
/** @protected @nodts */
11+
constructor() {
12+
super();
13+
}
14+
/** @nodts @return {!Cat} */
15+
static $create__() {
16+
Cat.$clinit();
17+
let $instance = new Cat();
18+
$instance.$ctor__sealedclasses_SealedClasses_Cat__void();
19+
return $instance;
20+
}
21+
/** @nodts */
22+
$ctor__sealedclasses_SealedClasses_Cat__void() {
23+
this.$ctor__sealedclasses_SealedClasses_Mammal__void();
24+
}
25+
/** @override @nodts */
26+
m_changeName__java_lang_String__void(/** ?string */ updatedName) {}
27+
/** @nodts */
28+
static $clinit() {
29+
Cat.$clinit = () =>{};
30+
Cat.$loadModules();
31+
Mammal.$clinit();
32+
}
33+
/** @nodts @return {boolean} */
34+
static $isInstance(/** ? */ instance) {
35+
return instance instanceof Cat;
36+
}
37+
38+
/** @nodts */
39+
static $loadModules() {}
40+
}
41+
$Util.$setClassMetadata(Cat, 'sealedclasses.SealedClasses$Cat');
42+
43+
exports = Cat;
44+
45+
//# sourceMappingURL=SealedClasses$Cat.js.map
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
goog.module('sealedclasses.SealedClasses.Cat');
2+
3+
goog.require('nativebootstrap.Util');
4+
goog.require('sealedclasses.SealedClasses.Mammal');
5+
6+
const Cat = goog.require('sealedclasses.SealedClasses.Cat$impl');
7+
exports = Cat;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
goog.module('sealedclasses.SealedClasses.Deer$impl');
2+
3+
const j_l_Object = goog.require('java.lang.Object$impl');
4+
const $Util = goog.require('nativebootstrap.Util$impl');
5+
const Animal = goog.require('sealedclasses.SealedClasses.Animal$impl');
6+
7+
/**
8+
* @final
9+
* @implements {Animal}
10+
*/
11+
class Deer extends j_l_Object {
12+
/** @protected @nodts */
13+
constructor() {
14+
super();
15+
}
16+
/** @nodts @return {!Deer} */
17+
static $create__() {
18+
let $instance = new Deer();
19+
$instance.$ctor__sealedclasses_SealedClasses_Deer__void();
20+
return $instance;
21+
}
22+
/** @nodts */
23+
$ctor__sealedclasses_SealedClasses_Deer__void() {
24+
this.$ctor__java_lang_Object__void();
25+
}
26+
/** @nodts */
27+
static $clinit() {
28+
Deer.$clinit = () =>{};
29+
Deer.$loadModules();
30+
j_l_Object.$clinit();
31+
}
32+
/** @nodts @return {boolean} */
33+
static $isInstance(/** ? */ instance) {
34+
return instance instanceof Deer;
35+
}
36+
37+
/** @nodts */
38+
static $loadModules() {}
39+
}
40+
Animal.$markImplementor(Deer);
41+
$Util.$setClassMetadata(Deer, 'sealedclasses.SealedClasses$Deer');
42+
43+
exports = Deer;
44+
45+
//# sourceMappingURL=SealedClasses$Deer.js.map
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
goog.module('sealedclasses.SealedClasses.Deer');
2+
3+
goog.require('java.lang.Object');
4+
goog.require('nativebootstrap.Util');
5+
goog.require('sealedclasses.SealedClasses.Animal');
6+
7+
const Deer = goog.require('sealedclasses.SealedClasses.Deer$impl');
8+
exports = Deer;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
goog.module('sealedclasses.SealedClasses.Dog$impl');
2+
3+
const $Util = goog.require('nativebootstrap.Util$impl');
4+
const Mammal = goog.require('sealedclasses.SealedClasses.Mammal$impl');
5+
6+
/**
7+
* @final
8+
*/
9+
class Dog extends Mammal {
10+
/** @protected @nodts */
11+
constructor() {
12+
super();
13+
}
14+
/** @nodts @return {!Dog} */
15+
static $create__() {
16+
Dog.$clinit();
17+
let $instance = new Dog();
18+
$instance.$ctor__sealedclasses_SealedClasses_Dog__void();
19+
return $instance;
20+
}
21+
/** @nodts */
22+
$ctor__sealedclasses_SealedClasses_Dog__void() {
23+
this.$ctor__sealedclasses_SealedClasses_Mammal__void();
24+
}
25+
/** @override @nodts */
26+
m_changeName__java_lang_String__void(/** ?string */ updatedName) {}
27+
/** @nodts */
28+
static $clinit() {
29+
Dog.$clinit = () =>{};
30+
Dog.$loadModules();
31+
Mammal.$clinit();
32+
}
33+
/** @nodts @return {boolean} */
34+
static $isInstance(/** ? */ instance) {
35+
return instance instanceof Dog;
36+
}
37+
38+
/** @nodts */
39+
static $loadModules() {}
40+
}
41+
$Util.$setClassMetadata(Dog, 'sealedclasses.SealedClasses$Dog');
42+
43+
exports = Dog;
44+
45+
//# sourceMappingURL=SealedClasses$Dog.js.map

0 commit comments

Comments
 (0)