Skip to content

Commit

Permalink
Support case ->
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Jan 15, 2019
1 parent 9df3e64 commit 99747a0
Show file tree
Hide file tree
Showing 35 changed files with 153 additions and 163 deletions.
Expand Up @@ -9,7 +9,7 @@
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.stmt.SwitchEntryStmt;
import com.github.javaparser.ast.stmt.SwitchEntry;
import com.github.javaparser.ast.stmt.SwitchStmt;
import com.github.javaparser.generator.Generator;
import com.github.javaparser.utils.Log;
Expand Down Expand Up @@ -38,7 +38,7 @@ public void generate() {
annotateGenerated(kindEnum);

final SwitchStmt valueOfSwitch = kindEnum.findFirst(SwitchStmt.class).orElseThrow(() -> new AssertionError("Can't find valueOf switch."));
valueOfSwitch.findAll(SwitchEntryStmt.class).stream().filter(e -> e.getLabel().isPresent()).forEach(Node::remove);
valueOfSwitch.findAll(SwitchEntry.class).stream().filter(e -> e.getLabel().isPresent()).forEach(Node::remove);

final CompilationUnit constantsCu = generatedJavaCcSourceRoot.parse("com.github.javaparser", "GeneratedJavaParserConstants.java");
final ClassOrInterfaceDeclaration constants = constantsCu.getInterfaceByName("GeneratedJavaParserConstants").orElseThrow(() -> new AssertionError("Can't find class in java file."));
Expand All @@ -59,7 +59,7 @@ public void generate() {
}

private void generateValueOfEntry(SwitchStmt valueOfSwitch, String name, IntegerLiteralExpr kind) {
final SwitchEntryStmt entry = new SwitchEntryStmt(kind, new NodeList<>(new ReturnStmt(name)));
final SwitchEntry entry = new SwitchEntry(kind, new NodeList<>(new ReturnStmt(name)));
valueOfSwitch.getEntries().addFirst(entry);
}

Expand Down
Expand Up @@ -125,7 +125,7 @@ public class MetaModelGenerator {
add(IfStmt.class);
add(LabeledStmt.class);
add(ReturnStmt.class);
add(SwitchEntryStmt.class);
add(SwitchEntry.class);
add(SwitchStmt.class);
add(SynchronizedStmt.class);
add(ThrowStmt.class);
Expand Down
Expand Up @@ -231,7 +231,7 @@ void everyTokenHasACategory() throws IOException {
Path tokenTypesPath = mavenModuleRoot(JavaParserTest.class).resolve("../javaparser-core/src/main/java/com/github/javaparser/TokenTypes.java");
CompilationUnit tokenTypesCu = JavaParser.parse(tokenTypesPath);
// -1 to take off the default: case.
int switchEntries = tokenTypesCu.findAll(SwitchEntryStmt.class).size() - 1;
int switchEntries = tokenTypesCu.findAll(SwitchEntry.class).size() - 1;
// The amount of "case XXX:" in TokenTypes.java should be equal to the amount of tokens JavaCC knows about:
assertEquals(tokenCount, switchEntries);
}
Expand Down
Expand Up @@ -5,29 +5,26 @@
import org.junit.jupiter.api.Test;

class SwitchExprTest {
@Disabled("to be implemented")
@Test
void jep325Example1() {
JavaParser.parseStatement("switch (day) {\n" +
" case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);\n" +
// " case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);\n" +
" case TUESDAY -> System.out.println(7);\n" +
" case THURSDAY, SATURDAY -> System.out.println(8);\n" +
// " case THURSDAY, SATURDAY -> System.out.println(8);\n" +
" case WEDNESDAY -> System.out.println(9);\n" +
"}};");
"}");
}

@Disabled("to be implemented")
@Test
void jep325Example2() {
JavaParser.parseStatement("int numLetters = switch (day) {\n" +
" case MONDAY, FRIDAY, SUNDAY -> 6;\n" +
// " case MONDAY, FRIDAY, SUNDAY -> 6;\n" +
" case TUESDAY -> 7;\n" +
" case THURSDAY, SATURDAY -> 8;\n" +
// " case THURSDAY, SATURDAY -> 8;\n" +
" case WEDNESDAY -> 9;\n" +
"};");
}

@Disabled("to be implemented")
@Test
void jep325Example3() {
JavaParser.parseBodyDeclaration("static void howMany(int k) {\n" +
Expand All @@ -39,7 +36,6 @@ void jep325Example3() {
"}");
}

@Disabled("to be implemented")
@Test
void jep325Example4() {
JavaParser.parseStatement("T result = switch (arg) {\n" +
Expand All @@ -49,7 +45,6 @@ void jep325Example4() {
"};");
}

@Disabled("to be implemented")
@Test
void jep325Example5() {
JavaParser.parseStatement("int j = switch (day) {\n" +
Expand All @@ -63,7 +58,7 @@ void jep325Example5() {
"};");
}

@Disabled("to be implemented")
@Disabled
@Test
void jep325Example6() {
JavaParser.parseStatement("int result = switch (s) {\n" +
Expand All @@ -76,12 +71,4 @@ void jep325Example6() {
" break 0;\n" +
"};");
}

@Test
void placeholderTest() {
JavaParser.parseStatement("int result = switch (s) {\n" +
" case \"Foo\": \n" +
" break;\n" +
"};");
}
}
Expand Up @@ -437,7 +437,7 @@ public void visit(SuperExpr n, Void arg) {
}

@Override
public void visit(SwitchEntryStmt n, Void arg) {
public void visit(SwitchEntry n, Void arg) {
assertParentIsSet(n);
super.visit(n, arg);
}
Expand Down
Expand Up @@ -442,7 +442,7 @@ public void visit(final SuperExpr n, final Object arg) {
}

@Override
public void visit(final SwitchEntryStmt n, final Object arg) {
public void visit(final SwitchEntry n, final Object arg) {
doTest(n);
super.visit(n, arg);
}
Expand Down
Expand Up @@ -27,13 +27,12 @@
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.nodeTypes.SwitchNode;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.stmt.SwitchEntryStmt;
import com.github.javaparser.ast.stmt.SwitchEntry;
import com.github.javaparser.ast.visitor.CloneVisitor;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
import com.github.javaparser.metamodel.JavaParserMetaModel;
import com.github.javaparser.metamodel.SwitchStmtMetaModel;

import java.util.Optional;
import java.util.function.Consumer;
import static com.github.javaparser.utils.Utils.assertNotNull;
Expand All @@ -45,30 +44,30 @@
* and the contents of the { ... } are the entries.
*
* @author Julio Vilmar Gesser
* @see SwitchEntryStmt
* @see SwitchEntry
* @see com.github.javaparser.ast.stmt.SwitchStmt
* @see SwitchNode
*/
public final class SwitchExpr extends Expression implements SwitchNode {

private Expression selector;

private NodeList<SwitchEntryStmt> entries;
private NodeList<SwitchEntry> entries;

public SwitchExpr() {
this(null, new NameExpr(), new NodeList<>());
}

@AllFieldsConstructor
public SwitchExpr(final Expression selector, final NodeList<SwitchEntryStmt> entries) {
public SwitchExpr(final Expression selector, final NodeList<SwitchEntry> entries) {
this(null, selector, entries);
}

/**
* This constructor is used by the parser and is considered private.
*/
@Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
public SwitchExpr(TokenRange tokenRange, Expression selector, NodeList<SwitchEntryStmt> entries) {
public SwitchExpr(TokenRange tokenRange, Expression selector, NodeList<SwitchEntry> entries) {
super(tokenRange);
setSelector(selector);
setEntries(entries);
Expand All @@ -88,11 +87,11 @@ public <A> void accept(final VoidVisitor<A> v, final A arg) {
}

@Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
public NodeList<SwitchEntryStmt> getEntries() {
public NodeList<SwitchEntry> getEntries() {
return entries;
}

public SwitchEntryStmt getEntry(int i) {
public SwitchEntry getEntry(int i) {
return getEntries().get(i);
}

Expand All @@ -102,7 +101,7 @@ public Expression getSelector() {
}

@Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
public SwitchExpr setEntries(final NodeList<SwitchEntryStmt> entries) {
public SwitchExpr setEntries(final NodeList<SwitchEntry> entries) {
assertNotNull(entries);
if (entries == this.entries) {
return (SwitchExpr) this;
Expand All @@ -119,7 +118,7 @@ public SwitchExpr setEntries(final NodeList<SwitchEntryStmt> entries) {
* @deprecated use a method on getEntries instead
*/
@Deprecated
public SwitchExpr setEntry(int i, SwitchEntryStmt entry) {
public SwitchExpr setEntry(int i, SwitchEntry entry) {
getEntries().set(i, entry);
return this;
}
Expand All @@ -128,7 +127,7 @@ public SwitchExpr setEntry(int i, SwitchEntryStmt entry) {
* @deprecated use a method on getEntries instead
*/
@Deprecated
public SwitchExpr addEntry(SwitchEntryStmt entry) {
public SwitchExpr addEntry(SwitchEntry entry) {
getEntries().add(entry);
return this;
}
Expand Down Expand Up @@ -174,7 +173,7 @@ public boolean replace(Node node, Node replacementNode) {
return false;
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i) == node) {
entries.set(i, (SwitchEntryStmt) replacementNode);
entries.set(i, (SwitchEntry) replacementNode);
return true;
}
}
Expand Down
Expand Up @@ -4,21 +4,21 @@
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.stmt.SwitchEntryStmt;
import com.github.javaparser.ast.stmt.SwitchEntry;

import java.util.Optional;

/**
* The common interface of {@link com.github.javaparser.ast.expr.SwitchExpr} and {@link com.github.javaparser.ast.stmt.SwitchStmt}
*/
public interface SwitchNode {
NodeList<SwitchEntryStmt> getEntries();
NodeList<SwitchEntry> getEntries();

SwitchEntryStmt getEntry(int i);
SwitchEntry getEntry(int i);

Expression getSelector();

SwitchNode setEntries(NodeList<SwitchEntryStmt> entries);
SwitchNode setEntries(NodeList<SwitchEntry> entries);

SwitchNode setSelector(Expression selector);

Expand Down
Expand Up @@ -216,8 +216,8 @@ public boolean isSwitchEntryStmt() {
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public SwitchEntryStmt asSwitchEntryStmt() {
throw new IllegalStateException(f("%s is not an SwitchEntryStmt", this));
public SwitchEntry asSwitchEntryStmt() {
throw new IllegalStateException(f("%s is not an SwitchEntry", this));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand Down Expand Up @@ -333,7 +333,7 @@ public void ifReturnStmt(Consumer<ReturnStmt> action) {
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public void ifSwitchEntryStmt(Consumer<SwitchEntryStmt> action) {
public void ifSwitchEntryStmt(Consumer<SwitchEntry> action) {
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand Down Expand Up @@ -426,7 +426,7 @@ public Optional<ReturnStmt> toReturnStmt() {
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public Optional<SwitchEntryStmt> toSwitchEntryStmt() {
public Optional<SwitchEntry> toSwitchEntryStmt() {
return Optional.empty();
}

Expand Down

0 comments on commit 99747a0

Please sign in to comment.