From 247be3b3ee4c9e4eb23ebc37a801c074b0671e51 Mon Sep 17 00:00:00 2001 From: sotty Date: Fri, 13 Jun 2014 01:16:33 +0100 Subject: [PATCH] Java Statement Fluent Model Update Copyright Conflicts: api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java impl/src/main/java/org/jboss/forge/roaster/model/impl/AnnotationImpl.java impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java Conflicts: impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java --- .../org/jboss/forge/roaster/model/Block.java | 18 + .../forge/roaster/model/ExpressionHolder.java | 12 + .../jboss/forge/roaster/model/JavaClass.java | 2 + .../model/expressions/AccessBuilder.java | 87 ++ .../roaster/model/expressions/Accessor.java | 25 + .../roaster/model/expressions/Argument.java | 25 + .../model/expressions/ArgumentHolder.java | 38 + .../ArrayConstructorExpression.java | 57 + .../model/expressions/ArrayIndexer.java | 37 + .../roaster/model/expressions/ArrayInit.java | 59 + .../model/expressions/AssignExpression.java | 63 + .../roaster/model/expressions/Assignment.java | 52 + .../expressions/BasicExpressionFactory.java | 164 +++ .../model/expressions/BooleanLiteral.java | 29 + .../model/expressions/CastExpression.java | 37 + .../model/expressions/CharacterLiteral.java | 29 + .../model/expressions/ClassLiteral.java | 31 + .../model/expressions/ConstructorBuilder.java | 50 + .../expressions/ConstructorExpression.java | 26 + .../model/expressions/DeclareExpression.java | 85 ++ .../roaster/model/expressions/Expression.java | 29 + .../model/expressions/ExpressionSource.java | 24 + .../model/expressions/Expressions.java | 523 ++++++++ .../roaster/model/expressions/Field.java | 32 + .../roaster/model/expressions/Getter.java | 30 + .../expressions/InstanceofExpression.java | 35 + .../expressions/InvocationTargetHolder.java | 37 + .../roaster/model/expressions/Literal.java | 23 + .../model/expressions/LiteralBuilder.java | 95 ++ .../expressions/MethodCallExpression.java | 31 + .../expressions/MethodInvokeExpression.java | 33 + .../MethodWithArgumentsInvokeExpression.java | 25 + .../expressions/NonPrimitiveExpression.java | 24 + .../model/expressions/NullLiteral.java | 23 + .../model/expressions/NumberLiteral.java | 29 + .../forge/roaster/model/expressions/Op.java | 57 + .../model/expressions/OperatorExpression.java | 40 + .../model/expressions/OrdinalArgument.java | 40 + .../model/expressions/ParenExpression.java | 31 + .../model/expressions/PostFixExpression.java | 24 + .../roaster/model/expressions/PostfixOp.java | 40 + .../roaster/model/expressions/PrefixOp.java | 44 + .../model/expressions/PrimitiveLiteral.java | 23 + .../roaster/model/expressions/Setter.java | 30 + .../model/expressions/StringLiteral.java | 32 + .../roaster/model/expressions/Super.java | 25 + .../model/expressions/TernaryExpression.java | 66 + .../model/expressions/ThisLiteral.java | 25 + .../model/expressions/UnaryExpression.java | 43 + .../roaster/model/expressions/Variable.java | 28 + .../roaster/model/source/BlockHolder.java | 16 + .../roaster/model/source/BlockSource.java | 34 + .../roaster/model/source/MethodBody.java | 23 + .../roaster/model/source/MethodSource.java | 32 +- .../model/statements/AssertStatement.java | 51 + .../model/statements/AssignmentStatement.java | 68 + .../model/statements/BlockStatement.java | 25 + .../roaster/model/statements/BodyHolder.java | 40 + .../model/statements/BranchingStatement.java | 23 + .../model/statements/BreakStatement.java | 38 + .../model/statements/ContinueStatement.java | 38 + .../statements/ControlFlowStatement.java | 25 + .../model/statements/DeclareStatement.java | 117 ++ .../model/statements/DoWhileStatement.java | 38 + .../statements/EvalExpressionStatement.java | 33 + .../model/statements/ExpressionStatement.java | 33 + .../model/statements/ForEachStatement.java | 67 + .../model/statements/ForStatement.java | 68 + .../roaster/model/statements/IfStatement.java | 80 ++ .../model/statements/InvokeStatement.java | 68 + .../model/statements/LoopingStatement.java | 23 + .../model/statements/ReturnStatement.java | 40 + .../roaster/model/statements/Statement.java | 49 + .../model/statements/StatementSource.java | 24 + .../roaster/model/statements/Statements.java | 269 ++++ .../model/statements/SuperStatement.java | 39 + .../model/statements/SwitchCaseStatement.java | 37 + .../model/statements/SwitchStatement.java | 83 ++ .../model/statements/SynchStatement.java | 39 + .../model/statements/ThisStatement.java | 39 + .../model/statements/ThrowStatement.java | 37 + .../model/statements/TryCatchStatement.java | 72 + .../model/statements/WhileStatement.java | 38 + .../forge/roaster/spi/ExpressionFactory.java | 28 + .../forge/roaster/spi/StatementFactory.java | 165 +++ .../jboss/forge/roaster/model/ASTNode.java | 19 + .../forge/roaster/model/impl/BlockImpl.java | 61 + .../forge/roaster/model/impl/JDTHelper.java | 79 ++ .../roaster/model/impl/JavaClassImpl.java | 6 + .../forge/roaster/model/impl/MethodImpl.java | 91 +- .../forge/roaster/model/impl/NodeImpl.java | 78 ++ .../roaster/model/impl/ParameterImpl.java | 1 + .../expressions/AbstractMethodInvokeImpl.java | 66 + .../model/impl/expressions/AccessorImpl.java | 64 + .../impl/expressions/ArrayAccessImpl.java | 96 ++ .../model/impl/expressions/ArrayImpl.java | 119 ++ .../model/impl/expressions/ArrayInitImpl.java | 110 ++ .../model/impl/expressions/AssignImpl.java | 106 ++ .../impl/expressions/BaseInvokeableImpl.java | 35 + .../BasicExpressionFactoryImpl.java | 290 ++++ .../impl/expressions/BooleanLiteralImpl.java | 59 + .../model/impl/expressions/CastImpl.java | 85 ++ .../expressions/CharacterLiteralImpl.java | 60 + .../impl/expressions/ClassLiteralImpl.java | 61 + .../impl/expressions/ConstructorImpl.java | 94 ++ .../expressions/DeclareExpressionImpl.java | 154 +++ .../impl/expressions/DotAccessorImpl.java | 132 ++ .../impl/expressions/ExpressionBuilder.java | 310 +++++ .../impl/expressions/ExpressionChainLink.java | 24 + .../impl/expressions/ExpressionImpl.java | 39 + .../model/impl/expressions/FieldImpl.java | 92 ++ .../model/impl/expressions/GetterImpl.java | 83 ++ .../impl/expressions/InstanceofImpl.java | 74 + .../impl/expressions/InvokeableImpl.java | 39 + .../expressions/JdtExpressionWrapper.java | 25 + .../model/impl/expressions/LiteralImpl.java | 27 + .../impl/expressions/MethodInvokeImpl.java | 72 + .../NonPrimitiveExpressionImpl.java | 49 + .../model/impl/expressions/NotImpl.java | 27 + .../impl/expressions/NullLiteralImpl.java | 55 + .../impl/expressions/NumberLiteralImpl.java | 59 + .../model/impl/expressions/OperatorImpl.java | 125 ++ .../model/impl/expressions/ParenImpl.java | 76 ++ .../model/impl/expressions/PostFixImpl.java | 85 ++ .../impl/expressions/SelfArgumentImpl.java | 59 + .../model/impl/expressions/SetterImpl.java | 80 ++ .../impl/expressions/SimpleAccessorImpl.java | 36 + .../expressions/StaticClassAccessorImpl.java | 61 + .../impl/expressions/StringLiteralImpl.java | 59 + .../impl/expressions/SuperFieldImpl.java | 84 ++ .../impl/expressions/SuperGetterImpl.java | 86 ++ .../model/impl/expressions/SuperImpl.java | 62 + .../expressions/SuperMethodInvokeImpl.java | 67 + .../impl/expressions/SuperSetterImpl.java | 72 + .../model/impl/expressions/TernaryImpl.java | 108 ++ .../model/impl/expressions/UnaryImpl.java | 83 ++ .../impl/expressions/VarArgumentImpl.java | 73 + .../impl/statements/AssertStatementImpl.java | 89 ++ .../impl/statements/AssignStatementImpl.java | 109 ++ .../impl/statements/BlockStatementImpl.java | 36 + .../impl/statements/BodiedStatementImpl.java | 66 + .../impl/statements/BreakStatementImpl.java | 69 + .../statements/ContinueStatementImpl.java | 69 + .../impl/statements/DeclareStatementImpl.java | 176 +++ .../impl/statements/DoWhileStatementImpl.java | 102 ++ .../statements/ExpressionStatementImpl.java | 75 ++ .../impl/statements/ForEachStatementImpl.java | 135 ++ .../impl/statements/ForStatementImpl.java | 153 +++ .../impl/statements/IfStatementImpl.java | 146 ++ .../model/impl/statements/InnerBlockImpl.java | 124 ++ .../impl/statements/InvokeStatementImpl.java | 129 ++ .../impl/statements/JdtStatementWrapper.java | 26 + .../impl/statements/ReturnStatementImpl.java | 71 + .../impl/statements/StatementBuilder.java | 288 ++++ .../impl/statements/StatementFactoryImpl.java | 162 +++ .../model/impl/statements/StatementImpl.java | 53 + .../impl/statements/SuperStatementImpl.java | 81 ++ .../statements/SwitchCaseStatementImpl.java | 129 ++ .../impl/statements/SwitchStatementImpl.java | 171 +++ .../impl/statements/SynchStatementImpl.java | 102 ++ .../impl/statements/ThisStatementImpl.java | 81 ++ .../impl/statements/ThrowStatementImpl.java | 71 + .../statements/TryCatchStatementImpl.java | 163 +++ .../impl/statements/WhileStatementImpl.java | 101 ++ .../roaster/spi/ExpressionFactoryImpl.java | 71 + ....jboss.forge.roaster.spi.ExpressionFactory | 1 + ...g.jboss.forge.roaster.spi.StatementFactory | 1 + .../statements/ConstructorStatementsTest.java | 43 + .../statements/ControlFlowStatementsTest.java | 192 +++ .../statements/DeclareStatementsTest.java | 112 ++ .../model/statements/ForStatementsTest.java | 74 + .../model/statements/IfStatementsTest.java | 102 ++ .../statements/InvokeStatementsTest.java | 159 +++ .../statements/ModifyMethodBodyTest.java | 68 + .../statements/RebuildStatementTest.java | 1194 +++++++++++++++++ .../statements/ReturnStatementsTest.java | 272 ++++ .../roaster/model/statements/SuperTest.java | 66 + .../model/statements/WhileStatementsTest.java | 79 ++ 178 files changed, 13812 insertions(+), 22 deletions(-) create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/Block.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/ExpressionHolder.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/AccessBuilder.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/Accessor.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/Argument.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/ArgumentHolder.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayConstructorExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayIndexer.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayInit.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/AssignExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/Assignment.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/BasicExpressionFactory.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/BooleanLiteral.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/CastExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/CharacterLiteral.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/ClassLiteral.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorBuilder.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/DeclareExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/Expression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/ExpressionSource.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/Expressions.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/Field.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/Getter.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/InstanceofExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/InvocationTargetHolder.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/Literal.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/LiteralBuilder.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodCallExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodInvokeExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodWithArgumentsInvokeExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/NonPrimitiveExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/NullLiteral.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/NumberLiteral.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/Op.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/OperatorExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/OrdinalArgument.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/ParenExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/PostFixExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/PostfixOp.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/PrefixOp.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/PrimitiveLiteral.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/Setter.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/StringLiteral.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/Super.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/TernaryExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/ThisLiteral.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/UnaryExpression.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/expressions/Variable.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/source/BlockHolder.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/source/BlockSource.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/source/MethodBody.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/AssertStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/AssignmentStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/BlockStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/BodyHolder.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/BranchingStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/BreakStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/ContinueStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/ControlFlowStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/DeclareStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/DoWhileStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/EvalExpressionStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/ExpressionStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/ForEachStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/ForStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/IfStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/InvokeStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/LoopingStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/ReturnStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/Statement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/StatementSource.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/Statements.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/SuperStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchCaseStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/SynchStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/ThisStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/ThrowStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/TryCatchStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/model/statements/WhileStatement.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactory.java create mode 100644 api/src/main/java/org/jboss/forge/roaster/spi/StatementFactory.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/ASTNode.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/BlockImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/NodeImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AbstractMethodInvokeImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AccessorImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayAccessImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayInitImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AssignImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BaseInvokeableImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BasicExpressionFactoryImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BooleanLiteralImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CastImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CharacterLiteralImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ClassLiteralImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ConstructorImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DeclareExpressionImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DotAccessorImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionBuilder.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionChainLink.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/FieldImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/GetterImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InstanceofImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InvokeableImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/JdtExpressionWrapper.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/LiteralImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/MethodInvokeImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NonPrimitiveExpressionImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NotImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NullLiteralImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NumberLiteralImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/OperatorImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ParenImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/PostFixImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SelfArgumentImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SetterImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SimpleAccessorImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/StaticClassAccessorImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/StringLiteralImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperFieldImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperGetterImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperMethodInvokeImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperSetterImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/TernaryImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/UnaryImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/VarArgumentImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssertStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssignStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BlockStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BodiedStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BreakStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ContinueStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DeclareStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DoWhileStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ExpressionStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ForEachStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ForStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/IfStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InnerBlockImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InvokeStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/JdtStatementWrapper.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ReturnStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementBuilder.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementFactoryImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SuperStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchCaseStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SynchStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThisStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThrowStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/TryCatchStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/WhileStatementImpl.java create mode 100644 impl/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactoryImpl.java create mode 100644 impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.ExpressionFactory create mode 100644 impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.StatementFactory create mode 100644 impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ConstructorStatementsTest.java create mode 100644 impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ControlFlowStatementsTest.java create mode 100644 impl/src/test/java/org/jboss/forge/test/roaster/model/statements/DeclareStatementsTest.java create mode 100644 impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ForStatementsTest.java create mode 100644 impl/src/test/java/org/jboss/forge/test/roaster/model/statements/IfStatementsTest.java create mode 100644 impl/src/test/java/org/jboss/forge/test/roaster/model/statements/InvokeStatementsTest.java create mode 100644 impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ModifyMethodBodyTest.java create mode 100644 impl/src/test/java/org/jboss/forge/test/roaster/model/statements/RebuildStatementTest.java create mode 100644 impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ReturnStatementsTest.java create mode 100644 impl/src/test/java/org/jboss/forge/test/roaster/model/statements/SuperTest.java create mode 100644 impl/src/test/java/org/jboss/forge/test/roaster/model/statements/WhileStatementsTest.java diff --git a/api/src/main/java/org/jboss/forge/roaster/model/Block.java b/api/src/main/java/org/jboss/forge/roaster/model/Block.java new file mode 100644 index 00000000..ec117e3c --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/Block.java @@ -0,0 +1,18 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model; + +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.source.BlockHolder; + +public interface Block, + P extends BlockHolder> + extends Origin

