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 index b1244711..59468c94 100644 --- 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 @@ -19,41 +19,48 @@ 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 getTarget() { - return this.target; - } - - @Override - public M setTarget( ExpressionSource target ) { - this.target = (ExpressionSource) target; - return (M) this; - } - - @Override - public List> getArguments() { - return Collections.unmodifiableList( arguments ); - } + 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 getTarget() + { + return this.target; + } + + @Override + public M setTarget(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 index 7f56918f..1380c93c 100644 --- 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 @@ -19,38 +19,46 @@ 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 ); - } + 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 index f5b0cb0c..b0de0280 100644 --- 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 @@ -15,69 +15,81 @@ import org.jboss.forge.roaster.model.source.JavaSource; public class ArrayAccessImpl, - P extends ExpressionHolder> - extends SimpleAccessorImpl,ArrayAccess> - implements ArrayIndexer { + P extends ExpressionHolder> + extends SimpleAccessorImpl,ArrayAccess> + implements ArrayIndexer +{ - private ExpressionSource,?> index; - private ExpressionSource,?> target; - private ArrayAccess axx; + private ExpressionSource,?> index; + private ExpressionSource,?> target; + private ArrayAccess axx; - public ArrayAccessImpl( ExpressionSource index ) { - ExpressionSource,?> cast = (ExpressionSource, ?>) index; - this.index = cast; - } + 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 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 ArrayAccess getInternal() + { + return axx; + } - @Override - public void setInternal(ArrayAccess jdtNode) { - super.setInternal(jdtNode); - this.axx = jdtNode; - } + @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,?> chainExpression(ExpressionSource child) + { + ExpressionSource,?> cast = (ExpressionSource,?>) child; + this.target = cast; + return cast; + } - @Override - public ExpressionSource, ?> getIndex() { - return index; - } + @Override + public ExpressionSource,?> getIndex() + { + return index; + } - @Override - public ArrayIndexer setIndex( ExpressionSource index ) { - this.index = (ExpressionSource, ?>) index; - return this; - } + @Override + public ArrayIndexer setIndex(ExpressionSource index) + { + this.index = (ExpressionSource,?>) index; + return this; + } - @Override - public ExpressionSource, ?> getTarget() { - return target; - } + @Override + public ExpressionSource,?> getTarget() + { + return target; + } - @Override - public ArrayIndexer setTarget( ExpressionSource target ) { - this.target = (ExpressionSource, ?>) target; - return this; - } + @Override + public ArrayIndexer setTarget(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 index 842564e0..3ddf5698 100644 --- 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 @@ -23,81 +23,97 @@ 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 ); - } +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 index 1189caea..01debbe6 100644 --- 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 @@ -18,76 +18,93 @@ import java.util.Collections; import java.util.List; -public class ArrayInitImpl, - P extends ExpressionHolder> - extends NonPrimitiveExpressionImpl,ArrayInitializer> - implements ArrayInit { +public class ArrayInitImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,ArrayInitializer> + implements ArrayInit +{ - private List,?>> elements = Collections.EMPTY_LIST; + private List,?>> elements = Collections.EMPTY_LIST; - private ArrayInitializer init; + 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(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 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 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 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 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 ArrayInitializer getInternal() + { + return init; + } - @Override - public void setInternal(ArrayInitializer jdtNode) { - super.setInternal(jdtNode); - this.init = jdtNode; - } + @Override + public void setInternal(ArrayInitializer jdtNode) + { + super.setInternal(jdtNode); + this.init = jdtNode; + } - @Override - public List, ?>> getElements() { - return Collections.unmodifiableList( elements ); - } + @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 index 15fefdf2..ac1f1d96 100644 --- 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 @@ -12,82 +12,95 @@ 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.Expression; 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; - } + 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 index 4bfb7379..73aaf9a9 100644 --- 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 @@ -12,21 +12,24 @@ 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 { + P extends ExpressionHolder, + E extends MethodInvokeExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends AccessorImpl + implements MethodInvokeExpression +{ - protected String method; + protected String method; - public BaseInvokeableImpl( String method ) { - this.method = method; - } + public BaseInvokeableImpl(String method) + { + this.method = method; + } - @Override - public String getMethodName() { - return 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 index 6bc18fa2..aea864d1 100644 --- 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 @@ -39,202 +39,249 @@ 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 PrimitiveLiteral literal( Number val ) { - return new NumberLiteralImpl( val ); - } - - @Override - public PrimitiveLiteral 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 PrimitiveLiteral 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, Argument arg ) { - return new UnaryImpl( op, arg ); - } - - @Override - public NullLiteral nullLiteral() { - return new NullLiteralImpl(); - } - - @Override - public UnaryExpression not( Argument 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>(); - } + 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 PrimitiveLiteral literal(Number val) + { + return new NumberLiteralImpl(val); + } + + @Override + public PrimitiveLiteral 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 PrimitiveLiteral 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, Argument arg) + { + return new UnaryImpl(op, arg); + } + + @Override + public NullLiteral nullLiteral() + { + return new NullLiteralImpl(); + } + + @Override + public UnaryExpression not(Argument 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 index ad77239c..58368ab1 100644 --- 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 @@ -14,39 +14,46 @@ 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; - } + 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 index e16a6685..46e6b3c4 100644 --- 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 @@ -15,56 +15,65 @@ 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 { +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 org.eclipse.jdt.core.dom.CastExpression cast; - private String type; - private ExpressionSource,?> expression; + private String type; + private ExpressionSource,?> expression; - public CastImpl( String klass, ExpressionSource expression ) { - this.type = klass; - this.expression = (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 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 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 void setInternal(org.eclipse.jdt.core.dom.CastExpression jdtNode) + { + super.setInternal(jdtNode); + this.cast = jdtNode; + } - @Override - public String getType() { - return type; - } + @Override + public String getType() + { + return type; + } - @Override - public ExpressionSource,?> getExpression() { - return expression; - } + @Override + public ExpressionSource,?> getExpression() + { + return expression; + } } 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 index 4031c400..d27c3882 100644 --- 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 @@ -14,40 +14,47 @@ 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; - } + 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 index 9c5e8bc5..75b30f8f 100644 --- 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 @@ -15,40 +15,47 @@ 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; - } + 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 index 4c8c315b..452efcfd 100644 --- 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 @@ -23,62 +23,73 @@ 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]" ); - } + 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 index 40e5882f..0f64743c 100644 --- 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 @@ -23,111 +23,132 @@ 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 ); + 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)); } - } - - 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; - } + 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 index cbeb3798..1d8bba73 100644 --- 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 @@ -23,55 +23,62 @@ import org.jboss.forge.roaster.spi.ExpressionFactoryImpl; public class DotAccessorImpl, - P extends ExpressionHolder, - E extends NonPrimitiveExpression> - extends ExpressionImpl - implements Accessor + 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 ); - } + 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); + } /* @@ -84,38 +91,43 @@ public Variable var( String varName ) { */ - @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" ); - } + @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 index 0d20c8b7..aac479e8 100644 --- 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 @@ -94,190 +94,217 @@ import static org.jboss.forge.roaster.model.expressions.Expressions.var; import static org.jboss.forge.roaster.model.expressions.Expressions.vec; -public class ExpressionBuilder { +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() ) ); + 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() ), (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).setTarget( 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; - } + 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()), (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).setTarget(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/ExpressionImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionImpl.java index aa3e595b..d5370174 100644 --- 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 @@ -13,22 +13,27 @@ 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; - } + 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 index 979ee4b8..d5f6d0a2 100644 --- 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 @@ -15,59 +15,70 @@ import org.jboss.forge.roaster.model.source.JavaSource; public class FieldImpl, - P extends ExpressionHolder> - extends SimpleAccessorImpl,FieldAccess> - implements Field { + P extends ExpressionHolder> + extends SimpleAccessorImpl,FieldAccess> + implements Field +{ - private FieldAccess fld; + private FieldAccess fld; - private String fieldName; - private ExpressionSource,?> expression; + private String fieldName; + private ExpressionSource,?> expression; - public FieldImpl( String fieldName ) { - this.fieldName = fieldName; - } + 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 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 FieldAccess getInternal() + { + return fld; + } - @Override - public void setInternal( FieldAccess jdtNode) { - super.setInternal(jdtNode); - this.fld = jdtNode; - } + @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 ExpressionSource,?> chainExpression(ExpressionSource child) + { + ExpressionSource,?> cast = (ExpressionSource,?>) child; + this.expression = cast; + return cast; + } - @Override - public String getFieldName() { - return fieldName; - } + @Override + public String getFieldName() + { + return fieldName; + } - @Override - public ExpressionSource, ?> getSource() { - return expression; - } + @Override + public ExpressionSource,?> getSource() + { + return expression; + } } 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 index e20a2882..81854246 100644 --- 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 @@ -16,57 +16,67 @@ import org.jboss.forge.roaster.model.source.JavaSource; public class GetterImpl, - P extends ExpressionHolder> - extends InvokeableImpl,MethodInvocation> - implements Getter { + P extends ExpressionHolder> + extends InvokeableImpl,MethodInvocation> + implements Getter +{ - protected MethodInvocation invoke; + protected MethodInvocation invoke; - private String fieldName; + private String fieldName; - public GetterImpl( String fieldName, String type ) { - super( JDTHelper.getter( fieldName, type ) ); - this.fieldName = fieldName; - } + public GetterImpl(String fieldName, String type) + { + super(JDTHelper.getter(fieldName, type)); + this.fieldName = fieldName; + } - @Override - public ExpressionSource, ?> getTarget() { - return target; - } + @Override + public ExpressionSource,?> getTarget() + { + return target; + } - @Override - public Getter setTarget( ExpressionSource target ) { - this.target = chainExpression( (ExpressionSource) target ); - return this; - } + @Override + public Getter setTarget(ExpressionSource target) + { + this.target = chainExpression((ExpressionSource) target); + return this; + } - @Override - public MethodInvocation materialize( AST ast ) { - if (isMaterialized()) { - return invoke; - } - invoke = ast.newMethodInvocation(); + @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; - } + invoke.setName(ast.newSimpleName(method)); + if (target != null) + { + invoke.setExpression(wireAndGetExpression(target, this, ast)); + } + return invoke; + } - @Override - public MethodInvocation getInternal() { - return invoke; - } + @Override + public MethodInvocation getInternal() + { + return invoke; + } - @Override - public void setInternal(MethodInvocation jdtNode) { - super.setInternal(jdtNode); - this.invoke = jdtNode; - } + @Override + public void setInternal(MethodInvocation jdtNode) + { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } - @Override - public String getFieldName() { - return fieldName; - } + @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 index 407f7044..75ff9575 100644 --- 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 @@ -15,51 +15,60 @@ import org.jboss.forge.roaster.model.source.JavaSource; public class InstanceofImpl, - P extends ExpressionHolder> - extends NonPrimitiveExpressionImpl,org.eclipse.jdt.core.dom.InstanceofExpression> - implements InstanceofExpression { + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,org.eclipse.jdt.core.dom.InstanceofExpression> + implements InstanceofExpression +{ - private org.eclipse.jdt.core.dom.InstanceofExpression isA; + private org.eclipse.jdt.core.dom.InstanceofExpression isA; - private String typeName; - private ExpressionSource,?> expression; + private String typeName; + private ExpressionSource,?> expression; - public InstanceofImpl( String klass, ExpressionSource expression ) { - this.typeName = klass; - this.expression = (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 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 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; - } + @Override + public void setInternal(org.eclipse.jdt.core.dom.InstanceofExpression jdtNode) + { + super.setInternal(jdtNode); + this.isA = jdtNode; + } - public String getTypeName() { - return typeName; - } + public String getTypeName() + { + return typeName; + } - @Override - public ExpressionSource, ?> getExpression() { - return expression; - } + @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 index 3724903b..d143b3f3 100644 --- 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 @@ -14,28 +14,32 @@ 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 ); - } - - @Override - public Accessor dot() { - return new DotAccessorImpl( this ); - } - - public ExpressionSource chainExpression( ExpressionSource child ) { - ExpressionSource cast = ( ExpressionSource ) child; - this.target = cast; - return cast; - } + 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); + } + + @Override + public Accessor dot() + { + return new DotAccessorImpl(this); + } + + 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 index 771be4e3..a9dfe3d9 100644 --- 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 @@ -15,10 +15,11 @@ import org.jboss.forge.roaster.model.source.JavaSource; public interface JdtExpressionWrapper, - P extends ExpressionHolder, - J extends org.eclipse.jdt.core.dom.Expression> - extends ASTNode { + 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 ); + 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 index d4a9f980..49afe707 100644 --- 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 @@ -13,12 +13,15 @@ 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 { + P extends ExpressionHolder, + L extends Literal, + J extends Expression> + extends ExpressionImpl + implements Literal +{ - public LiteralImpl() {} + 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 index 6660bbb1..d6c7138f 100644 --- 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 @@ -13,55 +13,61 @@ import org.jboss.forge.roaster.model.expressions.Argument; import org.jboss.forge.roaster.model.expressions.ExpressionChainLink; import org.jboss.forge.roaster.model.expressions.MethodCallExpression; -import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; import org.jboss.forge.roaster.model.source.JavaSource; -import java.util.List; - public class MethodInvokeImpl, - P extends ExpressionHolder> - extends AbstractMethodInvokeImpl,MethodInvocation> - implements MethodCallExpression, - ExpressionChainLink> { + P extends ExpressionHolder> + extends AbstractMethodInvokeImpl,MethodInvocation> + implements MethodCallExpression, + ExpressionChainLink> +{ - protected MethodInvocation invoke; + protected MethodInvocation invoke; - public MethodInvokeImpl( String method ) { - super( method ); - } + public MethodInvokeImpl(String method) + { + super(method); + } - @Override - public MethodCallExpression setMethodName( String name ) { - this.method = name; - return this; - } + @Override + public MethodCallExpression setMethodName(String name) + { + this.method = name; + return this; + } - @Override - public MethodInvocation materialize( AST ast ) { - if (isMaterialized()) { - return invoke; - } - invoke = ast.newMethodInvocation(); + @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; - } + 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 MethodInvocation getInternal() + { + return invoke; + } - @Override - public void setInternal( MethodInvocation jdtNode ) { - super.setInternal(jdtNode); - this.invoke = jdtNode; - } + @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 index 796b4493..ad3ba723 100644 --- 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 @@ -15,29 +15,35 @@ 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 ); - } + 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 index 68066421..5816bb2d 100644 --- 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 @@ -13,11 +13,13 @@ import org.jboss.forge.roaster.model.source.JavaSource; public class NotImpl, - P extends ExpressionHolder> - extends UnaryImpl { + P extends ExpressionHolder> + extends UnaryImpl +{ - public NotImpl( Argument expr ) { - super( PrefixOp.NOT, expr ); - } + public NotImpl(Argument expr) + { + super(PrefixOp.NOT, 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 index 94cfb543..9b19cf43 100644 --- 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 @@ -13,35 +13,43 @@ 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; - } + 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 index 92e7b290..07db43d4 100644 --- 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 @@ -14,39 +14,46 @@ 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; - } + 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 index ab42cb7d..422a41ed 100644 --- 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 @@ -22,87 +22,104 @@ 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 ); - } - } +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 index ecb8a020..d172c0e8 100644 --- 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 @@ -10,59 +10,67 @@ 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.Expression; 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 { + P extends ExpressionHolder> + extends ExpressionFactoryImpl,ParenthesizedExpression> + implements ParenExpression +{ - ParenthesizedExpression paren; + ParenthesizedExpression paren; - private ExpressionSource,?> inner; + private ExpressionSource,?> inner; - public ParenImpl( ExpressionSource child ) { - inner = (ExpressionSource, ?>) child; - } + public ParenImpl(ExpressionSource child) + { + inner = (ExpressionSource,?>) child; + } - @Override - public ParenthesizedExpression materialize( AST ast ) { - if (isMaterialized()) { - return paren; - } - paren = ast.newParenthesizedExpression(); + @Override + public ParenthesizedExpression materialize(AST ast) + { + if (isMaterialized()) + { + return paren; + } + paren = ast.newParenthesizedExpression(); - if ( inner != null ) { - paren.setExpression( wireAndGetExpression( inner, this, ast ) ); - } - return paren; - } + if (inner != null) + { + paren.setExpression(wireAndGetExpression(inner, this, ast)); + } + return paren; + } - @Override - public ParenthesizedExpression getInternal() { - return paren; - } + @Override + public ParenthesizedExpression getInternal() + { + return paren; + } - @Override - public void setInternal(ParenthesizedExpression jdtNode) { - super.setInternal(jdtNode); - this.paren = jdtNode; - } + @Override + public void setInternal(ParenthesizedExpression jdtNode) + { + super.setInternal(jdtNode); + this.paren = jdtNode; + } - @Override - public ExpressionSource, ?> getExpression() { - return inner; - } + @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 ); - } + 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 index 4ac7f89a..8b9c66cb 100644 --- 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 @@ -18,52 +18,61 @@ import org.jboss.forge.roaster.model.source.JavaSource; public class PostFixImpl, - P extends ExpressionHolder> - extends NonPrimitiveExpressionImpl,PostfixExpression> - implements PostFixExpression { + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,PostfixExpression> + implements PostFixExpression +{ - private PostfixExpression post; + private PostfixExpression post; - private PostfixOp op; - private OrdinalArgument,?> arg; + private PostfixOp op; + private OrdinalArgument,?> arg; - public PostFixImpl( PostfixOp op, OrdinalArgument root ) { - this.op = op; - this.arg = (OrdinalArgument, ?>) root; - } + public PostFixImpl(PostfixOp op, OrdinalArgument root) + { + this.op = op; + this.arg = (OrdinalArgument,?>) root; + } - @Override - public PostfixExpression materialize( AST ast ) { - if (isMaterialized()) { - return post; - } - post = ast.newPostfixExpression(); - post.setOperator( PostfixExpression.Operator.toOperator( op.getOp() ) ); + @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; - } + if (arg != null) + { + post.setOperand(wireAndGetExpression(arg, this, ast)); + } + return post; + } - @Override - public PostfixExpression getInternal() { - return post; - } + @Override + public PostfixExpression getInternal() + { + return post; + } - @Override - public void setInternal( PostfixExpression jdtNode ) { - super.setInternal(jdtNode); - this.post = jdtNode; - } + @Override + public void setInternal(PostfixExpression jdtNode) + { + super.setInternal(jdtNode); + this.post = jdtNode; + } - @Override - public ExpressionSource, ?> getExpression() { - return arg; - } + @Override + public ExpressionSource,?> getExpression() + { + return arg; + } - @Override - public String getOperator() { - return op.getOp(); - } + @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 index f4770c49..b5196dff 100644 --- 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 @@ -15,36 +15,44 @@ import org.jboss.forge.roaster.model.source.JavaSource; public class SelfArgumentImpl, - P extends ExpressionHolder> - extends AccessorImpl,ThisExpression> - implements ThisLiteral { - - 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; - } + P extends ExpressionHolder> + extends AccessorImpl,ThisExpression> + implements ThisLiteral +{ + + 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 index cc1fc5d8..9ccf6500 100644 --- 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 @@ -12,60 +12,69 @@ 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.Getter; 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 { + P extends ExpressionHolder> + extends AbstractMethodInvokeImpl,MethodInvocation> + implements Setter +{ - protected MethodInvocation invoke; + protected MethodInvocation invoke; - public SetterImpl( String fieldName, String type, ExpressionSource value ) { - super( JDTHelper.setter( fieldName, type ) ); - addArgument( (Argument, ?>) value ); - } + public SetterImpl(String fieldName, String type, ExpressionSource value) + { + super(JDTHelper.setter(fieldName, type)); + addArgument((Argument,?>) value); + } - @Override - public Setter setTarget( ExpressionSource target ) { - this.target = chainExpression( (ExpressionSource) target ); - return this; - } + @Override + public Setter setTarget(ExpressionSource target) + { + this.target = chainExpression((ExpressionSource) target); + return this; + } - @Override - public MethodInvocation materialize( AST ast ) { - if (isMaterialized()) { - return invoke; - } - invoke = ast.newMethodInvocation(); + @Override + public MethodInvocation materialize(AST ast) + { + if (isMaterialized()) + { + return invoke; + } + invoke = ast.newMethodInvocation(); - invoke.setName(ast.newSimpleName(method)); + invoke.setName(ast.newSimpleName(method)); - if ( target != null ) { - invoke.setExpression( wireAndGetExpression( target, this, ast ) ); - } + if (target != null) + { + invoke.setExpression(wireAndGetExpression(target, this, ast)); + } - for ( Argument,?> arg : arguments ) { - invoke.arguments().add( wireAndGetExpression( arg, this, ast ) ); - } + for (Argument,?> arg : arguments) + { + invoke.arguments().add(wireAndGetExpression(arg, this, ast)); + } - return invoke; - } + return invoke; + } - @Override - public MethodInvocation getInternal() { - return invoke; - } + @Override + public MethodInvocation getInternal() + { + return invoke; + } - @Override - public ExpressionSource, ?> getValue() { - if ( getArguments().isEmpty() ) { - return null; - } - return getArguments().get( 0 ); - } + @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 index 90120bc4..4b20c81f 100644 --- 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 @@ -15,19 +15,22 @@ 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 { + P extends ExpressionHolder, + E extends OrdinalArgument, + J extends Expression> + extends AccessorImpl + implements OrdinalArgument +{ - public PostFixExpression> inc() { - return new PostFixImpl>( PostfixOp.INC, this ); - } + public PostFixExpression> inc() + { + return new PostFixImpl>(PostfixOp.INC, this); + } - public PostFixExpression> dec() { - return new PostFixImpl>( PostfixOp.DEC, this ); - } + public PostFixExpression> dec() + { + return new PostFixImpl>(PostfixOp.DEC, 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 index e427ead9..b2b3a1cd 100644 --- 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 @@ -16,40 +16,47 @@ 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" ); - } + 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 index dc695759..441ae618 100644 --- 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 @@ -13,40 +13,47 @@ 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; - } + 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 index dd232a1b..86ac37f4 100644 --- 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 @@ -15,53 +15,62 @@ import org.jboss.forge.roaster.model.source.JavaSource; public class SuperFieldImpl, - P extends ExpressionHolder> - extends SimpleAccessorImpl,SuperFieldAccess> - implements Field { + P extends ExpressionHolder> + extends SimpleAccessorImpl,SuperFieldAccess> + implements Field +{ - private SuperFieldAccess fld; + private SuperFieldAccess fld; - private String fieldName; - private ExpressionSource,?> expression = new SuperImpl>(); + private String fieldName; + private ExpressionSource,?> expression = new SuperImpl>(); - public SuperFieldImpl( String fieldName ) { - this.fieldName = fieldName; - } + 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 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 SuperFieldAccess getInternal() + { + return fld; + } - @Override - public void setInternal(SuperFieldAccess jdtNode) { - super.setInternal(jdtNode); - this.fld = jdtNode; - } + @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 ExpressionSource,?> chainExpression(ExpressionSource child) + { + ExpressionSource,?> cast = (ExpressionSource,?>) child; + return cast; + } - @Override - public String getFieldName() { - return fieldName; - } + @Override + public String getFieldName() + { + return fieldName; + } - @Override - public ExpressionSource, ?> getSource() { - return expression; - } + @Override + public ExpressionSource,?> getSource() + { + return expression; + } } 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 index 12066cfa..88a96002 100644 --- 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 @@ -12,66 +12,75 @@ 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.expressions.MethodCallExpression; 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 { +public class SuperGetterImpl, + P extends ExpressionHolder> + extends InvokeableImpl,SuperMethodInvocation> + implements Getter +{ - protected SuperMethodInvocation invoke; + protected SuperMethodInvocation invoke; - private String fieldName; + private String fieldName; - public SuperGetterImpl(String fieldName, String type) { - super(JDTHelper.getter(fieldName, type)); - target = new SuperImpl>(); - this.fieldName = 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; - setTarget( cast ); - return cast; - } + public ExpressionSource,?> chainExpression(ExpressionSource child) + { + ExpressionSource,?> cast = (ExpressionSource,?>) child; + setTarget(cast); + return cast; + } - @Override - public Getter setTarget( ExpressionSource target) { - chainExpression( (ExpressionSource) target ); - return this; - } + @Override + public Getter setTarget(ExpressionSource target) + { + chainExpression((ExpressionSource) target); + return this; + } - @Override - public ExpressionSource, ?> getTarget() { - return target; - } + @Override + public ExpressionSource,?> getTarget() + { + 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 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 SuperMethodInvocation getInternal() + { + return invoke; + } - @Override - public void setInternal(SuperMethodInvocation jdtNode) { - super.setInternal(jdtNode); - this.invoke = jdtNode; - } + @Override + public void setInternal(SuperMethodInvocation jdtNode) + { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } - @Override - public String getFieldName() { - return fieldName; - } + @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 index 524af3a3..4e851a3f 100644 --- 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 @@ -18,38 +18,45 @@ 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 ); - } + 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 index dd9775ae..3d34c27b 100644 --- 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 @@ -15,45 +15,53 @@ 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; - } + 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 index 3359f16e..242546a9 100644 --- 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 @@ -10,55 +10,63 @@ 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.Expression; 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 { + P extends ExpressionHolder> + extends AbstractMethodInvokeImpl,SuperMethodInvocation> + implements Setter +{ - private SuperMethodInvocation setter; + private SuperMethodInvocation setter; - private ExpressionSource,?> value; + private ExpressionSource,?> value; - public SuperSetterImpl( String fieldName, String type, ExpressionSource value ) { - super( JDTHelper.setter( fieldName, type ) ); - this.value = (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 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 SuperMethodInvocation getInternal() + { + return setter; + } - @Override - public void setInternal( SuperMethodInvocation jdtNode ) { - super.setInternal( jdtNode ); - this.setter = jdtNode; - } + @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 ); - } + @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 index b7caff17..aa9845c6 100644 --- 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 @@ -14,79 +14,95 @@ 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; - } +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 index 5da30ba9..11cd2373 100644 --- 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 @@ -17,52 +17,61 @@ import org.jboss.forge.roaster.model.source.JavaSource; public class UnaryImpl, - P extends ExpressionHolder> - extends NonPrimitiveExpressionImpl,PrefixExpression> - implements UnaryExpression { + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,PrefixExpression> + implements UnaryExpression +{ - PrefixExpression expr; + PrefixExpression expr; - private Argument,?> arg; - private PrefixOp op; + private Argument,?> arg; + private PrefixOp op; - public UnaryImpl( PrefixOp op, Argument expr ) { - this.arg = (Argument, ?>) expr; - this.op = op; - } + public UnaryImpl(PrefixOp op, Argument expr) + { + this.arg = (Argument,?>) expr; + this.op = op; + } - @Override - public PrefixExpression materialize( AST ast ) { - if (isMaterialized()) { - return expr; - } - expr = ast.newPrefixExpression(); - expr.setOperator( PrefixExpression.Operator.toOperator( op.getOp() ) ); + @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; - } + if (arg != null) + { + expr.setOperand(wireAndGetExpression(arg, this, ast)); + } + return expr; + } - @Override - public PrefixExpression getInternal() { - return expr; - } + @Override + public PrefixExpression getInternal() + { + return expr; + } - @Override - public void setInternal(PrefixExpression jdtNode) { - super.setInternal(jdtNode); - this.expr = jdtNode; - } + @Override + public void setInternal(PrefixExpression jdtNode) + { + super.setInternal(jdtNode); + this.expr = jdtNode; + } - @Override - public ExpressionSource, ?> getExpression() { - return arg; - } + @Override + public ExpressionSource,?> getExpression() + { + return arg; + } - @Override - public String getOperator() { - return op.getOp(); - } + @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 index d900fa1c..305e7699 100644 --- 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 @@ -15,50 +15,58 @@ import org.jboss.forge.roaster.model.source.JavaSource; public class VarArgumentImpl, - P extends ExpressionHolder> - extends SimpleAccessorImpl,Name> - implements Variable { + P extends ExpressionHolder> + extends SimpleAccessorImpl,Name> + implements Variable +{ - private Name var; + private Name var; - private String name; + private String name; - private ExpressionSource,?> expression; + private ExpressionSource,?> expression; - public VarArgumentImpl( String name ) { - this.name = name; - } + 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 materialize(AST ast) + { + if (isMaterialized()) + { + return var; + } + var = ast.newName(name); + return var; + } - @Override - public Name getInternal() { - return var; - } + @Override + public Name getInternal() + { + return var; + } - @Override - public void setInternal(Name jdtNode) { - super.setInternal(jdtNode); - this.var = jdtNode; - } + @Override + public void setInternal(Name jdtNode) + { + super.setInternal(jdtNode); + this.var = jdtNode; + } - @Override - public String getName() { - return name; - } + @Override + public String getName() + { + return name; + } - @Override - public ExpressionSource,?> chainExpression( ExpressionSource child ) { - ExpressionSource,?> cast = ( ExpressionSource,?> ) child; - this.expression = cast; - return cast; - } + @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 index 796c8415..5d9bc16d 100644 --- 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 @@ -14,65 +14,76 @@ import org.jboss.forge.roaster.model.statements.AssertStatement; public class AssertStatementImpl, - P extends BlockHolder> - extends StatementImpl,org.eclipse.jdt.core.dom.AssertStatement> - implements AssertStatement { + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.AssertStatement> + implements AssertStatement +{ - private org.eclipse.jdt.core.dom.AssertStatement assrt; + private org.eclipse.jdt.core.dom.AssertStatement assrt; - private ExpressionSource,?> assertion; - private ExpressionSource,?> message; + 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 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 ExpressionSource,?> getAssertion() + { + return assertion; + } - @Override - public AssertStatement setAssertion( ExpressionSource expression ) { - ExpressionSource,?> cast = (ExpressionSource, ?>) expression; - this.assertion = cast; - cast.setOrigin( this ); - return this; - } + @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 ExpressionSource,?> getMessage() + { + return message; + } - @Override - public AssertStatement setMessage( ExpressionSource msg ) { - ExpressionSource,?> cast = (ExpressionSource, ?>) msg; - this.message = cast; - cast.setOrigin( this ); - return this; - } + @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 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; - } + @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 index 21a1c565..6969d4a6 100644 --- 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 @@ -19,77 +19,91 @@ 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, ?> getExpression() { - return inner; - } +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,?> getExpression() + { + 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 index 231a5539..a3d895a1 100644 --- 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 @@ -7,31 +7,30 @@ 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.BlockSource; import org.jboss.forge.roaster.model.source.JavaSource; -import org.jboss.forge.roaster.model.source.MethodSource; import org.jboss.forge.roaster.model.statements.BlockStatement; -import org.jboss.forge.roaster.model.statements.Statement; import org.jboss.forge.roaster.model.statements.StatementSource; public class BlockStatementImpl, - P extends BlockHolder> - extends InnerBlockImpl> - implements BlockStatement { + P extends BlockHolder> + extends InnerBlockImpl> + implements BlockStatement +{ - public BlockStatementImpl() { - } + public BlockStatementImpl() + { + } - public BlockStatementImpl( StatementSource body ) { - addStatement( body ); - } + public BlockStatementImpl(StatementSource body) + { + addStatement(body); + } - @Override - public BlockStatement setLabel( String label ) { - this.label = label; - return this; - } + @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 index 6da8a155..a5e58cd6 100644 --- 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 @@ -18,41 +18,49 @@ 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; - } - } + 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 index 993e6ed3..304973a9 100644 --- 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 @@ -13,47 +13,57 @@ 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; - } + 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 index 48504d04..0ef869ca 100644 --- 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 @@ -13,47 +13,57 @@ 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; - } + 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 index e3b686d7..4ebaaa86 100644 --- 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 @@ -24,128 +24,153 @@ 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; - } +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 index 7ac6ea3b..ed6c05b2 100644 --- 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 @@ -18,71 +18,85 @@ import org.jboss.forge.roaster.model.statements.StatementSource; public class DoWhileStatementImpl, - P extends BlockHolder> - extends BodiedStatementImpl,DoStatement> - implements DoWhileStatement { + P extends BlockHolder> + extends BodiedStatementImpl,DoStatement> + implements DoWhileStatement +{ - private DoStatement rep; + private DoStatement rep; - private BlockStatement> body; - private ExpressionSource,?> condition; + private BlockStatement> body; + private ExpressionSource,?> condition; - public DoWhileStatementImpl() { } + public DoWhileStatementImpl() + { + } - @Override - public BlockStatement> getBody() { - return body; - } + @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(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 DoWhileStatement setBody(StatementSource statement) + { + this.body = wrap(statement); + return this; + } - @Override - public ExpressionSource, ?> getCondition() { - return condition; - } + @Override + public ExpressionSource,?> getCondition() + { + return condition; + } - @Override - public DoWhileStatement setCondition( ExpressionSource expr ) { - ExpressionSource,?> cast = (ExpressionSource, ?>) expr; - this.condition = cast; - return this; - } + @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(); + @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; - } + 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 DoStatement getInternal() + { + return rep; + } - @Override - public void setInternal(DoStatement jdtNode) { - super.setInternal(jdtNode); - this.rep = jdtNode; - } + @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 index 86f10a0f..dc9bd46f 100644 --- 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 @@ -14,57 +14,67 @@ import org.jboss.forge.roaster.model.statements.EvalExpressionStatement; public class ExpressionStatementImpl, - P extends BlockHolder> - extends StatementImpl,org.eclipse.jdt.core.dom.ExpressionStatement> - implements EvalExpressionStatement { + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.ExpressionStatement> + implements EvalExpressionStatement +{ - private org.eclipse.jdt.core.dom.ExpressionStatement exprStatement; + private org.eclipse.jdt.core.dom.ExpressionStatement exprStatement; - private ExpressionSource,?> expression; + private ExpressionSource,?> expression; - public ExpressionStatementImpl( ExpressionSource expr ) { - setExpr( expr ); - } + 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; - } + public ExpressionStatementImpl() + { + } - @Override - public org.eclipse.jdt.core.dom.ExpressionStatement getInternal() { - return exprStatement; - } + @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 void setInternal(org.eclipse.jdt.core.dom.ExpressionStatement jdtNode) { - super.setInternal( jdtNode ); - this.exprStatement = jdtNode; - } + @Override + public org.eclipse.jdt.core.dom.ExpressionStatement getInternal() + { + return exprStatement; + } - @Override - public ExpressionSource, ?> getExpr() { - return expression; - } + @Override + public void setInternal(org.eclipse.jdt.core.dom.ExpressionStatement jdtNode) + { + super.setInternal(jdtNode); + this.exprStatement = jdtNode; + } - @Override - public EvalExpressionStatement setExpr( ExpressionSource expr ) { - ExpressionSource,?> cast = (ExpressionSource, ?>) expr; - this.expression = cast; - cast.setOrigin( this ); - return this; - } + @Override + public ExpressionSource,?> getExpr() + { + return expression; + } - @Override - public ExpressionSource, ?> getExpression() { - return expression; - } + @Override + public EvalExpressionStatement setExpr(ExpressionSource expr) + { + ExpressionSource,?> cast = (ExpressionSource,?>) expr; + this.expression = cast; + cast.setOrigin(this); + return this; + } + + @Override + public ExpressionSource,?> getExpression() + { + return expression; + } } 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 index 54205c4b..0d8b6146 100644 --- 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 @@ -18,100 +18,118 @@ 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; - } +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 index 44890f1b..29301ee4 100644 --- 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 @@ -8,8 +8,6 @@ package org.jboss.forge.roaster.model.impl.statements; 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.expressions.DeclareExpression; import org.jboss.forge.roaster.model.expressions.ExpressionSource; import org.jboss.forge.roaster.model.source.BlockHolder; @@ -24,111 +22,132 @@ 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; - } + 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 index 387c1002..51894f90 100644 --- 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 @@ -16,110 +16,131 @@ 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; - } +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 index 2dddcac3..050519f7 100644 --- 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 @@ -11,96 +11,114 @@ import org.jboss.forge.roaster.model.statements.Statement; import org.jboss.forge.roaster.model.statements.StatementSource; -import java.awt.*; 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 ) ); - } + 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 index a1dbe60b..72e97656 100644 --- 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 @@ -25,86 +25,105 @@ 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 getMethod() { - return inner.getMethodName(); - } - - public InvokeStatement setMethod( String method ) { - if ( inner instanceof MethodCallExpression ) { - ((MethodCallExpression)inner).setMethodName( method ); - } - return this; - } - - @Override - public Accessor,?>, ?> getTarget() { - return (Accessor,?>, ?>) inner.getTarget(); - } - - @Override - public InvokeStatement setTarget( Accessor target ) { - inner.setTarget( 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, ?> getExpression() { - return inner; - } + 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 getMethod() + { + return inner.getMethodName(); + } + + public InvokeStatement setMethod(String method) + { + if (inner instanceof MethodCallExpression) + { + ((MethodCallExpression) inner).setMethodName(method); + } + return this; + } + + @Override + public Accessor,?>,?> getTarget() + { + return (Accessor,?>,?>) inner.getTarget(); + } + + @Override + public InvokeStatement setTarget(Accessor target) + { + inner.setTarget(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,?> getExpression() + { + 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 index e0a3da3b..b5ef8747 100644 --- 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 @@ -14,12 +14,13 @@ import org.jboss.forge.roaster.model.statements.Statement; public interface JdtStatementWrapper, - P extends BlockHolder, - J extends org.eclipse.jdt.core.dom.Statement> - extends ASTNode { + 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 ); + // 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 index d5f3967b..e911c190 100644 --- 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 @@ -14,48 +14,58 @@ 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; - } + 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 index 64c40df8..4cbb19b6 100644 --- 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 @@ -68,7 +68,6 @@ 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; @@ -86,181 +85,202 @@ 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() ) ); +public class StatementBuilder +{ - break; - case DO_STATEMENT : - DoStatement doWhil = (DoStatement) jdtStatement; - roasterStmt = newDoWhile() - .setCondition( asRoasterExpression( doWhil.getExpression() ) ) - .setBody( asRoasterStatement( doWhil.getBody() ) ); + 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 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; - } + 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 index f7e24f8e..2bf2184b 100644 --- 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 @@ -14,10 +14,10 @@ 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.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; @@ -34,105 +34,129 @@ 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(); - } + 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 index 0420906d..87c429aa 100644 --- 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 @@ -17,32 +17,37 @@ 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; - } + 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 index feb15973..a0733926 100644 --- 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 @@ -19,52 +19,63 @@ import java.util.List; public class SuperStatementImpl, - P extends BlockHolder> - extends StatementImpl,SuperConstructorInvocation> - implements SuperStatement { + P extends BlockHolder> + extends StatementImpl,SuperConstructorInvocation> + implements SuperStatement +{ - private SuperConstructorInvocation invoke; + private SuperConstructorInvocation invoke; - private List,?>> argumentList = Collections.EMPTY_LIST; + private List,?>> argumentList = Collections.EMPTY_LIST; - public SuperStatementImpl() { } + public SuperStatementImpl() + { + } - @Override - public List,?>> getArguments() { - return Collections.unmodifiableList( argumentList ); - } + @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 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(); + @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; - } + for (Argument argument : argumentList) + { + invoke.arguments().add(wireAndGetExpression(argument, this, ast)); + } + return invoke; + } - @Override - public SuperConstructorInvocation getInternal() { - return invoke; - } + @Override + public SuperConstructorInvocation getInternal() + { + return invoke; + } - @Override - public void setInternal( SuperConstructorInvocation jdtNode ) { - super.setInternal( jdtNode ); - this.invoke = jdtNode; - } + @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 index 23fe2f43..20f9815e 100644 --- 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 @@ -17,81 +17,113 @@ 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 ); - } +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 index e1f0b6e6..517bf34b 100644 --- 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 @@ -8,7 +8,6 @@ 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.expressions.ExpressionSource; import org.jboss.forge.roaster.model.source.BlockHolder; import org.jboss.forge.roaster.model.source.BlockSource; @@ -24,123 +23,147 @@ 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)); - } +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 index 55bed127..5f0b1dc1 100644 --- 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 @@ -18,71 +18,85 @@ import org.jboss.forge.roaster.model.statements.SynchStatement; public class SynchStatementImpl, - P extends BlockHolder> - extends BodiedStatementImpl,SynchronizedStatement> - implements SynchStatement { + P extends BlockHolder> + extends BodiedStatementImpl,SynchronizedStatement> + implements SynchStatement +{ - private SynchronizedStatement synch; + private SynchronizedStatement synch; - private ExpressionSource,?> expr; - private BlockStatement> body; + private ExpressionSource,?> expr; + private BlockStatement> body; - public SynchStatementImpl() { } + public SynchStatementImpl() + { + } - @Override - public SynchronizedStatement materialize( AST ast ) { - if (synch != null) { - return synch; - } - synch = ast.newSynchronizedStatement(); + @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; - } + 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 SynchronizedStatement getInternal() + { + return synch; + } - @Override - public void setInternal(SynchronizedStatement jdtNode) { - super.setInternal(jdtNode); - this.synch = jdtNode; - } + @Override + public void setInternal(SynchronizedStatement jdtNode) + { + super.setInternal(jdtNode); + this.synch = jdtNode; + } - @Override - public ExpressionSource, ?> getSynchOn() { - return expr; - } + @Override + public ExpressionSource,?> getSynchOn() + { + return expr; + } - @Override - public SynchStatement setSynchOn( ExpressionSource expr ) { - ExpressionSource,?> cast = (ExpressionSource, ?>) expr; - this.expr = cast; - return this; - } + @Override + public SynchStatement setSynchOn(ExpressionSource expr) + { + ExpressionSource,?> cast = (ExpressionSource,?>) expr; + this.expr = cast; + return this; + } - @Override - public BlockStatement> getBody() { - return body; - } + @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(BlockSource body) + { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } - @Override - public SynchStatement setBody( StatementSource body ) { - this.body = wrap(body); - 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 index 9936de43..a0c90c23 100644 --- 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 @@ -19,52 +19,63 @@ import java.util.List; public class ThisStatementImpl, - P extends BlockHolder> - extends StatementImpl,ConstructorInvocation> - implements ThisStatement { + P extends BlockHolder> + extends StatementImpl,ConstructorInvocation> + implements ThisStatement +{ - private ConstructorInvocation invoke; + private ConstructorInvocation invoke; - private List,?>> argumentList = Collections.EMPTY_LIST; + private List,?>> argumentList = Collections.EMPTY_LIST; - public ThisStatementImpl() { } + public ThisStatementImpl() + { + } - @Override - public List,?>> getArguments() { - return Collections.unmodifiableList( argumentList ); - } + @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 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(); + @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; - } + for (Argument argument : argumentList) + { + invoke.arguments().add(wireAndGetExpression(argument, this, ast)); + } + return invoke; + } - @Override - public ConstructorInvocation getInternal() { - return invoke; - } + @Override + public ConstructorInvocation getInternal() + { + return invoke; + } - @Override - public void setInternal(ConstructorInvocation jdtNode) { - super.setInternal(jdtNode); - this.invoke = jdtNode; - } + @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 index b352b02b..9a27e7ab 100644 --- 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 @@ -14,48 +14,58 @@ 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; - } + 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 index 8e1bd631..49779fa7 100644 --- 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 @@ -27,116 +27,137 @@ 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; - } + 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 index bccc772f..a1cef70d 100644 --- 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 @@ -17,71 +17,85 @@ import org.jboss.forge.roaster.model.statements.WhileStatement; public class WhileStatementImpl, - P extends BlockHolder> - extends BodiedStatementImpl,org.eclipse.jdt.core.dom.WhileStatement> - implements WhileStatement { + P extends BlockHolder> + extends BodiedStatementImpl,org.eclipse.jdt.core.dom.WhileStatement> + implements WhileStatement +{ - private org.eclipse.jdt.core.dom.WhileStatement rep; + private org.eclipse.jdt.core.dom.WhileStatement rep; - private BlockStatement> body; - private ExpressionSource,?> condition; + private BlockStatement> body; + private ExpressionSource,?> condition; - public WhileStatementImpl() { } + public WhileStatementImpl() + { + } - @Override - public ExpressionSource, ?> getCondition() { - return condition; - } + @Override + public ExpressionSource,?> getCondition() + { + return condition; + } - @Override - public WhileStatement setCondition( ExpressionSource expr ) { - ExpressionSource,?> cast = (ExpressionSource, ?>) expr; - this.condition = cast; - return this; - } + @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(); + @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; - } + 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 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 void setInternal(org.eclipse.jdt.core.dom.WhileStatement jdtNode) + { + super.setInternal(jdtNode); + this.rep = jdtNode; + } - @Override - public BlockStatement> getBody() { - return body; - } + @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(BlockSource body) + { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } - @Override - public WhileStatement setBody( StatementSource body ) { - this.body = wrap( body ); - return this; - } + @Override + public WhileStatement setBody(StatementSource body) + { + this.body = wrap(body); + return this; + } } 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 index ae32d9f6..0a6d0e33 100644 --- 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 @@ -11,8 +11,6 @@ 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.newSuper; import static org.jboss.forge.roaster.model.statements.Statements.newThis; @@ -25,21 +23,21 @@ public class ConstructorStatementsTest @Test public void testEmptyConstructor() throws Exception { - String target = "this(x);"; - MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public Foo()" ); + String target = "this(x);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public Foo()"); - method.setBody( newThis().addArgument( var( "x" ) ) ); - assertEquals( target, method.getBody().trim() ); + 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()" ); + String target = "super(x);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public Foo()"); - method.setBody( newSuper().addArgument( var( "x" ) ) ); - assertEquals( target, method.getBody().trim() ); + 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 index 3b2aab35..eb75e87e 100644 --- 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 @@ -12,9 +12,9 @@ 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.construct; 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; @@ -40,152 +40,152 @@ public class ControlFlowStatementsTest public void testSimpleLabeledStatement() throws Exception { String target = "Out: return x;"; - MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); - method.setBody( newReturn().setLabel( "Out" ).setReturn( var( "x" ) ) ); + method.setBody(newReturn().setLabel("Out").setReturn(var("x"))); - assertEquals( target, method.getBody().trim() ); + 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()" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); - method.setBody( newBlock() - .addStatement( newIf().setCondition( literal( true ) ).setThen( newBreak() ) ) ); + method.setBody(newBlock() + .addStatement(newIf().setCondition(literal(true)).setThen(newBreak()))); - assertEquals( target, method.getBody().trim() ); + 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()" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); - method.setBody( newBlock() - .addStatement( newWhile().setCondition( literal( true ) ).setBody( newContinue() ) ) ); + method.setBody(newBlock() + .addStatement(newWhile().setCondition(literal(true)).setBody(newContinue()))); - assertEquals( target, method.getBody().trim() ); + 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()" ); + 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" ) ) ); + method.setBody(newBlock() + .addStatement(newWhile().setCondition(literal(true)).setBody( + newBreak().setBreakToLabel("exit"))) + .addStatement(newReturn().setReturn(var("x")).setLabel("exit"))); - assertEquals( target, method.getBody().trim() ); + 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() ); + 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()" ); + 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" ) ) ) ); + method.setBody(newThrow().setThrowable(construct(UnsupportedOperationException.class).addArgument(literal("sorry")))); - assertEquals( target, method.getBody().trim() ); + 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().setMethod( "foo" ).setTarget( var( "x" ) ) ) - .addCatch( declare( NullPointerException.class, "npe" ), newInvoke().setMethod( "printStackTrace" ).setTarget( var( "npe" ) ) ) - .addCatch( declare( IllegalStateException.class, "ise" ), newInvoke().setMethod( "printStackTrace" ).setTarget( var( "ise" ) ) ) - .setFinally( newReturn() ) - ); - - assertEquals( target, method.getBody().trim() ); + 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().setMethod("foo").setTarget(var("x"))) + .addCatch(declare(NullPointerException.class, "npe"), newInvoke().setMethod("printStackTrace").setTarget(var("npe"))) + .addCatch(declare(IllegalStateException.class, "ise"), newInvoke().setMethod("printStackTrace").setTarget(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()" ); + 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() ) ) ); + method.setBody(newSynchronized().setSynchOn(thisLiteral()).setBody(newBlock().addStatement(var("x").inc()))); - assertEquals( target, method.getBody().trim() ); + 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() ); + 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()" ); + 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" ) ) ); + method.setBody(newAssert() + .setAssertion(operator(Op.GREATER).addArgument(var("x")).addArgument(literal(0))) + .setMessage(literal("x GT 0"))); - assertEquals( target, method.getBody().trim() ); + 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 index 86627ab6..5e52b591 100644 --- 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 @@ -12,11 +12,15 @@ 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; -import static org.jboss.forge.roaster.model.statements.Statements.*; -import static org.jboss.forge.roaster.model.expressions.Expressions.*; - public class DeclareStatementsTest { @@ -25,84 +29,84 @@ public class DeclareStatementsTest 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 )" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo( String x )"); - method.setBody( newDeclare().setVariable( Integer.class, "y", construct( Integer.class ) ) ); + method.setBody(newDeclare().setVariable(Integer.class, "y", construct(Integer.class))); - assertEquals( target, method.getBody().trim() ); + 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 )" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo( String x )"); - method.setBody( newDeclare().setVariable( int.class, "y" ) ); + method.setBody(newDeclare().setVariable(int.class, "y")); - assertEquals( target, method.getBody().trim() ); + 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 )" ); + @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 ) ) ); + method.setBody(newDeclare().setVariable(Integer.class, "y", literal(0))); - assertEquals( target, method.getBody().trim() ); - } + 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 )" ); + @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" ) ); + method.setBody(newDeclare().setVariable(int.class, "x").addVariable("y", literal(0)).addVariable("z", literal(3)).addVariable("w")); - assertEquals( target, method.getBody().trim() ); - } + 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 )" ); + @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" ) ) ); + method.setBody(newAssign().setLeft(field("name")).setRight(var("x"))); - assertEquals( target, method.getBody().trim() ); - } + 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 )" ); + @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" ) ) ) ); + method.setBody(newAssign() + .setLeft(field("name")) + .setRight(operator(Op.PLUS).addArgument(field("name")).addArgument(literal(" Doe")))); - assertEquals( target, method.getBody().trim() ); - } + 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 )" ); + @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>" ) ) ); + method.setBody(newDeclare() + .setVariable("java.util.Map>", + "y", + construct("java.util.HashMap>"))); - assertEquals( target, method.getBody().trim() ); - } + 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 index e96a8fb6..d05e9f8d 100644 --- 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 @@ -13,11 +13,17 @@ 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; -import static org.jboss.forge.roaster.model.statements.Statements.*; -import static org.jboss.forge.roaster.model.expressions.Expressions.*; - public class ForStatementsTest { @@ -26,25 +32,25 @@ 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()" ); + 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() - .setMethod( "println" ) - .setTarget( classStatic( System.class ).field( "out" ) ) - .addArgument( var( "j" ) ) ) ); + 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() + .setMethod("println") + .setTarget(classStatic(System.class).field("out")) + .addArgument(var("j")))); - assertEquals( target, method.getBody().trim() ); + assertEquals(target, method.getBody().trim()); } @Test @@ -52,17 +58,17 @@ 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()" ); + 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() - .setTarget( classStatic( System.class ).field( "out" ) ) - .setMethod( "println" ) - .addArgument( var( "name" ) ) ) ); + method.setBody(newForEach() + .setIterator(String.class, "name") + .setSource(var("p").getter("names", String.class)) + .setBody(newInvoke() + .setTarget(classStatic(System.class).field("out")) + .setMethod("println") + .addArgument(var("name")))); - assertEquals( target, method.getBody().trim() ); + 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 index a6cd9a97..bab75bf7 100644 --- 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 @@ -12,84 +12,91 @@ 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; -import static org.jboss.forge.roaster.model.statements.Statements.*; -import static org.jboss.forge.roaster.model.expressions.Expressions.*; - 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() ); + 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 + @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()" ); + 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 ) ) ) ); + 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() ); + 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()" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); - method.setBody( newIf() - .setCondition( not( var( "x" ) ) ) - .setThen( newReturn().setReturn( literal( false ) ) ) ); + method.setBody(newIf() + .setCondition(not(var("x"))) + .setThen(newReturn().setReturn(literal(false)))); - assertEquals( target, method.getBody().trim() ); + 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()" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); - method.setBody( newIf() - .setCondition( not( not( var( "x" ) ) ) ) - .setThen( newReturn().setReturn( literal( false ) ) ) ); + method.setBody(newIf() + .setCondition(not(not(var("x")))) + .setThen(newReturn().setReturn(literal(false)))); - assertEquals( target, method.getBody().trim() ); + 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()" ); + 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() ); + 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 index 70802987..945fd467 100644 --- 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 @@ -12,140 +12,148 @@ 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; -import static org.jboss.forge.roaster.model.statements.Statements.*; -import static org.jboss.forge.roaster.model.expressions.Expressions.*; - 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()" ); + @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().setMethod( "random" ).setTarget( classStatic( Math.class ) ) ); + method.setBody(newInvoke().setMethod("random").setTarget(classStatic(Math.class))); - assertEquals( target, method.getBody().trim() ); - } + 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()" ); + @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().setTarget( classStatic( Math.class ) ).setMethod( "round" ).addArgument( literal( 2.4f ) ) ); + method.setBody(newInvoke().setTarget(classStatic(Math.class)).setMethod("round").addArgument(literal(2.4f))); - assertEquals( target, method.getBody().trim() ); - } + 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()" ); + @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 ) ).setTarget( classStatic( Math.class ) ).setMethod( "min" ) ); + method.setBody(newInvoke().addArgument(literal(3)).addArgument(literal(4)).setTarget(classStatic(Math.class)).setMethod("min")); - assertEquals( target, method.getBody().trim() ); - } + 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()" ); + @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 ) ).setTarget( classStatic( Math.class ) ).setMethod( "min" ) ); + method.setBody(newInvoke().addArgument(literal(3)).addArgument(literal(4)).setTarget(classStatic(Math.class)).setMethod("min")); - assertEquals( target, method.getBody().trim() ); - } + 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()" ); + @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().setTarget( classStatic( Math.class ) ).setMethod( "min" ) - .addArgument( invoke( "max" ).setTarget( classStatic( Math.class ) ) - .addArgument( invoke( "round" ).setTarget( classStatic( Math.class ) ) - .addArgument( literal( 3 ) ) - .addArgument( literal( 4 ) ) ) - .addArgument( literal( 6 ) ) ) - .addArgument( literal( 4 ) ) ); + method.setBody(newInvoke().setTarget(classStatic(Math.class)).setMethod("min") + .addArgument(invoke("max").setTarget(classStatic(Math.class)) + .addArgument(invoke("round").setTarget(classStatic(Math.class)) + .addArgument(literal(3)) + .addArgument(literal(4))) + .addArgument(literal(6))) + .addArgument(literal(4))); - assertEquals( target, method.getBody().trim() ); - } + 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()" ); + @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().setTarget( classStatic( Math.class ) ).setMethod( "min" ) - .addArgument( operator( Op.PLUS ).addArgument( literal( 2 ) ).addArgument( literal( 3 ) ) ) - .addArgument( operator( Op.TIMES ).addArgument( literal( 4 ) ).addArgument( literal( 5 ) ) ) ); + method.setBody(newInvoke().setTarget(classStatic(Math.class)).setMethod("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() ); - } + 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()" ); + @Test + public void testInvokeOnThis() throws Exception + { + String target = "this.toString();"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void foo()"); - method.setBody( newInvoke().setTarget( thisLiteral() ).setMethod( "toString" ) ); + method.setBody(newInvoke().setTarget(thisLiteral()).setMethod("toString")); - assertEquals( target, method.getBody().trim() ); - } + 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()" ); + @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().setMethod( "toString" ) - .setTarget( - thisLiteral().getter( "name", "String" ).invoke( "foo" ).dot().invoke( "bar" ).dot().field( "baz" ) ) ); + method.setBody(newInvoke().setMethod("toString") + .setTarget( + thisLiteral().getter("name", "String").invoke("foo").dot().invoke("bar").dot().field("baz"))); - assertEquals( target, method.getBody().trim() ); - } + assertEquals(target, method.getBody().trim()); + } - @Test - public void testEval() { - String target = "3 + 4;"; - MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo()" ); + @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 ) ) ) ); + method.setBody(newEval().setExpr(operator(Op.PLUS).addArgument(literal(3)).addArgument(literal(4)))); - assertEquals( target, method.getBody().trim() ); - } + 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()" ); + @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" ) ) ); + method.setBody(newEval().setExpr(construct(Integer.class).addArgument(literal(42)).invoke("toString"))); - assertEquals( target, method.getBody().trim() ); - } + 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()" ); + @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 ) ) ); + method.setBody(newEval().setExpr(construct(Integer.class))); - assertEquals( target, method.getBody().trim() ); - } + 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 index 4cb6b88b..10ccc51c 100644 --- 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 @@ -1,7 +1,6 @@ package org.jboss.forge.test.roaster.model.statements; import org.jboss.forge.roaster.Roaster; -import org.jboss.forge.roaster.model.Block; import org.jboss.forge.roaster.model.JavaClass; import org.jboss.forge.roaster.model.expressions.Variable; import org.jboss.forge.roaster.model.impl.statements.JdtStatementWrapper; @@ -20,49 +19,50 @@ 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.assertThat; import static org.junit.Assert.assertTrue; -public class ModifyMethodBodyTest { +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 )" ); + @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" ) ) ); + method.setBody(newReturn().setReturn(var("x"))); - assertEquals( target, method.getBody().trim() ); + assertEquals(target, method.getBody().trim()); - BlockSource body = method.getBodyAsBlock(); - assertEquals( 1, body.getStatements().size() ); - assertTrue( body.getStatements().get( 0 ) instanceof ReturnStatement ); - } + 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" + - "}"; + @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); + 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().setTarget( classStatic( System.class ).field( "out" ) ) - .setMethod( "println" ) - .addArgument( var( "x" ) ) ); + BlockSource body = method.getBodyAsBlock(); + body.addStatement(0, newInvoke().setTarget(classStatic(System.class).field("out")) + .setMethod("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 ); + 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" ) ); - } + 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 index b38eaa87..172fde79 100644 --- 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 @@ -72,1059 +72,1123 @@ 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.getSource() 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.getTarget() 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 ) ).getExpression() 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.getMethod() ); - 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 )).getExpression() instanceof PostFixExpression ); - assertTrue( swich.getStatements().get( 5 ) instanceof ExpressionStatement ); - assertTrue( ((ExpressionStatement) swich.getStatements().get( 5 )).getExpression() 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.getMethod() ); - assertEquals( 3, inv.getArguments().size() ); - assertTrue( statements.get( 1 ) instanceof ExpressionStatement ); - assertTrue( ( (ExpressionStatement) statements.get( 1 ) ).getExpression() 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.getMethod() ); - assertTrue( inv.getTarget() instanceof MethodCallExpression ); - MethodCallExpression call = (MethodCallExpression) inv.getTarget(); - assertTrue( call.getTarget() 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.getMethod() ); - assertTrue( inv.getArguments().get( 0 ) instanceof NumberLiteral ); - assertTrue( inv.getTarget() instanceof Getter ); - Getter call = (Getter) inv.getTarget(); - assertTrue( call.getTarget() instanceof Variable ); - - ExpressionSource inner = inv.getExpression(); - 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.getSource() 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.getSource(); - } - assertTrue( f.getSource() 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 )).getExpression(); - 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 )).getExpression(); - assertTrue( val instanceof MethodCallExpression ); - assertEquals( 0, ( (MethodCallExpression) val ).getArguments().size() ); - assertTrue( ( (MethodCallExpression) val ).getTarget() 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 ).getTarget() 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.getTarget() instanceof ArrayIndexer ); - ArrayIndexer iy = (ArrayIndexer) ix.getTarget(); - assertTrue( iy.getTarget() 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(); - } +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.getSource() 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.getTarget() 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)).getExpression() 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.getMethod()); + 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)).getExpression() instanceof PostFixExpression); + assertTrue(swich.getStatements().get(5) instanceof ExpressionStatement); + assertTrue(((ExpressionStatement) swich.getStatements().get(5)).getExpression() 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.getMethod()); + assertEquals(3, inv.getArguments().size()); + assertTrue(statements.get(1) instanceof ExpressionStatement); + assertTrue(((ExpressionStatement) statements.get(1)).getExpression() 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.getMethod()); + assertTrue(inv.getTarget() instanceof MethodCallExpression); + MethodCallExpression call = (MethodCallExpression) inv.getTarget(); + assertTrue(call.getTarget() 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.getMethod()); + assertTrue(inv.getArguments().get(0) instanceof NumberLiteral); + assertTrue(inv.getTarget() instanceof Getter); + Getter call = (Getter) inv.getTarget(); + assertTrue(call.getTarget() instanceof Variable); + + ExpressionSource inner = inv.getExpression(); + 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.getSource() 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.getSource(); + } + assertTrue(f.getSource() 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)).getExpression(); + 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)).getExpression(); + assertTrue(val instanceof MethodCallExpression); + assertEquals(0, ((MethodCallExpression) val).getArguments().size()); + assertTrue(((MethodCallExpression) val).getTarget() 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).getTarget() 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.getTarget() instanceof ArrayIndexer); + ArrayIndexer iy = (ArrayIndexer) ix.getTarget(); + assertTrue(iy.getTarget() 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 index e3295c13..7488f765 100644 --- 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 @@ -10,253 +10,263 @@ 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.JavaSource; 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; -import static org.jboss.forge.roaster.model.statements.Statements.*; -import static org.jboss.forge.roaster.model.expressions.Expressions.*; - public class ReturnStatementsTest { @Test public void testReturn() throws Exception { String target = "return;"; - MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); - method.setBody( newReturn() ); + method.setBody(newReturn()); - assertEquals( target, method.getBody().trim() ); + 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 )" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String echo( String x )"); - method.setBody( newReturn().setReturn( var( "x" ) ) ); + method.setBody(newReturn().setReturn(var("x"))); - assertEquals( target, method.getBody().trim() ); + 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 )" ); + 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").setTarget(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") + .setTarget(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")).dot().invoke("getRes"))); - method.setBody( newReturn().setReturn( operator( Op.PLUS ).addArgument( var( "x" ) ).addArgument( literal( 5 ) ) ) ); + 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, literal(2))))); - assertEquals( target, method.getBody().trim() ); + 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" ).setTarget( 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" ) - .setTarget( 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 ) ) ) ); - ; + @Test + public void testReturnUnaryParen() throws Exception + { + String target = "return 3 * -(2 + 1);"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public String foo()"); - 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" ) ).dot().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, 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, - paren( operator( Op.PLUS ) - .addArgument( literal( 2 ) ).addArgument( literal( 1 ) ) ) ) ) ) ); - - assertEquals( target, method.getBody().trim() ); - } + method.setBody(newReturn().setReturn(operator(Op.TIMES) + .addArgument(literal(3)) + .addArgument(operator(PrefixOp.MINUS, + paren(operator(Op.PLUS) + .addArgument(literal(2)).addArgument(literal(1))))))); - @Test - public void testReturnArray() throws Exception - { - String target = "return new int[2][3];"; - MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public int[][] foo()" ); + assertEquals(target, method.getBody().trim()); + } - method.setBody( newReturn().setReturn( newArray( int.class ).addDimension( literal( 2 ) ).addDimension( literal( 3 ) ) ) ); + @Test + public void testReturnArray() throws Exception + { + String target = "return new int[2][3];"; + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public int[][] foo()"); - assertEquals( target, method.getBody().trim() ); - } + method.setBody(newReturn().setReturn(newArray(int.class).addDimension(literal(2)).addDimension(literal(3)))); - @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()" ); + assertEquals(target, method.getBody().trim()); + } - 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 ) ) - ) - ) ) ); + @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()); + } - 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()"); - @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) + )); - method.setBody( newReturn().setReturn( - var( "x" ).itemAt( literal( 4 ) ).itemAt( literal( 2 ) ).getter( "y", String.class ) - ) ); - - assertEquals( target, method.getBody().trim() ); - } + 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()" ); + @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" ) ) ); + method.setBody(newReturn().setReturn(field("foo").getter("bar", String.class.getName()).invoke("getRes"))); - assertEquals( target, method.getBody().trim() ); - } + 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 index e6d749ba..d45ac41d 100644 --- 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 @@ -11,19 +11,7 @@ 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.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.sup; -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.statements.Statements.newReturn; import static org.junit.Assert.assertEquals; @@ -34,44 +22,44 @@ public class SuperTest public void testSuperField() throws Exception { String target = "return super.x;"; - MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); - method.setBody( newReturn().setReturn( sup().field( "x" ) ) ); + method.setBody(newReturn().setReturn(sup().field("x"))); - assertEquals( target, method.getBody().trim() ); + 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()" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); - method.setBody( newReturn().setReturn( sup().getter( "x", String.class ) ) ); + method.setBody(newReturn().setReturn(sup().getter("x", String.class))); - assertEquals( target, method.getBody().trim() ); + 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()" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); - method.setBody( newReturn().setReturn( sup().setter( "x", String.class, var( "y" ) ) ) ); + method.setBody(newReturn().setReturn(sup().setter("x", String.class, var("y")))); - assertEquals( target, method.getBody().trim() ); + 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()" ); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello()"); - method.setBody( newReturn().setReturn( sup().invoke( "foo" ).addArgument( var( "x" ) ) ) ); + method.setBody(newReturn().setReturn(sup().invoke("foo").addArgument(var("x")))); - assertEquals( target, method.getBody().trim() ); + 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 index 3c567a9f..eebbe763 100644 --- 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 @@ -12,11 +12,14 @@ 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; -import static org.jboss.forge.roaster.model.statements.Statements.*; -import static org.jboss.forge.roaster.model.expressions.Expressions.*; - public class WhileStatementsTest { @@ -24,34 +27,34 @@ public class WhileStatementsTest 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()" ); + 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 ) ) ) ) ); + 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() ); + 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()" ); + 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 ) ) ) ); + 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() ); + assertEquals(target, method.getBody().trim()); } @@ -59,17 +62,17 @@ public void testSimpleWhile2() throws Exception 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()" ); + 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 ) ) ) ) ); + 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() ); + assertEquals(target, method.getBody().trim()); }