Skip to content

Commit

Permalink
feature: add support for Java 16 pattern matching (JEP 394) (#4020)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell authored Aug 23, 2021
1 parent c964a75 commit e8c0d1b
Show file tree
Hide file tree
Showing 27 changed files with 546 additions and 103 deletions.
13 changes: 13 additions & 0 deletions doc/code_elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,19 @@ int x = switch(i) { // <-- switch expression
// fake field "class"
Class x = Number.class

```
### CtTypePattern
[(javadoc)](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/code/CtTypePattern.html)

```java

Object obj = null;
boolean longerThanTwo = false;
// String s is the type pattern, declaring a local variable
if (obj instanceof String s) {
longerThanTwo = s.length() > 2;
}

```
### CtUnaryOperator
[(javadoc)](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/code/CtUnaryOperator.html)
Expand Down
2 changes: 1 addition & 1 deletion spoon-control-flow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<dependency>
<groupId>fr.inria.gforge.spoon</groupId>
<artifactId>spoon-core</artifactId>
<version>9.0.0</version>
<version>9.2.0-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import spoon.reflect.code.CtTry;
import spoon.reflect.code.CtTryWithResource;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.code.CtTypePattern;
import spoon.reflect.code.CtUnaryOperator;
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.code.CtVariableWrite;
Expand Down Expand Up @@ -974,4 +975,9 @@ public void visitCtTypeMemberWildcardImportReference(CtTypeMemberWildcardImportR
public void visitCtYieldStatement(CtYieldStatement ctYieldStatement) {

}

@Override
public void visitCtTypePattern(CtTypePattern pattern) {

}
}
2 changes: 1 addition & 1 deletion spoon-smpl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
<dependency>
<groupId>fr.inria.gforge.spoon</groupId>
<artifactId>spoon-core</artifactId>
<version>9.0.0</version>
<version>9.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>fr.inria.gforge.spoon</groupId>
Expand Down
6 changes: 6 additions & 0 deletions spoon-smpl/src/main/java/spoon/smpl/Substitutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import spoon.reflect.code.CtTry;
import spoon.reflect.code.CtTryWithResource;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.code.CtTypePattern;
import spoon.reflect.code.CtUnaryOperator;
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.code.CtVariableWrite;
Expand Down Expand Up @@ -680,4 +681,9 @@ public void visitCtTypeMemberWildcardImportReference(CtTypeMemberWildcardImportR
public void visitCtYieldStatement(CtYieldStatement ctYieldStatement) {
throw new NotImplementedException("Not implemented");
}

@Override
public void visitCtTypePattern(CtTypePattern pattern) {
throw new NotImplementedException("Not implemented");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import spoon.reflect.code.CtTry;
import spoon.reflect.code.CtTryWithResource;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.code.CtTypePattern;
import spoon.reflect.code.CtUnaryOperator;
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.code.CtVariableWrite;
Expand Down Expand Up @@ -821,6 +822,11 @@ public void visitCtYieldStatement(CtYieldStatement ctYieldStatement) {
throw new NotImplementedException("Not implemented");
}

@Override
public void visitCtTypePattern(CtTypePattern pattern) {
throw new NotImplementedException("Not implemented");
}

/**
* Parameter identifiers.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/spoon/metamodel/Metamodel.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public static Set<CtType<?>> getAllMetamodelInterfaces() {
result.add(factory.Type().get(spoon.reflect.code.CtNewArray.class));
result.add(factory.Type().get(spoon.reflect.code.CtNewClass.class));
result.add(factory.Type().get(spoon.reflect.code.CtOperatorAssignment.class));
result.add(factory.Type().get(spoon.reflect.code.CtPattern.class));
result.add(factory.Type().get(spoon.reflect.code.CtRHSReceiver.class));
result.add(factory.Type().get(spoon.reflect.code.CtReturn.class));
result.add(factory.Type().get(spoon.reflect.code.CtStatement.class));
Expand All @@ -119,6 +120,7 @@ public static Set<CtType<?>> getAllMetamodelInterfaces() {
result.add(factory.Type().get(spoon.reflect.code.CtTry.class));
result.add(factory.Type().get(spoon.reflect.code.CtTryWithResource.class));
result.add(factory.Type().get(spoon.reflect.code.CtTypeAccess.class));
result.add(factory.Type().get(spoon.reflect.code.CtTypePattern.class));
result.add(factory.Type().get(spoon.reflect.code.CtUnaryOperator.class));
result.add(factory.Type().get(spoon.reflect.code.CtVariableAccess.class));
result.add(factory.Type().get(spoon.reflect.code.CtVariableRead.class));
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/spoon/reflect/code/CtPattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* SPDX-License-Identifier: (MIT OR CECILL-C)
*
* Copyright (C) 2006-2019 INRIA and contributors
*
* 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.
*/
package spoon.reflect.code;

import spoon.reflect.declaration.CtElement;

/**
* This interface represents a common super type for different
* patterns implemented in the Java language.
*/
public interface CtPattern extends CtElement {

}
66 changes: 66 additions & 0 deletions src/main/java/spoon/reflect/code/CtTypePattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* SPDX-License-Identifier: (MIT OR CECILL-C)
*
* Copyright (C) 2006-2019 INRIA and contributors
*
* 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.
*/
package spoon.reflect.code;

import spoon.reflect.annotations.PropertyGetter;
import spoon.reflect.annotations.PropertySetter;
import spoon.reflect.reference.CtTypeReference;
import spoon.support.UnsettableProperty;

import java.util.List;

import static spoon.reflect.path.CtRole.VARIABLE;

/**
* This code element defines a type pattern, introduced in Java 16
* by <a href=https://openjdk.java.net/jeps/394>JEP 394</a>.
* <p>
* Example:
* <pre>
* Object obj = null;
* boolean longerThanTwo = false;
* // String s is the type pattern, declaring a local variable
* if (obj instanceof String s) {
* longerThanTwo = s.length() > 2;
* }
* </pre>
*/
public interface CtTypePattern extends CtPattern, CtExpression<Void> {

/**
* Returns the local variable declared by this type pattern.
*
* @return the local variable.
*/
@PropertyGetter(role = VARIABLE)
CtLocalVariable<?> getVariable();

/**
* Sets the local variable for this type pattern.
*
* @param variable the variable to set for this type pattern.
* @return this pattern.
*/
@PropertySetter(role = VARIABLE)
CtTypePattern setVariable(CtLocalVariable<?> variable);

@Override
CtTypePattern clone();

@Override
@UnsettableProperty
List<CtTypeReference<?>> getTypeCasts();

@Override
@UnsettableProperty
<C extends CtExpression<Void>> C setTypeCasts(List<CtTypeReference<?>> types);

@Override
@UnsettableProperty
<C extends CtExpression<Void>> C addTypeCast(CtTypeReference<?> type);
}
7 changes: 7 additions & 0 deletions src/main/java/spoon/reflect/factory/CoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import spoon.reflect.code.CtTry;
import spoon.reflect.code.CtTryWithResource;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.code.CtTypePattern;
import spoon.reflect.code.CtUnaryOperator;
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.code.CtVariableWrite;
Expand Down Expand Up @@ -619,4 +620,10 @@ BodyHolderSourcePosition createBodyHolderSourcePosition(
* Creates a TextBlock String Literal
*/
CtTextBlock createTextBlock();

/**
* Creates a Type Pattern
* @return a new Type Pattern instance.
*/
CtTypePattern createTypePattern();
}
Loading

0 comments on commit e8c0d1b

Please sign in to comment.