, + BlockHolder { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/ExpressionHolder.java b/api/src/main/java/org/jboss/forge/roaster/model/ExpressionHolder.java new file mode 100644 index 00000000..a6886272 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/ExpressionHolder.java @@ -0,0 +1,12 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model; + +public interface ExpressionHolder> { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/JavaClass.java b/api/src/main/java/org/jboss/forge/roaster/model/JavaClass.java index cc7df803..897c22c8 100644 --- a/api/src/main/java/org/jboss/forge/roaster/model/JavaClass.java +++ b/api/src/main/java/org/jboss/forge/roaster/model/JavaClass.java @@ -8,6 +8,7 @@ package org.jboss.forge.roaster.model; import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.source.JavaClassSource; /** * Represents a Java {@code class} type. See {@link Roaster} for various options in generating {@link JavaClass} @@ -33,4 +34,5 @@ public interface JavaClass> extends */ public boolean isLocalClass(); + public JavaClassSource asJavaClassSource(); } \ No newline at end of file diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/AccessBuilder.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/AccessBuilder.java new file mode 100644 index 00000000..ffc40f18 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/AccessBuilder.java @@ -0,0 +1,87 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract factory interface that supports the construction of accessor expressions, in source format + * The expression returns an object with accessible fields, accessors and methods + * + * @author Davide Sottara + */ +public interface AccessBuilder, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> +{ + + /** + * Returns a field access expression + * Can be invoked on a parent expression + * @param field The name of the field + * @return a field accessor + */ + public Field field(String field); + + /** + * Returns a getter expression + * Can be invoked on a parent expression + * @param field The name of the field + * @param klass The name of the type of the field + * @return a getter method invocation expression + */ + public Getter getter(String field, String klass); + + /** + * Returns a getter expression + * Can be invoked on a parent expression + * @param field The name of the field + * @param klass The type of the field + * @return a getter method invocation expression + */ + public Getter getter(String field, Class klass); + + /** + * Returns a setter expression + * Can be invoked on a parent expression + * @param field The name of the field + * @param klass The name of the type of the field + * @param value The expression returning the value to be set + * @return a setter method invocation expression + */ + public Setter setter(String field, String klass, ExpressionSource value); + + /** + * Returns a setter expression + * Can be invoked on a parent expression + * @param field The name of the field + * @param klass The type of the field + * @param value The expression returning the value to be set + * @return a setter method invocation expression + */ + public Setter setter(String field, Class klass, ExpressionSource value); + + /** + * Returns a method invocation expression + * Can be invoked on a parent expression + * @param method The name of the method + * @return a method invocation expression + */ + public MethodCallExpression invoke(String method); + + /** + * Returns an array indexing expression + * Can be invoked on a parent expression + * @param index The expression returning the index used for accessing the array + * @return an array indexing expression + */ + public ArrayIndexer itemAt(ExpressionSource index); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Accessor.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Accessor.java new file mode 100644 index 00000000..e3787f03 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Accessor.java @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract interface that represents accessors (getters, fields, etc..) in source format + * + * @author Davide Sottara + */ +public interface Accessor, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends Argument, + AccessBuilder +{ + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Argument.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Argument.java new file mode 100644 index 00000000..0cc3ba92 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Argument.java @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract marker interface that represents operation or method arguments in source format + * + * @author Davide Sottara + */ +public interface Argument, + P extends ExpressionHolder, + E extends ExpressionSource> + extends ExpressionSource +{ + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArgumentHolder.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArgumentHolder.java new file mode 100644 index 00000000..437b3497 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArgumentHolder.java @@ -0,0 +1,38 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +/** + * Abstract marker interface that represents expressions requiring arguments + * + * @author Davide Sottara + */ +public interface ArgumentHolder, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends ExpressionHolder +{ + + /** + * Adds an argument to the expression + * @param arg The argument to be added + * @return The expression itself + */ + public E addArgument(Argument arg); + + /** + * Returns the current list of arguments + * @return An immutable list containing the arguments + */ + public List> getArguments(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayConstructorExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayConstructorExpression.java new file mode 100644 index 00000000..ac8ca45c --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayConstructorExpression.java @@ -0,0 +1,57 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +/** + * Represent an array constructor expression in source format + * + * @author Davide Sottara + */ +public interface ArrayConstructorExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression> +{ + + /** + * Adds a dimension to the array, allocating dim slots + * @param dim the number of elements in the new array dimension + * @return The ArrayConstructorExpression/code> itself + */ + public ArrayConstructorExpression addDimension(ExpressionSource dim); + + /** + * Initializes the array using an ArrayInit expressoin + * @param array the initial value for the array variable + * @return The ArrayConstructorExpression/code> itself + */ + public ArrayConstructorExpression init(ArrayInit array); + + /** + * Returns the array initialization expression, or null if none has been set + * @return An ArrayInit expression + */ + public ArrayInit> getInit(); + + /** + * Returns the expressions defining the number of slots for each array dimension + * @return An immutable list containing the expressions initializing each dimension + */ + public List,?>> getDimensions(); + + /** + * Returns the number of dimensions of the array being constructed + * @return the number of dimensions for this array + */ + public int getDimension(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayIndexer.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayIndexer.java new file mode 100644 index 00000000..dc53456d --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayIndexer.java @@ -0,0 +1,37 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent an array indexing expression in source format + * + * @author Davide Sottara + */ +public interface ArrayIndexer, P extends ExpressionHolder> + extends OrdinalArgument>, + NonPrimitiveExpression>, + InvocationTargetHolder> +{ + + /** + * Returns the expression returning the index to be accessed + * @return The expression returning the index used by this ArrayIndexer + */ + public ExpressionSource,?> getIndex(); + + /** + * Sets the expression returning the index to be used for accessing the array + * @param index An expression returning an integer, used to access the array + * @return The ArrayIndexer itself + */ + public ArrayIndexer setIndex(ExpressionSource index); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayInit.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayInit.java new file mode 100644 index 00000000..9182ba41 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayInit.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +/** + * Represent an array constructor expression in source format + * + * @author Davide Sottara + */ +public interface ArrayInit, P extends ExpressionHolder> + extends ExpressionSource>, + NonPrimitiveExpression> +{ + + /** + * Adds a sub-array literal to this array constructing expression + * @param subRow The sub-array + * @return The ArrayInit itself + */ + public ArrayInit addElement(ArrayInit subRow); + + /** + * Adds an element to this array constructing expression + * @param subElement The expression returning the element to be addeed to the array + * @return The ArrayInit itself + */ + public ArrayInit addElement(ExpressionSource subElement); + + /** + * Returns the current elements used to initialize the array + * @return An immutable list containing the element expressions + */ + public List,?>> getElements(); + + /** + * Counts and returns the number of elements in this array + * @return + */ + public int size(); + + /** + * Returns the number of dimensions in the array, as inferred by the init expressions + * Example : { {1}, {2}, {3} } size() is 3, but getDimension() is 2 + * @return the dimension + */ + public int getDimension(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/AssignExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/AssignExpression.java new file mode 100644 index 00000000..cc44330a --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/AssignExpression.java @@ -0,0 +1,63 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent an assignment expression in source format + * + * @author Davide Sottara + */ +public interface AssignExpression, + P extends ExpressionHolder> + extends NonPrimitiveExpression>, + Argument> +{ + + /** + * Returns the assignment operator + * @return A string representing the assignment operator used in this expression + * @see org.jboss.forge.roaster.model.expressions.Assignment + */ + public String getOperator(); + + /** + * Sets the assignment operator + * @param op The assignment operator + * @return This AssignExpression expression + */ + public AssignExpression setOperator(Assignment op); + + /** + * Returns the left accessor expression + * @return The LHS of the assignment + */ + public Accessor,?> getLeft(); + + /** + * Sets the left expression returning the variable to be assigned + * @param left The LHS of the assignment + * @return This AssignExpression expression + */ + public AssignExpression setLeft(Accessor left); + + /** + * Returns the right value expression + * @return The RHS of the assignment + */ + public ExpressionSource,?> getRight(); + + /** + * Sets the right expression, whose value will be assigned + * @param right The RHS of the assignment + * @return This AssignExpression expression + */ + public AssignExpression setRight(ExpressionSource right); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Assignment.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Assignment.java new file mode 100644 index 00000000..aed79729 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Assignment.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +public enum Assignment +{ + + ASSIGN("="), + DIVIDE_ASSIGN("/="), + PLUS_ASSIGN("+="), + MINUS_ASSIGN("-="), + TIMES_ASSIGN("*="), + REMAINDER_ASSIGN("%="), + + LEFT_SHIFT_ASSIGN("<<="), + RIGHT_SHIFT_ASSIGN(">>="), + RIGHT_SHIFT_UNSIGNED_ASSIGN(">>>="), + BITWISE_XOR_ASSIGN("^="), + BITWISE_AND_ASSIGN("&="), + BITWISE_OR_ASSIGN("|="); + + private String op; + + Assignment(String o) + { + op = o; + } + + public String getOp() + { + return op; + } + + + public static Assignment build(String x) + { + for (Assignment en : Assignment.values()) + { + if (en.getOp().equals(x)) + { + return en; + } + } + throw new IllegalStateException("Unrecognized operation " + x); + } + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/BasicExpressionFactory.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/BasicExpressionFactory.java new file mode 100644 index 00000000..46a1b36a --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/BasicExpressionFactory.java @@ -0,0 +1,164 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract factory interface that supports the construction of basic, simple expressions, in source format + * + * @author Davide Sottara + */ +public interface BasicExpressionFactory, + P extends ExpressionHolder> + extends LiteralBuilder, + ConstructorBuilder +{ + + /** + * Creates a new variable dereferencing expression + * @param varName the name of the variable + * @return a new Variable + */ + public Variable var(String varName); + + /** + * Creates a new infix operator expression with the given op + * @param op the infix operator + * @return a new OperatorExpression with no operands + */ + public OperatorExpression operator(Op op); + + /** + * Creates a new prefix operator expression with the given op + * @param operator the prefix operator + * @return a new UnaryExpression with no operand + */ + public UnaryExpression operator(PrefixOp operator); + + /** + * Creates a new ternary expression + * @return a new, empty TernaryExpression + */ + public TernaryExpression ternary(); + + /** + * Creates a cast expression + * @param klass the name of the type to cast to + * @param arg the expression being cast + * @return a new CastExpression + */ + public CastExpression cast(String klass, ExpressionSource arg); + + /** + * Creates a cast expression + * @param klass the type to cast to + * @param arg the expression being cast + * @return a new CastExpression + */ + public CastExpression cast(Class klass, ExpressionSource arg); + + /** + * Creates a new parenthesized expression wrapping the given expression + * @param inner the inner expression + * @return a new ParenExpression + */ + public ParenExpression paren(ExpressionSource inner); + + /** + * Creates a specialized unary expression, using the operator not (!) + * @param arg the operand expression + * @return a new UnaryExpression + */ + public UnaryExpression not(ExpressionSource arg); + + /** + * Creates a new assignment expression with the given operator + * @param operator the assignment operator + * @return a new, empty AssignExpression + */ + public AssignExpression assign(Assignment operator); + + /** + * Creates a new variable declaration expression + * @param klass the name of the type of the variable + * @param name the name of the variable + * @return a new, uninitialized DeclareExpression + */ + public DeclareExpression declare(String klass, String name); + + /** + * Creates a new variable declaration expression + * @param klass the type of the variable + * @param name the name of the variable + * @return a new, uninitialized DeclareExpression + */ + public DeclareExpression declare(Class klass, String name); + + /** + * Creates a new variable declaration expression, initializing it + * @param klass the name of the type of the variable + * @param name the name of the variable + * @param init the initialization expression + * @return a new DeclareExpression + */ + public DeclareExpression declare(String klass, String name, ExpressionSource init); + + /** + * Creates a new variable declaration expression, initializing it + * @param klass the type of the variable + * @param name the name of the variable + * @param init the initialization expression + * @return a new DeclareExpression + */ + public DeclareExpression declare(Class klass, String name, ExpressionSource init); + + /** + * Creates a new accessor giving access to the static members of a class + * @param klass the name of the class + * @return a new Accessor targeting the given class + */ + public Accessor classStatic(String klass); + + /** + * Creates a new accessor giving access to the static members of a class + * @param klass the class + * @return a new Accessor targeting the given class + */ + public Accessor classStatic(Class klass); + + /** + * Creates a new instanceof expression + * @param klass the name of the type to check for membership + * @param arg the expression returning the value to check for class membership + * @return a new InstanceofExpression + */ + public InstanceofExpression instanceOf(String klass, ExpressionSource arg); + + /** + * Creates a new instanceof expression + * @param klass the type to check for membership + * @param arg the expression returning the value to check for class membership + * @return a new InstanceofExpression + */ + public InstanceofExpression instanceOf(Class klass, ExpressionSource arg); + + /** + * Creates a new super accessor expression + * @return a new Super accessor + */ + public Super sup(); + + /** + * Creates a new array initialization expression + * @return a new ArrayInit + */ + public ArrayInit> vec(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/BooleanLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/BooleanLiteral.java new file mode 100644 index 00000000..77ea134b --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/BooleanLiteral.java @@ -0,0 +1,29 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a boolean literal expression in source format + * + * @author Davide Sottara + */ +public interface BooleanLiteral, + P extends ExpressionHolder> + extends PrimitiveLiteral +{ + + /** + * Returns the value represented by this literal expression + * @return the literal value of this expression + */ + public Boolean getValue(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/CastExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/CastExpression.java new file mode 100644 index 00000000..6821c9a3 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/CastExpression.java @@ -0,0 +1,37 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a cast expression in source format + * + * @author Davide Sottara + */ +public interface CastExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression> +{ + + /** + * Returns the type of the cast + * @return the name of the type the expression is being cast to + */ + public String getType(); + + /** + * Returns the expression that is being cast + * @return the expression being cast + */ + public ExpressionSource,?> getExpression(); + + public CastExpression setExpression( ExpressionSource expr ); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/CharacterLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/CharacterLiteral.java new file mode 100644 index 00000000..aff32411 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/CharacterLiteral.java @@ -0,0 +1,29 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a character literal expression in source format + * + * @author Davide Sottara + */ +public interface CharacterLiteral, + P extends ExpressionHolder> + extends PrimitiveLiteral +{ + + /** + * Returns the value represented by this literal expression + * @return the literal value of this expression + */ + public Character getValue(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ClassLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ClassLiteral.java new file mode 100644 index 00000000..6006cfe5 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ClassLiteral.java @@ -0,0 +1,31 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a class literal expression in source format + * + * @author Davide Sottara + */ +public interface ClassLiteral, + P extends ExpressionHolder> + extends Literal>, + NonPrimitiveExpression>, + Accessor> +{ + + /** + * Returns the name of the class represented by this literal expression + * @return the name of the class + */ + public String getValue(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorBuilder.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorBuilder.java new file mode 100644 index 00000000..66f33bf2 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorBuilder.java @@ -0,0 +1,50 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract factory interface supporting the construction of constructor expressions, in source format + * + * @author Davide Sottara + */ +public interface ConstructorBuilder, + P extends ExpressionHolder> +{ + + /** + * Creates a ConstructorExpression for the given class name + * @param klass the name of the class to instantiate + * @return A ConstructorExpression for class klass + */ + public ConstructorExpression construct(String klass); + + /** + * Creates a ConstructorExpression for the given class + * @param klass the class to instantiate + * @return A ConstructorExpression for class klass + */ + public ConstructorExpression construct(Class klass); + + /** + * Creates an Array Creation expression + * @param klass The type of the array to be created + * @return A ArrayConstructorExpression of the given type + */ + public ArrayConstructorExpression newArray(Class klass); + + /** + * Creates an Array Creation expression + * @param klass The name of the type of the array to be created + * @return A ArrayConstructorExpression of the given type + */ + public ArrayConstructorExpression newArray(String klass); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorExpression.java new file mode 100644 index 00000000..da3685bc --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorExpression.java @@ -0,0 +1,26 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent an object constructor expression in source format + * + * @author Davide Sottara + */ +public interface ConstructorExpression, + P extends ExpressionHolder> + extends Argument>, + Accessor>, + ArgumentHolder>, + NonPrimitiveExpression> +{ + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/DeclareExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/DeclareExpression.java new file mode 100644 index 00000000..c34161b5 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/DeclareExpression.java @@ -0,0 +1,85 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; +import java.util.Map; + +/** + * Represent a variable declaration expression in source format + * + * @author Davide Sottara + */ +public interface DeclareExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression> +{ + + /** + * Adds an additional variable to the declaration and initializes it + * @param name The name of the new variable + * @param expr The expression used to initialize the variable + * @return this DeclareExpression itself + */ + public DeclareExpression addDeclaration(String name, ExpressionSource expr); + + /** + * Adds an additional variable to the declaration + * @param name The name of the new variable + * @return this DeclareExpression itself + */ + public DeclareExpression addDeclaration(String name); + + + /** + * Returns the name of the first variable in this declaration + * @return + */ + public String getVariableName(); + + /** + * Returns the names of all the variables in this declaration + * @return An immutable list containing the variable names + */ + public List getVariableNames(); + + /** + * Returns the name of the type of the variable(s) + * @return The type of this declaration + */ + public String getVariableType(); + + /** + * Returns the initialization expression of the first declared variable (if any) + * Notice that this is different from the first initialization in the declaration (e.g. int a, b = 0;) + * @return The expression used to initialize the first variable in the declaration + */ + public ExpressionSource,?> getInitExpression(); + + /** + * Returns the declared variables, associated to their initialization expression(s), if any + * @return An immutable map associating the declared variable names to their initialization expressions + */ + public Map,?>> getInitExpressions(); + + /** + * Returns true if this declaration contains exactly one variable + * @return true if this declaration contains exactly one variable + */ + public boolean isSingleDeclaration(); + + /** + * Returns the number of declared variables + * @return the number of declared variables + */ + public int getNumVariables(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Expression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Expression.java new file mode 100644 index 00000000..a5e9912e --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Expression.java @@ -0,0 +1,29 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.JavaType; + +/** + * Represent a java expression + * + * @author Davide Sottara + */ +public interface Expression, + P extends ExpressionHolder> + extends Origin

+{ + + /** + * Sets the origin of this expression + * @param origin + */ + public void setOrigin(P origin); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ExpressionSource.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ExpressionSource.java new file mode 100644 index 00000000..74a7e94b --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ExpressionSource.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a java expression in source format + * + * @author Davide Sottara + */ +public interface ExpressionSource, + P extends ExpressionHolder, + E extends ExpressionSource> + extends Expression +{ + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Expressions.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Expressions.java new file mode 100644 index 00000000..15ee7e11 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Expressions.java @@ -0,0 +1,523 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.spi.ExpressionFactory; + +import java.util.ServiceLoader; + +public abstract class Expressions +{ + + protected static ExpressionFactory factory; + + @SuppressWarnings("unchecked") + protected static > ExpressionFactory getFactory() + { + synchronized (Expressions.class) + { + ServiceLoader sl = ServiceLoader.load(ExpressionFactory.class, Expressions.class.getClassLoader()); + if (sl.iterator().hasNext()) + { + factory = sl.iterator().next(); + } else + { + throw new IllegalStateException("No ExpressionFactory implementation available, unable to continue"); + } + } + return factory; + } + + /** + * Creates a String literal expression + * @param val The String value + * @param the Java Source type + * @return A literal expression that evaluates to the given String + */ + @SuppressWarnings("unchecked") + public static > StringLiteral literal(String val) + { + return (StringLiteral) getFactory().literal(val); + } + + /** + * Creates a Number literal expression + * @param val The Number value + * @param the Java Source type + * @return A literal expression that evaluates to the given Number + */ + @SuppressWarnings("unchecked") + public static > NumberLiteral literal(Number val) + { + return (NumberLiteral) getFactory().literal(val); + } + + /** + * Creates a Char literal expression + * @param val The Char value + * @param the Java Source type + * @return A literal expression that evaluates to the given Char + */ + @SuppressWarnings("unchecked") + public static > CharacterLiteral literal(Character val) + { + return (CharacterLiteral) getFactory().literal(val); + } + + /** + * Creates a Boolean literal expression + * @param val The Boolean value + * @param the Java Source type + * @return A literal expression that evaluates to the given Boolean + */ + @SuppressWarnings("unchecked") + public static > BooleanLiteral literal(Boolean val) + { + return (BooleanLiteral) getFactory().literal(val); + } + + /** + * Creates a default literal expression of the given type: + * The default for booleans is false; numbers are mapped to the appropriate literal "0"; + * Passing any Object class (including String) returns a null literal expression + * @param klass The name of the type for which a default expression is required + * @param the Java Source type + * @return A literal expression that evaluates to "0"/null, depending on the klass + */ + @SuppressWarnings("unchecked") + public static > Literal zeroLiteral(String klass) + { + return (Literal) getFactory().zeroLiteral(klass); + } + + /** + * Creates a default literal expression of the given type: + * The default for booleans is false; numbers are mapped to the appropriate literal "0"; + * Passing any Object class (including String) returns a null literal expression + * @param klass The type for which a default expression is required + * @param the Java Source type + * @return A literal expression that evaluates to "0"/null, depending on the klass + */ + @SuppressWarnings("unchecked") + public static > Literal zeroLiteral(Class klass) + { + return (Literal) getFactory().zeroLiteral(klass); + } + + /** + * Creates a literal expression that evaluates to "this" + * @param the Java Source type + * @return A literal expression representing "this" object + */ + @SuppressWarnings("unchecked") + public static > ThisLiteral thisLiteral() + { + return (ThisLiteral) getFactory().thisLiteral(); + } + + /** + * Creates a literal expression that evaluates to null + * @param the Java Source type + * @return A null literal expression + */ + @SuppressWarnings("unchecked") + public static > NullLiteral nullLiteral() + { + return (NullLiteral) getFactory().nullLiteral(); + } + + /** + * Creates a literal expression that evaluates to the class of the given name + * @param klass The name of the class + * @param the Java Source type + * @return A class literal expression + */ + @SuppressWarnings("unchecked") + public static > ClassLiteral classLiteral(String klass) + { + return (ClassLiteral) getFactory().classLiteral(klass); + } + + /** + * Creates a literal expression that evaluates to the given class + * @param klass The name of the class + * @param the Java Source type + * @return A class literal expression + */ + @SuppressWarnings("unchecked") + public static > ClassLiteral classLiteral(Class klass) + { + return (ClassLiteral) getFactory().classLiteral(klass); + } + + /** + * Creates an accessor expression off the class with the given name, to invoke static methods and properties + * @param klass The name of the class + * @param the Java Source type + * @return A static class accessor expression + */ + @SuppressWarnings("unchecked") + public static > Accessor classStatic(String klass) + { + return (Accessor) getFactory().classStatic(klass); + } + + /** + * Creates an accessor expression off the given class, to invoke static methods and properties + * @param klass The name of the class + * @param the Java Source type + * @return A static class accessor expression + */ + @SuppressWarnings("unchecked") + public static > Accessor classStatic(Class klass) + { + return (Accessor) getFactory().classStatic(klass); + } + + /** + * Creates a constructor expression for the class of the given name + * @param klass The name of the class + * @param the Java Source type + * @return A new, empty constructor expression for the given class + */ + @SuppressWarnings("unchecked") + public static > ConstructorExpression construct(String klass) + { + return (ConstructorExpression) getFactory().construct(klass); + } + + /** + * Creates a constructor expression for the given class + * @param klass The name of the class + * @param the Java Source type + * @return A new, emptyconstructor expression for the given class + */ + @SuppressWarnings("unchecked") + public static > ConstructorExpression construct(Class klass) + { + return (ConstructorExpression) getFactory().construct(klass); + } + + /** + * Creates a new array creation expression. The type of the array is the class of the given name + * @param klass The name of the array type + * @param the Java Source type + * @return A new, empty array creation expression for the given class + */ + @SuppressWarnings("unchecked") + public static > ArrayConstructorExpression newArray(String klass) + { + return (ArrayConstructorExpression) getFactory().newArray(klass); + } + + /** + * Creates a new array creation expression. The type of the array is the given class + * @param klass The name of the array type + * @param the Java Source type + * @return A new, empty array creation expression for the given class + */ + @SuppressWarnings("unchecked") + public static > ArrayConstructorExpression newArray(Class klass) + { + return (ArrayConstructorExpression) getFactory().newArray(klass); + } + + /** + * Creates an array initializer expression, to be used with an array declaration + * @param the Java Source type + * @return A new array initialization expression + */ + @SuppressWarnings("unchecked") + public static > ArrayInit vec() + { + return (ArrayInit) getFactory().vec(); + } + + /** + * Creates a variable reference expression + * @param variable The name of the variable + * @param the Java Source type + * @return A new expression that evaluates to the value of the variable with name variable + */ + @SuppressWarnings("unchecked") + public static > Variable var(String variable) + { + return (Variable) getFactory().var(variable); + } + + /** + * Creates an infix operator expression, using the given operator operator + * @param operator The operator for the expression + * @param the Java Source type + * @return A new infix operator expression with no arguments + */ + @SuppressWarnings("unchecked") + public static > OperatorExpression operator(Op operator) + { + return (OperatorExpression) getFactory().operator(operator); + } + + /** + * Creates a unary, prefix operator expression, using the given operator operator + * @param operator The prefix operator for the expression + * @param the Java Source type + * @return A new unary prefix operator expression with no argument + */ + @SuppressWarnings("unchecked") + public static > UnaryExpression operator(PrefixOp operator) + { + return (UnaryExpression) getFactory().operator(operator); + } + + /** + * Creates a new ternary expression + * @param the Java Source type + * @return A new, empty ternary expression + */ + @SuppressWarnings("unchecked") + public static > TernaryExpression ternary() + { + return (TernaryExpression) getFactory().ternary(); + } + + /** + * Creates a cast expression, casting the given expression to the type of the given klass name + * @param klass The name of the class to cast the expression to + * @param expression The expression to be cast + * @param the Java Source type + * @return A new cast expression + */ + @SuppressWarnings("unchecked") + public static > Argument cast(String klass, ExpressionSource expression) + { + return (Argument) getFactory().cast(klass, expression); + } + + /** + * Creates a cast expression, casting the given expression to the type of the given klass + * @param klass The class to cast the expression to + * @param expression The expression to be cast + * @param the Java Source type + * @return A new cast expression + */ + @SuppressWarnings("unchecked") + public static > Argument cast(Class klass, ExpressionSource expression) + { + return (Argument) getFactory().cast(klass, expression); + } + + /** + * Creates a parenthesis expression wrapping the given inner expression + * @param the Java Source type + * @return A new, parenthesis expression + */ + @SuppressWarnings("unchecked") + public static > ParenExpression paren(ExpressionSource inner) + { + return (ParenExpression) getFactory().paren(inner); + } + + /** + * Creates a unary, boolean "not" expression that negates the given inner argument + * @param inner The expression to be negated + * @param the Java Source type + * @return A new "not" expression + */ + @SuppressWarnings("unchecked") + public static > UnaryExpression not(ExpressionSource inner) + { + return (UnaryExpression) getFactory().not(inner); + } + + /** + * Creates a new assignment expression using the given assignment operator + * @param operator The assignment operator + * @param the Java Source type + * @return Creates a new assignment expression + */ + @SuppressWarnings("unchecked") + public static > AssignExpression assign(Assignment operator) + { + return (AssignExpression) getFactory().assign(operator); + } + + /** + * Creates a variable declaration expression, given a variable name and a class name + * @param klass The name of the type of the declared variable + * @param name The name of the declared variable + * @param the Java Source type + * @return A new variable declaration expression. + */ + @SuppressWarnings("unchecked") + public static > DeclareExpression declare(String klass, String name) + { + return (DeclareExpression) getFactory().declare(klass, name); + } + + /** + * Creates a variable declaration expression, given a variable name and a class + * @param klass The type of the declared variable + * @param name The name of the declared variable + * @param the Java Source type + * @return A new variable declaration expressions. + */ + @SuppressWarnings("unchecked") + public static > DeclareExpression declare(Class klass, String name) + { + return (DeclareExpression) getFactory().declare(klass, name); + } + + /** + * Creates a variable declaration expression, given a variable name, a class name and an initialization expression + * @param klass The name of the type of the declared variable + * @param name The name of the declared variable + * @param init The expression providing the initial value of the variable + * @param the Java Source type + * @return A variable declaration expressions with an initializer. + */ + @SuppressWarnings("unchecked") + public static > DeclareExpression declare(String klass, String name, ExpressionSource init) + { + return (DeclareExpression) getFactory().declare(klass, name, init); + } + + /** + * Creates a variable declaration expression, given a variable name, a class and an initialization expression + * @param klass The type of the declared variable + * @param name The name of the declared variable + * @param init The expression providing the initial value of the variable + * @param the Java Source type + * @return A variable declaration expressions with an initializer. + */ + @SuppressWarnings("unchecked") + public static > DeclareExpression declare(Class klass, String name, ExpressionSource init) + { + return (DeclareExpression) getFactory().declare(klass, name, init); + } + + /** + * Creates a new method invocation expression + * @param method The name of the method to invoke + * @param the Java Source type + * @return A new method ivocation expression with no arguments + */ + @SuppressWarnings("unchecked") + public static > MethodCallExpression invoke(String method) + { + return (MethodCallExpression) getFactory().invoke(method); + } + + /** + * Creates a field accessor expression + * @param field The name of the field to access + * @param the Java Source type + * @return A new accessor for the given field + */ + @SuppressWarnings("unchecked") + public static > Field field(String field) + { + return (Field) getFactory().field(field); + } + + /** + * Creates a specialized getter method expression. + * The method name will start with "get" or "is" depending on the field type, and will be camel-cased according to the javabean conventions + * @param field The field exposed by the getter + * @param klass The name of the type of the field exposed by the getter. + * @param the Java Source type the Java Source type + * @return A new getter expression for the given field + */ + @SuppressWarnings("unchecked") + public static > Getter getter(String field, String klass) + { + return (Getter) getFactory().getter(field, klass); + } + + /** + * Creates a specialized getter method expression. + * The method name will start with "get" or "is" depending on the field type, and will be camel-cased according to the javabean conventions + * @param field The field exposed by the getter + * @param klass The type of the field exposed by the getter. + * @param the Java Source type + * @return A new getter expression for the given field + */ + @SuppressWarnings("unchecked") + public static > Getter getter(String field, Class klass) + { + return (Getter) getFactory().getter(field, klass); + } + + /** + * Creates a specialized setter method expression. + * The method name will start with "set" and will be camel-cased according to the javabean conventions + * @param field The field exposed by the getter + * @param klass The name of the type of the field exposed by the setter. + * @param value The expression providing the new value for the field + * @param the Java Source type + * @return A new setter expression for the given field + */ + @SuppressWarnings("unchecked") + public static > Setter setter(String field, String klass, ExpressionSource value) + { + return (Setter) getFactory().setter(field, klass, value); + } + + /** + * Creates a specialized setter method expression. + * The method name will start with "set" and will be camel-cased according to the javabean conventions + * @param field The field exposed by the getter + * @param klass The type of the field exposed by the setter. + * @param value The expression providing the new value for the field + * @param the Java Source type + * @return A new setter expression for the given field + */ + @SuppressWarnings("unchecked") + public static > Setter setter(String field, Class klass, ExpressionSource value) + { + return (Setter) getFactory().setter(field, klass, value); + } + + /** + * Creates an accessor for the parent class + * @param the Java Source type + * @return A new "super" accessor + */ + @SuppressWarnings("unchecked") + public static > Super sup() + { + return (Super) getFactory().sup(); + } + + /** + * Creates a new instanceof expression for the given expression and type name + * @param klass The name of the type for the instanceof check + * @param expression The expression to be evaluated + * @param the Java Source type + * @return A new instanceof expression + */ + @SuppressWarnings("unchecked") + public static > InstanceofExpression instanceOf(String klass, ExpressionSource expression) + { + return (InstanceofExpression) getFactory().instanceOf(klass, expression); + } + + /** + * Creates a new instanceof expression for the given expression and type + * @param klass The type for the instanceof check + * @param expression The expression to be evaluated + * @param the Java Source type + * @return A new instanceof expression + */ + @SuppressWarnings("unchecked") + public static > InstanceofExpression instanceOf(Class klass, ExpressionSource expression) + { + return (InstanceofExpression) getFactory().instanceOf(klass, expression); + } + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Field.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Field.java new file mode 100644 index 00000000..4c84b4de --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Field.java @@ -0,0 +1,32 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a field access expression in source format + * + * @author Davide Sottara + */ +public interface Field, + P extends ExpressionHolder> + extends OrdinalArgument>, + NonPrimitiveExpression>, + InvocationTargetHolder> +{ + + /** + * Returns the field name + * @return the field name + */ + public String getFieldName(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Getter.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Getter.java new file mode 100644 index 00000000..8a47f6a4 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Getter.java @@ -0,0 +1,30 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a getter method invocation expression in source format + * + * @author Davide Sottara + */ +public interface Getter, + P extends ExpressionHolder> + extends Accessor>, + MethodInvokeExpression> +{ + + /** + * Returns the name of the field this getter is accessing + * @return the name of the field this getter is accessing + */ + public String getFieldName(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/InstanceofExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/InstanceofExpression.java new file mode 100644 index 00000000..fa4f0430 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/InstanceofExpression.java @@ -0,0 +1,35 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent an instanceof expression in source format + * + * @author Davide Sottara + */ +public interface InstanceofExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression> +{ + + /** + * Returns the name of the type instanceof is being checked against + * @return the name of the type instanceof is being checked against + */ + public String getTypeName(); + + /** + * Returns the expression being tested for instanceof + * @return the expression being tested for instanceof + */ + public ExpressionSource,?> getExpression(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/InvocationTargetHolder.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/InvocationTargetHolder.java new file mode 100644 index 00000000..61e61573 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/InvocationTargetHolder.java @@ -0,0 +1,37 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract marker interface that represents expressions that can be invoked + * on the result of the evaluation of another expression + * + * @author Davide Sottara + */ +public interface InvocationTargetHolder, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> +{ + + /** + * The expression on which this Expression is invoked + * @return The expression on which this Expression is invoked + */ + public ExpressionSource getInvocationTarget(); + + /** + * Sets the expression on which this Expression is invoked + * @param target The expression on which this Expression is invoked + * @return This Expression itself + */ + public E setInvocationTarget(ExpressionSource target); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Literal.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Literal.java new file mode 100644 index 00000000..bf15d123 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Literal.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract interface that represents a literal expression in source format + * + * @author Davide Sottara + */ +public interface Literal, + P extends ExpressionHolder, + E extends Literal> + extends Argument +{ +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/LiteralBuilder.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/LiteralBuilder.java new file mode 100644 index 00000000..9e76dab5 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/LiteralBuilder.java @@ -0,0 +1,95 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract factory interface that supports the creation of literal expressions in source format + * + * @author Davide Sottara + */ +public interface LiteralBuilder, + P extends ExpressionHolder> +{ + + /** + * Creates a String literal + * @param val The string value + * @return a new StringLiteral + */ + public StringLiteral literal(String val); + + /** + * Creates a Number Literal + * @param val The Number value + * @return a new NumberLiteral + */ + public NumberLiteral literal(Number val); + + /** + * Creates a CharacterLiteral + * @param val The Character value + * @return a new CharacterLiteral + */ + public CharacterLiteral literal(Character val); + + /** + * Creates a BooleanLiteral + * @param val The Boolean value + * @return a new BooleanLiteral + */ + public BooleanLiteral literal(Boolean val); + + /** + * Creates a literal based on the klass name passed as an argument. + * Boolean returns false, Numbers return the appropriate representation for 0, + * Objects return null + * @param klass The name of the type of literal to create + * @return a default Literal for the given type + */ + public Literal zeroLiteral(String klass); + + /** + * Creates a literal based on the klass name passed as an argument. + * Boolean returns false, Numbers return the appropriate representation for 0, + * Objects return null + * @param klass The type of literal to create + * @return a default Literal for the given type + */ + public Literal zeroLiteral(Class klass); + + /** + * Creates a literal expression pointing to this object + * @return a new ThisLiteral + */ + public ThisLiteral thisLiteral(); + + /** + * Creates a null literal expression + * @return a new NullLiteral + */ + public NullLiteral nullLiteral(); + + /** + * Creates a classs literal expression + * @param klass The name of the class + * @return a new ClassLiteral + */ + public ClassLiteral classLiteral(String klass); + + /** + * Creates a classs literal expression + * @param klass The class + * @return a new ClassLiteral + */ + public ClassLiteral classLiteral(Class klass); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodCallExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodCallExpression.java new file mode 100644 index 00000000..07dfef50 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodCallExpression.java @@ -0,0 +1,31 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a method call expression in source format + * + * @author Davide Sottara + */ +public interface MethodCallExpression, + P extends ExpressionHolder> + extends MethodWithArgumentsInvokeExpression> +{ + + /** + * Sets the name of the method to be invoked + * @param name The name of the method + * @return this MethodCallExpression itself + */ + public MethodCallExpression setMethodName(String name); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodInvokeExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodInvokeExpression.java new file mode 100644 index 00000000..0ce4f7c2 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodInvokeExpression.java @@ -0,0 +1,33 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract interface that represent a method / accessor expression in source format + * + * @author Davide Sottara + */ +public interface MethodInvokeExpression, + P extends ExpressionHolder, + M extends MethodInvokeExpression> + extends Argument, + Accessor, + NonPrimitiveExpression, + InvocationTargetHolder +{ + + /** + * Returns the name of the method being invoked + * @return the name of the method + */ + public String getMethodName(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodWithArgumentsInvokeExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodWithArgumentsInvokeExpression.java new file mode 100644 index 00000000..6c6957af --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodWithArgumentsInvokeExpression.java @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract interface that represents a method invocation expression requiring arguments, in source format + * + * @author Davide Sottara + */ +public interface MethodWithArgumentsInvokeExpression, + P extends ExpressionHolder, + M extends MethodWithArgumentsInvokeExpression> + extends MethodInvokeExpression, + ArgumentHolder +{ + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/NonPrimitiveExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/NonPrimitiveExpression.java new file mode 100644 index 00000000..d2f94f81 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/NonPrimitiveExpression.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract interface that represents a non-primitive expression in source format + * + * @author Davide Sottara + */ +public interface NonPrimitiveExpression, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends ExpressionSource, + ExpressionHolder +{ +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/NullLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/NullLiteral.java new file mode 100644 index 00000000..c5250103 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/NullLiteral.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a null literal expression in source format + * + * @author Davide Sottara + */ +public interface NullLiteral, + P extends ExpressionHolder> + extends Literal> +{ +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/NumberLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/NumberLiteral.java new file mode 100644 index 00000000..6aefbd25 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/NumberLiteral.java @@ -0,0 +1,29 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a number literal expression in source format + * + * @author Davide Sottara + */ +public interface NumberLiteral, + P extends ExpressionHolder> + extends PrimitiveLiteral +{ + + /** + * Returns the value represented by this literal expression + * @return the literal value of this expression + */ + public Number getValue(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Op.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Op.java new file mode 100644 index 00000000..57167a51 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Op.java @@ -0,0 +1,57 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +public enum Op +{ + + TIMES("*"), + DIVIDE("/"), + REMAINDER("%"), + PLUS("+"), + MINUS("-"), + LEFT_SHIFT("<<"), + RIGHT_SHIFT_SIGNED(">>"), + RIGHT_SHIFT_UNSIGNED(">>>"), + LESS("<"), + GREATER(">"), + LESS_EQUALS("<="), + GREATER_EQUALS(">="), + EQUALS("=="), + NOT_EQUALS("!="), + BITWISE_XOR("^"), + BITWISE_AND("&"), + BITWISE_OR("|"), + OR("||"), + AND("&&"); + + private String op; + + Op(String o) + { + op = o; + } + + public String getOp() + { + return op; + } + + public static Op build(String x) + { + for (Op en : Op.values()) + { + if (en.getOp().equals(x)) + { + return en; + } + } + throw new IllegalStateException("Unrecognized operation " + x); + } +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/OperatorExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/OperatorExpression.java new file mode 100644 index 00000000..0944c091 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/OperatorExpression.java @@ -0,0 +1,40 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent an infix operator expression in source format + * + * @author Davide Sottara + */ +public interface OperatorExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression>, + ArgumentHolder> +{ + + /** + * Adds an argument to this operation, as the result of evaluating an expression + * @param arg The argument to be added + * @return this OperatorExpression itself + */ + public OperatorExpression addArgument(Argument arg); + + /** + * Returns the infix operator used to combine the arguments + * @return the infix operator used to combine the arguments + * @see org.jboss.forge.roaster.model.expressions.Op + */ + public String getOperator(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/OrdinalArgument.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/OrdinalArgument.java new file mode 100644 index 00000000..6540c217 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/OrdinalArgument.java @@ -0,0 +1,40 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Marker interface representing expressions returning values that can be incremented or decremented, in source format + * + * @author Davide Sottara + */ +public interface OrdinalArgument, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends Argument, + Accessor, + NonPrimitiveExpression +{ + + /** + * Creates a postfix self-increment expression having the result of this expression as an argument + * @return a new PostFixExpression + * @see org.jboss.forge.roaster.model.expressions.PostfixOp + */ + public PostFixExpression> inc(); + + /** + * Creates a postfix self-decrement expression having the result of this expression as an argument + * @return a new PostFixExpression + * @see org.jboss.forge.roaster.model.expressions.PostfixOp + */ + public PostFixExpression> dec(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ParenExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ParenExpression.java new file mode 100644 index 00000000..a949e4ee --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ParenExpression.java @@ -0,0 +1,31 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.spi.ExpressionFactory; + +/** + * Represent a parenthesized expression in source format + * + * @author Davide Sottara + */ +public interface ParenExpression, + P extends ExpressionHolder> + extends ExpressionFactory>, + NonPrimitiveExpression>, + Argument> +{ + + /** + * Returns the expression within parenthesis + * @return the argument of this Pare + */ + public ExpressionSource,?> getExpression(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/PostFixExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PostFixExpression.java new file mode 100644 index 00000000..56704c12 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PostFixExpression.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + + +/** + * Represent a postfix operator expression in source format + * + * @author Davide Sottara + */ +public interface PostFixExpression, + P extends ExpressionHolder> + extends UnaryExpression +{ + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/PostfixOp.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PostfixOp.java new file mode 100644 index 00000000..fa0fbfc6 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PostfixOp.java @@ -0,0 +1,40 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +public enum PostfixOp +{ + + INC("++"), + DEC("--"); + + private String op; + + PostfixOp(String o) + { + op = o; + } + + public String getOp() + { + return op; + } + + public static PostfixOp build(String x) + { + for (PostfixOp en : PostfixOp.values()) + { + if (en.getOp().equals(x)) + { + return en; + } + } + throw new IllegalStateException("Unrecognized operation " + x); + } +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/PrefixOp.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PrefixOp.java new file mode 100644 index 00000000..52b4c40f --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PrefixOp.java @@ -0,0 +1,44 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +public enum PrefixOp +{ + + INC("++"), + DEC("--"), + PLUS("+"), + MINUS("-"), + NOT("!"), + NEG("~"); + + private String op; + + PrefixOp(String o) + { + op = o; + } + + public String getOp() + { + return op; + } + + public static PrefixOp build(String x) + { + for (PrefixOp en : PrefixOp.values()) + { + if (en.getOp().equals(x)) + { + return en; + } + } + throw new IllegalStateException("Unrecognized operation " + x); + } +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/PrimitiveLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PrimitiveLiteral.java new file mode 100644 index 00000000..77131ddd --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PrimitiveLiteral.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract interface that represents literal expression in source format + * + * @author Davide Sottara + */ +public interface PrimitiveLiteral, + P extends ExpressionHolder> + extends Literal> +{ +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Setter.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Setter.java new file mode 100644 index 00000000..63fde886 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Setter.java @@ -0,0 +1,30 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a setter method invocation expression in source format + * + * @author Davide Sottara + */ +public interface Setter, + P extends ExpressionHolder> + extends ExpressionSource>, + MethodWithArgumentsInvokeExpression> +{ + + /** + * Returns the expression that evaluates to the value being set + * @return the expression that evaluates to the value being set + */ + public ExpressionSource,?> getValue(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/StringLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/StringLiteral.java new file mode 100644 index 00000000..6b211c91 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/StringLiteral.java @@ -0,0 +1,32 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a string literal expression in source format + * + * @author Davide Sottara + */ +public interface StringLiteral, + P extends ExpressionHolder> + extends Literal>, + NonPrimitiveExpression>, + Accessor> +{ + + /** + * Returns the value represented by this literal expression + * @return the literal value of this expression + */ + public String getValue(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Super.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Super.java new file mode 100644 index 00000000..697e6b3d --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Super.java @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a "super" literal accessor expression in source format + * + * @author Davide Sottara + */ +public interface Super, + P extends ExpressionHolder> + extends AccessBuilder>, + NonPrimitiveExpression> +{ + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/TernaryExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/TernaryExpression.java new file mode 100644 index 00000000..c7967a65 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/TernaryExpression.java @@ -0,0 +1,66 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a ternary expression in source format + * + * @author Davide Sottara + */ +public interface TernaryExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression> +{ + + /** + * Returns the expression defining the condition of this ternary expression + * @return the condition part of this TernaryExpression + */ + public ExpressionSource,?> getCondition(); + + /** + * Sets the condition of this ternary expression + * @param expression The condition expression + * @return this TernaryExpression itself + */ + public TernaryExpression setCondition(ExpressionSource expression); + + + /** + * Returns the expression defining the then of this ternary expression + * @return the then part of this TernaryExpression + */ + public ExpressionSource,?> getThenExpression(); + + /** + * Sets the then of this ternary expression + * @param onTrue The then expression + * @return this TernaryExpression itself + */ + public TernaryExpression setThenExpression(ExpressionSource onTrue); + + + /** + * Returns the expression defining the else of this ternary expression + * @return the else part of this TernaryExpression + */ + public ExpressionSource,?> getElseExpression(); + + /** + * Sets the else of this ternary expression + * @param onFalse The else expression + * @return this TernaryExpression itself + */ + public TernaryExpression setElseExpression(ExpressionSource onFalse); + + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ThisLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ThisLiteral.java new file mode 100644 index 00000000..9e21eac8 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ThisLiteral.java @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a "this" literal accessor expression in source format + * + * @author Davide Sottara + */ +public interface ThisLiteral, + P extends ExpressionHolder> + extends Literal>, + NonPrimitiveExpression>, + Accessor> +{ +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/UnaryExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/UnaryExpression.java new file mode 100644 index 00000000..613b73b8 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/UnaryExpression.java @@ -0,0 +1,43 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a prexif, unary expression in source format + * + * @author Davide Sottara + */ +public interface UnaryExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression> +{ + + /** + * Returns the single argument of this expression + * @return the Argument of this UnaryExpression + */ + public Argument,?> getExpression(); + + /** + * Sets the argument of this expression + * @param arg the argument + * @return this UnaryExpression itself + */ + public UnaryExpression setExpression( ExpressionSource arg ); + + /** + * Returns the operator used in this expression + * @return the operator used in this expression + * @see org.jboss.forge.roaster.model.expressions.PrefixOp + */ + public String getOperator(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Variable.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Variable.java new file mode 100644 index 00000000..5ca185ce --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Variable.java @@ -0,0 +1,28 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represent a variable de-referencing expression in source format + * + * @author Davide Sottara + */ +public interface Variable, + P extends ExpressionHolder> + extends OrdinalArgument> +{ + + /** + * Returns the name of the variable + * @return the name of the variable + */ + String getName(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/source/BlockHolder.java b/api/src/main/java/org/jboss/forge/roaster/model/source/BlockHolder.java new file mode 100644 index 00000000..d00d71f8 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/source/BlockHolder.java @@ -0,0 +1,16 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.source; + +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.JavaType; + +public interface BlockHolder> { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/source/BlockSource.java b/api/src/main/java/org/jboss/forge/roaster/model/source/BlockSource.java new file mode 100644 index 00000000..e3b1812f --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/source/BlockSource.java @@ -0,0 +1,34 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.source; + +import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.statements.StatementSource; + +import java.util.List; + +public interface BlockSource, + P extends BlockHolder, + S extends BlockSource> + extends Block { + + List> getStatements(); + + BlockSource addStatement( StatementSource statement ); + + BlockSource addStatement( int index, StatementSource statement ); + + BlockSource addStatement( ExpressionSource statement ); + + BlockSource addStatement( int index, ExpressionSource statement ); + + BlockSource removeStatement( StatementSource statement ); + + BlockSource removeStatement( int index ); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/source/MethodBody.java b/api/src/main/java/org/jboss/forge/roaster/model/source/MethodBody.java new file mode 100644 index 00000000..bf8601c5 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/source/MethodBody.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.source; + +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.Method; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.source.MethodSource; + +/** + * The body of a method + */ +public interface MethodBody, T extends MethodSource> + extends BlockHolder, + Origin { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java b/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java index 73d0ffb5..a1af604c 100644 --- a/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java +++ b/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java @@ -7,11 +7,16 @@ package org.jboss.forge.roaster.model.source; -import java.util.List; - import org.jboss.forge.roaster.model.JavaClass; import org.jboss.forge.roaster.model.JavaType; import org.jboss.forge.roaster.model.Method; +import org.jboss.forge.roaster.model.expressions.Expression; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.ReturnStatement; +import org.jboss.forge.roaster.model.statements.Statement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +import java.util.List; /** * Represents a Java Method in source form. @@ -21,7 +26,9 @@ */ public interface MethodSource> extends Method>, AbstractableSource>, - MemberSource>, GenericCapableSource> + MemberSource>, + GenericCapableSource>, + BlockHolder { /** * Sets the synchronized keyword in this element. @@ -135,4 +142,23 @@ public interface MethodSource> extends Method removeParameter(JavaType type, String name); + /** + * @param body + * @return + */ + MethodSource setBody( StatementSource body ); + + /** + * @param body + * @return + */ + MethodSource setBody( BlockSource body ); + + /** + * Returns the body of this method as a Roaster Block + * @return + */ + BlockSource,?> getBodyAsBlock(); + + } \ No newline at end of file diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/AssertStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/AssertStatement.java new file mode 100644 index 00000000..6991b24a --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/AssertStatement.java @@ -0,0 +1,51 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents an assert statement in source format + * + * @author Davide Sottara + */ +public interface AssertStatement, + P extends BlockHolder> + extends StatementSource> +{ + + /** + * Returns the assertion expression + * @return the assertion expression + */ + public ExpressionSource,?> getAssertion(); + + /** + * Sets the assertion expression + * @param expression the assertion expression expected to hold true + * @return this AssertStatement itself + */ + public AssertStatement setAssertion(ExpressionSource expression); + + + /** + * Returns the message expression + * @return the message expression + */ + public ExpressionSource,?> getMessage(); + + /** + * Sets the expression that evaluates to the message to be displayed in case of failed assertion + * @param msg The message expression + * @return this AssertStatement itself + */ + public AssertStatement setMessage(ExpressionSource msg); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/AssignmentStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/AssignmentStatement.java new file mode 100644 index 00000000..6097f603 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/AssignmentStatement.java @@ -0,0 +1,68 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.AssignExpression; +import org.jboss.forge.roaster.model.expressions.Assignment; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents an assignment statement in source format + * + * @author Davide Sottara + */ +public interface AssignmentStatement, + P extends BlockHolder> + extends ExpressionStatement> +{ + + /** + * Returns the LHS expression of this assignment statement + * @return the LHS expression of this assignment statement + */ + public Accessor>,?> getLeft(); + + /** + * Sets the LHS of the assignment + * @param left the expression that evaluates to the variable to be assigned + * @return this AssignmentStatement itself + */ + public AssignmentStatement setLeft(Accessor left); + + /** + * Returns the assignment operator used in this statement + * @return the assignment operator used in this statement + * @see org.jboss.forge.roaster.model.expressions.Assignment + */ + public String getAssignmentOperator(); + + /** + * Sets the assignment operator to be used in this statement + * @param op the assignment operator + * @return this AssignmentStatement itself + */ + public AssignmentStatement setAssignmentOperator(Assignment op); + + /** + * Returns the RHS expression of this assignment statement + * @return the RHS expression of this assignment statement + */ + public ExpressionSource>,?> getRight(); + + /** + * Sets the RHS of the assignment + * @param right the expression that evaluates to the value to be assigned + * @return this AssignmentStatement itself + */ + public AssignmentStatement setRight(ExpressionSource right); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/BlockStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/BlockStatement.java new file mode 100644 index 00000000..796b5ae5 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/BlockStatement.java @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents a code block statement in source format + * + * @author Davide Sottara + */ + +public interface BlockStatement, + P extends BlockHolder> + extends BlockSource>, + StatementSource> +{ +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/BodyHolder.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/BodyHolder.java new file mode 100644 index 00000000..9c8f82ec --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/BodyHolder.java @@ -0,0 +1,40 @@ +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents a {@link org.jboss.forge.roaster.model.statements.Statement} + * that is the parent of a body block + * + * @author Davide Sottara + */ + +public interface BodyHolder, + P extends BlockHolder, + B extends BodyHolder> + extends BlockHolder +{ + + /** + * Returns the body as a block containing other statements + * @return the body as a block containing other statements + */ + public BlockStatement getBody(); + + /** + * Sets the body from a whole block + * @param body The block containing the body statements + * @return this BodyHolder itself + */ + public B setBody(BlockSource body); + + /** + * Sets the body from a single statement, wrapping it inside a block + * @param body The statement that will become the body + * @return this BodyHolder itself + */ + public B setBody(StatementSource body); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/BranchingStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/BranchingStatement.java new file mode 100644 index 00000000..d6239e9d --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/BranchingStatement.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract interface that represents a branching statement in source format + * + * @author Davide Sottara + */ +public interface BranchingStatement, + P extends BlockHolder, + S extends BranchingStatement> + extends ControlFlowStatement +{ +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/BreakStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/BreakStatement.java new file mode 100644 index 00000000..a677cdf8 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/BreakStatement.java @@ -0,0 +1,38 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents a break statement in source format + * + * @author Davide Sottara + */ + +public interface BreakStatement, + P extends BlockHolder> + extends BranchingStatement> +{ + + /** + * Returns the label to jump to in case of break + * @return the label to jump to in case of break + */ + public String getBreakToLabel(); + + /** + * Sets the label to jump to in case of break + * @param label The label + * @return this BreakStatement itself + */ + public BreakStatement setBreakToLabel(String label); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ContinueStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ContinueStatement.java new file mode 100644 index 00000000..d5e5ee1c --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ContinueStatement.java @@ -0,0 +1,38 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents a continue statement in source format + * + * @author Davide Sottara + */ + +public interface ContinueStatement, + P extends BlockHolder> + extends BranchingStatement> +{ + + /** + * Returns the label to jump to in case of continue + * @return the label to jump to in case of continue + */ + public String getContinueToLabel(); + + /** + * Sets the label to jump to in case of continue + * @param label The label + * @return this ContinueStatement itself + */ + public ContinueStatement setContinueToLabel(String label); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ControlFlowStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ControlFlowStatement.java new file mode 100644 index 00000000..c55d3d29 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ControlFlowStatement.java @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract interface that represents a control flow statement in source format + * + * @author Davide Sottara + */ +public interface ControlFlowStatement, + P extends BlockHolder, + S extends ControlFlowStatement> + extends StatementSource, + ExpressionHolder +{ +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/DeclareStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/DeclareStatement.java new file mode 100644 index 00000000..f365ed78 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/DeclareStatement.java @@ -0,0 +1,117 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; +import java.util.Map; + +/** + * Represents a variable declaration statement in source format + * + * @author Davide Sottara + */ +public interface DeclareStatement, + P extends BlockHolder> + extends StatementSource> +{ + + + /** + * Returns the names of the variables declared in this statement + * @return the names of the variables declared in this statement + */ + public List getVariableNames(); + + /** + * Returns the name of the first variable declared in this statement + * @return the name of the first variable declared in this statement + */ + public String getVariableName(); + + /** + * Returns the type of the variables declared in this statement + * @return the type of the variables declared in this statement + */ + public String getVariableType(); + + /** + * Returns the declared variables, associated to their initialization expression(s), if any + * @return An immutable map associating the declared variable names to their initialization expressions + */ + public Map,?>> getInitExpressions(); + + /** + * Returns the initialization expression of the first declared variable (if any) + * Notice that this is different from the first initialization in the declaration (e.g. int a, b = 0;) + * @return The expression used to initialize the first variable in the declaration + */ + public ExpressionSource,?> getInitExpression(); + + /** + * Returns true if this declaration statement contains exactly one variable + * @return true if this declaration statement contains exactly one variable + */ + public boolean isSingleDeclaration(); + + + /** + * Sets the name and type of the first variable declared by this statement + * @param name The name of the new variable + * @param klass The type of the variable + * @return this DeclareStatement itself + */ + public DeclareStatement setVariable(Class klass, String name); + + /** + * Sets the name and type of the first variable declared by this statement + * @param name The name of the new variable + * @param klass The name of the type of the variable + * @return this DeclareStatement itself + */ + public DeclareStatement setVariable(String klass, String name); + + /** + * Sets the name and type of the first variable declared by this statement and initializes it + * @param name The name of the new variable + * @param klass The type of the variable + * @param init The expression used to initialize the variable + * @return this DeclareStatement itself + */ + public DeclareStatement setVariable(Class klass, String name, ExpressionSource init); + + /** + * Sets the name and type of the first variable declared by this statement and initializes it + * @param name The name of the new variable + * @param klass The name of the type of the variable + * @param init The expression used to initialize the variable + * @return this DeclareStatement itself + */ + public DeclareStatement setVariable(String klass, String name, ExpressionSource init); + + /** + * Adds an additional variable to the declaration statement + * @param name The name of the new variable + * @return this DeclareStatement itself + */ + public DeclareStatement addVariable(String name); + + /** + * Adds an additional variable to the declaration statement and initializes it + * @param name The name of the new variable + * @param init The expression used to initialize the variable + * @return this DeclareStatement itself + */ + public DeclareStatement addVariable(String name, ExpressionSource init); + + +} \ No newline at end of file diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/DoWhileStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/DoWhileStatement.java new file mode 100644 index 00000000..c9841981 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/DoWhileStatement.java @@ -0,0 +1,38 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents a do while loop statement in source format + * + * @author Davide Sottara + */ +public interface DoWhileStatement, + P extends BlockHolder> + extends LoopingStatement>, + BodyHolder> +{ + + /** + * Returns the conditional expression evaluated by this loop + * @return the conditional expression evaluated by this loop + */ + public ExpressionSource,?> getCondition(); + + /** + * Sets the condition expression evaluated by this loop + * @param expr The condition expression + * @return this DoWhileStatement itself + */ + public DoWhileStatement setCondition(ExpressionSource expr); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/EvalExpressionStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/EvalExpressionStatement.java new file mode 100644 index 00000000..be029a51 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/EvalExpressionStatement.java @@ -0,0 +1,33 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents an expression evaluation statement in source format + * + * @author Davide Sottara + */ +public interface EvalExpressionStatement, + P extends BlockHolder> + extends ExpressionStatement> +{ + + /** + * Sets the inner expression to be evaluated by this statement + * @param expr the expression to be evaluated + * @return this EvalExpressionStatement itself + */ + public EvalExpressionStatement setExpr(ExpressionSource expr); + +} + + diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ExpressionStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ExpressionStatement.java new file mode 100644 index 00000000..cc3f820e --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ExpressionStatement.java @@ -0,0 +1,33 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract interface that represents generic expression/method/etc.. statements in source format + * + * @author Davide Sottara + */ +public interface ExpressionStatement, + P extends BlockHolder, + S extends ExpressionStatement> + extends StatementSource, + ExpressionHolder +{ + + /** + * Returns the inner expression evaluated by this statement + * @return the inner expression evaluated by this statement + */ + public ExpressionSource getExpr(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ForEachStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ForEachStatement.java new file mode 100644 index 00000000..10b4dba9 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ForEachStatement.java @@ -0,0 +1,67 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents an enhanced for loop statement in source format + * + * @author Davide Sottara + */ +public interface ForEachStatement, + P extends BlockHolder> + extends LoopingStatement>, + BodyHolder> +{ + + /** + * Returns the name of the iterator variable + * @return the name of the iterator variable + */ + public String getIteratorName(); + + /** + * Returns the name of the type of the iterator variable + * @return the name of the type of the iterator variable + */ + public String getIteratorType(); + + + /** + * Sets the name and type of the iterator variable + * @param klass the name of the type of the iterator + * @param name the name of the iterator + * @return this ForEachStatement itself + */ + public ForEachStatement setIterator(String klass, String name); + + /** + * Sets the name and type of the iterator variable + * @param klass the type of the iterator + * @param name the name of the iterator + * @return this ForEachStatement itself + */ + public ForEachStatement setIterator(Class klass, String name); + + /** + * Returns the expression that evaluates to the collection to be iterated + * @return the expression that evaluates to the collection to be iterated + */ + public ExpressionSource,?> getSource(); + + /** + * Sets the expression that evaluates to the collection to be iterated + * @param expr + * @return this ForEachStatement itself + */ + public ForEachStatement setSource(ExpressionSource expr); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ForStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ForStatement.java new file mode 100644 index 00000000..f5fcfd8a --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ForStatement.java @@ -0,0 +1,68 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +/** + * Represents a for loop statement in source format + * + * @author Davide Sottara + */ +public interface ForStatement, + P extends BlockHolder> + extends LoopingStatement>, + BodyHolder> +{ + + /** + * Returns the declaration of the iterator variables + * @return the declaration of the iterator variables + */ + public DeclareExpression> getDeclaration(); + + /** + * Sets the declaration of the iterator variables, including their initial values + * @param declaration the declaration of the iterator variables + * @return this ForStatement itself + */ + public ForStatement setDeclaration(DeclareExpression declaration); + + /** + * Returns the conditional expression evaluated by this loop + * @return the conditional expression evaluated by this loop + */ + public ExpressionSource,?> getCondition(); + + /** + * Sets the condition expression evaluated by this loop + * @param expr The condition expression + * @return this ForStatement itself + */ + public ForStatement setCondition(ExpressionSource expr); + + + /** + * Returns the expressions used to update the iterator variables + * @return An immutable collection holding the expressions + */ + public List,?>> getUpdates(); + + /** + * Adds an update expression + * @param expression the expression to be added + * @return this ForStatement itself + */ + public ForStatement addUpdate(ExpressionSource expression); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/IfStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/IfStatement.java new file mode 100644 index 00000000..4d0872e0 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/IfStatement.java @@ -0,0 +1,80 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents an if statement in source format + * + * @author Davide Sottara + */ +public interface IfStatement, + P extends BlockHolder> + extends ControlFlowStatement>, + BodyHolder> +{ + + /** + * Returns the conditional expression evaluated by this if statement + * @return the conditional expression evaluated by this if statement + */ + public ExpressionSource,?> getCondition(); + + /** + * Sets the condition expression evaluated by this if statement + * @param expr The condition expression + * @return this IfStatement itself + */ + public IfStatement setCondition(ExpressionSource expr); + + + /** + * Returns the body of the then part + * @return the body of the then part + */ + public BlockStatement> getThen(); + + /** + * Sets the body of the then part from a single statement, wrapping it inside a block + * @param ifBlock The statement that will become the body + * @return this IfStatement itself + */ + public IfStatement setThen(StatementSource ifBlock); + + /** + * Sets the body of the then part from a whole block + * @param ifBlock The block containing the body statements + * @return this IfStatement itself + */ + public IfStatement setThen(BlockSource ifBlock); + + /** + * Returns the body of the else part + * @return the body of the else part + */ + public BlockStatement> getElse(); + + /** + * Sets the body of the else part from a single statement, wrapping it inside a block + * @param elseBlock The statement that will become the body + * @return this IfStatement itself + */ + public IfStatement setElse(StatementSource elseBlock); + + /** + * Sets the body of the else part from a whole block + * @param elseBlock The block containing the body statements + * @return this IfStatement itself + */ + public IfStatement setElse(BlockSource elseBlock); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/InvokeStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/InvokeStatement.java new file mode 100644 index 00000000..c91c3c99 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/InvokeStatement.java @@ -0,0 +1,68 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.expressions.MethodWithArgumentsInvokeExpression; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +/** + * Represents a method invocation expression statement in source format + * + * @author Davide Sottara + */ +public interface InvokeStatement, + P extends BlockHolder> + extends ExpressionStatement> +{ + + /** + * Returns the name of the method being invoked + * @return the name of the method + */ + public String getMethodName(); + + /** + * Sets the name of the method to be invoked + * @param method The name of the method + * @return this InvokeStatement itself + */ + public InvokeStatement setMethodName(String method); + + /** + * The expression returning the object on which this InvokeStatement is invoked + * @return The expression returning the object on which this InvokeStatement is invoked + */ + public Accessor,?>,?> getInvocationTarget(); + + /** + * Sets the expression returning the object on which this InvokeStatement is invoked + * @param target The expression returning the object on which this InvokeStatement is invoked + * @return This InvokeStatement itself + */ + public InvokeStatement setInvocationTarget(Accessor target); + + /** + * Returns the current list of arguments + * @return An immutable list containing the arguments + */ + public List,?>,?>> getArguments(); + + /** + * Adds an argument to the method + * @param argument The argument to be added + * @return The InvokeStatement itself + */ + public InvokeStatement addArgument(Argument argument); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/LoopingStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/LoopingStatement.java new file mode 100644 index 00000000..997ad0b1 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/LoopingStatement.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract interface that represents a loop statement in source format + * + * @author Davide Sottara + */ +public interface LoopingStatement, + P extends BlockHolder, + S extends LoopingStatement> + extends ControlFlowStatement +{ +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ReturnStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ReturnStatement.java new file mode 100644 index 00000000..ba3dc91a --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ReturnStatement.java @@ -0,0 +1,40 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents a return statement in source format + * + * @author Davide Sottara + */ +public interface ReturnStatement, + P extends BlockHolder> + extends BranchingStatement>, + ExpressionHolder +{ + + /** + * Returns the expression used to compute the return value + * @return the expression used to compute the return value + */ + public ExpressionSource,?> getReturn(); + + /** + * Sets the expression used to compute the value returned by the statement + * @param expr the return value expression + * @return this ReturnStatement + */ + public ReturnStatement setReturn(ExpressionSource expr); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/Statement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/Statement.java new file mode 100644 index 00000000..f0a9fbcb --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/Statement.java @@ -0,0 +1,49 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.JavaType; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract interface that represents a java statement + * + * @author Davide Sottara + */ +public interface Statement, + P extends BlockHolder, + S extends Statement> + extends Origin

, + ExpressionHolder +{ + + public void setOrigin(P origin); + + /** + * Assigns a label to the statement + * @param label the label + * @return This Statement itself + */ + public S setLabel(String label); + + /** + * Returns the label for this statement, if any + * @return the label associated to the statement + */ + public String getLabel(); + + /** + * Checks whether a label has been associated to the statement + * @return true if the statement has a label, false otherwise + */ + public boolean hasLabel(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/StatementSource.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/StatementSource.java new file mode 100644 index 00000000..9e5671af --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/StatementSource.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Abstract interface that represents a java statement in source format + * + * @author Davide Sottara + */ +public interface StatementSource, + P extends BlockHolder, + S extends StatementSource> + extends Statement +{ +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/Statements.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/Statements.java new file mode 100644 index 00000000..0f050425 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/Statements.java @@ -0,0 +1,269 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.spi.StatementFactory; + +import java.util.ServiceLoader; + +public abstract class Statements +{ + + protected static StatementFactory factory; + + @SuppressWarnings("unchecked") + protected static > StatementFactory getFactory() + { + synchronized (Statements.class) + { + ServiceLoader sl = ServiceLoader.load(StatementFactory.class, Statements.class.getClassLoader()); + if (sl.iterator().hasNext()) + { + factory = sl.iterator().next(); + } else + { + throw new IllegalStateException("No StatementFactory implementation available, unable to continue"); + } + } + return factory; + } + + /** + * Creates a return statement + * @param the Java Source type + * @return a new, empty return statement + */ + @SuppressWarnings("unchecked") + public static > ReturnStatement newReturn() + { + return (ReturnStatement) getFactory().newReturn(); + } + + /** + * Creates an if statement + * @param the Java Source type + * @return a new, empty if statement + */ + @SuppressWarnings("unchecked") + public static > IfStatement> newIf() + { + return (IfStatement>) getFactory().newIf(); + } + + /** + * Creates an variable assignment statement + * @param the Java Source type + * @return a new, empty assign statement + */ + @SuppressWarnings("unchecked") + public static > AssignmentStatement> newAssign() + { + return (AssignmentStatement>) getFactory().newAssign(); + } + + /** + * Creates a variable declaration statement + * @param the Java Source type + * @return a new, empty declaration statement + */ + @SuppressWarnings("unchecked") + public static > DeclareStatement> newDeclare() + { + return (DeclareStatement>) getFactory().newDeclare(); + } + + /** + * Creates a for loop statement + * @param the Java Source type + * @return a new, empty for statement + */ + @SuppressWarnings("unchecked") + public static > ForStatement> newFor() + { + return (ForStatement>) getFactory().newFor(); + } + + /** + * Creates an enhanced for loop statement + * @param the Java Source type + * @return a new, emtpy enhanced for statement + */ + @SuppressWarnings("unchecked") + public static > ForEachStatement> newForEach() + { + return (ForEachStatement>) getFactory().newForEach(); + } + + /** + * Creates a specialized expression statement, which invokes a method + * @param the Java Source type + * @return a new, empty method invocation expression statement + */ + @SuppressWarnings("unchecked") + public static > InvokeStatement> newInvoke() + { + return (InvokeStatement>) getFactory().newInvoke(); + } + + /** + * Creates a while do loop statement + * @param the Java Source type + * @return a new, empty while statement + */ + @SuppressWarnings("unchecked") + public static > WhileStatement> newWhile() + { + return (WhileStatement>) getFactory().newWhile(); + } + + /** + * Creates a do while loop statement + * @param the Java Source type + * @return a new, empty do statement + */ + @SuppressWarnings("unchecked") + public static > DoWhileStatement> newDoWhile() + { + return (DoWhileStatement>) getFactory().newDoWhile(); + } + + /** + * Creates a block statement + * @param the Java Source type + * @return a new, empty block statement + */ + @SuppressWarnings("unchecked") + public static > BlockStatement> newBlock() + { + return (BlockStatement>) getFactory().newBlock(); + } + + /** + * Creates a break statement + * @param the Java Source type + * @return a new, empty continue statement + */ + @SuppressWarnings("unchecked") + public static > BreakStatement> newBreak() + { + return (BreakStatement>) getFactory().newBreak(); + } + + /** + * Creates a continue statement + * @param the Java Source type + * @return a new, empty continue statement + */ + @SuppressWarnings("unchecked") + public static > ContinueStatement> newContinue() + { + return (ContinueStatement>) getFactory().newContinue(); + } + + /** + * Creates a throw statement + * @param the Java Source type + * @return a new, empty throw statement + */ + @SuppressWarnings("unchecked") + public static > ThrowStatement> newThrow() + { + return (ThrowStatement>) getFactory().newThrow(); + } + + /** + * Creates a try .. catch .. finally statement + * @param the Java Source type + * @return a new, empty try statement + */ + @SuppressWarnings("unchecked") + public static > TryCatchStatement> newTryCatch() + { + return (TryCatchStatement>) getFactory().newTryCatch(); + } + + /** + * Creates a new synchronized block statement + * @param the Java Source type + * @return a new, empty synchronized statement + */ + @SuppressWarnings("unchecked") + public static > SynchStatement> newSynchronized() + { + return (SynchStatement>) getFactory().newSynchronized(); + } + + /** + * Creates a local constructor call statement + * @param the Java Source type + * @return a new, empty constructor call statement + */ + @SuppressWarnings("unchecked") + public static > ThisStatement> newThis() + { + return (ThisStatement>) getFactory().newThis(); + } + + /** + * Creates a super constructor call statement + * @param the Java Source type + * @return a new, empty super constructor call statement + */ + @SuppressWarnings("unchecked") + public static > SuperStatement> newSuper() + { + return (SuperStatement>) getFactory().newSuper(); + } + + /** + * Creates a new assert statement + * @param the Java Source type + * @return a new, empty assert statement + */ + @SuppressWarnings("unchecked") + public static > AssertStatement> newAssert() + { + return (AssertStatement>) getFactory().newAssert(); + } + + /** + * Creates a new switch statement + * @param the Java Source type + * @return a new, empty switch statement + */ + @SuppressWarnings("unchecked") + public static > SwitchStatement> newSwitch() + { + return (SwitchStatement>) getFactory().newSwitch(); + } + + /** + * Creates a new expression evaluation statement + * @param the Java Source type + * @return a new, empty expression statement + */ + @SuppressWarnings("unchecked") + public static > EvalExpressionStatement> newEval() + { + return (EvalExpressionStatement>) getFactory().newEval(); + } + + /** + * Creates a case statement to be used within a switch statement + * @param the Java Source type + * @return a new, empty case statement + */ + @SuppressWarnings("unchecked") + public static > SwitchCaseStatement>> newCase() + { + return (SwitchCaseStatement>>) getFactory().newCase(); + } + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/SuperStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/SuperStatement.java new file mode 100644 index 00000000..278a0450 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/SuperStatement.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +/** + * Represents an super constructor invocation statement in source format + * + * @author Davide Sottara + */ +public interface SuperStatement, + P extends BlockHolder> + extends StatementSource> +{ + + /** + * Returns the current list of arguments + * @return An immutable list containing the arguments + */ + public List,?>> getArguments(); + + /** + * Adds an argument to the super constructor invocation + * @param argument The argument to be added + * @return The SuperStatement itself + */ + public SuperStatement addArgument(Argument argument); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchCaseStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchCaseStatement.java new file mode 100644 index 00000000..92afd195 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchCaseStatement.java @@ -0,0 +1,37 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents a case statement in source format, to be used with a switch statement + * + * @author Davide Sottara + */ +public interface SwitchCaseStatement, + P extends SwitchStatement>> + extends StatementSource> +{ + + /** + * Returns the expression evaluated in this case + * @return the expression evaluated in this case + */ + public ExpressionSource,?> getCaseExpression(); + + /** + * Sets the expression for this case + * @param src the case expression + * @return this SwitchCaseStatement itself + */ + public SwitchCaseStatement setCaseExpression(ExpressionSource src); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchStatement.java new file mode 100644 index 00000000..8e713154 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchStatement.java @@ -0,0 +1,83 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +/** + * Represents a switch statement in source format + * + * @author Davide Sottara + */ +public interface SwitchStatement, + P extends BlockHolder> + extends ControlFlowStatement>, + BodyHolder> +{ + + /** + * Returns a list containing all the individual case expressions in this statement + * @return + */ + public List,?>> getCaseExpressions(); + + /** + * Adds a case expression to the statement. + * It will be wrapped internally with a SwitchCaseStatement + * @param option the case expression + * @return this SwitchStatement itself + */ + public SwitchStatement addCase(ExpressionSource option); + + /** + * Adds a case statement, wrapping a case expression + * @param option the case statement + * @return this SwitchStatement itself + */ + public SwitchStatement addCase(SwitchCaseStatement option); + + /** + * Adds the "default" case statement + * @return this SwitchStatement itself + */ + public SwitchStatement addDefault(); + + + /** + * Returns all the code statements in the body of this switch, including the SwitchCaseStatement, + * appropriately interleaved + * @return An immutable list with the statements in the body of this SwitchStatement + */ + public List,?>> getStatements(); + + /** + * Adds a code statement to the body of this SwitchStatement + * @param arg the statement to be added + * @return this SwitchStatement itself + */ + public SwitchStatement addStatement(StatementSource arg); + + + /** + * Returns the expression returning the value on which the cases are evaluated + * @return the expression on which the cases are evaluated + */ + public ExpressionSource,?> getSwitch(); + + /** + * Sets the switch expression + * @param expr the expression on which the cases are evaluated + * @return this SwitchStatement itself + */ + public SwitchStatement setSwitch(ExpressionSource expr); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/SynchStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/SynchStatement.java new file mode 100644 index 00000000..9cbc4083 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/SynchStatement.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents a synchronized block statement in source format + * + * @author Davide Sottara + */ +public interface SynchStatement, + P extends BlockHolder> + extends ControlFlowStatement>, + BodyHolder> +{ + + /** + * Returns the expression that evaluates to the lock object + * @return the expression that evaluates to the lock object + */ + public ExpressionSource,?> getSynchOn(); + + + /** + * Sets the expression that evaluates to the lock object + * @param expr the expression returning the lock object + * @return this SynchStatement itself + */ + public SynchStatement setSynchOn(ExpressionSource expr); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ThisStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ThisStatement.java new file mode 100644 index 00000000..e55fb3d3 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ThisStatement.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +/** + * Represents a constructor call statement in source format + * + * @author Davide Sottara + */ +public interface ThisStatement, + P extends BlockHolder> + extends StatementSource> +{ + + /** + * Returns the current list of arguments + * @return An immutable list containing the arguments + */ + public List,?>> getArguments(); + + /** + * Adds an argument to the constructor invocation + * @param argument The argument to be added + * @return The ThisStatement itself + */ + public ThisStatement addArgument(Argument argument); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ThrowStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ThrowStatement.java new file mode 100644 index 00000000..8bc4ae10 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ThrowStatement.java @@ -0,0 +1,37 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents a throw statement in source format + * + * @author Davide Sottara + */ +public interface ThrowStatement, + P extends BlockHolder> + extends ControlFlowStatement> +{ + + /** + * Returns the expression that evaluates to the throwable object + * @return the expression that evaluates to the throwable object + */ + public ExpressionSource,?> getThrowable(); + + /** + * Sets the expression that evaluates to the throwable object + * @param expr the expression returning the throwable object + * @return this Thr itself + */ + public ThrowStatement setThrowable(ExpressionSource expr); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/TryCatchStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/TryCatchStatement.java new file mode 100644 index 00000000..4a7ff671 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/TryCatchStatement.java @@ -0,0 +1,72 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.Map; + +/** + * Represents a try catch statement in source format + * + * @author Davide Sottara + */ +public interface TryCatchStatement, + P extends BlockHolder> + extends ControlFlowStatement>, + BodyHolder> +{ + + /** + * Returns a map that associates each catch-ed throwable declaration to its handling block + * @return an immutable map associating the declarations of the throwables to their handling blocks + */ + public Map>,BlockStatement>> getCatches(); + + /** + * Adds a catch block to the statement, associating it to a throwable declaration. + * @param declaration The declaration of the Throwable to catch + * @param block The catch block + * @return this TryCatchStatement itself + */ + public TryCatchStatement addCatch(DeclareExpression declaration, + BlockStatement block); + + /** + * Adds a catch block to the statement, associating it to a throwable declaration. + * @param declaration The declaration of the Throwable to catch + * @param block A single statement which will become the catch block + * @return this TryCatchStatement itself + */ + public TryCatchStatement addCatch(DeclareExpression declaration, + StatementSource block); + + + /** + * Returns the body of the finally part + * @return the body of the finally part + */ + public BlockStatement> getFinally(); + + /** + * Sets the body of the finally part from a whole block + * @param block The block containing the body statements + * @return this TryCatchStatement itself + */ + public TryCatchStatement setFinally(BlockStatement block); + + /** + * Sets the body of the finally part from a single statement, wrapping it inside a block + * @param block The statement that will become the body + * @return this TryCatchStatement itself + */ + public TryCatchStatement setFinally(StatementSource block); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/WhileStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/WhileStatement.java new file mode 100644 index 00000000..77637aaa --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/WhileStatement.java @@ -0,0 +1,38 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * Represents a while loop statement in source format + * + * @author Davide Sottara + */ +public interface WhileStatement, + P extends BlockHolder> + extends LoopingStatement>, + BodyHolder> +{ + + /** + * Returns the conditional expression evaluated by this while statement + * @return the conditional expression evaluated by this while statement + */ + public ExpressionSource,?> getCondition(); + + /** + * Sets the condition expression evaluated by this while statement + * @param expr The condition expression + * @return this While itself + */ + public WhileStatement setCondition(ExpressionSource expr); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactory.java b/api/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactory.java new file mode 100644 index 00000000..828d3b1c --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactory.java @@ -0,0 +1,28 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.spi; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.AccessBuilder; +import org.jboss.forge.roaster.model.expressions.BasicExpressionFactory; +import org.jboss.forge.roaster.model.expressions.NonPrimitiveExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +/** + * This interface supports the creation of expressions to be used in statements + * @param + * @see org.jboss.forge.roaster.spi.StatementFactory + */ +public interface ExpressionFactory, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends AccessBuilder, + BasicExpressionFactory { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/spi/StatementFactory.java b/api/src/main/java/org/jboss/forge/roaster/spi/StatementFactory.java new file mode 100644 index 00000000..94029cb2 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/spi/StatementFactory.java @@ -0,0 +1,165 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.spi; + +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.AssertStatement; +import org.jboss.forge.roaster.model.statements.AssignmentStatement; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.BreakStatement; +import org.jboss.forge.roaster.model.statements.EvalExpressionStatement; +import org.jboss.forge.roaster.model.statements.ContinueStatement; +import org.jboss.forge.roaster.model.statements.DeclareStatement; +import org.jboss.forge.roaster.model.statements.DoWhileStatement; +import org.jboss.forge.roaster.model.statements.ForEachStatement; +import org.jboss.forge.roaster.model.statements.ForStatement; +import org.jboss.forge.roaster.model.statements.IfStatement; +import org.jboss.forge.roaster.model.statements.InvokeStatement; +import org.jboss.forge.roaster.model.statements.ReturnStatement; +import org.jboss.forge.roaster.model.statements.SuperStatement; +import org.jboss.forge.roaster.model.statements.SwitchCaseStatement; +import org.jboss.forge.roaster.model.statements.SwitchStatement; +import org.jboss.forge.roaster.model.statements.SynchStatement; +import org.jboss.forge.roaster.model.statements.ThisStatement; +import org.jboss.forge.roaster.model.statements.ThrowStatement; +import org.jboss.forge.roaster.model.statements.TryCatchStatement; +import org.jboss.forge.roaster.model.statements.WhileStatement; + +/** + * This interface supports the creation of statements to be used in a source code block + * @param + * @see org.jboss.forge.roaster.spi.ExpressionFactory + */ +public interface StatementFactory> { + + /** + * Creates a return statement + * @return A new return statement + */ + public ReturnStatement newReturn(); + + /** + * Creates a variable assignment statement + * @return A new variable assignment statement + */ + public AssignmentStatement newAssign(); + + /** + * Creates a variable declaration statement + * @return A new variable declaration statement + */ + public DeclareStatement newDeclare(); + + /** + * Creates a method invocation statement + * @return A new method invocation expression statement + */ + public InvokeStatement newInvoke(); + + /** + * Creates an if statement + * @return A new if statement + */ + public IfStatement newIf(); + + /** + * Creates a while loop statement + * @return A new while loop statement + */ + public WhileStatement newWhile(); + + /** + * Creates a for loop statement + * @return A new for loop statement + */ + public ForStatement newFor(); + + /** + * Creates an enhanced for loop statement + * @return A new enhanced for loop statement + */ + public ForEachStatement newForEach(); + + /** + * Creates a do while loop statement + * @return A new while loop statement + */ + public DoWhileStatement newDoWhile(); + + /** + * Creates a break statement + * @return A new break statement + */ + public BreakStatement newBreak(); + + /** + * Creates a continue statement + * @return A new continue statement + */ + public ContinueStatement newContinue(); + + /** + * Creates a throw statement + * @return A new throw statement + */ + public ThrowStatement newThrow(); + + /** + * Creates a try-catch statement + * @return A new try-catch statement + */ + public TryCatchStatement newTryCatch(); + + /** + * Creates a synchronized block statement + * @return A new synchronized block statement + */ + public SynchStatement newSynchronized(); + + /** + * Creates a constructor call statement + * @return A new constructor call statement + */ + public ThisStatement newThis(); + + /** + * Creates a super constructor call statement + * @return A new super constructor call statement + */ + public SuperStatement newSuper(); + + /** + * Creates an assert statement + * @return A new assert statement + */ + public AssertStatement newAssert(); + + /** + * Creates a switch statement + * @return A new switch case statement + */ + public SwitchStatement newSwitch(); + + /** + * Creates an expression statement + * @return A new expression statement + */ + public EvalExpressionStatement newEval(); + + /** + * Creates a block statement + * @return A new block statement + */ + public BlockStatement newBlock(); + + /** + * Creates a case element for a switch statement + * @return A new switch-case element + */ + public SwitchCaseStatement newCase(); +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/ASTNode.java b/impl/src/main/java/org/jboss/forge/roaster/model/ASTNode.java new file mode 100644 index 00000000..16b5bd57 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/ASTNode.java @@ -0,0 +1,19 @@ +package org.jboss.forge.roaster.model; + + +import org.eclipse.jdt.core.dom.AST; + +public interface ASTNode { + + AST getAst(); + + J materialize( AST ast ); + + J getInternal(); + + void setInternal(J jdtNode); + + boolean isMaterialized(); + + void setAst( AST ast ); +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/BlockImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/BlockImpl.java new file mode 100644 index 00000000..cfad292c --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/BlockImpl.java @@ -0,0 +1,61 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.LabeledStatement; +import org.eclipse.jdt.core.dom.Statement; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.impl.statements.JdtStatementWrapper; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + + +public abstract class BlockImpl, + P extends BlockHolder, + B extends Block> + extends NodeImpl + implements Block, + JdtStatementWrapper { + + protected org.eclipse.jdt.core.dom.Block jdtBlock; + + @Override + public boolean isMaterialized() { + return getInternal() != null; + } + + @Override + public org.eclipse.jdt.core.dom.Block getInternal() { + return jdtBlock; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.Block jdtNode) { + super.setInternal(jdtNode); + this.jdtBlock = jdtNode; + } + + public org.eclipse.jdt.core.dom.Statement wireAndGetStatement( org.jboss.forge.roaster.model.statements.Statement statement, B parent, AST ast ) { + ASTNode node = (ASTNode) statement; + statement.setOrigin( parent ); + node.setAst( ast ); + Statement stat = node.materialize( ast ); + + if ( statement.hasLabel() ) { + LabeledStatement labeledStatement = ast.newLabeledStatement(); + labeledStatement.setBody( stat ); + labeledStatement.setLabel( ast.newSimpleName( statement.getLabel() ) ); + return labeledStatement; + } else { + return stat; + } + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JDTHelper.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JDTHelper.java index 63d6c277..d489639b 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JDTHelper.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JDTHelper.java @@ -6,19 +6,23 @@ */ package org.jboss.forge.roaster.model.impl; +import java.util.Arrays; import java.util.List; +import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ArrayType; import org.eclipse.jdt.core.dom.BodyDeclaration; import org.eclipse.jdt.core.dom.EnumDeclaration; import org.eclipse.jdt.core.dom.ParameterizedType; import org.eclipse.jdt.core.dom.PrimitiveType; import org.eclipse.jdt.core.dom.QualifiedType; +import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.SimpleType; import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; import org.eclipse.jdt.core.dom.Type; import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.jdt.core.dom.WildcardType; +import org.jboss.forge.roaster.model.util.Types; /** * Help with Eclipse JDT common operations. @@ -86,4 +90,79 @@ public static List getInterfaces(final BodyDeclaration dec) return (List) dec.getStructuralProperty(desc); } + public static Type getType( Class klass, AST ast ) + { + return getType( klass.getName(), ast ); + } + + public static Type getType( String klass, AST ast ) + { + if ( Types.isPrimitive( klass ) ) { + return ast.newPrimitiveType( PrimitiveType.toCode( klass ) ); + } else if ( Types.isGeneric( klass ) ) { + String base = Types.stripGenerics( klass ); + ParameterizedType param = ast.newParameterizedType( getType( base, ast ) ); + for (String typeP : Types.getGenericsTypeParameter( klass ).split(",")) + { + param.typeArguments().add( getType( typeP, ast ) ); + } + return param; + } else if ( Types.isArray( klass ) ) { + String base = Types.stripArray( klass ); + return ast.newArrayType( getType( base, ast ), Types.getArrayDimension( klass ) ); + } else if ( Types.isQualified( klass ) ) { + return ast.newQualifiedType( ast.newSimpleType( ast.newName( Types.getPackage( klass ) ) ), ast.newSimpleName( Types.toSimpleName( klass ) ) ); + } else if ( Types.isSimpleName( klass ) ) { + return ast.newSimpleType( ast.newName( klass ) ); + } + return null; + } + + public static String getter( String fieldName, String type ) { + if ( boolean.class.getName().equals( type ) ) { + return "is" + capitalize( fieldName ); + } + return "get" + capitalize( fieldName ); + } + + public static String setter( String fieldName, String type ) { + return "set" + capitalize( fieldName ); + } + + public static String capitalize( String fieldName ) { + return fieldName.substring( 0, 1 ).toUpperCase() + fieldName.substring( 1 ); + } + + public static String fieldForGetter( String name ) { + if ( name.startsWith( "get" ) ) { + return firstCharToLowerCase( name.substring( 3 ) ); + } else if ( name.startsWith( "is" ) ) { + return firstCharToLowerCase( name.substring( 2 ) ); + } else { + return name; + } + } + + public static String fieldForSetter( String name ) { + if ( name.startsWith( "set" ) ) { + return firstCharToLowerCase( name.substring( 3 ) ); + } else { + return name; + } + } + + public static String firstCharToLowerCase( String word ) { + if ( word.length() <= 1 ) { + return word.toLowerCase(); + } + return word.substring( 0, 1 ).toLowerCase() + word.substring( 1 ); + } + + public static boolean isGetter( String name, List arguments ) { + return name.startsWith( "get" ) || name.startsWith( "is" ) && arguments.isEmpty(); + } + + public static boolean isSetter( String name, List arguments ) { + return name.startsWith( "set" ) && arguments.size() == 1; + } } diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaClassImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaClassImpl.java index 964dc198..d5f31b3e 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaClassImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaClassImpl.java @@ -182,4 +182,10 @@ else if (Types.isGeneric(type)) return this; } + + @Override + public JavaClassSource asJavaClassSource() + { + return this; + } } \ No newline at end of file diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java index a08b57af..a45e54a7 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java @@ -6,11 +6,6 @@ */ package org.jboss.forge.roaster.model.impl; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.Block; @@ -31,21 +26,36 @@ import org.jboss.forge.roaster.model.Visibility; import org.jboss.forge.roaster.model.ast.AnnotationAccessor; import org.jboss.forge.roaster.model.ast.ModifierAccessor; +import org.jboss.forge.roaster.model.impl.statements.BlockStatementImpl; +import org.jboss.forge.roaster.model.impl.statements.InnerBlockImpl; +import org.jboss.forge.roaster.model.impl.statements.JdtStatementWrapper; +import org.jboss.forge.roaster.model.impl.statements.StatementBuilder; import org.jboss.forge.roaster.model.source.AnnotationSource; import org.jboss.forge.roaster.model.source.Import; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; import org.jboss.forge.roaster.model.source.JavaClassSource; import org.jboss.forge.roaster.model.source.JavaDocSource; import org.jboss.forge.roaster.model.source.JavaSource; import org.jboss.forge.roaster.model.source.MethodSource; import org.jboss.forge.roaster.model.source.ParameterSource; import org.jboss.forge.roaster.model.source.TypeVariableSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.Statements; import org.jboss.forge.roaster.model.util.Strings; import org.jboss.forge.roaster.model.util.Types; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + + /** * @author Lincoln Baxter, III */ -public class MethodImpl> implements MethodSource +public class MethodImpl> implements MethodSource, BlockHolder { private final AnnotationAccessor> annotations = new AnnotationAccessor>(); private final ModifierAccessor modifiers = new ModifierAccessor(); @@ -55,6 +65,8 @@ public class MethodImpl> implements MethodSource private CompilationUnit cu = null; private final MethodDeclaration method; + private BlockStatement> body; + private void init(final O parent) { this.parent = parent; @@ -83,8 +95,7 @@ public MethodImpl(final O parent, final String method) JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List> methods = temp.getMethods(); MethodDeclaration newMethod = (MethodDeclaration) methods.get(0).getInternal(); - MethodDeclaration subtree = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod); - this.method = subtree; + this.method = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod); } @Override @@ -132,13 +143,13 @@ public AnnotationSource addAnnotation(final Class addAnnotation(final String className) { - return annotations.addAnnotation(this, method, className); + return annotations.addAnnotation( this, method, className ); } @Override public List> getAnnotations() { - return annotations.getAnnotations(this, method); + return annotations.getAnnotations( this, method ); } @Override @@ -150,13 +161,13 @@ public boolean hasAnnotation(final Class removeAnnotation(final Annotation annotation) { - return annotations.removeAnnotation(this, method, annotation); + return annotations.removeAnnotation( this, method, annotation ); } @Override @@ -212,7 +223,7 @@ public MethodSource setBody(final String body) Block block = ((MethodDeclaration) methods.get(0).getInternal()).getBody(); block = (Block) ASTNode.copySubtree(method.getAST(), block); - method.setBody(block); + method.setBody( block ); } return this; } @@ -286,7 +297,7 @@ public MethodSource setReturnType(final String typeName) @Override public MethodSource setReturnType(final JavaType type) { - return setReturnType(type.getName()); + return setReturnType( type.getName() ); } @Override @@ -390,7 +401,7 @@ public MethodSource setNative(boolean value) @Override public boolean isNative() { - return modifiers.hasModifier(method, ModifierKeyword.NATIVE_KEYWORD); + return modifiers.hasModifier( method, ModifierKeyword.NATIVE_KEYWORD ); } @Override @@ -504,13 +515,13 @@ public MethodSource setProtected() @Override public Visibility getVisibility() { - return Visibility.getFrom(this); + return Visibility.getFrom( this ); } @Override public MethodSource setVisibility(final Visibility scope) { - return Visibility.set(this, scope); + return Visibility.set( this, scope ); } /* @@ -721,13 +732,13 @@ public MethodSource removeTypeVariable(String name) @Override public MethodSource removeTypeVariable(TypeVariable typeVariable) { - return removeTypeVariable(typeVariable.getName()); + return removeTypeVariable( typeVariable.getName() ); } @Override public ParameterSource addParameter(Class type, String name) { - return addParameter(type.getCanonicalName(), name); + return addParameter( type.getCanonicalName(), name ); } @Override @@ -834,6 +845,7 @@ public boolean hasJavaDoc() public MethodSource removeJavaDoc() { method.setJavadoc(null); + return this; } @@ -848,4 +860,45 @@ public JavaDocSource> getJavaDoc() } return new JavaDocImpl>(this, javadoc); } + + + @Override + public MethodSource setBody( BlockSource statement ) + { + this.body = (BlockStatement>) statement; + + ( (BlockStatement>) statement ).setOrigin( this ); + JdtStatementWrapper,?> wrapper = (JdtStatementWrapper, ?>) body; + wrapper.setAst( cu.getAST() ); + Statement jdtStatement = wrapper.materialize( cu.getAST() ); + + method.setBody((Block) jdtStatement); + return this; + } + + @Override + public MethodSource setBody( StatementSource statement ) + { + BlockStatement> block = new BlockStatementImpl>(); + setBody( (BlockSource, ?>) block ); + block.addStatement( statement ); + return this; + } + + + public BlockSource,?> getBodyAsBlock() + { + if (body == null) { + body = new BlockStatementImpl(); + ((org.jboss.forge.roaster.model.ASTNode)body).setInternal(method.getBody()); + for (Object statement : method.getBody().statements()) { + StatementSource stat = StatementBuilder.asRoasterStatement((Statement) statement); + if (stat != null) { + body.addStatement(stat); + } + } + } + return body; + } + } diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/NodeImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/NodeImpl.java new file mode 100644 index 00000000..df4fe72d --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/NodeImpl.java @@ -0,0 +1,78 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.LabeledStatement; +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Expression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.expressions.JdtExpressionWrapper; +import org.jboss.forge.roaster.model.impl.statements.JdtStatementWrapper; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.Statement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.Statements; + +public abstract class NodeImpl, + T, + J extends org.eclipse.jdt.core.dom.ASTNode> + implements Origin, + ASTNode { + + protected T origin; + protected AST ast; + protected String label; + + @Override + public T getOrigin() { + return origin; + } + + public void setOrigin( T origin ) { + this.origin = origin; + } + + public AST getAst() { + return ast; + } + + public void setAst( AST ast ) { + this.ast = ast; + } + + public String getLabel() { + return label; + } + + public boolean hasLabel() { + return label != null && label.length() > 0; + } + + @Override + public void setInternal(J jdtNode) { + setAst(jdtNode.getAST()); + } + + @Override + public String toString() { + return getInternal() != null ? getInternal().toString() : ""; + } + + @Override + public boolean isMaterialized() { + return getInternal() != null; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/ParameterImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/ParameterImpl.java index f8d01e70..da680d5c 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/ParameterImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/ParameterImpl.java @@ -17,6 +17,7 @@ import org.jboss.forge.roaster.model.ast.ModifierAccessor; import org.jboss.forge.roaster.model.source.AnnotationSource; import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.source.MethodSource; import org.jboss.forge.roaster.model.source.ParameterSource; /** diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AbstractMethodInvokeImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AbstractMethodInvokeImpl.java new file mode 100644 index 00000000..ef773d5e --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AbstractMethodInvokeImpl.java @@ -0,0 +1,66 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ArgumentHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public abstract class AbstractMethodInvokeImpl, + P extends ExpressionHolder, + M extends MethodInvokeExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends InvokeableImpl + implements MethodInvokeExpression, + ArgumentHolder +{ + + protected List> arguments = Collections.EMPTY_LIST; + + public AbstractMethodInvokeImpl(String method) + { + super(method); + } + + @Override + public M addArgument(Argument argument) + { + if (arguments.isEmpty()) + { + arguments = new LinkedList>(); + } + arguments.add((Argument) argument); + return (M) this; + } + + @Override + public ExpressionSource getInvocationTarget() + { + return this.target; + } + + @Override + public M setInvocationTarget(ExpressionSource target) + { + this.target = (ExpressionSource) target; + return (M) this; + } + + @Override + public List> getArguments() + { + return Collections.unmodifiableList(arguments); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AccessorImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AccessorImpl.java new file mode 100644 index 00000000..1380c93c --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AccessorImpl.java @@ -0,0 +1,64 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.ArrayIndexer; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.NonPrimitiveExpression; +import org.jboss.forge.roaster.model.expressions.Setter; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class AccessorImpl, + P extends ExpressionHolder, + E extends NonPrimitiveExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends NonPrimitiveExpressionImpl + implements Accessor +{ + + public Field field(String fieldName) + { + return new DotAccessorImpl(this).field(fieldName); + } + + public Getter getter(String field, String klass) + { + return new DotAccessorImpl(this).getter(field, klass); + } + + public Getter getter(String field, Class klass) + { + return new DotAccessorImpl(this).getter(field, klass); + } + + public Setter setter(String fldName, String type, ExpressionSource value) + { + return new DotAccessorImpl(this).setter(fldName, type, value); + } + + public Setter setter(String fldName, Class type, ExpressionSource value) + { + return new DotAccessorImpl(this).setter(fldName, type, value); + } + + public MethodCallExpression invoke(String method) + { + return new DotAccessorImpl(this).invoke(method); + } + + @Override + public ArrayIndexer itemAt(ExpressionSource index) + { + return new DotAccessorImpl(this).itemAt(index); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayAccessImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayAccessImpl.java new file mode 100644 index 00000000..6a48abd4 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayAccessImpl.java @@ -0,0 +1,96 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ArrayAccess; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ArrayIndexer; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class ArrayAccessImpl, + P extends ExpressionHolder> + extends SimpleAccessorImpl,ArrayAccess> + implements ArrayIndexer, + ExpressionChainLink> +{ + + private ExpressionSource,?> index; + private ExpressionSource,?> target; + private ArrayAccess axx; + + public ArrayAccessImpl(ExpressionSource index) + { + ExpressionSource,?> cast = (ExpressionSource,?>) index; + this.index = cast; + } + + @Override + public ArrayAccess materialize(AST ast) + { + if (isMaterialized()) + { + return axx; + } + axx = ast.newArrayAccess(); + axx.setIndex(wireAndGetExpression(index, this, ast)); + if (target != null) + { + axx.setArray(wireAndGetExpression(target, this, ast)); + } + return axx; + } + + @Override + public ArrayAccess getInternal() + { + return axx; + } + + @Override + public void setInternal(ArrayAccess jdtNode) + { + super.setInternal(jdtNode); + this.axx = jdtNode; + } + + @Override + public ExpressionSource,?> chainExpression(ExpressionSource child) + { + ExpressionSource,?> cast = (ExpressionSource,?>) child; + this.target = cast; + return cast; + } + + @Override + public ExpressionSource,?> getIndex() + { + return index; + } + + @Override + public ArrayIndexer setIndex(ExpressionSource index) + { + this.index = (ExpressionSource,?>) index; + return this; + } + + @Override + public ExpressionSource,?> getInvocationTarget() + { + return target; + } + + @Override + public ArrayIndexer setInvocationTarget(ExpressionSource target) + { + this.target = (ExpressionSource,?>) target; + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayImpl.java new file mode 100644 index 00000000..3ddf5698 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayImpl.java @@ -0,0 +1,119 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ArrayCreation; +import org.eclipse.jdt.core.dom.ArrayInitializer; +import org.eclipse.jdt.core.dom.ArrayType; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ArrayConstructorExpression; +import org.jboss.forge.roaster.model.expressions.ArrayInit; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + + +public class ArrayImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,ArrayCreation> + implements ArrayConstructorExpression +{ + + private ArrayCreation array; + + private String type; + private List,?>> dims = Collections.EMPTY_LIST; + private ArrayInit> init; + + public ArrayImpl(String type) + { + this.type = type; + } + + @Override + public ArrayCreation materialize(AST ast) + { + if (isMaterialized()) + { + return array; + } + array = ast.newArrayCreation(); + array.setType((ArrayType) JDTHelper.getType(type + new String(new char[getDimension()]).replace("\0", "[]"), ast)); + for (ExpressionSource,?> dim : dims) + { + array.dimensions().add(wireAndGetExpression(dim, this, ast)); + } + if (init != null) + { + array.setInitializer((ArrayInitializer) wireAndGetExpression(init, this, ast)); + } + return array; + } + + @Override + public ArrayCreation getInternal() + { + return array; + } + + @Override + public void setInternal(ArrayCreation jdtNode) + { + super.setInternal(jdtNode); + this.array = jdtNode; + } + + @Override + public ArrayConstructorExpression addDimension(ExpressionSource dim) + { + if (dims.isEmpty()) + { + dims = new ArrayList,?>>(); + } + dims.add((ExpressionSource,?>) dim); + return this; + } + + @Override + public ArrayConstructorExpression init(ArrayInit array) + { + this.init = (ArrayInit>) array; + return this; + } + + public int getDimension() + { + if (!dims.isEmpty()) + { + return dims.size(); + } else if (init != null) + { + return init.getDimension(); + } + return 0; + } + + @Override + public ArrayInit> getInit() + { + return init; + } + + @Override + public List,?>> getDimensions() + { + return Collections.unmodifiableList(dims); + } + +} + diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayInitImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayInitImpl.java new file mode 100644 index 00000000..01debbe6 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayInitImpl.java @@ -0,0 +1,110 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ArrayInitializer; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ArrayInit; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class ArrayInitImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,ArrayInitializer> + implements ArrayInit +{ + + private List,?>> elements = Collections.EMPTY_LIST; + + private ArrayInitializer init; + + @Override + public ArrayInit addElement(ArrayInit subRow) + { + if (elements.isEmpty()) + { + elements = new ArrayList(); + } + elements.add((ExpressionSource,?>) subRow); + return this; + } + + @Override + public ArrayInit addElement(ExpressionSource subElement) + { + if (elements.isEmpty()) + { + elements = new ArrayList(); + } + elements.add((ExpressionSource,?>) subElement); + return this; + } + + @Override + public int size() + { + return elements.size(); + } + + @Override + public int getDimension() + { + if (elements.isEmpty()) + { + return 0; + } else + { + if (elements.get(0) instanceof ArrayInit) + { + return elements.size(); + } else + { + return 1; + } + } + } + + @Override + public ArrayInitializer materialize(AST ast) + { + if (isMaterialized()) + { + return init; + } + this.init = ast.newArrayInitializer(); + for (ExpressionSource,?> src : elements) + { + this.init.expressions().add(wireAndGetExpression(src, this, ast)); + } + return init; + } + + @Override + public ArrayInitializer getInternal() + { + return init; + } + + @Override + public void setInternal(ArrayInitializer jdtNode) + { + super.setInternal(jdtNode); + this.init = jdtNode; + } + + @Override + public List,?>> getElements() + { + return Collections.unmodifiableList(elements); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AssignImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AssignImpl.java new file mode 100644 index 00000000..ac1f1d96 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AssignImpl.java @@ -0,0 +1,106 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.Assignment; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.AssignExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class AssignImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,Assignment> + implements AssignExpression +{ + + private Assignment axx; + + private Accessor,?> left; + private ExpressionSource,?> right; + private org.jboss.forge.roaster.model.expressions.Assignment op; + + public AssignImpl(org.jboss.forge.roaster.model.expressions.Assignment op) + { + this.op = op; + } + + @Override + public AssignExpression setLeft(Accessor left) + { + this.left = (Accessor,?>) left; + return this; + } + + @Override + public AssignExpression setRight(ExpressionSource right) + { + this.right = (ExpressionSource,?>) right; + return this; + } + + @Override + public Accessor,?> getLeft() + { + return left; + } + + @Override + public String getOperator() + { + return op.toString(); + } + + public AssignExpression setOperator(org.jboss.forge.roaster.model.expressions.Assignment op) + { + this.op = op; + return this; + } + + @Override + public ExpressionSource,?> getRight() + { + return right; + } + + @Override + public Assignment materialize(AST ast) + { + if (isMaterialized()) + { + return axx; + } + axx = ast.newAssignment(); + axx.setOperator(Assignment.Operator.toOperator(op.getOp())); + + if (left != null) + { + axx.setLeftHandSide(wireAndGetExpression(left, this, ast)); + } + if (right != null) + { + axx.setRightHandSide(wireAndGetExpression(right, this, ast)); + } + return axx; + } + + @Override + public Assignment getInternal() + { + return axx; + } + + @Override + public void setInternal(Assignment jdtNode) + { + super.setInternal(jdtNode); + this.axx = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BaseInvokeableImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BaseInvokeableImpl.java new file mode 100644 index 00000000..73aaf9a9 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BaseInvokeableImpl.java @@ -0,0 +1,35 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class BaseInvokeableImpl, + P extends ExpressionHolder, + E extends MethodInvokeExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends AccessorImpl + implements MethodInvokeExpression +{ + + protected String method; + + public BaseInvokeableImpl(String method) + { + this.method = method; + } + + @Override + public String getMethodName() + { + return method; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BasicExpressionFactoryImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BasicExpressionFactoryImpl.java new file mode 100644 index 00000000..de6c4357 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BasicExpressionFactoryImpl.java @@ -0,0 +1,290 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ArrayConstructorExpression; +import org.jboss.forge.roaster.model.expressions.ArrayInit; +import org.jboss.forge.roaster.model.expressions.AssignExpression; +import org.jboss.forge.roaster.model.expressions.Assignment; +import org.jboss.forge.roaster.model.expressions.BasicExpressionFactory; +import org.jboss.forge.roaster.model.expressions.BooleanLiteral; +import org.jboss.forge.roaster.model.expressions.CastExpression; +import org.jboss.forge.roaster.model.expressions.CharacterLiteral; +import org.jboss.forge.roaster.model.expressions.ClassLiteral; +import org.jboss.forge.roaster.model.expressions.ConstructorExpression; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.InstanceofExpression; +import org.jboss.forge.roaster.model.expressions.Literal; +import org.jboss.forge.roaster.model.expressions.NullLiteral; +import org.jboss.forge.roaster.model.expressions.NumberLiteral; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.expressions.OperatorExpression; +import org.jboss.forge.roaster.model.expressions.ParenExpression; +import org.jboss.forge.roaster.model.expressions.PrefixOp; +import org.jboss.forge.roaster.model.expressions.PrimitiveLiteral; +import org.jboss.forge.roaster.model.expressions.StringLiteral; +import org.jboss.forge.roaster.model.expressions.Super; +import org.jboss.forge.roaster.model.expressions.TernaryExpression; +import org.jboss.forge.roaster.model.expressions.ThisLiteral; +import org.jboss.forge.roaster.model.expressions.UnaryExpression; +import org.jboss.forge.roaster.model.expressions.Variable; +import org.jboss.forge.roaster.model.impl.NodeImpl; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class BasicExpressionFactoryImpl, + P extends ExpressionHolder, + J extends org.eclipse.jdt.core.dom.Expression> + extends NodeImpl + implements BasicExpressionFactory +{ + + + public BasicExpressionFactoryImpl() + { + } + + @Override + public J materialize(AST ast) + { + throw new UnsupportedOperationException("Method should not be called directly on this class"); + } + + @Override + public J getInternal() + { + throw new UnsupportedOperationException("Method should not be called directly on this class"); + } + + @Override + public StringLiteral literal(String val) + { + return new StringLiteralImpl(val); + } + + @Override + public NumberLiteral literal(Number val) + { + return new NumberLiteralImpl(val); + } + + @Override + public CharacterLiteral literal(Character val) + { + return new CharacterLiteralImpl(val); + } + + @Override + public ClassLiteral classLiteral(Class klass) + { + return classLiteral(klass.getCanonicalName()); + } + + @Override + public ClassLiteral classLiteral(String klass) + { + return new ClassLiteralImpl(klass); + } + + @Override + public BooleanLiteral literal(Boolean val) + { + return new BooleanLiteralImpl(val); + } + + public Literal zeroLiteral(Class klass) + { + return zeroLiteral(klass.getCanonicalName()); + } + + public Literal zeroLiteral(String klass) + { + if (boolean.class.getName().equals(klass)) + { + return literal(false); + } else if (byte.class.getName().equals(klass)) + { + return literal(0); + } else if (char.class.getName().equals(klass)) + { + return literal(0); + } else if (double.class.getName().equals(klass)) + { + return literal(0.0); + } else if (float.class.getName().equals(klass)) + { + return literal(0.0f); + } else if (int.class.getName().equals(klass)) + { + return literal(0); + } else if (long.class.getName().equals(klass)) + { + return literal(0L); + } else if (short.class.getName().equals(klass)) + { + return literal(0); + } else + { + return nullLiteral(); + } + } + + @Override + public OperatorExpression operator(Op op) + { + return new OperatorImpl(op); + } + + @Override + public UnaryExpression operator(PrefixOp op) + { + return new UnaryImpl(op); + } + + @Override + public NullLiteral nullLiteral() + { + return new NullLiteralImpl(); + } + + @Override + public UnaryExpression not(ExpressionSource arg) + { + return new NotImpl(arg); + } + + @Override + public DeclareExpression declare(String klass, String name) + { + return new DeclareExpressionImpl(klass, name); + } + + @Override + public DeclareExpression declare(Class klass, String name) + { + return declare(klass.getCanonicalName(), name); + } + + @Override + public DeclareExpression declare(String klass, String name, ExpressionSource init) + { + return new DeclareExpressionImpl(klass, name, init); + } + + @Override + public DeclareExpression declare(Class klass, String name, ExpressionSource init) + { + return declare(klass.getCanonicalName(), name, init); + } + + @Override + public CastExpression cast(String klass, ExpressionSource arg) + { + CastExpression expr = new CastImpl(klass, arg); + return expr; + } + + @Override + public CastExpression cast(Class klass, ExpressionSource arg) + { + return cast(klass.getCanonicalName(), arg); + } + + @Override + public ConstructorExpression construct(String klass) + { + return new ConstructorImpl(klass); + } + + @Override + public ConstructorExpression construct(Class klass) + { + return construct(klass.getCanonicalName()); + } + + @Override + public ArrayConstructorExpression newArray(String klass) + { + return new ArrayImpl(klass); + } + + @Override + public ArrayConstructorExpression newArray(Class klass) + { + return newArray(klass.getCanonicalName()); + } + + @Override + public Variable var(String varName) + { + return new VarArgumentImpl(varName); + } + + @Override + public AssignExpression assign(Assignment operator) + { + return new AssignImpl(operator); + } + + @Override + public ThisLiteral thisLiteral() + { + return new SelfArgumentImpl(); + } + + public ParenExpression paren(ExpressionSource child) + { + return new ParenImpl(child); + } + + @Override + public TernaryExpression ternary() + { + return new TernaryImpl(); + } + + public Accessor classStatic(String klass) + { + return new StaticClassAccessorImpl(klass); + } + + public Accessor classStatic(Class klass) + { + return classStatic(klass.getCanonicalName()); + } + + @Override + public InstanceofExpression instanceOf(String klass, ExpressionSource arg) + { + InstanceofExpression expr = new InstanceofImpl(klass, arg); + return expr; + } + + @Override + public InstanceofExpression instanceOf(Class klass, ExpressionSource arg) + { + return instanceOf(klass.getCanonicalName(), arg); + } + + @Override + public Super sup() + { + return new SuperImpl(); + } + + @Override + public ArrayInit> vec() + { + return new ArrayInitImpl>(); + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BooleanLiteralImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BooleanLiteralImpl.java new file mode 100644 index 00000000..58368ab1 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BooleanLiteralImpl.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.BooleanLiteral; +import org.jboss.forge.roaster.model.expressions.PrimitiveLiteral; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class BooleanLiteralImpl, + P extends ExpressionHolder> + extends LiteralImpl,org.eclipse.jdt.core.dom.BooleanLiteral> + implements BooleanLiteral +{ + + private boolean val; + + private org.eclipse.jdt.core.dom.BooleanLiteral literal; + + public BooleanLiteralImpl(boolean val) + { + this.val = val; + } + + @Override + public org.eclipse.jdt.core.dom.BooleanLiteral materialize(AST ast) + { + if (isMaterialized()) + { + return literal; + } + literal = ast.newBooleanLiteral(val); + return literal; + } + + @Override + public org.eclipse.jdt.core.dom.BooleanLiteral getInternal() + { + return literal; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.BooleanLiteral jdtNode) + { + super.setInternal(jdtNode); + this.literal = jdtNode; + } + + public Boolean getValue() + { + return val; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CastImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CastImpl.java new file mode 100644 index 00000000..a96791fc --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CastImpl.java @@ -0,0 +1,85 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ParenthesizedExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.CastExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class CastImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,org.eclipse.jdt.core.dom.CastExpression> + implements CastExpression +{ + + private org.eclipse.jdt.core.dom.CastExpression cast; + + private String type; + private ExpressionSource,?> expression; + + public CastImpl(String klass, ExpressionSource expression) + { + this.type = klass; + this.expression = (ExpressionSource,?>) expression; + } + + @Override + public org.eclipse.jdt.core.dom.CastExpression materialize(AST ast) + { + if (isMaterialized()) + { + return cast; + } + cast = ast.newCastExpression(); + cast.setType(JDTHelper.getType(type, ast)); + if (expression != null) + { + org.eclipse.jdt.core.dom.Expression expr = wireAndGetExpression(expression, this, ast); + ParenthesizedExpression paren = ast.newParenthesizedExpression(); + paren.setExpression(expr); + cast.setExpression(paren); + } + return cast; + } + + @Override + public org.eclipse.jdt.core.dom.CastExpression getInternal() + { + return cast; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.CastExpression jdtNode) + { + super.setInternal(jdtNode); + this.cast = jdtNode; + } + + @Override + public String getType() + { + return type; + } + + @Override + public ExpressionSource,?> getExpression() + { + return expression; + } + + @Override + public CastExpression setExpression(ExpressionSource expr) + { + this.expression = (ExpressionSource,?>) expr; + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CharacterLiteralImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CharacterLiteralImpl.java new file mode 100644 index 00000000..d27c3882 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CharacterLiteralImpl.java @@ -0,0 +1,60 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.CharacterLiteral; +import org.jboss.forge.roaster.model.expressions.PrimitiveLiteral; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class CharacterLiteralImpl, + P extends ExpressionHolder> + extends LiteralImpl,org.eclipse.jdt.core.dom.CharacterLiteral> + implements CharacterLiteral +{ + + private Character val; + + private org.eclipse.jdt.core.dom.CharacterLiteral literal; + + public CharacterLiteralImpl(Character val) + { + this.val = val; + } + + @Override + public org.eclipse.jdt.core.dom.CharacterLiteral materialize(AST ast) + { + if (isMaterialized()) + { + return literal; + } + literal = ast.newCharacterLiteral(); + literal.setCharValue(val); + return literal; + } + + @Override + public org.eclipse.jdt.core.dom.CharacterLiteral getInternal() + { + return literal; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.CharacterLiteral jdtNode) + { + super.setInternal(jdtNode); + this.literal = jdtNode; + } + + public Character getValue() + { + return val; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ClassLiteralImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ClassLiteralImpl.java new file mode 100644 index 00000000..75b30f8f --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ClassLiteralImpl.java @@ -0,0 +1,61 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.TypeLiteral; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ClassLiteral; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class ClassLiteralImpl, + P extends ExpressionHolder> + extends AccessorImpl,TypeLiteral> + implements ClassLiteral +{ + + private String val; + + private TypeLiteral literal; + + public ClassLiteralImpl(String val) + { + this.val = val; + } + + @Override + public TypeLiteral materialize(AST ast) + { + if (isMaterialized()) + { + return literal; + } + literal = ast.newTypeLiteral(); + literal.setType(JDTHelper.getType(val, ast)); + return literal; + } + + @Override + public TypeLiteral getInternal() + { + return literal; + } + + @Override + public void setInternal(TypeLiteral jdtNode) + { + super.setInternal(jdtNode); + this.literal = jdtNode; + } + + public String getValue() + { + return val; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ConstructorImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ConstructorImpl.java new file mode 100644 index 00000000..db781991 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ConstructorImpl.java @@ -0,0 +1,94 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ClassInstanceCreation; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ConstructorExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + + +public class ConstructorImpl, + P extends ExpressionHolder> + extends AccessorImpl,ClassInstanceCreation> + implements ConstructorExpression, + ExpressionChainLink +{ + + private ClassInstanceCreation constr; + + private String type; + private List,?>> arguments = Collections.EMPTY_LIST; + + public ConstructorImpl(String type) + { + this.type = type; + } + + @Override + public ConstructorExpression addArgument(Argument arg) + { + if (arguments.isEmpty()) + { + arguments = new LinkedList,?>>(); + } + arguments.add((Argument,?>) arg); + return this; + } + + @Override + public List,?>> getArguments() + { + return arguments; + } + + @Override + public ClassInstanceCreation materialize(AST ast) + { + if (isMaterialized()) + { + return constr; + } + constr = ast.newClassInstanceCreation(); + constr.setType(JDTHelper.getType(type, ast)); + + for (Argument,?> arg : arguments) + { + constr.arguments().add(wireAndGetExpression(arg, this, ast)); + } + return constr; + } + + @Override + public ClassInstanceCreation getInternal() + { + return constr; + } + + @Override + public void setInternal(ClassInstanceCreation jdtNode) + { + super.setInternal(jdtNode); + this.constr = jdtNode; + } + + @Override + public ExpressionSource chainExpression(ExpressionSource child) + { + throw new IllegalStateException("This method should not be invoked [constructors start chains]"); + } +} + diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DeclareExpressionImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DeclareExpressionImpl.java new file mode 100644 index 00000000..0f64743c --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DeclareExpressionImpl.java @@ -0,0 +1,154 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.VariableDeclarationExpression; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DeclareExpressionImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,VariableDeclarationExpression> + implements DeclareExpression +{ + + private VariableDeclarationExpression var; + + private String type; + private List names = new ArrayList(); + private Map,?>> initExpressions = new HashMap,?>>(); + + public DeclareExpressionImpl(String type, String name) + { + this.type = type; + this.names.add(name); + } + + public DeclareExpressionImpl(String klass, String name, ExpressionSource init) + { + this(klass, name); + this.initExpressions.put(name, (ExpressionSource,?>) init); + } + + public DeclareExpression addDeclaration(String name, ExpressionSource expr) + { + addDeclaration(name); + if (expr != null) + { + initExpressions.put(name, (ExpressionSource,?>) expr); + } + return this; + } + + public DeclareExpression addDeclaration(String name) + { + this.names.add(name); + return this; + } + + @Override + public VariableDeclarationExpression materialize(AST ast) + { + if (isMaterialized()) + { + return var; + } + VariableDeclarationFragment frag = ast.newVariableDeclarationFragment(); + + frag.setName(ast.newSimpleName(getVariableName())); + var = ast.newVariableDeclarationExpression(frag); + var.setType(JDTHelper.getType(getVariableType(), ast)); + if (getInitExpression() != null) + { + frag.setInitializer(wireAndGetExpression(getInitExpression(), this, ast)); + } + + if (names.size() > 1) + { + for (int j = 1; j < names.size(); j++) + { + VariableDeclarationFragment moreFrag = ast.newVariableDeclarationFragment(); + String n = names.get(j); + moreFrag.setName(ast.newSimpleName(n)); + if (initExpressions.containsKey(n)) + { + moreFrag.setInitializer(wireAndGetExpression(initExpressions.get(n), this, ast)); + } + var.fragments().add(moreFrag); + } + } + + return var; + } + + @Override + public VariableDeclarationExpression getInternal() + { + return var; + } + + @Override + public void setInternal(VariableDeclarationExpression jdtNode) + { + super.setInternal(jdtNode); + this.var = jdtNode; + } + + @Override + public String getVariableName() + { + return names.isEmpty() ? null : names.get(0); + } + + @Override + public String getVariableType() + { + return type; + } + + @Override + public List getVariableNames() + { + return names; + } + + @Override + public Map,?>> getInitExpressions() + { + return Collections.unmodifiableMap(initExpressions); + } + + @Override + public boolean isSingleDeclaration() + { + return names.size() == 1; + } + + @Override + public int getNumVariables() + { + return names.size(); + } + + @Override + public ExpressionSource,?> getInitExpression() + { + return getVariableName() != null ? initExpressions.get(getVariableName()) : null; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DotAccessorImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DotAccessorImpl.java new file mode 100644 index 00000000..2050a168 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DotAccessorImpl.java @@ -0,0 +1,132 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.ArrayIndexer; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Expressions; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.NonPrimitiveExpression; +import org.jboss.forge.roaster.model.expressions.Setter; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.spi.ExpressionFactoryImpl; + +public class DotAccessorImpl, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends ExpressionImpl + implements Accessor +{ + + private ExpressionFactoryImpl factory = new ExpressionFactoryImpl(); + + private ExpressionSource owner; + + public DotAccessorImpl(ExpressionSource parent) + { + this.owner = parent; + } + + @Override + public P getOrigin() + { + return null; + } + + @Override + public Field field(String fieldName) + { + Field field = factory.field(fieldName); + swap(field, this.owner); + return field; + } + + @Override + public Getter getter(String fieldName, String type) + { + Getter getter = factory.getter(fieldName, type); + swap(getter, this.owner); + return getter; + } + + @Override + public Getter getter(String field, Class klass) + { + return getter(field, klass.getName()); + } + + @Override + public Setter setter(String fldName, String type, ExpressionSource value) + { + Setter setter = factory.setter(fldName, type, value); + swap(setter, this.owner); + return setter; + } + + @Override + public Setter setter(String fldName, Class type, ExpressionSource value) + { + return setter(fldName, type.getName(), value); + } + + + /* + @Override + public Variable var( String varName ) { + Variable var = (Variable) Expressions.var( varName ); + swap( var, this.owner ); + return var; + } + */ + + + @Override + public MethodCallExpression invoke(String method) + { + MethodCallExpression invoke = (MethodCallExpression) Expressions.invoke(method); + swap(invoke, this.owner); + return invoke; + } + + @Override + public ArrayIndexer itemAt(ExpressionSource index) + { + ArrayIndexer accessor = factory.itemAt(index); + swap(accessor, this.owner); + return accessor; + } + + + private void swap(ExpressionHolder originalChild, ExpressionSource originalParent) + { + ExpressionChainLink sub = (ExpressionChainLink) originalChild; + ExpressionChainLink sup = (ExpressionChainLink) originalParent; + + sub.linkParent(sup.getParent()); + sup.linkParent(originalChild); + sub.chainExpression(originalParent); + } + + @Override + public org.eclipse.jdt.core.dom.Expression materialize(AST ast) + { + throw new UnsupportedOperationException("Method should not be called directly on this class"); + } + + @Override + public org.eclipse.jdt.core.dom.Expression getInternal() + { + throw new UnsupportedOperationException("Method should not be called directly on this class"); + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionBuilder.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionBuilder.java new file mode 100644 index 00000000..bb4e9c65 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionBuilder.java @@ -0,0 +1,310 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.ArrayAccess; +import org.eclipse.jdt.core.dom.ArrayCreation; +import org.eclipse.jdt.core.dom.ArrayInitializer; +import org.eclipse.jdt.core.dom.ArrayType; +import org.eclipse.jdt.core.dom.Assignment; +import org.eclipse.jdt.core.dom.BooleanLiteral; +import org.eclipse.jdt.core.dom.CastExpression; +import org.eclipse.jdt.core.dom.CharacterLiteral; +import org.eclipse.jdt.core.dom.ClassInstanceCreation; +import org.eclipse.jdt.core.dom.ConditionalExpression; +import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jdt.core.dom.FieldAccess; +import org.eclipse.jdt.core.dom.InfixExpression; +import org.eclipse.jdt.core.dom.InstanceofExpression; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.NumberLiteral; +import org.eclipse.jdt.core.dom.ParenthesizedExpression; +import org.eclipse.jdt.core.dom.PostfixExpression; +import org.eclipse.jdt.core.dom.PrefixExpression; +import org.eclipse.jdt.core.dom.QualifiedName; +import org.eclipse.jdt.core.dom.SimpleName; +import org.eclipse.jdt.core.dom.StringLiteral; +import org.eclipse.jdt.core.dom.SuperFieldAccess; +import org.eclipse.jdt.core.dom.SuperMethodInvocation; +import org.eclipse.jdt.core.dom.VariableDeclarationExpression; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ArrayConstructorExpression; +import org.jboss.forge.roaster.model.expressions.ArrayInit; +import org.jboss.forge.roaster.model.expressions.ConstructorExpression; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.Expressions; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.expressions.OperatorExpression; +import org.jboss.forge.roaster.model.expressions.OrdinalArgument; +import org.jboss.forge.roaster.model.expressions.PrefixOp; +import org.jboss.forge.roaster.model.impl.JDTHelper; + +import java.text.NumberFormat; +import java.text.ParseException; + +import static org.eclipse.jdt.core.dom.ASTNode.ARRAY_ACCESS; +import static org.eclipse.jdt.core.dom.ASTNode.ARRAY_CREATION; +import static org.eclipse.jdt.core.dom.ASTNode.ARRAY_INITIALIZER; +import static org.eclipse.jdt.core.dom.ASTNode.ASSIGNMENT; +import static org.eclipse.jdt.core.dom.ASTNode.BOOLEAN_LITERAL; +import static org.eclipse.jdt.core.dom.ASTNode.CAST_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.CHARACTER_LITERAL; +import static org.eclipse.jdt.core.dom.ASTNode.CLASS_INSTANCE_CREATION; +import static org.eclipse.jdt.core.dom.ASTNode.CONDITIONAL_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.FIELD_ACCESS; +import static org.eclipse.jdt.core.dom.ASTNode.INFIX_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.INSTANCEOF_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.METHOD_INVOCATION; +import static org.eclipse.jdt.core.dom.ASTNode.NULL_LITERAL; +import static org.eclipse.jdt.core.dom.ASTNode.NUMBER_LITERAL; +import static org.eclipse.jdt.core.dom.ASTNode.PARENTHESIZED_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.POSTFIX_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.PREFIX_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.QUALIFIED_NAME; +import static org.eclipse.jdt.core.dom.ASTNode.SIMPLE_NAME; +import static org.eclipse.jdt.core.dom.ASTNode.STRING_LITERAL; +import static org.eclipse.jdt.core.dom.ASTNode.SUPER_FIELD_ACCESS; +import static org.eclipse.jdt.core.dom.ASTNode.SUPER_METHOD_INVOCATION; +import static org.eclipse.jdt.core.dom.ASTNode.THIS_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.VARIABLE_DECLARATION_EXPRESSION; +import static org.jboss.forge.roaster.model.expressions.Expressions.assign; +import static org.jboss.forge.roaster.model.expressions.Expressions.cast; +import static org.jboss.forge.roaster.model.expressions.Expressions.declare; +import static org.jboss.forge.roaster.model.expressions.Expressions.getter; +import static org.jboss.forge.roaster.model.expressions.Expressions.instanceOf; +import static org.jboss.forge.roaster.model.expressions.Expressions.invoke; +import static org.jboss.forge.roaster.model.expressions.Expressions.literal; +import static org.jboss.forge.roaster.model.expressions.Expressions.newArray; +import static org.jboss.forge.roaster.model.expressions.Expressions.nullLiteral; +import static org.jboss.forge.roaster.model.expressions.Expressions.operator; +import static org.jboss.forge.roaster.model.expressions.Expressions.paren; +import static org.jboss.forge.roaster.model.expressions.Expressions.setter; +import static org.jboss.forge.roaster.model.expressions.Expressions.sup; +import static org.jboss.forge.roaster.model.expressions.Expressions.ternary; +import static org.jboss.forge.roaster.model.expressions.Expressions.thisLiteral; +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.expressions.Expressions.vec; + +public class ExpressionBuilder +{ + + public static org.jboss.forge.roaster.model.expressions.ExpressionSource asRoasterExpression(org.eclipse.jdt.core.dom.Expression jdtExpression) + { + org.jboss.forge.roaster.model.expressions.ExpressionSource roasterExpr; + switch (jdtExpression.getNodeType()) + { + //TODO FIXME : names are not necessarily vars, but it may not be possible to determine at this point + case SIMPLE_NAME: + roasterExpr = var(((SimpleName) jdtExpression).getIdentifier()); + break; + case QUALIFIED_NAME: + roasterExpr = var(((QualifiedName) jdtExpression).getFullyQualifiedName()); + break; + case STRING_LITERAL: + roasterExpr = literal(((StringLiteral) jdtExpression).getLiteralValue()); + break; + case BOOLEAN_LITERAL: + roasterExpr = literal(((BooleanLiteral) jdtExpression).booleanValue()); + break; + case CHARACTER_LITERAL: + roasterExpr = literal(((CharacterLiteral) jdtExpression).charValue()); + break; + case NULL_LITERAL: + roasterExpr = nullLiteral(); + break; + case NUMBER_LITERAL: + try + { + roasterExpr = literal(NumberFormat.getInstance().parse( + ((NumberLiteral) jdtExpression).getToken())); + } catch (ParseException e) + { + // we know it's a valid number, so this should never happen + e.printStackTrace(); + roasterExpr = null; + } + break; + case CONDITIONAL_EXPRESSION: + ConditionalExpression cond = (ConditionalExpression) jdtExpression; + roasterExpr = ternary().setCondition(asRoasterExpression(cond.getExpression())) + .setThenExpression(asRoasterExpression(cond.getThenExpression())) + .setElseExpression(asRoasterExpression(cond.getElseExpression())); + + break; + case CAST_EXPRESSION: + CastExpression cast = (CastExpression) jdtExpression; + roasterExpr = cast(cast.getType().toString(), asRoasterExpression(cast.getExpression())); + break; + case INFIX_EXPRESSION: + InfixExpression infix = (InfixExpression) jdtExpression; + roasterExpr = operator(Op.build(infix.getOperator().toString())) + .addArgument((Argument) asRoasterExpression(infix.getLeftOperand())) + .addArgument((Argument) asRoasterExpression(infix.getRightOperand())); + for (Object o : infix.extendedOperands()) + { + ((OperatorExpression) roasterExpr).addArgument((Argument) asRoasterExpression((Expression) o)); + } + break; + case FIELD_ACCESS: + FieldAccess fax = (FieldAccess) jdtExpression; + Accessor axx = (Accessor) asRoasterExpression(fax.getExpression()); + roasterExpr = axx.field(fax.getName().getIdentifier()); + break; + case INSTANCEOF_EXPRESSION: + InstanceofExpression iof = (InstanceofExpression) jdtExpression; + roasterExpr = instanceOf(iof.getRightOperand().toString(), asRoasterExpression(iof.getLeftOperand())); + break; + case THIS_EXPRESSION: + roasterExpr = thisLiteral(); + break; + case PREFIX_EXPRESSION: + PrefixExpression pref = (PrefixExpression) jdtExpression; + roasterExpr = operator(PrefixOp.build(pref.getOperator().toString())).setExpression( (Argument) asRoasterExpression(pref.getOperand())); + break; + case POSTFIX_EXPRESSION: + PostfixExpression post = (PostfixExpression) jdtExpression; + roasterExpr = asRoasterExpression(post.getOperand()); + if (post.getOperator() == PostfixExpression.Operator.INCREMENT) + { + roasterExpr = ((OrdinalArgument) roasterExpr).inc(); + } else + { + roasterExpr = ((OrdinalArgument) roasterExpr).dec(); + } + break; + case PARENTHESIZED_EXPRESSION: + ParenthesizedExpression par = (ParenthesizedExpression) jdtExpression; + roasterExpr = paren(asRoasterExpression(par.getExpression())); + break; + case CLASS_INSTANCE_CREATION: + ClassInstanceCreation cic = (ClassInstanceCreation) jdtExpression; + ConstructorExpression constr = Expressions.construct(cic.getType().toString()); + for (Object arg : cic.arguments()) + { + constr.addArgument((Argument) asRoasterExpression((Expression) arg)); + } + roasterExpr = constr; + break; + case METHOD_INVOCATION: + MethodInvocation invocation = (MethodInvocation) jdtExpression; + String name = invocation.getName().toString(); + if (JDTHelper.isGetter(name, invocation.arguments())) + { + roasterExpr = getter(JDTHelper.fieldForGetter(name), name.startsWith("is") ? boolean.class : Object.class); + } else if (JDTHelper.isSetter(name, invocation.arguments())) + { + roasterExpr = setter(JDTHelper.fieldForSetter(name), Object.class, asRoasterExpression((Expression) invocation.arguments().get(0))); + } else + { + roasterExpr = invoke(name); + for (Object arg : invocation.arguments()) + { + ((MethodCallExpression) roasterExpr).addArgument((Argument) asRoasterExpression((Expression) arg)); + } + } + if (invocation.getExpression() != null) + { + ((MethodInvokeExpression) roasterExpr).setInvocationTarget(asRoasterExpression(invocation.getExpression())); + } + break; + case ARRAY_ACCESS: + ArrayAccess arrAxx = (ArrayAccess) jdtExpression; + roasterExpr = ((Accessor) asRoasterExpression(arrAxx.getArray())).itemAt(asRoasterExpression(arrAxx.getIndex())); + break; + case ARRAY_CREATION: + ArrayCreation arrC = (ArrayCreation) jdtExpression; + ArrayConstructorExpression newArr = newArray(((ArrayType) arrC.getType()).getElementType().toString()); + for (Object o : arrC.dimensions()) + { + newArr.addDimension(asRoasterExpression((Expression) o)); + } + roasterExpr = newArr; + if (arrC.getInitializer() != null) + { + newArr.init((ArrayInit) asRoasterExpression(arrC.getInitializer())); + } + break; + case ARRAY_INITIALIZER: + ArrayInitializer arrInit = (ArrayInitializer) jdtExpression; + ArrayInit arrI = vec(); + for (Object xp : arrInit.expressions()) + { + arrI.addElement(asRoasterExpression((Expression) xp)); + } + roasterExpr = arrI; + break; + case ASSIGNMENT: + Assignment jass = (Assignment) jdtExpression; + roasterExpr = assign(org.jboss.forge.roaster.model.expressions.Assignment.build(jass.getOperator().toString())) + .setLeft((Accessor) asRoasterExpression(jass.getLeftHandSide())) + .setRight(asRoasterExpression(jass.getRightHandSide())); + break; + case SUPER_FIELD_ACCESS: + SuperFieldAccess supf = (SuperFieldAccess) jdtExpression; + roasterExpr = sup().field(supf.getName().toString()); + break; + case SUPER_METHOD_INVOCATION: + SuperMethodInvocation supm = (SuperMethodInvocation) jdtExpression; + String supmName = supm.getName().toString(); + if (JDTHelper.isGetter(supmName, supm.arguments())) + { + roasterExpr = sup().getter(JDTHelper.fieldForGetter(supmName), supmName.startsWith("is") ? boolean.class : Object.class); + } else + { + MethodCallExpression mce = sup().invoke(supm.getName().toString()); + for (Object o : supm.arguments()) + { + mce.addArgument((Argument) asRoasterExpression((Expression) o)); + } + roasterExpr = mce; + } + break; + case VARIABLE_DECLARATION_EXPRESSION: + VariableDeclarationExpression dek = (VariableDeclarationExpression) jdtExpression; + String type = dek.getType().toString(); + DeclareExpression rstDecl = null; + for (Object o : dek.fragments()) + { + VariableDeclarationFragment frag = (VariableDeclarationFragment) o; + if (rstDecl == null) + { + if (frag.getInitializer() != null) + { + rstDecl = declare(type, frag.getName().toString(), asRoasterExpression(frag.getInitializer())); + } else + { + rstDecl = declare(type, frag.getName().toString()); + } + } else + { + if (frag.getInitializer() != null) + { + rstDecl.addDeclaration(frag.getName().toString(), asRoasterExpression(frag.getInitializer())); + } else + { + rstDecl.addDeclaration(frag.getName().toString()); + } + } + } + roasterExpr = rstDecl; + break; + default: + throw new UnsupportedOperationException("Unable to handle expression of type: " + jdtExpression.getNodeType() + " \n >> " + jdtExpression.toString()); + } + if (roasterExpr != null) + { + ((org.jboss.forge.roaster.model.ASTNode) roasterExpr).setInternal(jdtExpression); + } + return roasterExpr; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionChainLink.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionChainLink.java new file mode 100644 index 00000000..a7346731 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionChainLink.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ExpressionChainLink, + P extends ExpressionHolder> +{ + + ExpressionSource chainExpression(ExpressionSource child); + + ExpressionHolder getParent(); + + void linkParent(ExpressionHolder parent); + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionImpl.java new file mode 100644 index 00000000..d5370174 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionImpl.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.NodeImpl; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class ExpressionImpl, + P extends ExpressionHolder, + E extends ExpressionSource, + J extends org.eclipse.jdt.core.dom.Expression> + extends NodeImpl + implements ExpressionSource +{ + + private ExpressionHolder parent; + + public ExpressionImpl() + { + } + + public ExpressionHolder getParent() + { + return parent; + } + + public void wireParent(ExpressionHolder parent) + { + this.parent = parent; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/FieldImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/FieldImpl.java new file mode 100644 index 00000000..311e2040 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/FieldImpl.java @@ -0,0 +1,92 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.FieldAccess; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class FieldImpl, + P extends ExpressionHolder> + extends SimpleAccessorImpl,FieldAccess> + implements Field, + ExpressionChainLink> +{ + + private FieldAccess fld; + + private String fieldName; + private ExpressionSource,?> expression; + + public FieldImpl(String fieldName) + { + this.fieldName = fieldName; + } + + @Override + public FieldAccess materialize(AST ast) + { + if (isMaterialized()) + { + return fld; + } + fld = ast.newFieldAccess(); + fld.setName(ast.newSimpleName(fieldName)); + if (expression == null) + { + fld.setExpression(ast.newThisExpression()); + } else + { + fld.setExpression(wireAndGetExpression(expression, this, ast)); + } + return fld; + } + + @Override + public FieldAccess getInternal() + { + return fld; + } + + @Override + public void setInternal(FieldAccess jdtNode) + { + super.setInternal(jdtNode); + this.fld = jdtNode; + } + + @Override + public ExpressionSource,?> chainExpression(ExpressionSource child) + { + ExpressionSource,?> cast = (ExpressionSource,?>) child; + this.expression = cast; + return cast; + } + + @Override + public String getFieldName() + { + return fieldName; + } + + @Override + public ExpressionSource,?> getInvocationTarget() + { + return expression; + } + + @Override + public Field setInvocationTarget(ExpressionSource target) + { + this.expression = (ExpressionSource,?>) target; + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/GetterImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/GetterImpl.java new file mode 100644 index 00000000..9d876415 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/GetterImpl.java @@ -0,0 +1,83 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class GetterImpl, + P extends ExpressionHolder> + extends InvokeableImpl,MethodInvocation> + implements Getter, + ExpressionChainLink> +{ + + protected MethodInvocation invoke; + + private String fieldName; + + public GetterImpl(String fieldName, String type) + { + super(JDTHelper.getter(fieldName, type)); + this.fieldName = fieldName; + } + + @Override + public ExpressionSource,?> getInvocationTarget() + { + return target; + } + + @Override + public Getter setInvocationTarget(ExpressionSource target) + { + this.target = chainExpression((ExpressionSource) target); + return this; + } + + @Override + public MethodInvocation materialize(AST ast) + { + if (isMaterialized()) + { + return invoke; + } + invoke = ast.newMethodInvocation(); + + invoke.setName(ast.newSimpleName(method)); + if (target != null) + { + invoke.setExpression(wireAndGetExpression(target, this, ast)); + } + return invoke; + } + + @Override + public MethodInvocation getInternal() + { + return invoke; + } + + @Override + public void setInternal(MethodInvocation jdtNode) + { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } + + @Override + public String getFieldName() + { + return fieldName; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InstanceofImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InstanceofImpl.java new file mode 100644 index 00000000..75ff9575 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InstanceofImpl.java @@ -0,0 +1,74 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.InstanceofExpression; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class InstanceofImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,org.eclipse.jdt.core.dom.InstanceofExpression> + implements InstanceofExpression +{ + + private org.eclipse.jdt.core.dom.InstanceofExpression isA; + + private String typeName; + private ExpressionSource,?> expression; + + public InstanceofImpl(String klass, ExpressionSource expression) + { + this.typeName = klass; + this.expression = (ExpressionSource,?>) expression; + } + + @Override + public org.eclipse.jdt.core.dom.InstanceofExpression materialize(AST ast) + { + if (isMaterialized()) + { + return isA; + } + isA = ast.newInstanceofExpression(); + isA.setRightOperand(JDTHelper.getType(typeName, ast)); + if (expression != null) + { + org.eclipse.jdt.core.dom.Expression expr = wireAndGetExpression(expression, this, ast); + isA.setLeftOperand(expr); + } + return isA; + } + + @Override + public org.eclipse.jdt.core.dom.InstanceofExpression getInternal() + { + return isA; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.InstanceofExpression jdtNode) + { + super.setInternal(jdtNode); + this.isA = jdtNode; + } + + public String getTypeName() + { + return typeName; + } + + @Override + public ExpressionSource,?> getExpression() + { + return expression; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InvokeableImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InvokeableImpl.java new file mode 100644 index 00000000..6331a5ab --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InvokeableImpl.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class InvokeableImpl, + P extends ExpressionHolder, + E extends MethodInvokeExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends BaseInvokeableImpl + implements MethodInvokeExpression +{ + + protected ExpressionSource target; + + + public InvokeableImpl(String method) + { + super(method); + } + + public ExpressionSource chainExpression(ExpressionSource child) + { + ExpressionSource cast = (ExpressionSource) child; + this.target = cast; + return cast; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/JdtExpressionWrapper.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/JdtExpressionWrapper.java new file mode 100644 index 00000000..a9dfe3d9 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/JdtExpressionWrapper.java @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface JdtExpressionWrapper, + P extends ExpressionHolder, + J extends org.eclipse.jdt.core.dom.Expression> + extends ASTNode +{ + + public org.eclipse.jdt.core.dom.Expression wireAndGetExpression(ExpressionSource expression, P parent, AST ast); + +} \ No newline at end of file diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/LiteralImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/LiteralImpl.java new file mode 100644 index 00000000..49afe707 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/LiteralImpl.java @@ -0,0 +1,27 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.Expression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Literal; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class LiteralImpl, + P extends ExpressionHolder, + L extends Literal, + J extends Expression> + extends ExpressionImpl + implements Literal +{ + + public LiteralImpl() + { + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/MethodInvokeImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/MethodInvokeImpl.java new file mode 100644 index 00000000..c8f3ea4d --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/MethodInvokeImpl.java @@ -0,0 +1,72 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class MethodInvokeImpl, + P extends ExpressionHolder> + extends AbstractMethodInvokeImpl,MethodInvocation> + implements MethodCallExpression, + ExpressionChainLink> +{ + + protected MethodInvocation invoke; + + public MethodInvokeImpl(String method) + { + super(method); + } + + @Override + public MethodCallExpression setMethodName(String name) + { + this.method = name; + return this; + } + + @Override + public MethodInvocation materialize(AST ast) + { + if (isMaterialized()) + { + return invoke; + } + invoke = ast.newMethodInvocation(); + + invoke.setName(ast.newSimpleName(method)); + if (target != null) + { + invoke.setExpression(wireAndGetExpression(target, this, ast)); + } + for (Argument,?> arg : arguments) + { + invoke.arguments().add(wireAndGetExpression(arg, this, ast)); + } + return invoke; + } + + @Override + public MethodInvocation getInternal() + { + return invoke; + } + + @Override + public void setInternal(MethodInvocation jdtNode) + { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NonPrimitiveExpressionImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NonPrimitiveExpressionImpl.java new file mode 100644 index 00000000..ad3ba723 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NonPrimitiveExpressionImpl.java @@ -0,0 +1,49 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.NonPrimitiveExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class NonPrimitiveExpressionImpl, + P extends ExpressionHolder, + E extends NonPrimitiveExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends ExpressionImpl + implements NonPrimitiveExpression, + JdtExpressionWrapper +{ + + private ExpressionHolder parent; + + public NonPrimitiveExpressionImpl() + { + } + + public ExpressionHolder getParent() + { + return parent; + } + + public void linkParent(ExpressionHolder parent) + { + this.parent = parent; + } + + public org.eclipse.jdt.core.dom.Expression wireAndGetExpression(ExpressionSource expression, E parent, AST ast) + { + ASTNode node = (ASTNode) expression; + expression.setOrigin(parent); + node.setAst(ast); + return node.materialize(ast); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NotImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NotImpl.java new file mode 100644 index 00000000..97173117 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NotImpl.java @@ -0,0 +1,27 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.PrefixOp; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class NotImpl, + P extends ExpressionHolder> + extends UnaryImpl +{ + + public NotImpl(ExpressionSource expr) + { + super(PrefixOp.NOT); + setExpression(expr); + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NullLiteralImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NullLiteralImpl.java new file mode 100644 index 00000000..9b19cf43 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NullLiteralImpl.java @@ -0,0 +1,55 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.NullLiteral; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class NullLiteralImpl, + P extends ExpressionHolder> + extends LiteralImpl,org.eclipse.jdt.core.dom.NullLiteral> + implements org.jboss.forge.roaster.model.expressions.NullLiteral +{ + + private org.eclipse.jdt.core.dom.NullLiteral literal; + + public NullLiteralImpl() + { + } + + @Override + public org.eclipse.jdt.core.dom.NullLiteral materialize(AST ast) + { + if (isMaterialized()) + { + return literal; + } + literal = ast.newNullLiteral(); + return literal; + } + + @Override + public org.eclipse.jdt.core.dom.NullLiteral getInternal() + { + return literal; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.NullLiteral jdtNode) + { + super.setInternal(jdtNode); + this.literal = jdtNode; + } + + public Object getValue() + { + return null; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NumberLiteralImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NumberLiteralImpl.java new file mode 100644 index 00000000..07db43d4 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NumberLiteralImpl.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.NumberLiteral; +import org.jboss.forge.roaster.model.expressions.PrimitiveLiteral; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class NumberLiteralImpl, + P extends ExpressionHolder> + extends LiteralImpl,org.eclipse.jdt.core.dom.NumberLiteral> + implements NumberLiteral +{ + + private Number val; + + private org.eclipse.jdt.core.dom.NumberLiteral literal; + + public NumberLiteralImpl(Number val) + { + this.val = val; + } + + @Override + public org.eclipse.jdt.core.dom.NumberLiteral materialize(AST ast) + { + if (isMaterialized()) + { + return literal; + } + literal = ast.newNumberLiteral(val.toString()); + return literal; + } + + @Override + public org.eclipse.jdt.core.dom.NumberLiteral getInternal() + { + return literal; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.NumberLiteral jdtNode) + { + super.setInternal(jdtNode); + this.literal = jdtNode; + } + + public Number getValue() + { + return val; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/OperatorImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/OperatorImpl.java new file mode 100644 index 00000000..422a41ed --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/OperatorImpl.java @@ -0,0 +1,125 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jdt.core.dom.InfixExpression; +import org.eclipse.jdt.core.dom.ParenthesizedExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.expressions.OperatorExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class OperatorImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,InfixExpression> + implements OperatorExpression +{ + + private InfixExpression expr; + + private Op operator; + private List,?>> argumentList = Collections.EMPTY_LIST; + + public OperatorImpl(Op op) + { + this.operator = op; + } + + @Override + public OperatorExpression addArgument(Argument arg) + { + if (argumentList.isEmpty()) + { + argumentList = new LinkedList,?>>(); + } + argumentList.add((Argument,?>) arg); + return this; + } + + public List,?>> getArguments() + { + return Collections.unmodifiableList(argumentList); + } + + public String getOperator() + { + return operator.getOp(); + } + + @Override + public InfixExpression materialize(AST ast) + { + if (isMaterialized()) + { + return expr; + } + expr = ast.newInfixExpression(); + + expr.setOperator(InfixExpression.Operator.toOperator(this.operator.getOp())); + + for (Argument,?> arg : argumentList) + { + wireArg(arg, ast); + } + return expr; + } + + @Override + public InfixExpression getInternal() + { + return expr; + } + + @Override + public void setInternal(InfixExpression jdtNode) + { + super.setInternal(jdtNode); + this.expr = jdtNode; + } + + protected void wireArg(Argument,?> arg, AST ast) + { + Expression child = wireAndGetExpression(arg, this, ast); + + if (child.getNodeType() == ASTNode.INFIX_EXPRESSION) + { + if (!expr.getOperator().equals(((InfixExpression) child).getOperator())) + { + ParenthesizedExpression par = ast.newParenthesizedExpression(); + par.setExpression(child); + child = par; + } + } + + InfixExpression infix = expr; + if ("MISSING".equals(infix.getLeftOperand().toString())) + { + infix.setLeftOperand(child); + } else if ("MISSING".equals(infix.getRightOperand().toString())) + { + infix.setRightOperand(child); + } else + { + org.eclipse.jdt.core.dom.Expression prev = infix.getRightOperand(); + InfixExpression more = ast.newInfixExpression(); + more.setOperator(infix.getOperator()); + infix.setRightOperand(more); + prev.delete(); + more.setLeftOperand(prev); + more.setRightOperand(child); + } + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ParenImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ParenImpl.java new file mode 100644 index 00000000..d172c0e8 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ParenImpl.java @@ -0,0 +1,76 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ParenthesizedExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.ParenExpression; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.spi.ExpressionFactoryImpl; + +public class ParenImpl, + P extends ExpressionHolder> + extends ExpressionFactoryImpl,ParenthesizedExpression> + implements ParenExpression +{ + + ParenthesizedExpression paren; + + private ExpressionSource,?> inner; + + public ParenImpl(ExpressionSource child) + { + inner = (ExpressionSource,?>) child; + } + + @Override + public ParenthesizedExpression materialize(AST ast) + { + if (isMaterialized()) + { + return paren; + } + paren = ast.newParenthesizedExpression(); + + if (inner != null) + { + paren.setExpression(wireAndGetExpression(inner, this, ast)); + } + return paren; + } + + @Override + public ParenthesizedExpression getInternal() + { + return paren; + } + + @Override + public void setInternal(ParenthesizedExpression jdtNode) + { + super.setInternal(jdtNode); + this.paren = jdtNode; + } + + @Override + public ExpressionSource,?> getExpression() + { + return inner; + } + + public org.eclipse.jdt.core.dom.Expression wireAndGetExpression(ExpressionSource,?> expression, ParenExpression parent, AST ast) + { + JdtExpressionWrapper,org.eclipse.jdt.core.dom.Expression> node = (JdtExpressionWrapper,org.eclipse.jdt.core.dom.Expression>) expression; + expression.setOrigin(parent); + node.setAst(ast); + return node.materialize(ast); + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/PostFixImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/PostFixImpl.java new file mode 100644 index 00000000..664acd07 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/PostFixImpl.java @@ -0,0 +1,85 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.PostfixExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.OrdinalArgument; +import org.jboss.forge.roaster.model.expressions.PostFixExpression; +import org.jboss.forge.roaster.model.expressions.PostfixOp; +import org.jboss.forge.roaster.model.expressions.UnaryExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class PostFixImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,PostfixExpression> + implements PostFixExpression +{ + + private PostfixExpression post; + + private PostfixOp op; + private OrdinalArgument,?> arg; + + public PostFixImpl(PostfixOp op) + { + this.op = op; + } + + @Override + public PostfixExpression materialize(AST ast) + { + if (isMaterialized()) + { + return post; + } + post = ast.newPostfixExpression(); + post.setOperator(PostfixExpression.Operator.toOperator(op.getOp())); + + if (arg != null) + { + post.setOperand(wireAndGetExpression(arg, this, ast)); + } + return post; + } + + @Override + public PostfixExpression getInternal() + { + return post; + } + + @Override + public void setInternal(PostfixExpression jdtNode) + { + super.setInternal(jdtNode); + this.post = jdtNode; + } + + @Override + public Argument,?> getExpression() + { + return arg; + } + + @Override + public PostFixExpression setExpression(ExpressionSource arg) + { + this.arg = (OrdinalArgument,?>) arg; + return this; + } + + @Override + public String getOperator() + { + return op.getOp(); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SelfArgumentImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SelfArgumentImpl.java new file mode 100644 index 00000000..d77700e8 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SelfArgumentImpl.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ThisExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.ThisLiteral; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class SelfArgumentImpl, + P extends ExpressionHolder> + extends AccessorImpl,ThisExpression> + implements ThisLiteral, + ExpressionChainLink +{ + + public SelfArgumentImpl() + { + } + + private ThisExpression self; + + @Override + public ThisExpression materialize(AST ast) + { + if (isMaterialized()) + { + return self; + } + self = ast.newThisExpression(); + return self; + } + + @Override + public ThisExpression getInternal() + { + return self; + } + + @Override + public void setInternal(ThisExpression jdtNode) + { + super.setInternal(jdtNode); + this.self = jdtNode; + } + + @Override + public ExpressionSource chainExpression(ExpressionSource child) + { + return (ExpressionSource) child; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SetterImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SetterImpl.java new file mode 100644 index 00000000..2df4c2d6 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SetterImpl.java @@ -0,0 +1,80 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Setter; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class SetterImpl, + P extends ExpressionHolder> + extends AbstractMethodInvokeImpl,MethodInvocation> + implements Setter +{ + + protected MethodInvocation invoke; + + public SetterImpl(String fieldName, String type, ExpressionSource value) + { + super(JDTHelper.setter(fieldName, type)); + addArgument((Argument,?>) value); + } + + @Override + public Setter setInvocationTarget(ExpressionSource target) + { + this.target = chainExpression((ExpressionSource) target); + return this; + } + + @Override + public MethodInvocation materialize(AST ast) + { + if (isMaterialized()) + { + return invoke; + } + invoke = ast.newMethodInvocation(); + + invoke.setName(ast.newSimpleName(method)); + + if (target != null) + { + invoke.setExpression(wireAndGetExpression(target, this, ast)); + } + + for (Argument,?> arg : arguments) + { + invoke.arguments().add(wireAndGetExpression(arg, this, ast)); + } + + return invoke; + } + + + @Override + public MethodInvocation getInternal() + { + return invoke; + } + + @Override + public ExpressionSource,?> getValue() + { + if (getArguments().isEmpty()) + { + return null; + } + return getArguments().get(0); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SimpleAccessorImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SimpleAccessorImpl.java new file mode 100644 index 00000000..aa36b8d7 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SimpleAccessorImpl.java @@ -0,0 +1,36 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.Expression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.OrdinalArgument; +import org.jboss.forge.roaster.model.expressions.PostFixExpression; +import org.jboss.forge.roaster.model.expressions.PostfixOp; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class SimpleAccessorImpl, + P extends ExpressionHolder, + E extends OrdinalArgument, + J extends Expression> + extends AccessorImpl + implements OrdinalArgument +{ + + + public PostFixExpression> inc() + { + return new PostFixImpl>(PostfixOp.INC).setExpression(this); + } + + public PostFixExpression> dec() + { + return new PostFixImpl>(PostfixOp.DEC).setExpression(this); + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/StaticClassAccessorImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/StaticClassAccessorImpl.java new file mode 100644 index 00000000..5ee72ecb --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/StaticClassAccessorImpl.java @@ -0,0 +1,61 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.Name; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ClassLiteral; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class StaticClassAccessorImpl, + P extends ExpressionHolder> + extends AccessorImpl,Name> + implements ExpressionChainLink +{ + + private Name klass; + + private String name; + + public StaticClassAccessorImpl(String name) + { + this.name = name; + } + + @Override + public Name materialize(AST ast) + { + if (isMaterialized()) + { + return klass; + } + klass = ast.newName(name); + return klass; + } + + @Override + public Name getInternal() + { + return klass; + } + + @Override + public void setInternal(Name jdtNode) + { + super.setInternal(jdtNode); + this.klass = jdtNode; + } + + @Override + public ExpressionSource chainExpression(ExpressionSource child) + { + throw new UnsupportedOperationException("Should not be invoked on this"); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/StringLiteralImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/StringLiteralImpl.java new file mode 100644 index 00000000..441ae618 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/StringLiteralImpl.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.StringLiteral; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class StringLiteralImpl, + P extends ExpressionHolder> + extends AccessorImpl,org.eclipse.jdt.core.dom.StringLiteral> + implements StringLiteral +{ + + private String val; + + private org.eclipse.jdt.core.dom.StringLiteral literal; + + public StringLiteralImpl(String val) + { + this.val = val; + } + + @Override + public org.eclipse.jdt.core.dom.StringLiteral materialize(AST ast) + { + if (isMaterialized()) + { + return literal; + } + literal = ast.newStringLiteral(); + literal.setLiteralValue(val); + return literal; + } + + @Override + public org.eclipse.jdt.core.dom.StringLiteral getInternal() + { + return literal; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.StringLiteral jdtNode) + { + super.setInternal(jdtNode); + this.literal = jdtNode; + } + + public String getValue() + { + return val; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperFieldImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperFieldImpl.java new file mode 100644 index 00000000..b0c781ae --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperFieldImpl.java @@ -0,0 +1,84 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SuperFieldAccess; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class SuperFieldImpl, + P extends ExpressionHolder> + extends SimpleAccessorImpl,SuperFieldAccess> + implements Field, + ExpressionChainLink> +{ + + private SuperFieldAccess fld; + + private String fieldName; + private ExpressionSource,?> expression = new SuperImpl>(); + + public SuperFieldImpl(String fieldName) + { + this.fieldName = fieldName; + } + + @Override + public SuperFieldAccess materialize(AST ast) + { + if (isMaterialized()) + { + return fld; + } + fld = ast.newSuperFieldAccess(); + fld.setName(ast.newSimpleName(fieldName)); + return fld; + } + + @Override + public SuperFieldAccess getInternal() + { + return fld; + } + + @Override + public void setInternal(SuperFieldAccess jdtNode) + { + super.setInternal(jdtNode); + this.fld = jdtNode; + } + + @Override + public ExpressionSource,?> chainExpression(ExpressionSource child) + { + ExpressionSource,?> cast = (ExpressionSource,?>) child; + return cast; + } + + @Override + public String getFieldName() + { + return fieldName; + } + + @Override + public ExpressionSource,?> getInvocationTarget() + { + return expression; + } + + @Override + public Field setInvocationTarget(ExpressionSource target) + { + this.expression = (ExpressionSource,?>) target; + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperGetterImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperGetterImpl.java new file mode 100644 index 00000000..6185c1ea --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperGetterImpl.java @@ -0,0 +1,86 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SuperMethodInvocation; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class SuperGetterImpl, + P extends ExpressionHolder> + extends InvokeableImpl,SuperMethodInvocation> + implements Getter +{ + + protected SuperMethodInvocation invoke; + + private String fieldName; + + public SuperGetterImpl(String fieldName, String type) + { + super(JDTHelper.getter(fieldName, type)); + target = new SuperImpl>(); + this.fieldName = fieldName; + } + + public ExpressionSource,?> chainExpression(ExpressionSource child) + { + ExpressionSource,?> cast = (ExpressionSource,?>) child; + setInvocationTarget(cast); + return cast; + } + + + @Override + public Getter setInvocationTarget(ExpressionSource target) + { + chainExpression((ExpressionSource) target); + return this; + } + + @Override + public ExpressionSource,?> getInvocationTarget() + { + return target; + } + + @Override + public SuperMethodInvocation materialize(AST ast) + { + if (isMaterialized()) + { + return invoke; + } + invoke = ast.newSuperMethodInvocation(); + invoke.setName(ast.newSimpleName(method)); + return invoke; + } + + @Override + public SuperMethodInvocation getInternal() + { + return invoke; + } + + @Override + public void setInternal(SuperMethodInvocation jdtNode) + { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } + + @Override + public String getFieldName() + { + return fieldName; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperImpl.java new file mode 100644 index 00000000..4e851a3f --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperImpl.java @@ -0,0 +1,62 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.Setter; +import org.jboss.forge.roaster.model.expressions.Super; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.spi.ExpressionFactoryImpl; + +public class SuperImpl, + P extends ExpressionHolder> + extends ExpressionFactoryImpl,org.eclipse.jdt.core.dom.Expression> + implements Super +{ + + @Override + public Field> field(String fieldName) + { + return new SuperFieldImpl>(fieldName); + + } + + @Override + public Getter> getter(String field, String klass) + { + return new SuperGetterImpl>(field, klass); + } + + @Override + public Getter> getter(String field, Class klass) + { + return getter(field, klass.getName()); + } + + @Override + public MethodCallExpression> invoke(String method) + { + return new SuperMethodInvokeImpl>(method); + } + + @Override + public Setter> setter(String fldName, Class type, ExpressionSource value) + { + return setter(fldName, type.getName(), value); + } + + @Override + public Setter> setter(String fldName, String type, ExpressionSource value) + { + return new SuperSetterImpl>(fldName, type, value); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperMethodInvokeImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperMethodInvokeImpl.java new file mode 100644 index 00000000..3d34c27b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperMethodInvokeImpl.java @@ -0,0 +1,67 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SuperMethodInvocation; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class SuperMethodInvokeImpl, + P extends ExpressionHolder> + extends AbstractMethodInvokeImpl,SuperMethodInvocation> + implements MethodCallExpression +{ + + protected SuperMethodInvocation invoke; + + public SuperMethodInvokeImpl(String method) + { + super(method); + target = new SuperImpl>(); + } + + @Override + public SuperMethodInvocation materialize(AST ast) + { + if (isMaterialized()) + { + return invoke; + } + invoke = ast.newSuperMethodInvocation(); + + invoke.setName(ast.newSimpleName(method)); + for (Argument,?> arg : arguments) + { + invoke.arguments().add(wireAndGetExpression(arg, this, ast)); + } + return invoke; + } + + @Override + public SuperMethodInvocation getInternal() + { + return invoke; + } + + @Override + public MethodCallExpression setMethodName(String name) + { + this.method = name; + return this; + } + + @Override + public void setInternal(SuperMethodInvocation jdtNode) + { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperSetterImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperSetterImpl.java new file mode 100644 index 00000000..242546a9 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperSetterImpl.java @@ -0,0 +1,72 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SuperMethodInvocation; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Setter; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class SuperSetterImpl, + P extends ExpressionHolder> + extends AbstractMethodInvokeImpl,SuperMethodInvocation> + implements Setter +{ + + private SuperMethodInvocation setter; + + private ExpressionSource,?> value; + + public SuperSetterImpl(String fieldName, String type, ExpressionSource value) + { + super(JDTHelper.setter(fieldName, type)); + this.value = (ExpressionSource,?>) value; + } + + @Override + public SuperMethodInvocation materialize(AST ast) + { + if (isMaterialized()) + { + return setter; + } + setter = ast.newSuperMethodInvocation(); + setter.setName(ast.newSimpleName(method)); + if (value != null) + { + setter.arguments().add(wireAndGetExpression(value, this, ast)); + } + return setter; + } + + @Override + public SuperMethodInvocation getInternal() + { + return setter; + } + + @Override + public void setInternal(SuperMethodInvocation jdtNode) + { + super.setInternal(jdtNode); + this.setter = jdtNode; + } + + @Override + public ExpressionSource,?> getValue() + { + if (getArguments().isEmpty()) + { + return null; + } + return getArguments().get(0); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/TernaryImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/TernaryImpl.java new file mode 100644 index 00000000..aa9845c6 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/TernaryImpl.java @@ -0,0 +1,108 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ConditionalExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.TernaryExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class TernaryImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,ConditionalExpression> + implements TernaryExpression +{ + + private ConditionalExpression ternary; + + private ExpressionSource,?> condition; + private ExpressionSource,?> onTrue; + private ExpressionSource,?> onFalse; + + public TernaryImpl() + { + } + + @Override + public TernaryExpression setCondition(ExpressionSource expression) + { + this.condition = (ExpressionSource,?>) expression; + return this; + } + + @Override + public TernaryExpression setThenExpression(ExpressionSource onTrue) + { + this.onTrue = (ExpressionSource,?>) onTrue; + return this; + } + + @Override + public TernaryExpression setElseExpression(ExpressionSource onFalse) + { + this.onFalse = (ExpressionSource,?>) onFalse; + return this; + } + + @Override + public ExpressionSource,?> getCondition() + { + return condition; + } + + @Override + public ExpressionSource,?> getThenExpression() + { + return onTrue; + } + + @Override + public ExpressionSource,?> getElseExpression() + { + return onFalse; + } + + @Override + public ConditionalExpression materialize(AST ast) + { + if (isMaterialized()) + { + return ternary; + } + ternary = ast.newConditionalExpression(); + + if (condition != null) + { + ternary.setExpression(wireAndGetExpression(condition, this, ast)); + } + if (onTrue != null) + { + ternary.setThenExpression(wireAndGetExpression(onTrue, this, ast)); + } + if (onFalse != null) + { + ternary.setElseExpression(wireAndGetExpression(onFalse, this, ast)); + } + return ternary; + } + + @Override + public ConditionalExpression getInternal() + { + return ternary; + } + + @Override + public void setInternal(ConditionalExpression jdtNode) + { + super.setInternal(jdtNode); + this.ternary = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/UnaryImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/UnaryImpl.java new file mode 100644 index 00000000..16bc34af --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/UnaryImpl.java @@ -0,0 +1,83 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.PrefixExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.PrefixOp; +import org.jboss.forge.roaster.model.expressions.UnaryExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class UnaryImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,PrefixExpression> + implements UnaryExpression +{ + + PrefixExpression expr; + + private Argument,?> arg; + private PrefixOp op; + + public UnaryImpl(PrefixOp op) + { + this.op = op; + } + + @Override + public PrefixExpression materialize(AST ast) + { + if (isMaterialized()) + { + return expr; + } + expr = ast.newPrefixExpression(); + expr.setOperator(PrefixExpression.Operator.toOperator(op.getOp())); + + if (arg != null) + { + expr.setOperand(wireAndGetExpression(arg, this, ast)); + } + return expr; + } + + @Override + public PrefixExpression getInternal() + { + return expr; + } + + @Override + public void setInternal(PrefixExpression jdtNode) + { + super.setInternal(jdtNode); + this.expr = jdtNode; + } + + @Override + public Argument,?> getExpression() + { + return arg; + } + + @Override + public UnaryExpression setExpression(ExpressionSource arg) + { + this.arg = (Argument,?>) arg; + return this; + } + + @Override + public String getOperator() + { + return op.getOp(); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/VarArgumentImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/VarArgumentImpl.java new file mode 100644 index 00000000..8fdb1c23 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/VarArgumentImpl.java @@ -0,0 +1,73 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.Name; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Variable; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class VarArgumentImpl, + P extends ExpressionHolder> + extends SimpleAccessorImpl,Name> + implements Variable, + ExpressionChainLink> +{ + + private Name var; + + private String name; + + private ExpressionSource,?> expression; + + public VarArgumentImpl(String name) + { + this.name = name; + } + + @Override + public Name materialize(AST ast) + { + if (isMaterialized()) + { + return var; + } + var = ast.newName(name); + return var; + } + + @Override + public Name getInternal() + { + return var; + } + + @Override + public void setInternal(Name jdtNode) + { + super.setInternal(jdtNode); + this.var = jdtNode; + } + + @Override + public String getName() + { + return name; + } + + + @Override + public ExpressionSource,?> chainExpression(ExpressionSource child) + { + ExpressionSource,?> cast = (ExpressionSource,?>) child; + this.expression = cast; + return cast; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssertStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssertStatementImpl.java new file mode 100644 index 00000000..5d9bc16d --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssertStatementImpl.java @@ -0,0 +1,89 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.AssertStatement; + +public class AssertStatementImpl, + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.AssertStatement> + implements AssertStatement +{ + + private org.eclipse.jdt.core.dom.AssertStatement assrt; + + private ExpressionSource,?> assertion; + private ExpressionSource,?> message; + + + @Override + public org.eclipse.jdt.core.dom.AssertStatement materialize(AST ast) + { + if (assrt != null) + { + return assrt; + } + assrt = ast.newAssertStatement(); + if (assertion != null) + { + assrt.setExpression(wireAndGetExpression(assertion, this, ast)); + } + if (message != null) + { + assrt.setMessage(wireAndGetExpression(message, this, ast)); + } + return assrt; + } + + @Override + public ExpressionSource,?> getAssertion() + { + return assertion; + } + + @Override + public AssertStatement setAssertion(ExpressionSource expression) + { + ExpressionSource,?> cast = (ExpressionSource,?>) expression; + this.assertion = cast; + cast.setOrigin(this); + return this; + } + + @Override + public ExpressionSource,?> getMessage() + { + return message; + } + + @Override + public AssertStatement setMessage(ExpressionSource msg) + { + ExpressionSource,?> cast = (ExpressionSource,?>) msg; + this.message = cast; + cast.setOrigin(this); + return this; + } + + @Override + public org.eclipse.jdt.core.dom.AssertStatement getInternal() + { + return assrt; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.AssertStatement jdtNode) + { + super.setInternal(jdtNode); + this.assrt = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssignStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssignStatementImpl.java new file mode 100644 index 00000000..dfbe9674 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssignStatementImpl.java @@ -0,0 +1,109 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ExpressionStatement; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.AssignExpression; +import org.jboss.forge.roaster.model.expressions.Assignment; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.expressions.AssignImpl; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.AssignmentStatement; + +public class AssignStatementImpl, + P extends BlockSource,P>> + extends StatementImpl,ExpressionStatement> + implements AssignmentStatement +{ + + private ExpressionStatement statement; + + private AssignExpression> inner; + + public AssignStatementImpl() + { + this.inner = new AssignImpl>(Assignment.ASSIGN); + } + + public AssignStatementImpl(AssignExpression axx) + { + this.inner = (AssignExpression>) axx; + } + + @Override + public AssignmentStatement setRight(ExpressionSource right) + { + inner.setRight(right); + return this; + } + + @Override + public AssignmentStatement setLeft(Accessor left) + { + inner.setLeft(left); + return this; + } + + @Override + public Accessor>,?> getLeft() + { + return inner.getLeft(); + } + + @Override + public ExpressionSource>,?> getRight() + { + return inner.getRight(); + } + + @Override + public String getAssignmentOperator() + { + return inner.getOperator(); + } + + @Override + public AssignmentStatement setAssignmentOperator(Assignment op) + { + inner.setOperator(op); + return this; + } + + public ExpressionStatement materialize(AST ast) + { + if (statement != null) + { + return statement; + } + statement = ast.newExpressionStatement(wireAndGetExpression(inner, this, ast)); + return statement; + } + + @Override + public ExpressionStatement getInternal() + { + return statement; + } + + @Override + public void setInternal(ExpressionStatement jdtNode) + { + super.setInternal(jdtNode); + this.statement = jdtNode; + } + + @Override + public ExpressionSource,?> getExpr() + { + return inner; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BlockStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BlockStatementImpl.java new file mode 100644 index 00000000..a3d895a1 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BlockStatementImpl.java @@ -0,0 +1,36 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +public class BlockStatementImpl, + P extends BlockHolder> + extends InnerBlockImpl> + implements BlockStatement +{ + + public BlockStatementImpl() + { + } + + public BlockStatementImpl(StatementSource body) + { + addStatement(body); + } + + @Override + public BlockStatement setLabel(String label) + { + this.label = label; + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BodiedStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BodiedStatementImpl.java new file mode 100644 index 00000000..a5e58cd6 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BodiedStatementImpl.java @@ -0,0 +1,66 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.LabeledStatement; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.BodyHolder; +import org.jboss.forge.roaster.model.statements.Statement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +public abstract class BodiedStatementImpl, + P extends BlockHolder, + S extends Statement&BodyHolder, + J extends org.eclipse.jdt.core.dom.Statement> + extends StatementImpl + implements BodyHolder, + JdtStatementWrapper +{ + + protected BodiedStatementImpl() + { + } + + protected BlockStatement wrap(StatementSource body) + { + if (body instanceof BlockStatement) + { + return (BlockStatement) body; + } else + { + BlockStatement block = new BlockStatementImpl(body); + return block; + } + } + + + public org.eclipse.jdt.core.dom.Statement wireAndGetStatement(Statement statement, S parent, AST ast) + { + ASTNode node = (ASTNode) statement; + statement.setOrigin(parent); + node.setAst(ast); + org.eclipse.jdt.core.dom.Statement stat = node.materialize(ast); + + if (statement.hasLabel()) + { + LabeledStatement labeledStatement = ast.newLabeledStatement(); + labeledStatement.setBody(stat); + labeledStatement.setLabel(ast.newSimpleName(statement.getLabel())); + return labeledStatement; + } else + { + return stat; + } + } + + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BreakStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BreakStatementImpl.java new file mode 100644 index 00000000..304973a9 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BreakStatementImpl.java @@ -0,0 +1,69 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BreakStatement; + +public class BreakStatementImpl, + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.BreakStatement> + implements BreakStatement +{ + + private org.eclipse.jdt.core.dom.BreakStatement ret; + + private String breakToLabel; + + public BreakStatementImpl() + { + } + + @Override + public org.eclipse.jdt.core.dom.BreakStatement materialize(AST ast) + { + if (ret != null) + { + return ret; + } + ret = ast.newBreakStatement(); + if (breakToLabel != null) + { + ret.setLabel(ast.newSimpleName(breakToLabel)); + } + return ret; + } + + @Override + public String getBreakToLabel() + { + return breakToLabel; + } + + @Override + public BreakStatement setBreakToLabel(String label) + { + this.breakToLabel = label; + return this; + } + + @Override + public org.eclipse.jdt.core.dom.BreakStatement getInternal() + { + return ret; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.BreakStatement jdtNode) + { + super.setInternal(jdtNode); + this.ret = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ContinueStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ContinueStatementImpl.java new file mode 100644 index 00000000..0ef869ca --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ContinueStatementImpl.java @@ -0,0 +1,69 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.ContinueStatement; + +public class ContinueStatementImpl, + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.ContinueStatement> + implements ContinueStatement +{ + + private org.eclipse.jdt.core.dom.ContinueStatement ret; + + private String continueToLabel; + + public ContinueStatementImpl() + { + } + + @Override + public org.eclipse.jdt.core.dom.ContinueStatement materialize(AST ast) + { + if (ret != null) + { + return ret; + } + ret = ast.newContinueStatement(); + if (continueToLabel != null) + { + ret.setLabel(ast.newSimpleName(continueToLabel)); + } + return ret; + } + + @Override + public org.eclipse.jdt.core.dom.ContinueStatement getInternal() + { + return ret; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.ContinueStatement jdtNode) + { + super.setInternal(jdtNode); + this.ret = jdtNode; + } + + @Override + public String getContinueToLabel() + { + return continueToLabel; + } + + @Override + public ContinueStatement setContinueToLabel(String label) + { + this.continueToLabel = label; + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DeclareStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DeclareStatementImpl.java new file mode 100644 index 00000000..4ebaaa86 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DeclareStatementImpl.java @@ -0,0 +1,176 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.eclipse.jdt.core.dom.VariableDeclarationStatement; +import org.jboss.forge.roaster.model.expressions.BasicExpressionFactory; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.impl.expressions.BasicExpressionFactoryImpl; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.DeclareStatement; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DeclareStatementImpl, + P extends BlockHolder> + extends StatementImpl,VariableDeclarationStatement> + implements DeclareStatement +{ + + private VariableDeclarationStatement var; + + private BasicExpressionFactory> factory = new BasicExpressionFactoryImpl,org.eclipse.jdt.core.dom.Expression>(); + + private Map,?>> initializers = new HashMap,?>>(); + private List names = new ArrayList(); + private String type; + + public DeclareStatementImpl() + { + } + + @Override + public List getVariableNames() + { + return names; + } + + @Override + public String getVariableName() + { + return names.get(0); + } + + @Override + public String getVariableType() + { + return type; + } + + @Override + public DeclareStatement setVariable(Class klass, String name) + { + return setVariable(klass.getName(), name); + } + + @Override + public DeclareStatement setVariable(String klass, String name) + { + return setVariable(klass, name, null); + } + + @Override + public DeclareStatement setVariable(Class klass, String name, ExpressionSource init) + { + return setVariable(klass.getName(), name, init); + } + + @Override + public DeclareStatement setVariable(String klass, String name, ExpressionSource init) + { + this.type = klass; + names.add(name); + if (init != null) + { + initializers.put(name, (ExpressionSource,?>) init); + } + return this; + } + + @Override + public DeclareStatement addVariable(String name) + { + names.add(name); + return this; + } + + @Override + public DeclareStatement addVariable(String name, ExpressionSource init) + { + addVariable(name); + if (init != null) + { + this.initializers.put(name, (ExpressionSource,?>) init); + } + return this; + } + + + @Override + public boolean isSingleDeclaration() + { + return names.size() == 1; + } + + + @Override + public Map,?>> getInitExpressions() + { + return Collections.unmodifiableMap(initializers); + } + + @Override + public ExpressionSource,?> getInitExpression() + { + return initializers.containsKey(getVariableName()) ? initializers.get(getVariableName()) : null; + } + + @Override + public VariableDeclarationStatement materialize(AST ast) + { + if (var != null) + { + return var; + } + + List vdfs = new ArrayList(names.size()); + for (String name : names) + { + VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment(); + vdf.setName(ast.newSimpleName(name)); + if (initializers.containsKey(name)) + { + vdf.setInitializer(wireAndGetExpression(initializers.get(name), this, ast)); + } + vdfs.add(vdf); + } + + var = ast.newVariableDeclarationStatement(vdfs.get(0)); + if (vdfs.size() > 1) + { + for (int j = 1; j < vdfs.size(); j++) + { + var.fragments().add(vdfs.get(j)); + } + } + + var.setType(JDTHelper.getType(type, ast)); + return var; + } + + @Override + public VariableDeclarationStatement getInternal() + { + return var; + } + + @Override + public void setInternal(VariableDeclarationStatement jdtNode) + { + super.setInternal(jdtNode); + this.var = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DoWhileStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DoWhileStatementImpl.java new file mode 100644 index 00000000..ed6c05b2 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DoWhileStatementImpl.java @@ -0,0 +1,102 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.DoStatement; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.DoWhileStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +public class DoWhileStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,DoStatement> + implements DoWhileStatement +{ + + private DoStatement rep; + + private BlockStatement> body; + private ExpressionSource,?> condition; + + public DoWhileStatementImpl() + { + } + + @Override + public BlockStatement> getBody() + { + return body; + } + + @Override + public DoWhileStatement setBody(BlockSource body) + { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } + + @Override + public DoWhileStatement setBody(StatementSource statement) + { + this.body = wrap(statement); + return this; + } + + @Override + public ExpressionSource,?> getCondition() + { + return condition; + } + + @Override + public DoWhileStatement setCondition(ExpressionSource expr) + { + ExpressionSource,?> cast = (ExpressionSource,?>) expr; + this.condition = cast; + return this; + } + + @Override + public DoStatement materialize(AST ast) + { + if (rep != null) + { + return rep; + } + rep = ast.newDoStatement(); + + if (body != null) + { + rep.setBody(wireAndGetStatement(body, this, ast)); + } + if (condition != null) + { + rep.setExpression(wireAndGetExpression(condition, this, ast)); + } + return rep; + } + + @Override + public DoStatement getInternal() + { + return rep; + } + + @Override + public void setInternal(DoStatement jdtNode) + { + super.setInternal(jdtNode); + this.rep = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ExpressionStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ExpressionStatementImpl.java new file mode 100644 index 00000000..6bf033bb --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ExpressionStatementImpl.java @@ -0,0 +1,75 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.EvalExpressionStatement; + +public class ExpressionStatementImpl, + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.ExpressionStatement> + implements EvalExpressionStatement +{ + + private org.eclipse.jdt.core.dom.ExpressionStatement exprStatement; + + private ExpressionSource,?> expression; + + public ExpressionStatementImpl(ExpressionSource expr) + { + setExpr(expr); + } + + + public ExpressionStatementImpl() + { + } + + @Override + public org.eclipse.jdt.core.dom.ExpressionStatement materialize(AST ast) + { + if (isMaterialized()) + { + return exprStatement; + } + exprStatement = ast.newExpressionStatement(wireAndGetExpression(expression, this, getAst())); + return exprStatement; + } + + @Override + public org.eclipse.jdt.core.dom.ExpressionStatement getInternal() + { + return exprStatement; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.ExpressionStatement jdtNode) + { + super.setInternal(jdtNode); + this.exprStatement = jdtNode; + } + + @Override + public ExpressionSource,?> getExpr() + { + return expression; + } + + @Override + public EvalExpressionStatement setExpr(ExpressionSource expr) + { + ExpressionSource,?> cast = (ExpressionSource,?>) expr; + this.expression = cast; + cast.setOrigin(this); + return this; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ForEachStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ForEachStatementImpl.java new file mode 100644 index 00000000..0d8b6146 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ForEachStatementImpl.java @@ -0,0 +1,135 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.EnhancedForStatement; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.ForEachStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +public class ForEachStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,EnhancedForStatement> + implements ForEachStatement +{ + + private EnhancedForStatement iter; + + private String name; + private String type; + private ExpressionSource,?> source; + private BlockStatement> body; + + public ForEachStatementImpl() + { + } + + @Override + public String getIteratorName() + { + return name; + } + + @Override + public String getIteratorType() + { + return type; + } + + @Override + public ForEachStatement setIterator(String klass, String name) + { + this.name = name; + this.type = klass; + return this; + } + + @Override + public ForEachStatement setIterator(Class klass, String name) + { + return setIterator(klass.getName(), name); + } + + @Override + public ExpressionSource,?> getSource() + { + return source; + } + + @Override + public ForEachStatement setSource(ExpressionSource expr) + { + ExpressionSource,?> cast = (ExpressionSource,?>) expr; + this.source = cast; + return this; + } + + @Override + public EnhancedForStatement materialize(AST ast) + { + if (iter != null) + { + return iter; + } + iter = ast.newEnhancedForStatement(); + + iter.getParameter().setName(iter.getAST().newSimpleName(name)); + iter.getParameter().setType(JDTHelper.getType(type, iter.getAST())); + + if (source != null) + { + iter.setExpression(wireAndGetExpression(source, this, ast)); + } + if (body != null) + { + iter.setBody(wireAndGetStatement(body, this, ast)); + } + return iter; + } + + @Override + public EnhancedForStatement getInternal() + { + return iter; + } + + @Override + public void setInternal(EnhancedForStatement jdtNode) + { + super.setInternal(jdtNode); + this.iter = jdtNode; + } + + @Override + public BlockStatement> getBody() + { + return body; + } + + @Override + public ForEachStatement setBody(BlockSource body) + { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } + + @Override + public ForEachStatement setBody(StatementSource statement) + { + this.body = wrap(statement); + return this; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ForStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ForStatementImpl.java new file mode 100644 index 00000000..29301ee4 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ForStatementImpl.java @@ -0,0 +1,153 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.ForStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class ForStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,org.eclipse.jdt.core.dom.ForStatement> + implements ForStatement +{ + + private org.eclipse.jdt.core.dom.ForStatement iter; + + private DeclareExpression> declaration; + private List,?>> updaters = Collections.EMPTY_LIST; + private ExpressionSource,?> condition; + private BlockStatement> body; + + public ForStatementImpl() + { + } + + @Override + public DeclareExpression> getDeclaration() + { + return declaration; + } + + @Override + public ForStatement setDeclaration(DeclareExpression declaration) + { + DeclareExpression> cast = (DeclareExpression>) declaration; + this.declaration = cast; + return this; + } + + @Override + public ExpressionSource,?> getCondition() + { + return condition; + } + + @Override + public ForStatement addUpdate(ExpressionSource expression) + { + ExpressionSource,?> cast = (ExpressionSource,?>) expression; + if (updaters.isEmpty()) + { + updaters = new LinkedList,?>>(); + } + updaters.add(cast); + return this; + } + + @Override + public ForStatement setCondition(ExpressionSource booleanExpression) + { + ExpressionSource,?> cast = (ExpressionSource,?>) booleanExpression; + this.condition = cast; + return this; + } + + @Override + public List,?>> getUpdates() + { + return Collections.unmodifiableList(updaters); + } + + @Override + public BlockStatement> getBody() + { + return body; + } + + @Override + public ForStatement setBody(BlockSource body) + { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } + + @Override + public ForStatement setBody(StatementSource statement) + { + this.body = wrap(statement); + return this; + } + + @Override + public org.eclipse.jdt.core.dom.ForStatement materialize(AST ast) + { + if (iter != null) + { + return iter; + } + + iter = ast.newForStatement(); + + if (declaration != null) + { + iter.initializers().add(wireAndGetExpression(declaration, this, ast)); + } + + if (condition != null) + { + iter.setExpression(wireAndGetExpression(condition, this, ast)); + } + + for (ExpressionSource,?> updater : updaters) + { + iter.updaters().add(wireAndGetExpression(updater, this, ast)); + } + + if (body != null) + { + iter.setBody(wireAndGetStatement(body, this, ast)); + } + + return iter; + } + + @Override + public org.eclipse.jdt.core.dom.ForStatement getInternal() + { + return iter; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.ForStatement jdtNode) + { + super.setInternal(jdtNode); + this.iter = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/IfStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/IfStatementImpl.java new file mode 100644 index 00000000..51894f90 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/IfStatementImpl.java @@ -0,0 +1,146 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.IfStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +public class IfStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,org.eclipse.jdt.core.dom.IfStatement> + implements IfStatement +{ + + private org.eclipse.jdt.core.dom.IfStatement iff; + + private ExpressionSource,?> condition; + private BlockStatement> thenBody; + private BlockStatement> elseBody; + + public IfStatementImpl() + { + } + + @Override + public ExpressionSource,?> getCondition() + { + return condition; + } + + @Override + public IfStatement setCondition(ExpressionSource condition) + { + ExpressionSource,?> cast = (ExpressionSource,?>) condition; + this.condition = cast; + return this; + } + + @Override + public BlockStatement> getThen() + { + return thenBody; + } + + @Override + public IfStatement setThen(BlockSource body) + { + BlockStatement> cast = (BlockStatement>) body; + this.thenBody = cast; + return this; + } + + @Override + public BlockStatement> getElse() + { + return elseBody; + } + + @Override + public IfStatement setThen(StatementSource statement) + { + this.thenBody = wrap(statement); + return this; + } + + @Override + public BlockStatement> getBody() + { + return thenBody; + } + + @Override + public IfStatement setBody(BlockSource body) + { + return setThen(body); + } + + @Override + public IfStatement setBody(StatementSource statement) + { + return setThen(statement); + } + + @Override + public IfStatement setElse(BlockSource body) + { + BlockStatement> cast = (BlockStatement>) body; + this.elseBody = cast; + return this; + } + + @Override + public IfStatement setElse(StatementSource statement) + { + this.elseBody = wrap(statement); + return this; + } + + + @Override + public org.eclipse.jdt.core.dom.IfStatement materialize(AST ast) + { + if (iff != null) + { + return iff; + } + iff = ast.newIfStatement(); + + if (condition != null) + { + iff.setExpression(wireAndGetExpression(condition, this, ast)); + } + if (thenBody != null) + { + iff.setThenStatement(wireAndGetStatement(thenBody, this, ast)); + } + if (elseBody != null) + { + iff.setElseStatement(wireAndGetStatement(elseBody, this, ast)); + } + return iff; + } + + @Override + public org.eclipse.jdt.core.dom.IfStatement getInternal() + { + return iff; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.IfStatement jdtNode) + { + super.setInternal(jdtNode); + this.iff = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InnerBlockImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InnerBlockImpl.java new file mode 100644 index 00000000..050519f7 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InnerBlockImpl.java @@ -0,0 +1,124 @@ +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.LabeledStatement; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.BlockImpl; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.Statement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public abstract class InnerBlockImpl, + P extends BlockHolder, + B extends BlockSource> + extends BlockImpl + implements BlockSource +{ + + private List> statements = Collections.EMPTY_LIST; + + @Override + public org.eclipse.jdt.core.dom.Block materialize(AST ast) + { + if (isMaterialized()) + { + return jdtBlock; + } + + jdtBlock = ast.newBlock(); + + for (Statement statement : statements) + { + jdtBlock.statements().add(wireAndGetStatement(statement, (B) this, ast)); + } + + return jdtBlock; + } + + + @Override + public List> getStatements() + { + return Collections.unmodifiableList(statements); + } + + @Override + public BlockSource addStatement(StatementSource statement) + { + return addStatement(statements.size(), statement); + } + + @Override + public BlockSource addStatement(int index, StatementSource statement) + { + StatementSource cast = (StatementSource) statement; + ASTNode rstStatement = (ASTNode) statement; + if (rstStatement.isMaterialized() && parentBlock(rstStatement) != jdtBlock) + { + throw new RuntimeException("Trying to add a statement belonging to a different block"); + } + if (statements.isEmpty()) + { + statements = new LinkedList>(); + } + statements.add(index, cast); + if (isMaterialized() && !rstStatement.isMaterialized()) + { + jdtBlock.statements().add(index, wireAndGetStatement(cast, (B) this, getAst())); + } + return this; + } + + private org.eclipse.jdt.core.dom.ASTNode parentBlock(ASTNode rstStatement) + { + org.eclipse.jdt.core.dom.ASTNode node = rstStatement.getInternal().getParent(); + if (node instanceof LabeledStatement) + { + node = node.getParent(); + } + return node; + } + + @Override + public BlockSource addStatement(ExpressionSource expression) + { + return addStatement(new ExpressionStatementImpl((ExpressionSource) expression)); + } + + @Override + public BlockSource addStatement(int index, ExpressionSource expression) + { + return addStatement(index, new ExpressionStatementImpl((ExpressionSource) expression)); + } + + public BlockSource removeStatement(StatementSource statement) + { + if (!statements.remove(statement)) + { + throw new RuntimeException("Trying to remove a not existing statement"); + } + + if (((JdtStatementWrapper) statement).isMaterialized()) + { + ((JdtStatementWrapper) statement).getInternal().delete(); + } + return this; + } + + public BlockSource removeStatement(int index) + { + if (statements == null) + { + throw new RuntimeException("Trying to remove a not existing statement"); + } + return removeStatement(statements.get(index)); + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InvokeStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InvokeStatementImpl.java new file mode 100644 index 00000000..cce99cc3 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InvokeStatementImpl.java @@ -0,0 +1,129 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ExpressionStatement; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ArgumentHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.expressions.MethodWithArgumentsInvokeExpression; +import org.jboss.forge.roaster.model.impl.expressions.MethodInvokeImpl; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.InvokeStatement; + +import java.util.Collections; +import java.util.List; + +public class InvokeStatementImpl, + P extends BlockHolder> + extends StatementImpl,ExpressionStatement> + implements InvokeStatement +{ + + private ExpressionStatement statement; + + private MethodInvokeExpression,?> inner; + + public InvokeStatementImpl() + { + inner = new MethodInvokeImpl>(null); + } + + public InvokeStatementImpl(MethodInvokeExpression expr) + { + this.inner = (MethodInvokeExpression,?>) expr; + } + + @Override + public String getMethodName() + { + return inner.getMethodName(); + } + + public InvokeStatement setMethodName(String method) + { + if (inner instanceof MethodCallExpression) + { + ((MethodCallExpression) inner).setMethodName(method); + } + return this; + } + + @Override + public Accessor,?>,?> getInvocationTarget() + { + return (Accessor,?>,?>) inner.getInvocationTarget(); + } + + @Override + public InvokeStatement setInvocationTarget(Accessor target) + { + inner.setInvocationTarget(target); + return this; + } + + @Override + public List,?>,?>> getArguments() + { + if (inner instanceof MethodWithArgumentsInvokeExpression) + { + return Collections.unmodifiableList(MethodWithArgumentsInvokeExpression.class.cast(inner).getArguments()); + } else + { + return Collections.emptyList(); + } + } + + @Override + public InvokeStatement addArgument(Argument argument) + { + if (inner instanceof ArgumentHolder) + { + ((ArgumentHolder,?>) inner).addArgument(argument); + } else + { + throw new UnsupportedOperationException("This method does not support arguments"); + } + return this; + } + + @Override + public ExpressionStatement materialize(AST ast) + { + if (statement != null) + { + return statement; + } + statement = ast.newExpressionStatement(wireAndGetExpression(inner, this, ast)); + return statement; + } + + @Override + public ExpressionStatement getInternal() + { + return statement; + } + + @Override + public void setInternal(ExpressionStatement jdtNode) + { + super.setInternal(jdtNode); + this.statement = jdtNode; + } + + @Override + public ExpressionSource,?> getExpr() + { + return inner; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/JdtStatementWrapper.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/JdtStatementWrapper.java new file mode 100644 index 00000000..b5ef8747 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/JdtStatementWrapper.java @@ -0,0 +1,26 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.Statement; + +public interface JdtStatementWrapper, + P extends BlockHolder, + J extends org.eclipse.jdt.core.dom.Statement> + extends ASTNode +{ + + // return type is not J because of LabeledStatements which wrap the actual Statement type + org.eclipse.jdt.core.dom.Statement wireAndGetStatement(Statement statement, + P parent, + AST ast); +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ReturnStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ReturnStatementImpl.java new file mode 100644 index 00000000..e911c190 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ReturnStatementImpl.java @@ -0,0 +1,71 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.ReturnStatement; + +public class ReturnStatementImpl, + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.ReturnStatement> + implements ReturnStatement +{ + + private org.eclipse.jdt.core.dom.ReturnStatement ret; + + private ExpressionSource,?> result; + + public ReturnStatementImpl() + { + } + + public ReturnStatement setReturn(ExpressionSource expression) + { + ExpressionSource,?> cast = (ExpressionSource,?>) expression; + result = cast; + cast.setOrigin(this); + return this; + } + + public ExpressionSource,?> getReturn() + { + return result; + } + + @Override + public org.eclipse.jdt.core.dom.ReturnStatement materialize(AST ast) + { + if (isMaterialized()) + { + return ret; + } + ret = ast.newReturnStatement(); + + if (result != null) + { + ret.setExpression(wireAndGetExpression(result, this, ast)); + } + return ret; + } + + @Override + public org.eclipse.jdt.core.dom.ReturnStatement getInternal() + { + return ret; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.ReturnStatement jdtNode) + { + super.setInternal(jdtNode); + this.ret = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementBuilder.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementBuilder.java new file mode 100644 index 00000000..4cbb19b6 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementBuilder.java @@ -0,0 +1,288 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AssertStatement; +import org.eclipse.jdt.core.dom.Block; +import org.eclipse.jdt.core.dom.BreakStatement; +import org.eclipse.jdt.core.dom.CatchClause; +import org.eclipse.jdt.core.dom.ConstructorInvocation; +import org.eclipse.jdt.core.dom.ContinueStatement; +import org.eclipse.jdt.core.dom.DoStatement; +import org.eclipse.jdt.core.dom.EnhancedForStatement; +import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jdt.core.dom.ExpressionStatement; +import org.eclipse.jdt.core.dom.ForStatement; +import org.eclipse.jdt.core.dom.IfStatement; +import org.eclipse.jdt.core.dom.LabeledStatement; +import org.eclipse.jdt.core.dom.ReturnStatement; +import org.eclipse.jdt.core.dom.Statement; +import org.eclipse.jdt.core.dom.SuperConstructorInvocation; +import org.eclipse.jdt.core.dom.SwitchCase; +import org.eclipse.jdt.core.dom.SwitchStatement; +import org.eclipse.jdt.core.dom.SynchronizedStatement; +import org.eclipse.jdt.core.dom.ThrowStatement; +import org.eclipse.jdt.core.dom.TryStatement; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.eclipse.jdt.core.dom.VariableDeclarationStatement; +import org.eclipse.jdt.core.dom.WhileStatement; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.AssignExpression; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.impl.BlockImpl; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.DeclareStatement; +import org.jboss.forge.roaster.model.statements.SuperStatement; +import org.jboss.forge.roaster.model.statements.ThisStatement; +import org.jboss.forge.roaster.model.statements.TryCatchStatement; + +import static org.eclipse.jdt.core.dom.ASTNode.ASSERT_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.BLOCK; +import static org.eclipse.jdt.core.dom.ASTNode.BREAK_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.CONSTRUCTOR_INVOCATION; +import static org.eclipse.jdt.core.dom.ASTNode.CONTINUE_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.DO_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.EMPTY_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.ENHANCED_FOR_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.EXPRESSION_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.FOR_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.IF_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.LABELED_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.RETURN_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.SUPER_CONSTRUCTOR_INVOCATION; +import static org.eclipse.jdt.core.dom.ASTNode.SWITCH_CASE; +import static org.eclipse.jdt.core.dom.ASTNode.SWITCH_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.SYNCHRONIZED_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.THROW_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.TRY_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.VARIABLE_DECLARATION_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.WHILE_STATEMENT; +import static org.jboss.forge.roaster.model.expressions.Expressions.declare; +import static org.jboss.forge.roaster.model.impl.expressions.ExpressionBuilder.asRoasterExpression; +import static org.jboss.forge.roaster.model.statements.Statements.newAssert; +import static org.jboss.forge.roaster.model.statements.Statements.newBlock; +import static org.jboss.forge.roaster.model.statements.Statements.newBreak; +import static org.jboss.forge.roaster.model.statements.Statements.newCase; +import static org.jboss.forge.roaster.model.statements.Statements.newContinue; +import static org.jboss.forge.roaster.model.statements.Statements.newDeclare; +import static org.jboss.forge.roaster.model.statements.Statements.newDoWhile; +import static org.jboss.forge.roaster.model.statements.Statements.newFor; +import static org.jboss.forge.roaster.model.statements.Statements.newForEach; +import static org.jboss.forge.roaster.model.statements.Statements.newIf; +import static org.jboss.forge.roaster.model.statements.Statements.newReturn; +import static org.jboss.forge.roaster.model.statements.Statements.newSuper; +import static org.jboss.forge.roaster.model.statements.Statements.newSwitch; +import static org.jboss.forge.roaster.model.statements.Statements.newSynchronized; +import static org.jboss.forge.roaster.model.statements.Statements.newThis; +import static org.jboss.forge.roaster.model.statements.Statements.newThrow; +import static org.jboss.forge.roaster.model.statements.Statements.newTryCatch; +import static org.jboss.forge.roaster.model.statements.Statements.newWhile; + +public class StatementBuilder +{ + + public static org.jboss.forge.roaster.model.statements.StatementSource asRoasterStatement(Statement jdtStatement) + { + org.jboss.forge.roaster.model.statements.StatementSource roasterStmt = null; + switch (jdtStatement.getNodeType()) + { + case RETURN_STATEMENT: + ReturnStatement returnStatement = (ReturnStatement) jdtStatement; + roasterStmt = newReturn() + .setReturn(asRoasterExpression(returnStatement.getExpression())); + break; + case ASSERT_STATEMENT: + AssertStatement assertStatement = (AssertStatement) jdtStatement; + roasterStmt = newAssert() + .setAssertion(asRoasterExpression(assertStatement.getExpression())) + .setMessage(asRoasterExpression(assertStatement.getMessage())); + break; + case BLOCK: + Block block = (Block) jdtStatement; + roasterStmt = newBlock(); + ((BlockImpl) roasterStmt).setInternal(block); + for (Object o : block.statements()) + { + ((BlockStatement) roasterStmt).addStatement(StatementBuilder.asRoasterStatement((Statement) o)); + } + break; + case VARIABLE_DECLARATION_STATEMENT: + VariableDeclarationStatement vds = (VariableDeclarationStatement) jdtStatement; + DeclareStatement declareStatement = newDeclare(); + for (Object var : vds.fragments()) + { + VariableDeclarationFragment vdf = (VariableDeclarationFragment) var; + if (vdf.getInitializer() != null) + { + declareStatement.setVariable(vds.getType().toString(), vdf.getName().toString(), asRoasterExpression(vdf.getInitializer())); + } else + { + declareStatement.setVariable(vds.getType().toString(), vdf.getName().toString()); + } + } + roasterStmt = declareStatement; + break; + case EMPTY_STATEMENT: + roasterStmt = null; + break; + case EXPRESSION_STATEMENT: + ExpressionSource expr = asRoasterExpression(((ExpressionStatement) jdtStatement).getExpression()); + if (expr instanceof MethodInvokeExpression) + { + roasterStmt = new InvokeStatementImpl((MethodInvokeExpression) expr); + } else if (expr instanceof AssignExpression) + { + roasterStmt = new AssignStatementImpl((AssignExpression) expr); + } else + { + roasterStmt = new ExpressionStatementImpl(expr); + } + break; + case CONSTRUCTOR_INVOCATION: + ThisStatement call = newThis(); + ConstructorInvocation civ = (ConstructorInvocation) jdtStatement; + for (Object o : civ.arguments()) + { + call.addArgument((Argument) asRoasterExpression((Expression) o)); + } + roasterStmt = call; + break; + case SUPER_CONSTRUCTOR_INVOCATION: + SuperStatement supCall = newSuper(); + SuperConstructorInvocation sup = (SuperConstructorInvocation) jdtStatement; + for (Object o : sup.arguments()) + { + supCall.addArgument((Argument) asRoasterExpression((Expression) o)); + } + roasterStmt = supCall; + break; + case WHILE_STATEMENT: + WhileStatement whil = (WhileStatement) jdtStatement; + roasterStmt = newWhile() + .setCondition(asRoasterExpression(whil.getExpression())) + .setBody(asRoasterStatement(whil.getBody())); + + break; + case DO_STATEMENT: + DoStatement doWhil = (DoStatement) jdtStatement; + roasterStmt = newDoWhile() + .setCondition(asRoasterExpression(doWhil.getExpression())) + .setBody(asRoasterStatement(doWhil.getBody())); + + break; + case BREAK_STATEMENT: + BreakStatement brik = (BreakStatement) jdtStatement; + roasterStmt = brik.getLabel() != null ? newBreak().setBreakToLabel(brik.getLabel().toString()) : newBreak(); + break; + case CONTINUE_STATEMENT: + ContinueStatement cont = (ContinueStatement) jdtStatement; + roasterStmt = newContinue().setContinueToLabel(cont.getLabel().toString()); + break; + case LABELED_STATEMENT: + LabeledStatement lbl = (LabeledStatement) jdtStatement; + roasterStmt = asRoasterStatement(lbl.getBody()); + roasterStmt.setLabel(lbl.getLabel().toString()); + jdtStatement = ((LabeledStatement) jdtStatement).getBody(); + break; + case ENHANCED_FOR_STATEMENT: + EnhancedForStatement foreach = (EnhancedForStatement) jdtStatement; + roasterStmt = newForEach().setIterator(foreach.getParameter().getType().toString(), foreach.getParameter().getName().toString()) + .setSource(asRoasterExpression(foreach.getExpression())) + .setBody(asRoasterStatement(foreach.getBody())); + break; + case TRY_STATEMENT: + TryStatement trial = (TryStatement) jdtStatement; + TryCatchStatement tryCatchStatement = newTryCatch() + .setBody(asRoasterStatement(trial.getBody())) + .setFinally(asRoasterStatement(trial.getFinally())); + for (Object o : trial.catchClauses()) + { + CatchClause c = (CatchClause) o; + tryCatchStatement.addCatch(declare(c.getException().getType().toString(), c.getException().getName().toString()), + asRoasterStatement(c.getBody())); + } + roasterStmt = tryCatchStatement; + break; + case THROW_STATEMENT: + ThrowStatement trow = (ThrowStatement) jdtStatement; + roasterStmt = newThrow().setThrowable(asRoasterExpression(trow.getExpression())); + break; + case IF_STATEMENT: + IfStatement iff = (IfStatement) jdtStatement; + org.jboss.forge.roaster.model.statements.IfStatement rstIf = newIf().setCondition(asRoasterExpression(iff.getExpression())); + if (iff.getThenStatement() != null) + { + rstIf.setThen(asRoasterStatement(iff.getThenStatement())); + } + if (iff.getElseStatement() != null) + { + rstIf.setElse(asRoasterStatement(iff.getElseStatement())); + } + roasterStmt = rstIf; + break; + case FOR_STATEMENT: + ForStatement forst = (ForStatement) jdtStatement; + org.jboss.forge.roaster.model.statements.ForStatement rstFor = newFor(); + if (forst.getExpression() != null) + { + rstFor.setCondition(asRoasterExpression(forst.getExpression())); + } + if (forst.getBody() != null) + { + rstFor.setBody(asRoasterStatement(forst.getBody())); + } + for (Object init : forst.initializers()) + { + rstFor.setDeclaration((DeclareExpression) asRoasterExpression((Expression) init)); + } + for (Object updt : forst.updaters()) + { + rstFor.addUpdate(asRoasterExpression((Expression) updt)); + } + roasterStmt = rstFor; + break; + case SYNCHRONIZED_STATEMENT: + SynchronizedStatement synch = (SynchronizedStatement) jdtStatement; + roasterStmt = newSynchronized().setSynchOn(asRoasterExpression(synch.getExpression())) + .setBody(asRoasterStatement(synch.getBody())); + break; + case SWITCH_STATEMENT: + SwitchStatement swicc = (SwitchStatement) jdtStatement; + org.jboss.forge.roaster.model.statements.SwitchStatement rstSwitch = newSwitch().setSwitch(asRoasterExpression(swicc.getExpression())); + for (Object o : swicc.statements()) + { + rstSwitch.addStatement(asRoasterStatement((Statement) o)); + } + roasterStmt = rstSwitch; + break; + case SWITCH_CASE: + SwitchCase cas = (SwitchCase) jdtStatement; + if (!cas.isDefault()) + { + roasterStmt = newCase().setCaseExpression(asRoasterExpression(cas.getExpression())); + } else + { + roasterStmt = newCase(); + } + break; + default: + throw new UnsupportedOperationException(" Unknown statement type : " + jdtStatement.getNodeType() + " >> " + jdtStatement.toString()); + } + if (roasterStmt != null) + { + ((org.jboss.forge.roaster.model.ASTNode) roasterStmt).setInternal(jdtStatement); + } + return roasterStmt; + } + + +} + + diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementFactoryImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementFactoryImpl.java new file mode 100644 index 00000000..2bf2184b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementFactoryImpl.java @@ -0,0 +1,162 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.AssertStatement; +import org.jboss.forge.roaster.model.statements.AssignmentStatement; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.BreakStatement; +import org.jboss.forge.roaster.model.statements.ContinueStatement; +import org.jboss.forge.roaster.model.statements.DeclareStatement; +import org.jboss.forge.roaster.model.statements.DoWhileStatement; +import org.jboss.forge.roaster.model.statements.EvalExpressionStatement; +import org.jboss.forge.roaster.model.statements.ForEachStatement; +import org.jboss.forge.roaster.model.statements.ForStatement; +import org.jboss.forge.roaster.model.statements.IfStatement; +import org.jboss.forge.roaster.model.statements.InvokeStatement; +import org.jboss.forge.roaster.model.statements.ReturnStatement; +import org.jboss.forge.roaster.model.statements.SuperStatement; +import org.jboss.forge.roaster.model.statements.SwitchCaseStatement; +import org.jboss.forge.roaster.model.statements.SwitchStatement; +import org.jboss.forge.roaster.model.statements.SynchStatement; +import org.jboss.forge.roaster.model.statements.ThisStatement; +import org.jboss.forge.roaster.model.statements.ThrowStatement; +import org.jboss.forge.roaster.model.statements.TryCatchStatement; +import org.jboss.forge.roaster.model.statements.WhileStatement; +import org.jboss.forge.roaster.spi.StatementFactory; + +public class StatementFactoryImpl, + P extends BlockSource,P>> + implements StatementFactory +{ + + public StatementFactoryImpl() + { + } + + + public ReturnStatement newReturn() + { + return new ReturnStatementImpl(); + } + + public AssignmentStatement newAssign() + { + return new AssignStatementImpl(); + } + + public DeclareStatement newDeclare() + { + return new DeclareStatementImpl(); + } + + public InvokeStatement newInvoke() + { + return new InvokeStatementImpl(); + } + + public IfStatement newIf() + { + return new IfStatementImpl(); + } + + public WhileStatement newWhile() + { + return new WhileStatementImpl(); + } + + public DoWhileStatement newDoWhile() + { + return new DoWhileStatementImpl(); + } + + public ForStatement newFor() + { + return new ForStatementImpl(); + } + + public ForEachStatement newForEach() + { + return new ForEachStatementImpl(); + } + + @Override + public BlockStatement newBlock() + { + return new BlockStatementImpl(); + } + + @Override + public BreakStatement newBreak() + { + return new BreakStatementImpl(); + } + + @Override + public ContinueStatement newContinue() + { + return new ContinueStatementImpl(); + } + + @Override + public ThrowStatement newThrow() + { + return new ThrowStatementImpl(); + } + + @Override + public TryCatchStatement newTryCatch() + { + return new TryCatchStatementImpl(); + } + + @Override + public SynchStatement newSynchronized() + { + return new SynchStatementImpl(); + } + + @Override + public ThisStatement newThis() + { + return new ThisStatementImpl(); + } + + @Override + public SuperStatement newSuper() + { + return new SuperStatementImpl(); + } + + @Override + public AssertStatement newAssert() + { + return new AssertStatementImpl(); + } + + @Override + public SwitchStatement newSwitch() + { + return new SwitchStatementImpl(); + } + + @Override + public EvalExpressionStatement newEval() + { + return new ExpressionStatementImpl(); + } + + @Override + public SwitchCaseStatement newCase() + { + return new SwitchCaseStatementImpl(); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementImpl.java new file mode 100644 index 00000000..87c429aa --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementImpl.java @@ -0,0 +1,53 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.NodeImpl; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.Statement; + +public abstract class StatementImpl, + P extends BlockHolder, + S extends Statement, + J extends org.eclipse.jdt.core.dom.Statement> + extends NodeImpl + implements Statement +{ + + protected StatementImpl() + { + } + + @Override + public S setLabel(String label) + { + this.label = label; + return (S) this; + } + + protected > org.eclipse.jdt.core.dom.Expression wireAndGetExpression(ExpressionSource expression, X parent, AST ast) + { + ASTNode node = (ASTNode) expression; + expression.setOrigin(parent); + node.setAst(ast); + return node.materialize(ast); + } + + + @Override + public boolean isMaterialized() + { + return getInternal() != null; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SuperStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SuperStatementImpl.java new file mode 100644 index 00000000..a0733926 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SuperStatementImpl.java @@ -0,0 +1,81 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SuperConstructorInvocation; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.SuperStatement; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class SuperStatementImpl, + P extends BlockHolder> + extends StatementImpl,SuperConstructorInvocation> + implements SuperStatement +{ + + private SuperConstructorInvocation invoke; + + private List,?>> argumentList = Collections.EMPTY_LIST; + + public SuperStatementImpl() + { + } + + @Override + public List,?>> getArguments() + { + return Collections.unmodifiableList(argumentList); + } + + @Override + public SuperStatement addArgument(Argument argument) + { + Argument,?> cast = (Argument,?>) argument; + if (argumentList.isEmpty()) + { + argumentList = new LinkedList,?>>(); + } + argumentList.add(cast); + return this; + } + + @Override + public SuperConstructorInvocation materialize(AST ast) + { + if (invoke != null) + { + return invoke; + } + invoke = ast.newSuperConstructorInvocation(); + + for (Argument argument : argumentList) + { + invoke.arguments().add(wireAndGetExpression(argument, this, ast)); + } + return invoke; + } + + @Override + public SuperConstructorInvocation getInternal() + { + return invoke; + } + + @Override + public void setInternal(SuperConstructorInvocation jdtNode) + { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchCaseStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchCaseStatementImpl.java new file mode 100644 index 00000000..20f9815e --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchCaseStatementImpl.java @@ -0,0 +1,129 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SwitchCase; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.SwitchCaseStatement; +import org.jboss.forge.roaster.model.statements.SwitchStatement; + +public class SwitchCaseStatementImpl, P extends BlockHolder> + implements SwitchCaseStatement>, + ASTNode +{ + + private ExpressionSource>,?> option; + + private SwitchCase inner; + private AST ast; + + public SwitchCaseStatementImpl() + { + } + + public SwitchCaseStatementImpl setLabel(String label) + { + return null; + } + + public String getLabel() + { + return null; + } + + public boolean hasLabel() + { + return false; + } + + public void setOrigin(SwitchStatement origin) + { + } + + public SwitchStatement getOrigin() + { + return null; + } + + @Override + public ExpressionSource>,?> getCaseExpression() + { + return option; + } + + @Override + public SwitchCaseStatement> setCaseExpression(ExpressionSource src) + { + this.option = (ExpressionSource>,?>) src; + return this; + } + + @Override + public AST getAst() + { + return null; + } + + @Override + public org.eclipse.jdt.core.dom.SwitchCase materialize(AST ast) + { + if (inner != null) + { + return inner; + } + inner = ast.newSwitchCase(); + if (option != null) + { + // case ... : + inner.setExpression(wireAndGetExpression(option, this, ast)); + } else + { + // default: + inner.setExpression(null); + } + return inner; + } + + @Override + public org.eclipse.jdt.core.dom.SwitchCase getInternal() + { + return inner; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.SwitchCase jdtNode) + { + this.inner = jdtNode; + } + + @Override + public boolean isMaterialized() + { + return inner != null; + } + + @Override + public void setAst(AST ast) + { + this.ast = ast; + } + + protected > org.eclipse.jdt.core.dom.Expression wireAndGetExpression(ExpressionSource expression, X parent, AST ast) + { + ASTNode node = (ASTNode) expression; + expression.setOrigin(parent); + node.setAst(ast); + return node.materialize(ast); + } + +} \ No newline at end of file diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchStatementImpl.java new file mode 100644 index 00000000..517bf34b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchStatementImpl.java @@ -0,0 +1,171 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.Statement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.SwitchCaseStatement; +import org.jboss.forge.roaster.model.statements.SwitchStatement; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class SwitchStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,org.eclipse.jdt.core.dom.SwitchStatement> + implements SwitchStatement +{ + + private org.eclipse.jdt.core.dom.SwitchStatement opts; + + private ExpressionSource,?> expression; + private List,?>> statements = new LinkedList,?>>(); + + public SwitchStatementImpl() + { + } + + + @Override + public org.eclipse.jdt.core.dom.SwitchStatement materialize(AST ast) + { + if (opts != null) + { + return opts; + } + opts = ast.newSwitchStatement(); + opts.setExpression(wireAndGetExpression(expression, this, ast)); + + for (Statement,?> stat : statements) + { + if (SwitchCaseStatementImpl.class.isInstance(stat)) + { + SwitchCaseStatementImpl mock = (SwitchCaseStatementImpl) stat; + mock.materialize(ast); + opts.statements().add(mock.getInternal()); + } else + { + opts.statements().add(wireAndGetStatement(stat, this, ast)); + } + } + return opts; + } + + @Override + public org.eclipse.jdt.core.dom.SwitchStatement getInternal() + { + return opts; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.SwitchStatement jdtNode) + { + super.setInternal(jdtNode); + this.opts = jdtNode; + } + + @Override + public List,?>> getCaseExpressions() + { + List,?>> cases = new ArrayList,?>>(); + for (StatementSource,?> stat : statements) + { + if (SwitchCaseStatementImpl.class.isInstance(stat)) + { + cases.add(((SwitchCaseStatementImpl) stat).getCaseExpression()); + } + } + return cases; + } + + @Override + public SwitchStatement addCase(ExpressionSource option) + { + ExpressionSource,?> cast = (ExpressionSource,?>) option; + statements.add(new SwitchCaseStatementImpl().setCaseExpression(cast)); + return this; + } + + @Override + public SwitchStatement addCase(SwitchCaseStatement option) + { + statements.add((StatementSource,?>) option); + return this; + } + + @Override + public List,?>> getStatements() + { + return Collections.unmodifiableList(statements); + } + + @Override + public SwitchStatement addDefault() + { + statements.add(new SwitchCaseStatementImpl()); + return this; + } + + @Override + public SwitchStatement addStatement(StatementSource arg) + { + StatementSource,?> cast = (StatementSource,?>) arg; + if (statements.isEmpty()) + { + statements = new LinkedList,?>>(); + } + statements.add(cast); + return this; + } + + @Override + public ExpressionSource,?> getSwitch() + { + return this.expression; + } + + @Override + public org.jboss.forge.roaster.model.statements.SwitchStatement setSwitch(ExpressionSource expr) + { + ExpressionSource,?> cast = (ExpressionSource,?>) expr; + this.expression = cast; + return this; + } + + @Override + public BlockStatement> getBody() + { + return null; + } + + @Override + public SwitchStatement setBody(BlockSource body) + { + addDefault(); + return addStatement((BlockStatement) body); + } + + @Override + public SwitchStatement setBody(StatementSource body) + { + addDefault(); + return addStatement(wrap(body)); + } + + +} + + diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SynchStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SynchStatementImpl.java new file mode 100644 index 00000000..5f0b1dc1 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SynchStatementImpl.java @@ -0,0 +1,102 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SynchronizedStatement; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.SynchStatement; + +public class SynchStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,SynchronizedStatement> + implements SynchStatement +{ + + private SynchronizedStatement synch; + + private ExpressionSource,?> expr; + private BlockStatement> body; + + public SynchStatementImpl() + { + } + + @Override + public SynchronizedStatement materialize(AST ast) + { + if (synch != null) + { + return synch; + } + synch = ast.newSynchronizedStatement(); + + if (expr != null) + { + synch.setExpression(wireAndGetExpression(expr, this, ast)); + } + if (body != null) + { + synch.setBody((org.eclipse.jdt.core.dom.Block) wireAndGetStatement(body, this, ast)); + } + return synch; + } + + @Override + public SynchronizedStatement getInternal() + { + return synch; + } + + @Override + public void setInternal(SynchronizedStatement jdtNode) + { + super.setInternal(jdtNode); + this.synch = jdtNode; + } + + @Override + public ExpressionSource,?> getSynchOn() + { + return expr; + } + + @Override + public SynchStatement setSynchOn(ExpressionSource expr) + { + ExpressionSource,?> cast = (ExpressionSource,?>) expr; + this.expr = cast; + return this; + } + + @Override + public BlockStatement> getBody() + { + return body; + } + + @Override + public SynchStatement setBody(BlockSource body) + { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } + + @Override + public SynchStatement setBody(StatementSource body) + { + this.body = wrap(body); + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThisStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThisStatementImpl.java new file mode 100644 index 00000000..a0c90c23 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThisStatementImpl.java @@ -0,0 +1,81 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ConstructorInvocation; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.ThisStatement; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class ThisStatementImpl, + P extends BlockHolder> + extends StatementImpl,ConstructorInvocation> + implements ThisStatement +{ + + private ConstructorInvocation invoke; + + private List,?>> argumentList = Collections.EMPTY_LIST; + + public ThisStatementImpl() + { + } + + @Override + public List,?>> getArguments() + { + return Collections.unmodifiableList(argumentList); + } + + @Override + public ThisStatement addArgument(Argument argument) + { + Argument,?> cast = (Argument,?>) argument; + if (argumentList.isEmpty()) + { + argumentList = new LinkedList,?>>(); + } + argumentList.add(cast); + return this; + } + + @Override + public ConstructorInvocation materialize(AST ast) + { + if (invoke != null) + { + return invoke; + } + invoke = ast.newConstructorInvocation(); + + for (Argument argument : argumentList) + { + invoke.arguments().add(wireAndGetExpression(argument, this, ast)); + } + return invoke; + } + + @Override + public ConstructorInvocation getInternal() + { + return invoke; + } + + @Override + public void setInternal(ConstructorInvocation jdtNode) + { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThrowStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThrowStatementImpl.java new file mode 100644 index 00000000..9a27e7ab --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThrowStatementImpl.java @@ -0,0 +1,71 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.ThrowStatement; + +public class ThrowStatementImpl, + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.ThrowStatement> + implements ThrowStatement +{ + + private ExpressionSource,?> expression; + + private org.eclipse.jdt.core.dom.ThrowStatement throwStatement; + + public ThrowStatementImpl() + { + } + + @Override + public org.eclipse.jdt.core.dom.ThrowStatement materialize(AST ast) + { + if (throwStatement != null) + { + return throwStatement; + } + throwStatement = ast.newThrowStatement(); + if (expression != null) + { + throwStatement.setExpression(wireAndGetExpression(expression, this, getAst())); + } + return throwStatement; + } + + @Override + public ExpressionSource,?> getThrowable() + { + return expression; + } + + @Override + public ThrowStatement setThrowable(ExpressionSource expr) + { + ExpressionSource,?> cast = (ExpressionSource,?>) expr; + this.expression = cast; + return this; + } + + @Override + public org.eclipse.jdt.core.dom.ThrowStatement getInternal() + { + return throwStatement; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.ThrowStatement jdtNode) + { + super.setInternal(jdtNode); + this.throwStatement = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/TryCatchStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/TryCatchStatementImpl.java new file mode 100644 index 00000000..49779fa7 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/TryCatchStatementImpl.java @@ -0,0 +1,163 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.CatchClause; +import org.eclipse.jdt.core.dom.SingleVariableDeclaration; +import org.eclipse.jdt.core.dom.TryStatement; +import org.eclipse.jdt.core.dom.VariableDeclarationExpression; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.TryCatchStatement; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +public class TryCatchStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,TryStatement> + implements TryCatchStatement +{ + + private TryStatement tryCatch; + + private Map>,BlockStatement>> catches = Collections.EMPTY_MAP; + private BlockStatement> body; + private BlockStatement> fine; + + public TryCatchStatementImpl() + { + } + + @Override + public TryStatement materialize(AST ast) + { + if (tryCatch != null) + { + return tryCatch; + } + tryCatch = ast.newTryStatement(); + + for (DeclareExpression> declare : catches.keySet()) + { + CatchClause clause = ast.newCatchClause(); + clause.setException(varToSvd((VariableDeclarationExpression) wireAndGetExpression(declare, this, ast), ast)); + clause.setBody((org.eclipse.jdt.core.dom.Block) wireAndGetStatement(catches.get(declare), this, ast)); + tryCatch.catchClauses().add(clause); + } + + if (fine != null) + { + tryCatch.setFinally((org.eclipse.jdt.core.dom.Block) wireAndGetStatement(fine, this, ast)); + } + if (body != null) + { + tryCatch.setBody((org.eclipse.jdt.core.dom.Block) wireAndGetStatement(body, this, ast)); + } + return tryCatch; + } + + @Override + public TryStatement getInternal() + { + return tryCatch; + } + + @Override + public void setInternal(TryStatement jdtNode) + { + super.setInternal(jdtNode); + this.tryCatch = jdtNode; + } + + private SingleVariableDeclaration varToSvd(VariableDeclarationExpression var, AST ast) + { + SingleVariableDeclaration svd = ast.newSingleVariableDeclaration(); + VariableDeclarationFragment frag = (VariableDeclarationFragment) var.fragments().get(0); + svd.setName(ast.newSimpleName(frag.getName().getIdentifier())); + svd.setType(JDTHelper.getType(var.getType().toString(), ast)); + return svd; + } + + @Override + public Map>,BlockStatement>> getCatches() + { + return Collections.unmodifiableMap(catches); + } + + @Override + public TryCatchStatement addCatch(DeclareExpression declaration, + BlockStatement block) + { + if (catches.isEmpty()) + { + catches = new LinkedHashMap>,BlockStatement>>(); + } + DeclareExpression> castKey = (DeclareExpression>) declaration; + BlockStatement> castValue = (BlockStatement>) block; + catches.put(castKey, castValue); + return this; + } + + @Override + public BlockStatement> getFinally() + { + return fine; + } + + @Override + public TryCatchStatement addCatch(DeclareExpression declaration, + StatementSource block) + { + return addCatch(declaration, wrap(block)); + } + + @Override + public BlockStatement> getBody() + { + return body; + } + + @Override + public TryCatchStatement setBody(BlockSource body) + { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } + + @Override + public TryCatchStatement setBody(StatementSource body) + { + this.body = wrap(body); + return this; + } + + @Override + public TryCatchStatement setFinally(BlockStatement block) + { + BlockStatement> cast = (BlockStatement>) block; + this.fine = cast; + return this; + } + + @Override + public TryCatchStatement setFinally(StatementSource block) + { + this.fine = wrap(block); + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/WhileStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/WhileStatementImpl.java new file mode 100644 index 00000000..a1cef70d --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/WhileStatementImpl.java @@ -0,0 +1,101 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.WhileStatement; + +public class WhileStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,org.eclipse.jdt.core.dom.WhileStatement> + implements WhileStatement +{ + + private org.eclipse.jdt.core.dom.WhileStatement rep; + + private BlockStatement> body; + private ExpressionSource,?> condition; + + public WhileStatementImpl() + { + } + + @Override + public ExpressionSource,?> getCondition() + { + return condition; + } + + @Override + public WhileStatement setCondition(ExpressionSource expr) + { + ExpressionSource,?> cast = (ExpressionSource,?>) expr; + this.condition = cast; + return this; + } + + @Override + public org.eclipse.jdt.core.dom.WhileStatement materialize(AST ast) + { + if (rep != null) + { + return rep; + } + rep = ast.newWhileStatement(); + + if (body != null) + { + rep.setBody(wireAndGetStatement(body, this, ast)); + } + if (condition != null) + { + rep.setExpression(wireAndGetExpression(condition, this, ast)); + } + return rep; + } + + @Override + public org.eclipse.jdt.core.dom.WhileStatement getInternal() + { + return rep; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.WhileStatement jdtNode) + { + super.setInternal(jdtNode); + this.rep = jdtNode; + } + + @Override + public BlockStatement> getBody() + { + return body; + } + + @Override + public WhileStatement setBody(BlockSource body) + { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } + + @Override + public WhileStatement setBody(StatementSource body) + { + this.body = wrap(body); + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactoryImpl.java b/impl/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactoryImpl.java new file mode 100644 index 00000000..5162f289 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactoryImpl.java @@ -0,0 +1,71 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.spi; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ArrayIndexer; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.NonPrimitiveExpression; +import org.jboss.forge.roaster.model.expressions.Setter; +import org.jboss.forge.roaster.model.impl.expressions.ArrayAccessImpl; +import org.jboss.forge.roaster.model.impl.expressions.BasicExpressionFactoryImpl; +import org.jboss.forge.roaster.model.impl.expressions.FieldImpl; +import org.jboss.forge.roaster.model.impl.expressions.GetterImpl; +import org.jboss.forge.roaster.model.impl.expressions.MethodInvokeImpl; +import org.jboss.forge.roaster.model.impl.expressions.SetterImpl; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class ExpressionFactoryImpl, + P extends ExpressionHolder, + E extends NonPrimitiveExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends BasicExpressionFactoryImpl + implements ExpressionFactory { + + + public ExpressionFactoryImpl() { } + + @Override + public Field field( String fieldName ) { + return new FieldImpl( fieldName ); + } + + @Override + public Getter getter( String fieldName, String type ) { + return new GetterImpl( fieldName, type ); + } + + @Override + public Getter getter( String fieldName, Class type ) { + return getter( fieldName, type.getName() ); + } + + @Override + public Setter setter( String fldName, String type, ExpressionSource value ) { + return new SetterImpl( fldName, type, value ); + } + + @Override + public Setter setter( String fldName, Class type, ExpressionSource value ) { + return setter( fldName, type.getName(), value ); + } + + @Override + public MethodCallExpression invoke( String method ) { + return new MethodInvokeImpl( method ); + } + + + @Override + public ArrayIndexer itemAt( ExpressionSource index ) { + return new ArrayAccessImpl( index ); + } +} diff --git a/impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.ExpressionFactory b/impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.ExpressionFactory new file mode 100644 index 00000000..b21e171f --- /dev/null +++ b/impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.ExpressionFactory @@ -0,0 +1 @@ +org.jboss.forge.roaster.spi.ExpressionFactoryImpl \ No newline at end of file diff --git a/impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.StatementFactory b/impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.StatementFactory new file mode 100644 index 00000000..bd4c0503 --- /dev/null +++ b/impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.StatementFactory @@ -0,0 +1 @@ +org.jboss.forge.roaster.model.impl.statements.StatementFactoryImpl \ No newline at end of file diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ConstructorStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ConstructorStatementsTest.java new file mode 100644 index 00000000..0a6d0e33 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ConstructorStatementsTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.statements.Statements.newSuper; +import static org.jboss.forge.roaster.model.statements.Statements.newThis; +import static org.junit.Assert.assertEquals; + + +public class ConstructorStatementsTest +{ + + @Test + public void testEmptyConstructor() throws Exception + { + String target = "this(x);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public Foo()"); + + method.setBody(newThis().addArgument(var("x"))); + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testSuperConstructor() throws Exception + { + String target = "super(x);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public Foo()"); + + method.setBody(newSuper().addArgument(var("x"))); + assertEquals(target, method.getBody().trim()); + } + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ControlFlowStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ControlFlowStatementsTest.java new file mode 100644 index 00000000..cb0bb64e --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ControlFlowStatementsTest.java @@ -0,0 +1,192 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.jboss.forge.roaster.model.expressions.Expressions.construct; +import static org.jboss.forge.roaster.model.expressions.Expressions.declare; +import static org.jboss.forge.roaster.model.expressions.Expressions.literal; +import static org.jboss.forge.roaster.model.expressions.Expressions.operator; +import static org.jboss.forge.roaster.model.expressions.Expressions.thisLiteral; +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.statements.Statements.newAssert; +import static org.jboss.forge.roaster.model.statements.Statements.newAssign; +import static org.jboss.forge.roaster.model.statements.Statements.newBlock; +import static org.jboss.forge.roaster.model.statements.Statements.newBreak; +import static org.jboss.forge.roaster.model.statements.Statements.newContinue; +import static org.jboss.forge.roaster.model.statements.Statements.newIf; +import static org.jboss.forge.roaster.model.statements.Statements.newInvoke; +import static org.jboss.forge.roaster.model.statements.Statements.newReturn; +import static org.jboss.forge.roaster.model.statements.Statements.newSwitch; +import static org.jboss.forge.roaster.model.statements.Statements.newSynchronized; +import static org.jboss.forge.roaster.model.statements.Statements.newThrow; +import static org.jboss.forge.roaster.model.statements.Statements.newTryCatch; +import static org.jboss.forge.roaster.model.statements.Statements.newWhile; +import static org.junit.Assert.assertEquals; + + +public class ControlFlowStatementsTest +{ + @Test + public void testSimpleLabeledStatement() throws Exception + { + String target = "Out: return x;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newReturn().setLabel("Out").setReturn(var("x"))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testBreak() throws Exception + { + String target = "if (true) {\n break;\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newBlock() + .addStatement(newIf().setCondition(literal(true)).setThen(newBreak()))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testContinue() throws Exception + { + String target = "while (true) {\n continue;\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newBlock() + .addStatement(newWhile().setCondition(literal(true)).setBody(newContinue()))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testBreakWithLabel() throws Exception + { + String target = "while (true) {\n break exit;\n}\n exit: return x;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newBlock() + .addStatement(newWhile().setCondition(literal(true)).setBody( + newBreak().setBreakToLabel("exit"))) + .addStatement(newReturn().setReturn(var("x")).setLabel("exit"))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testContinueWithLabel() throws Exception + { + String target = "while (true) {\n test: x=x + 1;\n continue test;\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newBlock() + .addStatement(newWhile().setCondition(literal(true)).setBody( + newBlock() + .addStatement( + newAssign().setLeft(var("x")).setRight(operator(Op.PLUS).addArgument(var("x")).addArgument(literal(1))) + .setLabel("test")) + .addStatement(newContinue().setContinueToLabel("test")) + ))); + + assertEquals(target, method.getBody().trim()); + } + + + @Test + public void testThrowable() throws Exception + { + String target = "throw new java.lang.UnsupportedOperationException(\"sorry\");"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newThrow().setThrowable(construct(UnsupportedOperationException.class).addArgument(literal("sorry")))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testTryCatch() throws Exception + { + String target = "try {\n" + + " x.foo();\n" + + "}\n catch (java.lang.NullPointerException npe) {\n" + + " npe.printStackTrace();\n" + + "}\ncatch (java.lang.IllegalStateException ise) {\n" + + " ise.printStackTrace();\n" + + "}\n finally {\n" + + " return;\n" + + "}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newTryCatch() + .setBody(newInvoke().setMethodName("foo").setInvocationTarget(var("x"))) + .addCatch(declare(NullPointerException.class, "npe"), newInvoke().setMethodName("printStackTrace").setInvocationTarget(var("npe"))) + .addCatch(declare(IllegalStateException.class, "ise"), newInvoke().setMethodName("printStackTrace").setInvocationTarget(var("ise"))) + .setFinally(newReturn()) + ); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testSynchronized() throws Exception + { + String target = "synchronized (this) {\n x++;\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newSynchronized().setSynchOn(thisLiteral()).setBody(newBlock().addStatement(var("x").inc()))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testSwitchCase() throws Exception + { + String target = "switch (x) {\n" + + "case 0:\n" + + " return true;\n" + + "case 1:\n" + + "break;\n" + + "case 2:\n" + + "default :\n" + + "return false;\n" + + "}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newSwitch().setSwitch(var("x")) + .addCase(literal(0)).addStatement(newReturn().setReturn(literal(true))) + .addCase(literal(1)).addStatement(newBreak()) + .addCase(literal(2)) + .addDefault() + .addStatement(newReturn().setReturn(literal(false))) + ); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testAssertion() throws Exception + { + String target = "assert x > 0 : \"x GT 0\";"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newAssert() + .setAssertion(operator(Op.GREATER).addArgument(var("x")).addArgument(literal(0))) + .setMessage(literal("x GT 0"))); + + assertEquals(target, method.getBody().trim()); + } + + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/DeclareStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/DeclareStatementsTest.java new file mode 100644 index 00000000..5e52b591 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/DeclareStatementsTest.java @@ -0,0 +1,112 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.jboss.forge.roaster.model.expressions.Expressions.construct; +import static org.jboss.forge.roaster.model.expressions.Expressions.field; +import static org.jboss.forge.roaster.model.expressions.Expressions.literal; +import static org.jboss.forge.roaster.model.expressions.Expressions.operator; +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.statements.Statements.newAssign; +import static org.jboss.forge.roaster.model.statements.Statements.newDeclare; +import static org.junit.Assert.assertEquals; + + +public class DeclareStatementsTest +{ + + @Test + public void testDeclare() throws Exception + { + String target = "java.lang.Integer y=new java.lang.Integer();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo( String x )"); + + method.setBody(newDeclare().setVariable(Integer.class, "y", construct(Integer.class))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testDeclarePrimitive() throws Exception + { + String target = "int y;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo( String x )"); + + method.setBody(newDeclare().setVariable(int.class, "y")); + + assertEquals(target, method.getBody().trim()); + } + + + @Test + public void testDeclareAndInit() throws Exception + { + String target = "java.lang.Integer y=0;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo( String x )"); + + method.setBody(newDeclare().setVariable(Integer.class, "y", literal(0))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testDeclareManyAndInit() throws Exception + { + String target = "int x, y=0, z=3, w;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo( String x )"); + + method.setBody(newDeclare().setVariable(int.class, "x").addVariable("y", literal(0)).addVariable("z", literal(3)).addVariable("w")); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testSimpleAssignment() throws Exception + { + String target = "this.name=x;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo( String x )"); + + method.setBody(newAssign().setLeft(field("name")).setRight(var("x"))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testAssignmentExpr() throws Exception + { + String target = "this.name=this.name + \" Doe\";"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo( String x )"); + + method.setBody(newAssign() + .setLeft(field("name")) + .setRight(operator(Op.PLUS).addArgument(field("name")).addArgument(literal(" Doe")))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testDeclareWithGenerics() throws Exception + { + String target = "java.util.Map> y=new java.util.HashMap>();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo( String x )"); + + method.setBody(newDeclare() + .setVariable("java.util.Map>", + "y", + construct("java.util.HashMap>"))); + + assertEquals(target, method.getBody().trim()); + } + + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ForStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ForStatementsTest.java new file mode 100644 index 00000000..747f2eb3 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ForStatementsTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Assignment; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.jboss.forge.roaster.model.expressions.Expressions.assign; +import static org.jboss.forge.roaster.model.expressions.Expressions.classStatic; +import static org.jboss.forge.roaster.model.expressions.Expressions.declare; +import static org.jboss.forge.roaster.model.expressions.Expressions.literal; +import static org.jboss.forge.roaster.model.expressions.Expressions.operator; +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.statements.Statements.newFor; +import static org.jboss.forge.roaster.model.statements.Statements.newForEach; +import static org.jboss.forge.roaster.model.statements.Statements.newInvoke; +import static org.junit.Assert.assertEquals; + +public class ForStatementsTest +{ + + @Test + public void testSimpleForWithPrint() throws Exception + { + + String target = "for (int j=0, k=0; j < 100; j++, k=k + 2) {\n java.lang.System.out.println(j);\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newFor() + .setDeclaration(declare(int.class, "j", literal(0)).addDeclaration("k", literal(0))) + .setCondition(operator(Op.LESS) + .addArgument(var("j")) + .addArgument(literal(100))) + .addUpdate(var("j").inc()) + .addUpdate(assign(Assignment.ASSIGN) + .setLeft(var("k")) + .setRight(operator(Op.PLUS) + .addArgument(var("k")) + .addArgument(literal(2)))) + .setBody(newInvoke() + .setMethodName("println") + .setInvocationTarget(classStatic(System.class).field("out")) + .addArgument(var("j")))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testSimpleForEachWithPrint() throws Exception + { + + String target = "for (java.lang.String name : p.getNames()) {\n java.lang.System.out.println(name);\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newForEach() + .setIterator(String.class, "name") + .setSource(var("p").getter("names", String.class)) + .setBody(newInvoke() + .setInvocationTarget(classStatic(System.class).field("out")) + .setMethodName("println") + .addArgument(var("name")))); + + assertEquals(target, method.getBody().trim()); + } + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/IfStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/IfStatementsTest.java new file mode 100644 index 00000000..bab75bf7 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/IfStatementsTest.java @@ -0,0 +1,102 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.jboss.forge.roaster.model.expressions.Expressions.instanceOf; +import static org.jboss.forge.roaster.model.expressions.Expressions.literal; +import static org.jboss.forge.roaster.model.expressions.Expressions.not; +import static org.jboss.forge.roaster.model.expressions.Expressions.nullLiteral; +import static org.jboss.forge.roaster.model.expressions.Expressions.operator; +import static org.jboss.forge.roaster.model.expressions.Expressions.thisLiteral; +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.statements.Statements.newAssign; +import static org.jboss.forge.roaster.model.statements.Statements.newBlock; +import static org.jboss.forge.roaster.model.statements.Statements.newIf; +import static org.jboss.forge.roaster.model.statements.Statements.newReturn; +import static org.junit.Assert.assertEquals; + +public class IfStatementsTest +{ + @Test + public void testSimpleIfWithCondition() throws Exception + { + String target = "if (x != null) {\n x=null;\n}\n else {\n x=y;\n x=x + 1;\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newIf() + .setCondition(operator(Op.NOT_EQUALS).addArgument(var("x")).addArgument(nullLiteral())) + .setThen(newAssign().setLeft(var("x")).setRight(nullLiteral())) + .setElse(newBlock() + .addStatement(newAssign().setLeft(var("x")).setRight(var("y"))) + .addStatement(newAssign().setLeft(var("x")).setRight( + operator(Op.PLUS) + .addArgument(var("x")) + .addArgument(literal(1)))))); + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testIfWithDotAccessor() throws Exception + { + String target = "if (this.x != y.z) {\n return false;\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newIf() + .setCondition(operator(Op.NOT_EQUALS) + .addArgument(thisLiteral().field("x")) + .addArgument(var("y").field("z"))) + .setThen(newReturn().setReturn(literal(false)))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testIfWithNegation() throws Exception + { + String target = "if (!x) {\n return false;\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newIf() + .setCondition(not(var("x"))) + .setThen(newReturn().setReturn(literal(false)))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testIfWithDoubleNegation() throws Exception + { + String target = "if (!!x) {\n return false;\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newIf() + .setCondition(not(not(var("x")))) + .setThen(newReturn().setReturn(literal(false)))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testIfWithInstanceof() throws Exception + { + String target = "if (x instanceof Foo) {\n return false;\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newIf() + .setCondition(instanceOf("Foo", var("x"))) + .setThen(newReturn().setReturn(literal(false)))); + assertEquals(target, method.getBody().trim()); + } + + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/InvokeStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/InvokeStatementsTest.java new file mode 100644 index 00000000..851650b4 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/InvokeStatementsTest.java @@ -0,0 +1,159 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.jboss.forge.roaster.model.expressions.Expressions.classStatic; +import static org.jboss.forge.roaster.model.expressions.Expressions.construct; +import static org.jboss.forge.roaster.model.expressions.Expressions.invoke; +import static org.jboss.forge.roaster.model.expressions.Expressions.literal; +import static org.jboss.forge.roaster.model.expressions.Expressions.operator; +import static org.jboss.forge.roaster.model.expressions.Expressions.thisLiteral; +import static org.jboss.forge.roaster.model.statements.Statements.newEval; +import static org.jboss.forge.roaster.model.statements.Statements.newInvoke; +import static org.junit.Assert.assertEquals; + + +public class InvokeStatementsTest +{ + + @Test + public void testInvoke() throws Exception + { + String target = "java.lang.Math.random();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo()"); + + method.setBody(newInvoke().setMethodName("random").setInvocationTarget(classStatic(Math.class))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testInvokeWithArg() throws Exception + { + String target = "java.lang.Math.round(2.4);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo()"); + + method.setBody(newInvoke().setInvocationTarget(classStatic(Math.class)).setMethodName("round").addArgument(literal(2.4f))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testInvokeWithArgs() throws Exception + { + String target = "java.lang.Math.min(3,4);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo()"); + + method.setBody(newInvoke().addArgument(literal(3)).addArgument(literal(4)).setInvocationTarget(classStatic(Math.class)).setMethodName("min")); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testInvokeWithArgsRevOrder() throws Exception + { + String target = "java.lang.Math.min(3,4);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo()"); + + method.setBody(newInvoke().addArgument(literal(3)).addArgument(literal(4)).setInvocationTarget(classStatic(Math.class)).setMethodName("min")); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testNestedInvoke() throws Exception + { + String target = "java.lang.Math.min(java.lang.Math.max(java.lang.Math.round(3,4),6),4);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo()"); + + method.setBody(newInvoke().setInvocationTarget(classStatic(Math.class)).setMethodName("min") + .addArgument(invoke("max").setInvocationTarget(classStatic(Math.class)) + .addArgument(invoke("round").setInvocationTarget(classStatic(Math.class)) + .addArgument(literal(3)) + .addArgument(literal(4))) + .addArgument(literal(6))) + .addArgument(literal(4))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testInvokeWithArgz() throws Exception + { + String target = "java.lang.Math.min(2 + 3,4 * 5);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo()"); + + method.setBody(newInvoke().setInvocationTarget(classStatic(Math.class)).setMethodName("min") + .addArgument(operator(Op.PLUS).addArgument(literal(2)).addArgument(literal(3))) + .addArgument(operator(Op.TIMES).addArgument(literal(4)).addArgument(literal(5)))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testInvokeOnThis() throws Exception + { + String target = "this.toString();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo()"); + + method.setBody(newInvoke().setInvocationTarget(thisLiteral()).setMethodName("toString")); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testInvokeOnLongChain() throws Exception + { + String target = "this.getName().foo().bar().baz.toString();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo()"); + + method.setBody(newInvoke().setMethodName("toString") + .setInvocationTarget( + thisLiteral().getter("name", "String").invoke("foo").invoke("bar").field("baz"))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testEval() + { + String target = "3 + 4;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo()"); + + method.setBody(newEval().setExpr(operator(Op.PLUS).addArgument(literal(3)).addArgument(literal(4)))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testNewInstanceWithMethod() + { + String target = "new java.lang.Integer(42).toString();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo()"); + + method.setBody(newEval().setExpr(construct(Integer.class).addArgument(literal(42)).invoke("toString"))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testNewInstance() + { + String target = "new java.lang.Integer();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo()"); + + method.setBody(newEval().setExpr(construct(Integer.class))); + + assertEquals(target, method.getBody().trim()); + } +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ModifyMethodBodyTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ModifyMethodBodyTest.java new file mode 100644 index 00000000..65547203 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ModifyMethodBodyTest.java @@ -0,0 +1,68 @@ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.JavaClass; +import org.jboss.forge.roaster.model.expressions.Variable; +import org.jboss.forge.roaster.model.impl.statements.JdtStatementWrapper; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.jboss.forge.roaster.model.statements.InvokeStatement; +import org.jboss.forge.roaster.model.statements.ReturnStatement; +import org.jboss.forge.roaster.model.statements.Statement; +import org.junit.Test; + +import java.util.List; + +import static org.jboss.forge.roaster.model.expressions.Expressions.classStatic; +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.statements.Statements.newInvoke; +import static org.jboss.forge.roaster.model.statements.Statements.newReturn; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class ModifyMethodBodyTest +{ + + @Test + public void testReturnArg() throws Exception + { + String target = "return x;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String echo( String x )"); + + method.setBody(newReturn().setReturn(var("x"))); + + assertEquals(target, method.getBody().trim()); + + BlockSource body = method.getBodyAsBlock(); + assertEquals(1, body.getStatements().size()); + assertTrue(body.getStatements().get(0) instanceof ReturnStatement); + } + + @Test + public void testModifySource() + { + String source = "public class Bean {\n" + + "public String echo(String x) {\n" + + " return x;\n" + + "}\n" + + "}"; + + JavaClassSource javaClassSource = Roaster.parse(JavaClass.class, source).asJavaClassSource(); + List> methods = javaClassSource.getMethods(); + assertEquals(1, methods.size()); + MethodSource method = methods.get(0); + + BlockSource body = method.getBodyAsBlock(); + body.addStatement(0, newInvoke().setInvocationTarget(classStatic(System.class).field("out")) + .setMethodName("println") + .addArgument(var("x"))); + + List statements = body.getStatements(); + assertEquals(2, statements.size()); + assertEquals("x", ((Variable) ((ReturnStatement) statements.get(1)).getReturn()).getName()); + assertTrue(statements.get(0) instanceof InvokeStatement); + + assertTrue(((JdtStatementWrapper) body).getInternal().toString().contains("System.out.println")); + } +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/RebuildStatementTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/RebuildStatementTest.java new file mode 100644 index 00000000..05e9ddde --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/RebuildStatementTest.java @@ -0,0 +1,1194 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.JavaClass; +import org.jboss.forge.roaster.model.expressions.ArrayConstructorExpression; +import org.jboss.forge.roaster.model.expressions.ArrayIndexer; +import org.jboss.forge.roaster.model.expressions.ArrayInit; +import org.jboss.forge.roaster.model.expressions.BooleanLiteral; +import org.jboss.forge.roaster.model.expressions.CastExpression; +import org.jboss.forge.roaster.model.expressions.CharacterLiteral; +import org.jboss.forge.roaster.model.expressions.ConstructorExpression; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.expressions.InstanceofExpression; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.NullLiteral; +import org.jboss.forge.roaster.model.expressions.NumberLiteral; +import org.jboss.forge.roaster.model.expressions.OperatorExpression; +import org.jboss.forge.roaster.model.expressions.ParenExpression; +import org.jboss.forge.roaster.model.expressions.PostFixExpression; +import org.jboss.forge.roaster.model.expressions.Setter; +import org.jboss.forge.roaster.model.expressions.StringLiteral; +import org.jboss.forge.roaster.model.expressions.Super; +import org.jboss.forge.roaster.model.expressions.TernaryExpression; +import org.jboss.forge.roaster.model.expressions.ThisLiteral; +import org.jboss.forge.roaster.model.expressions.UnaryExpression; +import org.jboss.forge.roaster.model.expressions.Variable; +import org.jboss.forge.roaster.model.impl.expressions.StringLiteralImpl; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.jboss.forge.roaster.model.statements.AssertStatement; +import org.jboss.forge.roaster.model.statements.AssignmentStatement; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.BreakStatement; +import org.jboss.forge.roaster.model.statements.ContinueStatement; +import org.jboss.forge.roaster.model.statements.DeclareStatement; +import org.jboss.forge.roaster.model.statements.DoWhileStatement; +import org.jboss.forge.roaster.model.statements.ExpressionStatement; +import org.jboss.forge.roaster.model.statements.ForEachStatement; +import org.jboss.forge.roaster.model.statements.ForStatement; +import org.jboss.forge.roaster.model.statements.IfStatement; +import org.jboss.forge.roaster.model.statements.InvokeStatement; +import org.jboss.forge.roaster.model.statements.ReturnStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.SuperStatement; +import org.jboss.forge.roaster.model.statements.SwitchCaseStatement; +import org.jboss.forge.roaster.model.statements.SwitchStatement; +import org.jboss.forge.roaster.model.statements.SynchStatement; +import org.jboss.forge.roaster.model.statements.ThisStatement; +import org.jboss.forge.roaster.model.statements.ThrowStatement; +import org.jboss.forge.roaster.model.statements.TryCatchStatement; +import org.jboss.forge.roaster.model.statements.WhileStatement; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class RebuildStatementTest +{ + + @Test + public void testRebuildReturnStatement() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return x; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertEquals("x", ((Variable) ((ReturnStatement) statements.get(0)).getReturn()).getName()); + } + + @Test + public void testRebuildReturnStatementBoolean() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return true; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertTrue(((ReturnStatement) statements.get(0)).getReturn() instanceof BooleanLiteral); + } + + @Test + public void testRebuildReturnStatementWithCast() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return (my.own.Klass) foo; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertTrue(((ReturnStatement) statements.get(0)).getReturn() instanceof CastExpression); + CastExpression cast = (CastExpression) ((ReturnStatement) statements.get(0)).getReturn(); + assertEquals("my.own.Klass", cast.getType()); + assertTrue(cast.getExpression() instanceof Variable); + } + + @Test + public void testRebuildReturnNull() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return null; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertTrue(((ReturnStatement) statements.get(0)).getReturn() instanceof NullLiteral); + } + + @Test + public void testRebuildReturnWithInstanceOf() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return x instanceof Test; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertTrue(((ReturnStatement) statements.get(0)).getReturn() instanceof InstanceofExpression); + InstanceofExpression iof = (InstanceofExpression) ((ReturnStatement) statements.get(0)).getReturn(); + assertEquals("Test", iof.getTypeName()); + assertTrue(iof.getExpression() instanceof Variable); + } + + @Test + public void testRebuildReturnWithPrefix() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return ++x; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertTrue(((ReturnStatement) statements.get(0)).getReturn() instanceof UnaryExpression); + UnaryExpression una = (UnaryExpression) ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(una.getExpression() instanceof Variable); + assertEquals("++", una.getOperator()); + } + + @Test + public void testRebuildReturnWithPostfix() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return x--; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertTrue(((ReturnStatement) statements.get(0)).getReturn() instanceof UnaryExpression); + PostFixExpression una = (PostFixExpression) ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(una.getExpression() instanceof Variable); + assertEquals("--", una.getOperator()); + } + + @Test + public void testRebuildVariableDeclarations() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " int y = 1; " + + " String z; " + + " Object q; " + + " my.pack.Foo foo; " + + "" + + " return z; " + + "} " + + "}"; + List statements = parseSource(source); + + String[] names = new String[]{"y", "z", "q", "foo"}; + String[] types = new String[]{"int", "String", "Object", "my.pack.Foo"}; + + assertEquals(5, statements.size()); + for (int j = 0; j < 4; j++) + { + assertTrue(statements.get(j) instanceof DeclareStatement); + DeclareStatement decl = ((DeclareStatement) statements.get(j)); + assertEquals(names[j], decl.getVariableName()); + assertEquals(types[j], decl.getVariableType()); + } + } + + @Test + public void testRebuildSuperField() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + "" + + " return super.z; " + + "} " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertTrue(((ReturnStatement) statements.get(0)).getReturn() instanceof Field); + Field f = (Field) ((ReturnStatement) statements.get(0)).getReturn(); + assertEquals("z", f.getFieldName()); + assertTrue(f.getInvocationTarget() instanceof Super); + } + + @Test + public void testRebuildSuperMethod() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + "" + + " return super.foo( 42 ); " + + "} " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertTrue(((ReturnStatement) statements.get(0)).getReturn() instanceof MethodCallExpression); + MethodCallExpression m = (MethodCallExpression) ((ReturnStatement) statements.get(0)).getReturn(); + assertEquals("foo", m.getMethodName()); + assertTrue(m.getInvocationTarget() instanceof Super); + assertEquals(1, m.getArguments().size()); + } + + @Test + public void testRebuildSuperGetter() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + "" + + " return super.getFoo(); " + + "} " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertTrue(((ReturnStatement) statements.get(0)).getReturn() instanceof Getter); + Getter m = (Getter) ((ReturnStatement) statements.get(0)).getReturn(); + assertEquals("foo", m.getFieldName()); + } + + @Test + public void testTheEmptyStatement() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " ; " + + "} " + + "}"; + List statements = parseSource(source); + assertEquals(0, statements.size()); + } + + @Test + public void testRebuildWhileStatementWithContinue() + { + String source = "public class Bean { " + + "public int echo(int x) { " + + " while ( x > 0 ) { " + + " x = x+1; " + + " continue foo; " + + " } " + + " return x; " + + "} " + + "}"; + List statements = parseSource(source); + assertEquals(2, statements.size()); + assertTrue(statements.get(0) instanceof WhileStatement); + WhileStatement whil = (WhileStatement) statements.get(0); + assertTrue(whil.getCondition() instanceof OperatorExpression); + assertEquals(2, whil.getBody().getStatements().size()); + assertTrue(whil.getBody().getStatements().get(0) instanceof AssignmentStatement); + assertTrue(whil.getBody().getStatements().get(1) instanceof ContinueStatement); + } + + @Test + public void testRebuildDoWhileStatementWithBreak() + { + String source = "public class Bean { " + + "public int echo(int x) { " + + " do { " + + " x = x+1; " + + " break; " + + " } while ( x > 0 ); " + + " return x; " + + "}"; + List statements = parseSource(source); + assertEquals(2, statements.size()); + assertTrue(statements.get(0) instanceof DoWhileStatement); + DoWhileStatement whil = (DoWhileStatement) statements.get(0); + assertTrue(whil.getCondition() instanceof OperatorExpression); + assertEquals(2, whil.getBody().getStatements().size()); + assertTrue(whil.getBody().getStatements().get(0) instanceof AssignmentStatement); + assertTrue(whil.getBody().getStatements().get(1) instanceof BreakStatement); + } + + @Test + public void testRebuildLabeledStatement() + { + String source = "public class Bean { " + + "public int echo(int x) { " + + " foo : do { " + + " } while ( true ); " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof DoWhileStatement); + DoWhileStatement whil = (DoWhileStatement) statements.get(0); + assertEquals("foo", whil.getLabel()); + assertTrue(whil.getCondition() instanceof BooleanLiteral); + assertEquals(0, whil.getBody().getStatements().size()); + } + + @Test + public void testRebuildEnhancedFor() + { + String source = "public class Bean { " + + "public int echo(int x) { " + + " for ( Integer x : numbers ) { " + + " x++; " + + " } " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ForEachStatement); + ForEachStatement foreach = (ForEachStatement) statements.get(0); + assertTrue(foreach.getSource() instanceof Variable); + assertEquals("x", foreach.getIteratorName()); + assertEquals(Integer.class.getSimpleName(), foreach.getIteratorType()); + assertEquals(1, foreach.getBody().getStatements().size()); + assertTrue(foreach.getBody().getStatements().get(0) instanceof ExpressionStatement); + assertTrue(((ExpressionStatement) foreach.getBody().getStatements().get(0)).getExpr() instanceof PostFixExpression); + } + + @Test + public void testRebuildAssert2() + { + String source = "public class Bean { " + + "public int echo(int x) { " + + " assert x > 0 : 42; " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof AssertStatement); + AssertStatement assrt = (AssertStatement) statements.get(0); + assertTrue(assrt.getAssertion() instanceof OperatorExpression); + assertTrue(assrt.getMessage() instanceof NumberLiteral); + } + + @Test + public void testRebuildThrow() + { + String source = "public class Bean { " + + "public int echo(int x) { " + + " throw new MysteryException(); " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ThrowStatement); + ThrowStatement trow = (ThrowStatement) statements.get(0); + assertTrue(trow.getThrowable() instanceof ConstructorExpression); + } + + @Test + public void testRebuildIfThenElse() + { + String source = "public class Bean { " + + "public int echo(int x) { " + + " if ( true ) { } else { } " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof IfStatement); + IfStatement ifte = (IfStatement) statements.get(0); + assertTrue(ifte.getCondition() instanceof BooleanLiteral); + assertEquals(0, ifte.getThen().getStatements().size()); + assertEquals(0, ifte.getElse().getStatements().size()); + } + + @Test + public void testRebuildIf() + { + String source = "public class Bean { " + + "public int echo(int x) { " + + " if ( x > 0 ) { return -1; } " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof IfStatement); + IfStatement ifte = (IfStatement) statements.get(0); + assertTrue(ifte.getCondition() instanceof OperatorExpression); + assertEquals(1, ifte.getThen().getStatements().size()); + assertTrue(ifte.getThen().getStatements().get(0) instanceof ReturnStatement); + assertNull(ifte.getElse()); + } + + @Test + public void testRebuildFor() + { + String source = "public class Bean { " + + "public int echo(int x) { " + + " for ( int j = 0; j < 5; j++ ) { " + + " x = 2*j; " + + " } " + + " } " + + "} "; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ForStatement); + ForStatement forst = (ForStatement) statements.get(0); + assertTrue(forst.getCondition() instanceof OperatorExpression); + + assertNotNull(forst.getDeclaration()); + assertTrue(forst.getDeclaration() instanceof DeclareExpression); + + assertEquals(1, forst.getUpdates().size()); + assertTrue(forst.getUpdates().get(0) instanceof PostFixExpression); + } + + @Test + public void testRebuildMultiVariableFor() + { + String source = "public class Bean { " + + "public int echo(int x) { " + + " for ( int j = 0, k = 5; j < 5; j++, k-- ) { " + + " } " + + " } " + + "} "; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ForStatement); + ForStatement forst = (ForStatement) statements.get(0); + assertTrue(forst.getCondition() instanceof OperatorExpression); + + assertEquals(2, forst.getDeclaration().getNumVariables()); + DeclareExpression decl = forst.getDeclaration(); + assertEquals(Arrays.asList("j", "k"), decl.getVariableNames()); + assertEquals("int", decl.getVariableType()); + assertTrue(decl.getInitExpressions().get("j") instanceof NumberLiteral); + assertTrue(decl.getInitExpressions().get("k") instanceof NumberLiteral); + + assertEquals(2, forst.getUpdates().size()); + assertTrue(forst.getUpdates().get(0) instanceof PostFixExpression); + + } + + @Test + public void testRebuildTryCatch() + { + String source = "public class Bean { " + + "public int echo(int x) { " + + " try { " + + " int x = 1+1;" + + " } catch ( Exception1 e1 ) { " + + " e1.printStackTrace(); " + + " } catch ( fully.qualified.Exception2 e2 ) { " + + " System.out.println( e2.getMessage() ); " + + " } finally { " + + " System.exit(-1); " + + " } " + + "} "; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof TryCatchStatement); + TryCatchStatement trial = (TryCatchStatement) statements.get(0); + + assertEquals(1, trial.getBody().getStatements().size()); + assertTrue(trial.getBody().getStatements().get(0) instanceof DeclareStatement); + + assertEquals(1, trial.getFinally().getStatements().size()); + assertTrue(trial.getFinally().getStatements().get(0) instanceof InvokeStatement); + InvokeStatement inv = (InvokeStatement) trial.getFinally().getStatements().get(0); + assertEquals("exit", inv.getMethodName()); + assertEquals(1, inv.getArguments().size()); + + assertEquals(2, trial.getCatches().size()); + Map catches = trial.getCatches(); + for (DeclareExpression key : catches.keySet()) + { + if ("e1".equals(key.getVariableName())) + { + assertEquals("Exception1", key.getVariableType()); + BlockSource b = catches.get(key); + assertEquals(1, b.getStatements().size()); + assertTrue(b.getStatements().get(0) instanceof InvokeStatement); + } else if ("e2".equals(key.getVariableName())) + { + assertEquals("fully.qualified.Exception2", key.getVariableType()); + BlockSource b = catches.get(key); + assertEquals(1, b.getStatements().size()); + assertTrue(b.getStatements().get(0) instanceof InvokeStatement); + } else + { + fail("Unrecognized catch declaration " + key); + } + } + } + + @Test + public void testMethodInvocationWithReturn() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return callIt( 3, \"aaa\", 1+1 ); " + + "} " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertTrue(((ReturnStatement) statements.get(0)).getReturn() instanceof MethodCallExpression); + MethodCallExpression call = (MethodCallExpression) ((ReturnStatement) statements.get(0)).getReturn(); + assertEquals("callIt", call.getMethodName()); + assertEquals(3, call.getArguments().size()); + assertTrue(call.getArguments().get(0) instanceof NumberLiteral); + assertTrue(call.getArguments().get(1) instanceof StringLiteral); + assertTrue(call.getArguments().get(2) instanceof OperatorExpression); + } + + @Test + public void testRebuildSynchronized() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " synchronized( x ) { " + + " x++; " + + " } " + + " } " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof SynchStatement); + SynchStatement synch = (SynchStatement) statements.get(0); + + assertTrue(synch.getSynchOn() instanceof Variable); + assertEquals(1, synch.getBody().getStatements().size()); + } + + @Test + public void testRebuildSwitchCase() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " switch( x ) { " + + " case 1 : return 0; " + + " case 2 : y++;" + + " case 3 : z++; break;" + + " case 4 : " + + " default : return -1; " + + " } " + + " } " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof SwitchStatement); + SwitchStatement swich = (SwitchStatement) statements.get(0); + assertTrue(swich.getSwitch() instanceof Variable); + + assertEquals(5, swich.getCaseExpressions().size()); + for (int j = 0; j < 4; j++) + { + assertTrue(swich.getCaseExpressions().get(j) instanceof NumberLiteral); + } + assertNull(swich.getCaseExpressions().get(4)); + + assertTrue(swich.getStatements().get(0) instanceof SwitchCaseStatement); + assertTrue(swich.getStatements().get(2) instanceof SwitchCaseStatement); + assertTrue(swich.getStatements().get(4) instanceof SwitchCaseStatement); + assertTrue(swich.getStatements().get(7) instanceof SwitchCaseStatement); + assertTrue(swich.getStatements().get(8) instanceof SwitchCaseStatement); + + assertTrue(swich.getStatements().get(1) instanceof ReturnStatement); + assertTrue(((ReturnStatement) swich.getStatements().get(1)).getReturn() instanceof NumberLiteral); + assertTrue(swich.getStatements().get(3) instanceof ExpressionStatement); + assertTrue(((ExpressionStatement) swich.getStatements().get(3)).getExpr() instanceof PostFixExpression); + assertTrue(swich.getStatements().get(5) instanceof ExpressionStatement); + assertTrue(((ExpressionStatement) swich.getStatements().get(5)).getExpr() instanceof PostFixExpression); + assertTrue(swich.getStatements().get(6) instanceof BreakStatement); + assertTrue(swich.getStatements().get(9) instanceof ReturnStatement); + assertTrue(((ReturnStatement) swich.getStatements().get(9)).getReturn() instanceof UnaryExpression); + } + + @Test + public void testMethodInvocations() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " callIt( 3, \"aaa\", 1+1 ); " + + " getFoo(); " + + "} " + + "}"; + List statements = parseSource(source); + assertEquals(2, statements.size()); + assertTrue(statements.get(0) instanceof InvokeStatement); + InvokeStatement inv = (InvokeStatement) statements.get(0); + assertEquals("callIt", inv.getMethodName()); + assertEquals(3, inv.getArguments().size()); + assertTrue(statements.get(1) instanceof ExpressionStatement); + assertTrue(((ExpressionStatement) statements.get(1)).getExpr() instanceof Getter); + } + + @Test + public void testMethodInvocationsChain() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " x.a().b(); " + + "} " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof InvokeStatement); + InvokeStatement inv = (InvokeStatement) statements.get(0); + assertEquals("b", inv.getMethodName()); + assertTrue(inv.getInvocationTarget() instanceof MethodCallExpression); + MethodCallExpression call = (MethodCallExpression) inv.getInvocationTarget(); + assertTrue(call.getInvocationTarget() instanceof Variable); + + } + + @Test + public void testMethodInvocationsChainGetSet() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " x.getA().setB( 0 ); " + + "} " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof InvokeStatement); + InvokeStatement inv = (InvokeStatement) statements.get(0); + assertEquals("setB", inv.getMethodName()); + assertTrue(inv.getArguments().get(0) instanceof NumberLiteral); + assertTrue(inv.getInvocationTarget() instanceof Getter); + Getter call = (Getter) inv.getInvocationTarget(); + assertTrue(call.getInvocationTarget() instanceof Variable); + + ExpressionSource inner = inv.getExpr(); + assertTrue(inner instanceof Setter); + Setter set = (Setter) inner; + assertTrue(set.getArguments().get(0) instanceof NumberLiteral); + assertTrue(set.getValue() instanceof NumberLiteral); + + } + + @Test + public void testParen() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return (2*1) + (x); " + + "} " + + "}"; + List statements = parseSource(source); + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + OperatorExpression op = (OperatorExpression) ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(op.getArguments().get(0) instanceof ParenExpression); + assertTrue(op.getArguments().get(1) instanceof ParenExpression); + assertTrue(((ParenExpression) op.getArguments().get(0)).getExpression() instanceof OperatorExpression); + } + + @Test + public void testRebuildVariableDeclarationsInline() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " int a = 3, b, c, d = 5; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof DeclareStatement); + DeclareStatement dec = (DeclareStatement) statements.get(0); + assertEquals(Arrays.asList("a", "b", "c", "d"), dec.getVariableNames()); + assertEquals("a", dec.getVariableName()); + assertTrue(dec.getInitExpression() instanceof NumberLiteral); + assertNull(dec.getInitExpressions().get("b")); + assertTrue(dec.getInitExpressions().get("d") instanceof NumberLiteral); + assertEquals(3L, ((NumberLiteral) dec.getInitExpression()).getValue()); + assertEquals(5L, ((NumberLiteral) dec.getInitExpressions().get("d")).getValue()); + } + + @Test + public void testComments() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " // test" + + " /* test2 */ " + + "} " + + "}"; + List statements = parseSource(source); + // TODO tests are not supported yet + assertTrue(statements.isEmpty()); + } + + @Test + public void testRebuildReturnStatementTernary() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return x > 0 ? 4 : 2; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertTrue(((ReturnStatement) statements.get(0)).getReturn() instanceof TernaryExpression); + TernaryExpression tern = (TernaryExpression) ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(tern.getCondition() instanceof OperatorExpression); + assertTrue(tern.getThenExpression() instanceof NumberLiteral); + assertTrue(tern.getElseExpression() instanceof NumberLiteral); + } + + @Test + public void testRebuildReturnStatementChar() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return 'x'; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + assertTrue(((ReturnStatement) statements.get(0)).getReturn() instanceof CharacterLiteral); + assertEquals((Character) 'x', ((CharacterLiteral) ((ReturnStatement) statements.get(0)).getReturn()).getValue()); + } + + @Test + public void testRebuildReturnStatementExpr() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return 3+5*4; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource ret = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(ret instanceof OperatorExpression); + OperatorExpression opxp = (OperatorExpression) ret; + assertTrue(opxp.getArguments().get(0) instanceof NumberLiteral); + assertEquals("+", opxp.getOperator()); + assertTrue(opxp.getArguments().get(1) instanceof OperatorExpression); + OperatorExpression sub = (OperatorExpression) opxp.getArguments().get(1); + assertEquals("*", sub.getOperator()); + assertEquals(4L, ((NumberLiteral) sub.getArguments().get(1)).getValue()); + assertEquals(5L, ((NumberLiteral) sub.getArguments().get(0)).getValue()); + } + + @Test + public void testRebuildReturnStatementWithLiteralExpression() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return 4+x; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource ret = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(ret instanceof OperatorExpression); + OperatorExpression opxp = (OperatorExpression) ret; + assertTrue(opxp.getArguments().get(0) instanceof NumberLiteral); + assertTrue(opxp.getArguments().get(1) instanceof Variable); + } + + @Test + public void testRebuildReturnStatementWithLiteralExpression2() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return 4+x+7; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource ret = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(ret instanceof OperatorExpression); + OperatorExpression opxp = (OperatorExpression) ret; + assertEquals(3, opxp.getArguments().size()); + assertTrue(opxp.getArguments().get(0) instanceof NumberLiteral); + assertTrue(opxp.getArguments().get(1) instanceof Variable); + } + + @Test + public void testRebuildReturnStatementWithThis() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return this.x; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource val = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(val instanceof Field); + Field f = (Field) val; + assertEquals("x", f.getFieldName()); + assertTrue(f.getInvocationTarget() instanceof ThisLiteral); + } + + @Test + public void testRebuildReturnStatementWithChain() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return this.a.b.c.d; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource val = ((ReturnStatement) statements.get(0)).getReturn(); + Field f = null; + String[] names = {"d", "c", "b", "a"}; + for (int j = 0; j < 4; j++) + { + assertTrue(val instanceof Field); + f = (Field) val; + assertEquals(names[j], f.getFieldName()); + val = f.getInvocationTarget(); + } + assertTrue(f.getInvocationTarget() instanceof ThisLiteral); + } + + @Test + public void testRebuildReturnStatementWithConstructor() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return new Integer(42); " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource val = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(val instanceof ConstructorExpression); + assertEquals(1, ((ConstructorExpression) val).getArguments().size()); + } + + @Test + public void testRebuildConstrCall() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " new Integer(42); " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ExpressionStatement); + ExpressionSource val = ((ExpressionStatement) statements.get(0)).getExpr(); + assertTrue(val instanceof ConstructorExpression); + assertEquals(1, ((ConstructorExpression) val).getArguments().size()); + } + + @Test + public void testRebuildConstrCallAsTarget() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " new Integer(42).toString(); " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ExpressionStatement); + ExpressionSource val = ((ExpressionStatement) statements.get(0)).getExpr(); + assertTrue(val instanceof MethodCallExpression); + assertEquals(0, ((MethodCallExpression) val).getArguments().size()); + assertTrue(((MethodCallExpression) val).getInvocationTarget() instanceof ConstructorExpression); + } + + @Test + public void testConstructorMutualCall() + { + String source = "public class Bean { " + + " public Bean() { this(42); } " + + " public Bean( int i ) {} " + + "}"; + JavaClassSource javaClassSource = Roaster.parse(JavaClass.class, source).asJavaClassSource(); + List> methods = javaClassSource.getMethods(); + assertEquals(2, methods.size()); + MethodSource method = methods.get(0); + assertEquals(1, method.getBodyAsBlock().getStatements().size()); + StatementSource st = method.getBodyAsBlock().getStatements().get(0); + assertTrue(st instanceof ThisStatement); + assertEquals(1, ((ThisStatement) st).getArguments().size()); + } + + @Test + public void testConstructorMutualCallSuper() + { + String source = "public class Bean { " + + " public Bean() { super(42); } " + + "}"; + JavaClassSource javaClassSource = Roaster.parse(JavaClass.class, source).asJavaClassSource(); + List> methods = javaClassSource.getMethods(); + assertEquals(1, methods.size()); + MethodSource method = methods.get(0); + assertEquals(1, method.getBodyAsBlock().getStatements().size()); + StatementSource st = method.getBodyAsBlock().getStatements().get(0); + assertTrue(st instanceof SuperStatement); + assertEquals(1, ((SuperStatement) st).getArguments().size()); + } + + @Test + public void testRebuildReturnStatementWithGetter() + { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return getFoo(); " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource val = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(val instanceof Getter); + assertEquals("foo", ((Getter) val).getFieldName()); + } + + @Test + public void testRebuildReturnKnownField() + { + String source = "public class Bean { " + + " private String str; " + + "public String echo(String x) { " + + " return this.str; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource val = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(val instanceof Field); + } + + @Test + public void testRebuildArrayAccess() + { + String source = "public class Bean { " + + "public String echo(String[] x) { " + + " return x[0]; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource val = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(val instanceof ArrayIndexer); + assertTrue(((ArrayIndexer) val).getIndex() instanceof NumberLiteral); + assertTrue(((ArrayIndexer) val).getInvocationTarget() instanceof Variable); + } + + @Test + public void testRebuildArrayAccessMultiple() + { + String source = "public class Bean { " + + "public String echo(String[] x) { " + + " return x[i][j]; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource val = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(val instanceof ArrayIndexer); + ArrayIndexer ix = (ArrayIndexer) val; + assertTrue(ix.getIndex() instanceof Variable); + assertEquals("j", ((Variable) ix.getIndex()).getName()); + assertTrue(ix.getInvocationTarget() instanceof ArrayIndexer); + ArrayIndexer iy = (ArrayIndexer) ix.getInvocationTarget(); + assertTrue(iy.getInvocationTarget() instanceof Variable); + assertTrue(iy.getIndex() instanceof Variable); + assertEquals("i", ((Variable) iy.getIndex()).getName()); + } + + @Test + public void testReturnArray() + { + String source = "public class Bean { " + + "public String[] echo() { " + + " return new String[3]; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource val = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(val instanceof ArrayConstructorExpression); + assertEquals(1, ((ArrayConstructorExpression) val).getDimensions().size()); + assertTrue(((ArrayConstructorExpression) val).getDimensions().get(0) instanceof NumberLiteral); + } + + @Test + public void testReturnArrayMultiDim() + { + String source = "public class Bean { " + + "public String[] echo() { " + + " return new String[3][j]; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource val = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(val instanceof ArrayConstructorExpression); + assertEquals(2, ((ArrayConstructorExpression) val).getDimensions().size()); + assertEquals(2, ((ArrayConstructorExpression) val).getDimension()); + assertTrue(((ArrayConstructorExpression) val).getDimensions().get(1) instanceof Variable); + } + + @Test + public void testReturnArrayWithInit() + { + String source = "public class Bean { " + + "public String[] echo() { " + + " return new String[] { \"a\", \"b\" } ; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource val = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(val instanceof ArrayConstructorExpression); + assertEquals(0, ((ArrayConstructorExpression) val).getDimensions().size()); + assertEquals(1, ((ArrayConstructorExpression) val).getDimension()); + assertNotNull(((ArrayConstructorExpression) val).getInit()); + ExpressionSource expr = ((ArrayConstructorExpression) val).getInit(); + assertTrue(expr instanceof ArrayInit); + ArrayInit init = (ArrayInit) expr; + assertEquals(2, init.getElements().size()); + assertTrue(init.getElements().get(0) instanceof StringLiteral); + assertTrue(init.getElements().get(1) instanceof StringLiteral); + } + + @Test + public void testAssignment() + { + String source = "public class Bean { " + + "public String[] echo() { " + + " x = 2; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof AssignmentStatement); + AssignmentStatement axx = (AssignmentStatement) statements.get(0); + assertTrue(axx.getLeft() instanceof Variable); + assertTrue(axx.getRight() instanceof NumberLiteral); + } + + + @Test + public void testReturnArrayWithInitMultiDimension() + { + String source = "public class Bean { " + + "public String[] echo() { " + + " return new String[][] { {\"a\", \"b\"}, {\"c\"} } ; " + + "} " + + "}"; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof ReturnStatement); + ExpressionSource val = ((ReturnStatement) statements.get(0)).getReturn(); + assertTrue(val instanceof ArrayConstructorExpression); + assertEquals(0, ((ArrayConstructorExpression) val).getDimensions().size()); + assertEquals(2, ((ArrayConstructorExpression) val).getDimension()); + assertNotNull(((ArrayConstructorExpression) val).getInit()); + ExpressionSource expr = ((ArrayConstructorExpression) val).getInit(); + assertTrue(expr instanceof ArrayInit); + ArrayInit init = (ArrayInit) expr; + assertEquals(2, init.getElements().size()); + assertTrue(init.getElements().get(0) instanceof ArrayInit); + assertTrue(init.getElements().get(1) instanceof ArrayInit); + ArrayInit d1 = (ArrayInit) init.getElements().get(0); + ArrayInit d2 = (ArrayInit) init.getElements().get(1); + assertEquals(2, d1.size()); + assertEquals(1, d2.size()); + } + + @Test + public void testRebuildAssertStatement() + { + String source = "public class Bean { " + + "public void foo() { " + + " assert x : \"So be it\"; " + + "} " + + "} "; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof AssertStatement); + AssertStatement as = (AssertStatement) statements.get(0); + assertEquals("So be it", ((StringLiteralImpl) as.getMessage()).getValue()); + assertEquals("x", ((Variable) as.getAssertion()).getName()); + } + + @Test + public void testRebuildBlockStatement() + { + String source = "public class Bean { " + + "public int foo() { " + + " { return 0; } " + + "} " + + "} "; + List statements = parseSource(source); + + assertEquals(1, statements.size()); + assertTrue(statements.get(0) instanceof BlockStatement); + BlockStatement bs = (BlockStatement) statements.get(0); + assertEquals(1, bs.getStatements().size()); + assertTrue(bs.getStatements().get(0) instanceof ReturnStatement); + ReturnStatement rs = (ReturnStatement) bs.getStatements().get(0); + ExpressionSource val = rs.getReturn(); + assertTrue(val instanceof NumberLiteral); + assertEquals(0L, ((NumberLiteral) val).getValue()); + } + + private List parseSource(String source) + { + JavaClassSource javaClassSource = Roaster.parse(JavaClass.class, source).asJavaClassSource(); + List> methods = javaClassSource.getMethods(); + assertEquals(1, methods.size()); + MethodSource method = methods.get(0); + + return method.getBodyAsBlock().getStatements(); + } + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ReturnStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ReturnStatementsTest.java new file mode 100644 index 00000000..d29f3b43 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ReturnStatementsTest.java @@ -0,0 +1,272 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.expressions.PrefixOp; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import java.util.UUID; + +import static org.jboss.forge.roaster.model.expressions.Expressions.cast; +import static org.jboss.forge.roaster.model.expressions.Expressions.classStatic; +import static org.jboss.forge.roaster.model.expressions.Expressions.field; +import static org.jboss.forge.roaster.model.expressions.Expressions.getter; +import static org.jboss.forge.roaster.model.expressions.Expressions.invoke; +import static org.jboss.forge.roaster.model.expressions.Expressions.literal; +import static org.jboss.forge.roaster.model.expressions.Expressions.newArray; +import static org.jboss.forge.roaster.model.expressions.Expressions.nullLiteral; +import static org.jboss.forge.roaster.model.expressions.Expressions.operator; +import static org.jboss.forge.roaster.model.expressions.Expressions.paren; +import static org.jboss.forge.roaster.model.expressions.Expressions.ternary; +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.expressions.Expressions.vec; +import static org.jboss.forge.roaster.model.statements.Statements.newReturn; +import static org.junit.Assert.assertEquals; + +public class ReturnStatementsTest +{ + @Test + public void testReturn() throws Exception + { + String target = "return;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newReturn()); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnArg() throws Exception + { + String target = "return x;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String echo( String x )"); + + method.setBody(newReturn().setReturn(var("x"))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnExpr() throws Exception + { + String target = "return x + 5;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String echo( String x )"); + + method.setBody(newReturn().setReturn(operator(Op.PLUS).addArgument(var("x")).addArgument(literal(5)))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnInvoke() throws Exception + { + String target = "return java.util.UUID.randomUUID();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String foo()"); + + method.setBody(newReturn().setReturn(invoke("randomUUID").setInvocationTarget(classStatic(UUID.class)))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnInvokeWithArgs() throws Exception + { + String target = "return java.util.UUID.fromString(\"xyz\");"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String foo()"); + + method.setBody(newReturn().setReturn(invoke("fromString") + .setInvocationTarget(classStatic(UUID.class)) + .addArgument(literal("xyz")))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnTernary() throws Exception + { + String target = "return x > 0 ? 4 : 5;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public int foo( int x )"); + + method.setBody(newReturn().setReturn(ternary() + .setCondition(operator(Op.GREATER).addArgument(var("x")).addArgument(literal(0))) + .setThenExpression(literal(4)) + .setElseExpression(literal(5)))); + ; + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnTernaryWithParen() throws Exception + { + String target = "return 1 + (x > 3 ? 4 : 5) + 2;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public int foo( in x )"); + + method.setBody(newReturn().setReturn( + operator(Op.PLUS) + .addArgument(literal(1)) + .addArgument(paren(ternary() + .setCondition(operator(Op.GREATER).addArgument(var("x")).addArgument(literal(3))) + .setThenExpression(literal(4)) + .setElseExpression(literal(5)))) + .addArgument(literal(2)))); + ; + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnCast() throws Exception + { + String target = "return (long)(x);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public long foo( int x )"); + + method.setBody(newReturn().setReturn(cast(long.class, var("x")))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnNull() throws Exception + { + String target = "return null;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String foo()"); + + method.setBody(newReturn().setReturn(nullLiteral())); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnChain() throws Exception + { + String target = "return getName().bar();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String foo()"); + + method.setBody(newReturn().setReturn(getter("name", "String").invoke("bar"))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnChainWithField() throws Exception + { + String target = "return getName().baz;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String foo()"); + + method.setBody(newReturn().setReturn(getter("name", "String").field("baz"))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnChainWithFields() throws Exception + { + String target = "return this.foo.bar.baz;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String foo()"); + + method.setBody(newReturn().setReturn(field("foo").field("bar").field("baz"))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnChainWithFieldsAndMethods() throws Exception + { + String target = "return this.foo.getBar().baz.doSomething(this.x.y).getRes();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String foo()"); + + method.setBody(newReturn().setReturn(field("foo").getter("bar", String.class.getName()).field("baz") + .invoke("doSomething").addArgument(field("x").field("y")).invoke("getRes"))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnUnary() throws Exception + { + String target = "return 3 * -2;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String foo()"); + + method.setBody(newReturn().setReturn(operator(Op.TIMES).addArgument(literal(3)).addArgument(operator(PrefixOp.MINUS).setExpression(literal(2))))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnUnaryParen() throws Exception + { + String target = "return 3 * -(2 + 1);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String foo()"); + + method.setBody(newReturn().setReturn(operator(Op.TIMES) + .addArgument(literal(3)) + .addArgument(operator(PrefixOp.MINUS).setExpression( + paren(operator(Op.PLUS) + .addArgument(literal(2)).addArgument(literal(1))))))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnArray() throws Exception + { + String target = "return new int[2][3];"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public int[][] foo()"); + + method.setBody(newReturn().setReturn(newArray(int.class).addDimension(literal(2)).addDimension(literal(3)))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnArrayWithImpliedInit() throws Exception + { + String target = "return new int[][]{{1,2},{3,4}};"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public int[][] foo()"); + + method.setBody(newReturn().setReturn(newArray(int.class).init( + vec().addElement( + vec().addElement(literal(1)).addElement(literal(2))) + .addElement( + vec().addElement(literal(3)).addElement(literal(4)) + ) + ))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testReturnArrayAccess() throws Exception + { + String target = "return x[4][2].getY();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public int[][] foo()"); + + method.setBody(newReturn().setReturn( + var("x").itemAt(literal(4)).itemAt(literal(2)).getter("y", String.class) + )); + + assertEquals(target, method.getBody().trim()); + } + + + @Test + public void testReturnChain2() throws Exception + { + String target = "return this.foo.getBar().getRes();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String foo()"); + + method.setBody(newReturn().setReturn(field("foo").getter("bar", String.class.getName()).invoke("getRes"))); + + assertEquals(target, method.getBody().trim()); + } +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/SuperTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/SuperTest.java new file mode 100644 index 00000000..d45ac41d --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/SuperTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.jboss.forge.roaster.model.expressions.Expressions.sup; +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.statements.Statements.newReturn; +import static org.junit.Assert.assertEquals; + +public class SuperTest +{ + @Test + public void testSuperField() throws Exception + { + String target = "return super.x;"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newReturn().setReturn(sup().field("x"))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testSuperGetter() throws Exception + { + String target = "return super.getX();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newReturn().setReturn(sup().getter("x", String.class))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testSuperSetter() throws Exception + { + String target = "return super.setX(y);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newReturn().setReturn(sup().setter("x", String.class, var("y")))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testSuperInvoke() throws Exception + { + String target = "return super.foo(x);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newReturn().setReturn(sup().invoke("foo").addArgument(var("x")))); + + assertEquals(target, method.getBody().trim()); + } + + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/WhileStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/WhileStatementsTest.java new file mode 100644 index 00000000..eebbe763 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/WhileStatementsTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.jboss.forge.roaster.model.expressions.Expressions.literal; +import static org.jboss.forge.roaster.model.expressions.Expressions.operator; +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.statements.Statements.newAssign; +import static org.jboss.forge.roaster.model.statements.Statements.newDoWhile; +import static org.jboss.forge.roaster.model.statements.Statements.newWhile; +import static org.junit.Assert.assertEquals; + + +public class WhileStatementsTest +{ + @Test + public void testSimpleWhile() throws Exception + { + String target = "while (x < 0) {\n x=x + 1;\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newWhile() + .setCondition(operator(Op.LESS) + .addArgument(var("x")) + .addArgument(literal(0))) + .setBody(newAssign() + .setLeft(var("x")) + .setRight(operator(Op.PLUS).addArgument(var("x")).addArgument(literal(1))))); + + assertEquals(target, method.getBody().trim()); + } + + @Test + public void testSimpleWhile2() throws Exception + { + String target = "while (x < 0) {\n x=x + 1;\n}"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newWhile() + .setBody(newAssign() + .setLeft(var("x")) + .setRight(operator(Op.PLUS).addArgument(var("x")).addArgument(literal(1)))) + .setCondition(operator(Op.LESS) + .addArgument(var("x")) + .addArgument(literal(0)))); + + assertEquals(target, method.getBody().trim()); + } + + + @Test + public void testSimpleDoWhile() throws Exception + { + String target = "do {\n x=x + 1;\n}\n while (x < 0);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); + + method.setBody(newDoWhile() + .setCondition(operator(Op.LESS) + .addArgument(var("x")) + .addArgument(literal(0))) + .setBody(newAssign() + .setLeft(var("x")) + .setRight(operator(Op.PLUS).addArgument(var("x")).addArgument(literal(1))))); + + assertEquals(target, method.getBody().trim()); + } + + +}