Skip to content

Commit

Permalink
consider cascading if in Concrete Syntax Model
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Nov 14, 2017
1 parent 11cf32b commit 2990332
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 17 deletions.
Expand Up @@ -40,7 +40,6 @@
import com.github.javaparser.TokenRange;
import com.github.javaparser.resolution.SymbolResolver;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;

import java.util.function.Consumer;

/**
Expand Down
Expand Up @@ -44,7 +44,6 @@
import com.github.javaparser.TokenRange;
import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;

import java.util.function.Consumer;

/**
Expand Down
Expand Up @@ -35,7 +35,7 @@
@Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
public enum ObservableProperty {

ANNOTATIONS(Type.MULTIPLE_REFERENCE), ANONYMOUS_CLASS_BODY(Type.MULTIPLE_REFERENCE), ARGUMENTS(Type.MULTIPLE_REFERENCE), ASTERISK(Type.SINGLE_ATTRIBUTE), BODY(Type.SINGLE_REFERENCE), CATCH_CLAUSES(Type.MULTIPLE_REFERENCE), CHECK(Type.SINGLE_REFERENCE), CLASS_BODY(Type.MULTIPLE_REFERENCE), CLASS_DECLARATION(Type.SINGLE_REFERENCE), CLASS_EXPR(Type.SINGLE_REFERENCE), COMMENT(Type.SINGLE_REFERENCE), COMPARE(Type.SINGLE_REFERENCE), COMPONENT_TYPE(Type.SINGLE_REFERENCE), CONDITION(Type.SINGLE_REFERENCE), CONTENT(Type.SINGLE_ATTRIBUTE), DEFAULT_VALUE(Type.SINGLE_REFERENCE), DIMENSION(Type.SINGLE_REFERENCE), ELEMENTS(Type.MULTIPLE_REFERENCE), ELEMENT_TYPE(Type.SINGLE_REFERENCE), ELSE_EXPR(Type.SINGLE_REFERENCE), ELSE_STMT(Type.SINGLE_REFERENCE), ENCLOSING_PARAMETERS(Type.SINGLE_ATTRIBUTE), ENTRIES(Type.MULTIPLE_REFERENCE), EXPRESSION(Type.SINGLE_REFERENCE), EXTENDED_TYPE(Type.SINGLE_REFERENCE), EXTENDED_TYPES(Type.MULTIPLE_REFERENCE), FINALLY_BLOCK(Type.SINGLE_REFERENCE), IDENTIFIER(Type.SINGLE_ATTRIBUTE), IMPLEMENTED_TYPES(Type.MULTIPLE_REFERENCE), IMPORTS(Type.MULTIPLE_REFERENCE), INDEX(Type.SINGLE_REFERENCE), INITIALIZATION(Type.MULTIPLE_REFERENCE), INITIALIZER(Type.SINGLE_REFERENCE), INNER(Type.SINGLE_REFERENCE), INTERFACE(Type.SINGLE_ATTRIBUTE), ITERABLE(Type.SINGLE_REFERENCE), LABEL(Type.SINGLE_REFERENCE), LEFT(Type.SINGLE_REFERENCE), LEVELS(Type.MULTIPLE_REFERENCE), MEMBERS(Type.MULTIPLE_REFERENCE), MEMBER_VALUE(Type.SINGLE_REFERENCE), MESSAGE(Type.SINGLE_REFERENCE), MODIFIERS(Type.MULTIPLE_ATTRIBUTE), MODULE(Type.SINGLE_REFERENCE), MODULE_NAMES(Type.MULTIPLE_REFERENCE), MODULE_STMTS(Type.MULTIPLE_REFERENCE), NAME(Type.SINGLE_REFERENCE), OPEN(Type.SINGLE_ATTRIBUTE), OPERATOR(Type.SINGLE_ATTRIBUTE), ORIGIN(Type.SINGLE_ATTRIBUTE), PACKAGE_DECLARATION(Type.SINGLE_REFERENCE), PAIRS(Type.MULTIPLE_REFERENCE), PARAMETER(Type.SINGLE_REFERENCE), PARAMETERS(Type.MULTIPLE_REFERENCE), QUALIFIER(Type.SINGLE_REFERENCE), RECEIVER_PARAMETER(Type.SINGLE_REFERENCE), RESOURCES(Type.MULTIPLE_REFERENCE), RIGHT(Type.SINGLE_REFERENCE), SCOPE(Type.SINGLE_REFERENCE), SELECTOR(Type.SINGLE_REFERENCE), STATEMENT(Type.SINGLE_REFERENCE), STATEMENTS(Type.MULTIPLE_REFERENCE), STATIC(Type.SINGLE_ATTRIBUTE), SUPER_TYPE(Type.SINGLE_REFERENCE), TARGET(Type.SINGLE_REFERENCE), THEN_EXPR(Type.SINGLE_REFERENCE), THEN_STMT(Type.SINGLE_REFERENCE), THIS(Type.SINGLE_ATTRIBUTE), THROWN_EXCEPTIONS(Type.MULTIPLE_REFERENCE), TRY_BLOCK(Type.SINGLE_REFERENCE), TYPE(Type.SINGLE_REFERENCE), TYPES(Type.MULTIPLE_REFERENCE), TYPE_ARGUMENTS(Type.MULTIPLE_REFERENCE), TYPE_BOUND(Type.MULTIPLE_REFERENCE), TYPE_PARAMETERS(Type.MULTIPLE_REFERENCE), UPDATE(Type.MULTIPLE_REFERENCE), VALUE(Type.SINGLE_REFERENCE), VALUES(Type.MULTIPLE_REFERENCE), VARIABLE(Type.SINGLE_REFERENCE), VARIABLES(Type.MULTIPLE_REFERENCE), VAR_ARGS(Type.SINGLE_ATTRIBUTE), VAR_ARGS_ANNOTATIONS(Type.MULTIPLE_REFERENCE), WITH_TYPES(Type.MULTIPLE_REFERENCE), ELSE_BLOCK(Type.SINGLE_ATTRIBUTE, true), EXPRESSION_BODY(Type.SINGLE_REFERENCE, true), MAXIMUM_COMMON_TYPE(Type.SINGLE_REFERENCE, true), POSTFIX(Type.SINGLE_ATTRIBUTE, true), PREFIX(Type.SINGLE_ATTRIBUTE, true), THEN_BLOCK(Type.SINGLE_ATTRIBUTE, true), USING_DIAMOND_OPERATOR(Type.SINGLE_ATTRIBUTE, true), RANGE, COMMENTED_NODE;
ANNOTATIONS(Type.MULTIPLE_REFERENCE), ANONYMOUS_CLASS_BODY(Type.MULTIPLE_REFERENCE), ARGUMENTS(Type.MULTIPLE_REFERENCE), ASTERISK(Type.SINGLE_ATTRIBUTE), BODY(Type.SINGLE_REFERENCE), CATCH_CLAUSES(Type.MULTIPLE_REFERENCE), CHECK(Type.SINGLE_REFERENCE), CLASS_BODY(Type.MULTIPLE_REFERENCE), CLASS_DECLARATION(Type.SINGLE_REFERENCE), CLASS_EXPR(Type.SINGLE_REFERENCE), COMMENT(Type.SINGLE_REFERENCE), COMPARE(Type.SINGLE_REFERENCE), COMPONENT_TYPE(Type.SINGLE_REFERENCE), CONDITION(Type.SINGLE_REFERENCE), CONTENT(Type.SINGLE_ATTRIBUTE), DEFAULT_VALUE(Type.SINGLE_REFERENCE), DIMENSION(Type.SINGLE_REFERENCE), ELEMENTS(Type.MULTIPLE_REFERENCE), ELEMENT_TYPE(Type.SINGLE_REFERENCE), ELSE_EXPR(Type.SINGLE_REFERENCE), ELSE_STMT(Type.SINGLE_REFERENCE), ENCLOSING_PARAMETERS(Type.SINGLE_ATTRIBUTE), ENTRIES(Type.MULTIPLE_REFERENCE), EXPRESSION(Type.SINGLE_REFERENCE), EXTENDED_TYPE(Type.SINGLE_REFERENCE), EXTENDED_TYPES(Type.MULTIPLE_REFERENCE), FINALLY_BLOCK(Type.SINGLE_REFERENCE), IDENTIFIER(Type.SINGLE_ATTRIBUTE), IMPLEMENTED_TYPES(Type.MULTIPLE_REFERENCE), IMPORTS(Type.MULTIPLE_REFERENCE), INDEX(Type.SINGLE_REFERENCE), INITIALIZATION(Type.MULTIPLE_REFERENCE), INITIALIZER(Type.SINGLE_REFERENCE), INNER(Type.SINGLE_REFERENCE), INTERFACE(Type.SINGLE_ATTRIBUTE), ITERABLE(Type.SINGLE_REFERENCE), LABEL(Type.SINGLE_REFERENCE), LEFT(Type.SINGLE_REFERENCE), LEVELS(Type.MULTIPLE_REFERENCE), MEMBERS(Type.MULTIPLE_REFERENCE), MEMBER_VALUE(Type.SINGLE_REFERENCE), MESSAGE(Type.SINGLE_REFERENCE), MODIFIERS(Type.MULTIPLE_ATTRIBUTE), MODULE(Type.SINGLE_REFERENCE), MODULE_NAMES(Type.MULTIPLE_REFERENCE), MODULE_STMTS(Type.MULTIPLE_REFERENCE), NAME(Type.SINGLE_REFERENCE), OPEN(Type.SINGLE_ATTRIBUTE), OPERATOR(Type.SINGLE_ATTRIBUTE), ORIGIN(Type.SINGLE_ATTRIBUTE), PACKAGE_DECLARATION(Type.SINGLE_REFERENCE), PAIRS(Type.MULTIPLE_REFERENCE), PARAMETER(Type.SINGLE_REFERENCE), PARAMETERS(Type.MULTIPLE_REFERENCE), QUALIFIER(Type.SINGLE_REFERENCE), RECEIVER_PARAMETER(Type.SINGLE_REFERENCE), RESOURCES(Type.MULTIPLE_REFERENCE), RIGHT(Type.SINGLE_REFERENCE), SCOPE(Type.SINGLE_REFERENCE), SELECTOR(Type.SINGLE_REFERENCE), STATEMENT(Type.SINGLE_REFERENCE), STATEMENTS(Type.MULTIPLE_REFERENCE), STATIC(Type.SINGLE_ATTRIBUTE), SUPER_TYPE(Type.SINGLE_REFERENCE), TARGET(Type.SINGLE_REFERENCE), THEN_EXPR(Type.SINGLE_REFERENCE), THEN_STMT(Type.SINGLE_REFERENCE), THIS(Type.SINGLE_ATTRIBUTE), THROWN_EXCEPTIONS(Type.MULTIPLE_REFERENCE), TRY_BLOCK(Type.SINGLE_REFERENCE), TYPE(Type.SINGLE_REFERENCE), TYPES(Type.MULTIPLE_REFERENCE), TYPE_ARGUMENTS(Type.MULTIPLE_REFERENCE), TYPE_BOUND(Type.MULTIPLE_REFERENCE), TYPE_PARAMETERS(Type.MULTIPLE_REFERENCE), UPDATE(Type.MULTIPLE_REFERENCE), VALUE(Type.SINGLE_REFERENCE), VALUES(Type.MULTIPLE_REFERENCE), VARIABLE(Type.SINGLE_REFERENCE), VARIABLES(Type.MULTIPLE_REFERENCE), VAR_ARGS(Type.SINGLE_ATTRIBUTE), VAR_ARGS_ANNOTATIONS(Type.MULTIPLE_REFERENCE), WITH_TYPES(Type.MULTIPLE_REFERENCE), ELSE_BLOCK(Type.SINGLE_ATTRIBUTE, true), EXPRESSION_BODY(Type.SINGLE_REFERENCE, true), MAXIMUM_COMMON_TYPE(Type.SINGLE_REFERENCE, true), POSTFIX(Type.SINGLE_ATTRIBUTE, true), PREFIX(Type.SINGLE_ATTRIBUTE, true), THEN_BLOCK(Type.SINGLE_ATTRIBUTE, true), USING_DIAMOND_OPERATOR(Type.SINGLE_ATTRIBUTE, true), CASCADING_IF_STMT(Type.SINGLE_ATTRIBUTE, true), RANGE, COMMENTED_NODE;

enum Type {

Expand Down
Expand Up @@ -178,7 +178,7 @@ public boolean hasElseBlock() {
}

@DerivedProperty
public boolean hasCascadingIf() {
public boolean hasCascadingIfStmt() {
return elseStmt instanceof IfStmt;
}

Expand Down
Expand Up @@ -38,7 +38,7 @@
*/
public interface GenericVisitor<R, A> {

//- Compilation Unit ----------------------------------
// - Compilation Unit ----------------------------------
@Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator")
R visit(CompilationUnit n, A arg);

Expand All @@ -54,7 +54,7 @@ public interface GenericVisitor<R, A> {
@Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator")
R visit(BlockComment n, A arg);

//- Body ----------------------------------------------
// - Body ----------------------------------------------
@Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator")
R visit(ClassOrInterfaceDeclaration n, A arg);

Expand Down Expand Up @@ -91,7 +91,7 @@ public interface GenericVisitor<R, A> {
@Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator")
R visit(JavadocComment n, A arg);

//- Type ----------------------------------------------
// - Type ----------------------------------------------
@Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator")
R visit(ClassOrInterfaceType n, A arg);

Expand Down Expand Up @@ -119,7 +119,7 @@ public interface GenericVisitor<R, A> {
@Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator")
R visit(UnknownType n, A arg);

//- Expression ----------------------------------------
// - Expression ----------------------------------------
@Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator")
R visit(ArrayAccessExpr n, A arg);

Expand Down Expand Up @@ -207,7 +207,7 @@ public interface GenericVisitor<R, A> {
@Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator")
R visit(MemberValuePair n, A arg);

//- Statements ----------------------------------------
// - Statements ----------------------------------------
@Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator")
R visit(ExplicitConstructorInvocationStmt n, A arg);

Expand Down
Expand Up @@ -14,7 +14,11 @@ public class IfStmtMetaModel extends StatementMetaModel {

public PropertyMetaModel thenStmtPropertyMetaModel;

public PropertyMetaModel cascadingIfPropertyMetaModel;

public PropertyMetaModel elseBlockPropertyMetaModel;

public PropertyMetaModel elseBranchPropertyMetaModel;

public PropertyMetaModel thenBlockPropertyMetaModel;
}
Expand Up @@ -663,8 +663,12 @@ private static void initializePropertyMetaModels() {
ifStmtMetaModel.getDeclaredPropertyMetaModels().add(ifStmtMetaModel.elseStmtPropertyMetaModel);
ifStmtMetaModel.thenStmtPropertyMetaModel = new PropertyMetaModel(ifStmtMetaModel, "thenStmt", com.github.javaparser.ast.stmt.Statement.class, Optional.of(statementMetaModel), false, false, false, false, false);
ifStmtMetaModel.getDeclaredPropertyMetaModels().add(ifStmtMetaModel.thenStmtPropertyMetaModel);
ifStmtMetaModel.cascadingIfPropertyMetaModel = new PropertyMetaModel(ifStmtMetaModel, "cascadingIf", boolean.class, Optional.empty(), false, false, false, false, false);
ifStmtMetaModel.getDerivedPropertyMetaModels().add(ifStmtMetaModel.cascadingIfPropertyMetaModel);
ifStmtMetaModel.elseBlockPropertyMetaModel = new PropertyMetaModel(ifStmtMetaModel, "elseBlock", boolean.class, Optional.empty(), false, false, false, false, false);
ifStmtMetaModel.getDerivedPropertyMetaModels().add(ifStmtMetaModel.elseBlockPropertyMetaModel);
ifStmtMetaModel.elseBranchPropertyMetaModel = new PropertyMetaModel(ifStmtMetaModel, "elseBranch", boolean.class, Optional.empty(), false, false, false, false, false);
ifStmtMetaModel.getDerivedPropertyMetaModels().add(ifStmtMetaModel.elseBranchPropertyMetaModel);
ifStmtMetaModel.thenBlockPropertyMetaModel = new PropertyMetaModel(ifStmtMetaModel, "thenBlock", boolean.class, Optional.empty(), false, false, false, false, false);
ifStmtMetaModel.getDerivedPropertyMetaModels().add(ifStmtMetaModel.thenBlockPropertyMetaModel);
labeledStmtMetaModel.labelPropertyMetaModel = new PropertyMetaModel(labeledStmtMetaModel, "label", com.github.javaparser.ast.expr.SimpleName.class, Optional.of(simpleNameMetaModel), false, false, false, false, false);
Expand Down
Expand Up @@ -649,7 +649,7 @@ private static CsmElement typeArguments() {
unindent())),
conditional(ObservableProperty.ELSE_STMT, IS_PRESENT,
sequence(token(GeneratedJavaParserConstants.ELSE),
conditional(ObservableProperty.ELSE_BLOCK, CsmConditional.Condition.FLAG,
conditional(Arrays.asList(ObservableProperty.ELSE_BLOCK, ObservableProperty.CASCADING_IF_STMT), CsmConditional.Condition.FLAG,
sequence(space(), child(ObservableProperty.ELSE_STMT)),
sequence(newline(), CsmElement.indent(), child(ObservableProperty.ELSE_STMT), unindent()))))
));
Expand Down
Expand Up @@ -26,9 +26,12 @@
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.printer.SourcePrinter;

import java.util.Arrays;
import java.util.List;

public class CsmConditional implements CsmElement {
private final Condition condition;
private final ObservableProperty property;
private final List<ObservableProperty> properties;
private final CsmElement thenElement;
private final CsmElement elseElement;

Expand All @@ -37,7 +40,10 @@ public Condition getCondition() {
}

public ObservableProperty getProperty() {
return property;
if (properties.size() > 1) {
throw new IllegalStateException();
}
return properties.get(0);
}

public CsmElement getThenElement() {
Expand Down Expand Up @@ -74,7 +80,17 @@ boolean evaluate(Node node, ObservableProperty property){
}

public CsmConditional(ObservableProperty property, Condition condition, CsmElement thenElement, CsmElement elseElement) {
this.property = property;
this.properties = Arrays.asList(property);
this.condition = condition;
this.thenElement = thenElement;
this.elseElement = elseElement;
}

public CsmConditional(List<ObservableProperty> properties, Condition condition, CsmElement thenElement, CsmElement elseElement) {
if (properties.size() < 1) {
throw new IllegalArgumentException();
}
this.properties = properties;
this.condition = condition;
this.thenElement = thenElement;
this.elseElement = elseElement;
Expand All @@ -86,7 +102,10 @@ public CsmConditional(ObservableProperty property, Condition condition, CsmEleme

@Override
public void prettyPrint(Node node, SourcePrinter printer) {
boolean test = condition.evaluate(node, property);
boolean test = false;
for (ObservableProperty prop : properties) {
test = test || condition.evaluate(node, prop);
}
if (test) {
thenElement.prettyPrint(node, printer);
} else {
Expand Down
Expand Up @@ -27,6 +27,7 @@
import com.github.javaparser.printer.SourcePrinter;

import java.util.Arrays;
import java.util.List;

import static com.github.javaparser.TokenTypes.*;
import static com.github.javaparser.utils.Utils.EOL;
Expand Down Expand Up @@ -79,6 +80,10 @@ static CsmElement conditional(ObservableProperty property, CsmConditional.Condit
return new CsmConditional(property, condition, thenElement, elseElement);
}

static CsmElement conditional(List<ObservableProperty> properties, CsmConditional.Condition condition, CsmElement thenElement, CsmElement elseElement) {
return new CsmConditional(properties, condition, thenElement, elseElement);
}

static CsmElement space() {
return new CsmToken(spaceTokenKind(), " ");
}
Expand Down
Expand Up @@ -15,23 +15,23 @@ public void issue1247withElseSingleStmt() {
IfStmt ifStmt = (IfStmt) JavaParser.parseStatement("if (cond) doSomething(); else doSomethingElse();");
assertEquals(false, ifStmt.hasElseBlock());
assertEquals(true, ifStmt.hasElseBranch());
assertEquals(false, ifStmt.hasCascadingIf());
assertEquals(false, ifStmt.hasCascadingIfStmt());
}

@Test
public void issue1247withElseBlockStmt() {
IfStmt ifStmt = (IfStmt) JavaParser.parseStatement("if (cond) doSomething(); else { doSomethingElse(); }");
assertEquals(true, ifStmt.hasElseBlock());
assertEquals(true, ifStmt.hasElseBranch());
assertEquals(false, ifStmt.hasCascadingIf());
assertEquals(false, ifStmt.hasCascadingIfStmt());
}

@Test
public void issue1247withElseSingleStmtWhichIsAnIf() {
IfStmt ifStmt = (IfStmt) JavaParser.parseStatement("if (cond1) doSomething(); else if (cond2) doSomethingElse();");
assertEquals(false, ifStmt.hasElseBlock());
assertEquals(true, ifStmt.hasElseBranch());
assertEquals(true, ifStmt.hasCascadingIf());
assertEquals(true, ifStmt.hasCascadingIfStmt());
}

}

0 comments on commit 2990332

Please sign in to comment.