Skip to content

Commit 4bb0454

Browse files
feature: sealed classes (JEP 409) (#4337)
Co-authored-by: I-Al-Istannen <[email protected]>
1 parent ef9714d commit 4bb0454

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+762
-60
lines changed

spoon-visualisation/src/test/java/spoon/visualisation/instrument/SpoonCodeInstrumentTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import spoon.reflect.cu.SourcePositionHolder;
5757
import spoon.reflect.declaration.CtClass;
5858
import spoon.reflect.declaration.CtElement;
59+
import spoon.reflect.declaration.CtSealable;
5960
import spoon.reflect.declaration.CtType;
6061
import spoon.reflect.path.CtPath;
6162
import spoon.reflect.visitor.CtVisitable;
@@ -232,7 +233,7 @@ void testSaveText(final FxRobot robot, @TempDir final Path tempDir) throws IOExc
232233
@Test
233234
void getDirectSpoonInterfacesOfClass() {
234235
final List<Class<?>> inter = spoonCodeInstrument.getDirectSpoonInterfaces(CtClass.class).collect(Collectors.toList());
235-
assertThat(inter).containsExactlyInAnyOrder(CtType.class, CtStatement.class);
236+
assertThat(inter).containsExactlyInAnyOrder(CtType.class, CtStatement.class, CtSealable.class);
236237
}
237238

238239
@Test

src/main/java/spoon/metamodel/Metamodel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public static Set<CtType<?>> getAllMetamodelInterfaces() {
152152
result.add(factory.Type().get(spoon.reflect.declaration.CtNamedElement.class));
153153
result.add(factory.Type().get(spoon.reflect.declaration.CtPackage.class));
154154
result.add(factory.Type().get(spoon.reflect.declaration.CtParameter.class));
155+
result.add(factory.Type().get(spoon.reflect.declaration.CtSealable.class));
155156
result.add(factory.Type().get(spoon.reflect.declaration.CtShadowable.class));
156157
result.add(factory.Type().get(spoon.reflect.declaration.CtType.class));
157158
result.add(factory.Type().get(spoon.reflect.declaration.CtTypeInformation.class));

src/main/java/spoon/reflect/declaration/CtClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* </pre>
3232
* @author Renaud Pawlak
3333
*/
34-
public interface CtClass<T> extends CtType<T>, CtStatement {
34+
public interface CtClass<T> extends CtType<T>, CtStatement, CtSealable {
3535
/**
3636
* Returns the anonymous blocks of this class.
3737
* Derived from {@link #getTypeMembers()}

src/main/java/spoon/reflect/declaration/CtEnum.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
import spoon.reflect.reference.CtTypeReference;
1111
import spoon.reflect.annotations.PropertyGetter;
1212
import spoon.reflect.annotations.PropertySetter;
13+
import spoon.support.DerivedProperty;
1314
import spoon.support.UnsettableProperty;
1415

16+
import java.util.Collection;
1517
import java.util.List;
18+
import java.util.Set;
1619

1720
import static spoon.reflect.path.CtRole.VALUE;
1821

@@ -80,4 +83,20 @@ public interface CtEnum<T extends Enum<?>> extends CtClass<T> {
8083
@Override
8184
@UnsettableProperty
8285
<C extends CtType<T>> C setSuperclass(CtTypeReference<?> superClass);
86+
87+
@Override
88+
@DerivedProperty
89+
Set<CtTypeReference<?>> getPermittedTypes();
90+
91+
@Override
92+
@UnsettableProperty
93+
CtEnum<T> setPermittedTypes(Collection<CtTypeReference<?>> permittedTypes);
94+
95+
@Override
96+
@UnsettableProperty
97+
CtEnum<T> addPermittedType(CtTypeReference<?> type);
98+
99+
@Override
100+
@UnsettableProperty
101+
CtEnum<T> removePermittedType(CtTypeReference<?> type);
83102
}

src/main/java/spoon/reflect/declaration/CtInterface.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* }
2222
* </pre>
2323
*/
24-
public interface CtInterface<T> extends CtType<T>, CtStatement {
24+
public interface CtInterface<T> extends CtType<T>, CtStatement, CtSealable {
2525
@Override
2626
CtInterface<T> clone();
2727

src/main/java/spoon/reflect/declaration/CtRecord.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package spoon.reflect.declaration;
99

10+
import java.util.Collection;
1011
import java.util.Set;
1112
import spoon.reflect.annotations.PropertyGetter;
1213
import spoon.reflect.annotations.PropertySetter;
@@ -40,4 +41,19 @@ public interface CtRecord extends CtClass<Object> {
4041
@Override
4142
@UnsettableProperty
4243
<C extends CtType<Object>> C setSuperclass(CtTypeReference<?> superClass);
44+
45+
@Override
46+
Set<CtTypeReference<?>> getPermittedTypes();
47+
48+
@Override
49+
@UnsettableProperty
50+
CtRecord setPermittedTypes(Collection<CtTypeReference<?>> permittedTypes);
51+
52+
@Override
53+
@UnsettableProperty
54+
CtRecord addPermittedType(CtTypeReference<?> type);
55+
56+
@Override
57+
@UnsettableProperty
58+
CtRecord removePermittedType(CtTypeReference<?> type);
4359
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* SPDX-License-Identifier: (MIT OR CECILL-C)
3+
*
4+
* Copyright (C) 2006-2019 INRIA and contributors
5+
*
6+
* Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
7+
*/
8+
package spoon.reflect.declaration;
9+
10+
import spoon.reflect.annotations.PropertyGetter;
11+
import spoon.reflect.annotations.PropertySetter;
12+
import spoon.reflect.path.CtRole;
13+
import spoon.reflect.reference.CtTypeReference;
14+
15+
import java.util.Collection;
16+
import java.util.Set;
17+
18+
/**
19+
* This interface represents any type that can be sealed.
20+
* See JLS 8.1.1.2
21+
*/
22+
public interface CtSealable {
23+
24+
/**
25+
* Returns the permitted types for this type.
26+
*
27+
* @return an unmodifiable view of the permitted types.
28+
*/
29+
@PropertyGetter(role = CtRole.PERMITTED_TYPE)
30+
Set<CtTypeReference<?>> getPermittedTypes();
31+
32+
/**
33+
* Sets the permitted types for this type.
34+
* Calling this method does not change the state of the {@link ModifierKind#SEALED} for this type.
35+
* The previously permitted types will be removed.
36+
*
37+
* @param permittedTypes the permitted types to set.
38+
* @return this.
39+
*/
40+
@PropertySetter(role = CtRole.PERMITTED_TYPE)
41+
CtSealable setPermittedTypes(Collection<CtTypeReference<?>> permittedTypes);
42+
43+
/**
44+
* Adds a permitted type to this type.
45+
* Calling this method does not change the state of the {@link ModifierKind#SEALED} for this type.
46+
*
47+
* @param type the type to add as permitted type.
48+
* @return this.
49+
*/
50+
@PropertySetter(role = CtRole.PERMITTED_TYPE)
51+
CtSealable addPermittedType(CtTypeReference<?> type);
52+
53+
/**
54+
* Adds a permitted type to this type.
55+
* Calling this method does not change the state of the {@link ModifierKind#SEALED} for this type.
56+
*
57+
* @param type the type to remove from this type's permitted types.
58+
* @return this.
59+
*/
60+
@PropertySetter(role = CtRole.PERMITTED_TYPE)
61+
CtSealable removePermittedType(CtTypeReference<?> type);
62+
}

src/main/java/spoon/reflect/declaration/ModifierKind.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,58 +20,67 @@ public enum ModifierKind {
2020
/**
2121
* The modifier <tt>public</tt>
2222
*/
23-
PUBLIC,
23+
PUBLIC("public"),
2424
/**
2525
* The modifier <tt>protected</tt>
2626
*/
27-
PROTECTED,
27+
PROTECTED("protected"),
2828
/**
2929
* The modifier <tt>private</tt>
3030
*/
31-
PRIVATE,
31+
PRIVATE("private"),
3232
/**
3333
* The modifier <tt>abstract</tt>
3434
*/
35-
ABSTRACT,
35+
ABSTRACT("abstract"),
3636
/**
3737
* The modifier <tt>static</tt>
3838
*/
39-
STATIC,
39+
STATIC("static"),
4040
/**
4141
* The modifier <tt>final</tt>
4242
*/
43-
FINAL,
43+
FINAL("final"),
4444
/**
4545
* The modifier <tt>transient</tt>
4646
*/
47-
TRANSIENT,
47+
TRANSIENT("transient"),
4848
/**
4949
* The modifier <tt>volatile</tt>
5050
*/
51-
VOLATILE,
51+
VOLATILE("volatile"),
5252
/**
5353
* The modifier <tt>synchronized</tt>
5454
*/
55-
SYNCHRONIZED,
55+
SYNCHRONIZED("synchronized"),
5656
/**
5757
* The modifier <tt>native</tt>
5858
*/
59-
NATIVE,
59+
NATIVE("native"),
6060
/**
6161
* The modifier <tt>strictfp</tt>
6262
*/
63-
STRICTFP;
63+
STRICTFP("strictfp"),
64+
/**
65+
* The modifier <tt>non-sealed</tt>
66+
*/
67+
NON_SEALED("non-sealed"),
68+
/**
69+
* The modifier <tt>sealed</tt>
70+
*/
71+
SEALED("sealed");
72+
73+
ModifierKind(String lowercase) {
74+
this.lowercase = lowercase;
75+
}
6476

65-
private String lowercase = null; // modifier name in lowercase
77+
private final String lowercase; // modifier name in lowercase
6678

6779
/**
6880
* Returns this modifier's name in lowercase.
6981
*/
7082
@Override
7183
public String toString() {
72-
if (lowercase == null) {
73-
lowercase = name().toLowerCase(java.util.Locale.US);
74-
}
7584
return lowercase;
7685
}
7786

0 commit comments

Comments
 (0)