diff --git a/api/src/main/java/org/jboss/forge/roaster/model/Block.java b/api/src/main/java/org/jboss/forge/roaster/model/Block.java new file mode 100644 index 00000000..ec117e3c --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/Block.java @@ -0,0 +1,18 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model; + +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.source.BlockHolder; + +public interface Block, + P extends BlockHolder> + extends Origin

, + BlockHolder { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/ExpressionHolder.java b/api/src/main/java/org/jboss/forge/roaster/model/ExpressionHolder.java new file mode 100644 index 00000000..a6886272 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/ExpressionHolder.java @@ -0,0 +1,12 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model; + +public interface ExpressionHolder> { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/JavaClass.java b/api/src/main/java/org/jboss/forge/roaster/model/JavaClass.java index cc7df803..897c22c8 100644 --- a/api/src/main/java/org/jboss/forge/roaster/model/JavaClass.java +++ b/api/src/main/java/org/jboss/forge/roaster/model/JavaClass.java @@ -8,6 +8,7 @@ package org.jboss.forge.roaster.model; import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.source.JavaClassSource; /** * Represents a Java {@code class} type. See {@link Roaster} for various options in generating {@link JavaClass} @@ -33,4 +34,5 @@ public interface JavaClass> extends */ public boolean isLocalClass(); + public JavaClassSource asJavaClassSource(); } \ No newline at end of file diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/AccessBuilder.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/AccessBuilder.java new file mode 100644 index 00000000..a746e7a4 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/AccessBuilder.java @@ -0,0 +1,32 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface AccessBuilder, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> { + + public Field field( String field ); + + public Getter getter( String field, String klass ); + + public Getter getter( String field, Class klass ); + + public Setter setter( String fldName, String type, ExpressionSource value ); + + public Setter setter( String fldName, Class type, ExpressionSource value ); + + public MethodCallExpression invoke( String method ); + + public ArrayIndexer itemAt( ExpressionSource index ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Accessor.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Accessor.java new file mode 100644 index 00000000..699099fb --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Accessor.java @@ -0,0 +1,19 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface Accessor, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends Argument, + AccessBuilder { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Argument.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Argument.java new file mode 100644 index 00000000..f6c09871 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Argument.java @@ -0,0 +1,19 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface Argument, + P extends ExpressionHolder, + E extends ExpressionSource> + extends ExpressionSource { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArgumentHolder.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArgumentHolder.java new file mode 100644 index 00000000..6d0731ae --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArgumentHolder.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +public interface ArgumentHolder, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends ExpressionHolder { + + public E addArgument( Argument arg ); + + public List> getArguments(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayConstructorExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayConstructorExpression.java new file mode 100644 index 00000000..41cda4fb --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayConstructorExpression.java @@ -0,0 +1,29 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +public interface ArrayConstructorExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression> { + + public ArrayConstructorExpression addDimension( ExpressionSource dim ); + + public ArrayConstructorExpression init( ArrayInit array ); + + public ArrayInit> getInit(); + + public List,?>> getDimensions(); + + public int getDimension(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayIndexer.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayIndexer.java new file mode 100644 index 00000000..856e0c01 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayIndexer.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ArrayIndexer,P extends ExpressionHolder> + extends OrdinalArgument>, + NonPrimitiveExpression> { + + public ExpressionSource,?> getIndex(); + + public ArrayIndexer setIndex( ExpressionSource index ); + + public ExpressionSource,?> getTarget(); + + public ArrayIndexer setTarget( ExpressionSource target ); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayInit.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayInit.java new file mode 100644 index 00000000..2a708a31 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ArrayInit.java @@ -0,0 +1,30 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +public interface ArrayInit,P extends ExpressionHolder> + extends ExpressionSource>, + NonPrimitiveExpression> { + + public ArrayInit addElement( ArrayInit subRow ); + + public ArrayInit addElement( ExpressionSource subElement ); + + public List,?>> getElements(); + + public int size(); + + public int getDimension(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/AssignExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/AssignExpression.java new file mode 100644 index 00000000..74165b61 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/AssignExpression.java @@ -0,0 +1,29 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface AssignExpression, + P extends ExpressionHolder> + extends NonPrimitiveExpression>, + Argument> { + + public String getOperator(); + + public AssignExpression setOperator( Assignment op ); + + public Accessor,?> getLeft(); + + public AssignExpression setLeft( Accessor left ); + + public ExpressionSource,?> getRight(); + + public AssignExpression setRight( ExpressionSource right ); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Assignment.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Assignment.java new file mode 100644 index 00000000..d5f8de7f --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Assignment.java @@ -0,0 +1,46 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +public enum Assignment { + + ASSIGN( "=" ), + DIVIDE_ASSIGN( "/=" ), + PLUS_ASSIGN( "+=" ), + MINUS_ASSIGN( "-=" ), + TIMES_ASSIGN( "*=" ), + REMAINDER_ASSIGN( "%=" ), + + LEFT_SHIFT_ASSIGN( "<<=" ), + RIGHT_SHIFT_ASSIGN( ">>=" ), + RIGHT_SHIFT_UNSIGNED_ASSIGN( ">>>=" ), + BITWISE_XOR_ASSIGN( "^=" ), + BITWISE_AND_ASSIGN( "&=" ), + BITWISE_OR_ASSIGN( "|=" ); + + private String op; + + Assignment( String o ) { + op = o; + } + + public String getOp() { + return op; + } + + + public static Assignment build( String x ) { + for ( Assignment en : Assignment.values() ) { + if ( en.getOp().equals( x ) ) { + return en; + } + } + throw new IllegalStateException( "Unrecognized operation " + x ); + } + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/BasicExpressionFactory.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/BasicExpressionFactory.java new file mode 100644 index 00000000..53257254 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/BasicExpressionFactory.java @@ -0,0 +1,56 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface BasicExpressionFactory, + P extends ExpressionHolder> + extends LiteralBuilder, + ConstructorBuilder { + + public Variable var( String varName ); + + public OperatorExpression operator( Op op ); + + public UnaryExpression operator( PrefixOp operator, Argument arg ); + + public TernaryExpression ternary(); + + public CastExpression cast( String klass, ExpressionSource arg ); + + public CastExpression cast( Class klass, ExpressionSource arg ); + + public ParenExpression paren( ExpressionSource inner ); + + public UnaryExpression not( Argument arg ); + + public AssignExpression assign( Assignment operator ); + + public DeclareExpression declare( String klass, String name ); + + public DeclareExpression declare( Class klass, String name ); + + public DeclareExpression declare( String klass, String name, ExpressionSource init ); + + public DeclareExpression declare( Class klass, String name, ExpressionSource init ); + + public Accessor classStatic( String klass ); + + public Accessor classStatic( Class klass ); + + public InstanceofExpression instanceOf( String klass, ExpressionSource arg ); + + public InstanceofExpression instanceOf( Class klass, ExpressionSource arg ); + + public Super sup(); + + public ArrayInit> vec(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/BooleanLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/BooleanLiteral.java new file mode 100644 index 00000000..85637323 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/BooleanLiteral.java @@ -0,0 +1,19 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface BooleanLiteral, + P extends ExpressionHolder> + extends PrimitiveLiteral { + + public Boolean getValue(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/CastExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/CastExpression.java new file mode 100644 index 00000000..1e496996 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/CastExpression.java @@ -0,0 +1,21 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface CastExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression> { + + public String getType(); + + public ExpressionSource,?> getExpression(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ChainableExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ChainableExpression.java new file mode 100644 index 00000000..f4c33c2c --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ChainableExpression.java @@ -0,0 +1,21 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + + +public interface ChainableExpression, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends NonPrimitiveExpression { + + public Accessor dot(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/CharacterLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/CharacterLiteral.java new file mode 100644 index 00000000..34821b7f --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/CharacterLiteral.java @@ -0,0 +1,19 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface CharacterLiteral, + P extends ExpressionHolder> + extends PrimitiveLiteral { + + public Character getValue(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ClassLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ClassLiteral.java new file mode 100644 index 00000000..d78bab86 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ClassLiteral.java @@ -0,0 +1,19 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ClassLiteral, + P extends ExpressionHolder> + extends Literal>, + NonPrimitiveExpression>, + Accessor> { +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorBuilder.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorBuilder.java new file mode 100644 index 00000000..59b56095 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorBuilder.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ConstructorBuilder, + P extends ExpressionHolder> { + + public ConstructorExpression construct( String klass ); + + public ConstructorExpression construct( Class klass ); + + public ArrayConstructorExpression newArray( Class klass ); + + public ArrayConstructorExpression newArray( String klass ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorExpression.java new file mode 100644 index 00000000..f7d12057 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ConstructorExpression.java @@ -0,0 +1,20 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ConstructorExpression, + P extends ExpressionHolder> + extends Argument>, + Accessor>, + ArgumentHolder>, + NonPrimitiveExpression> { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/DeclareExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/DeclareExpression.java new file mode 100644 index 00000000..81464469 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/DeclareExpression.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; +import java.util.Map; + +public interface DeclareExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression> { + + public DeclareExpression addDeclaration( String name, ExpressionSource expr ); + + public DeclareExpression addDeclaration( String name ); + + + public String getVariableName(); + + public List getVariableNames(); + + public String getVariableType(); + + public ExpressionSource,?> getInitExpression(); + + public Map,?>> getInitExpressions(); + + public boolean isSingleDeclaration(); + + public int getNumVariables(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Expression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Expression.java new file mode 100644 index 00000000..be32c92a --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Expression.java @@ -0,0 +1,19 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.JavaType; + +public interface Expression, + P extends ExpressionHolder> + extends Origin

{ + + public void setOrigin( P origin ); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ExpressionChainLink.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ExpressionChainLink.java new file mode 100644 index 00000000..d8da1e0b --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ExpressionChainLink.java @@ -0,0 +1,22 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ExpressionChainLink, + P extends ExpressionHolder> { + + ExpressionSource chainExpression( ExpressionSource child ); + + ExpressionHolder getParent(); + + void linkParent( ExpressionHolder parent ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ExpressionSource.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ExpressionSource.java new file mode 100644 index 00000000..d0a1c1de --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ExpressionSource.java @@ -0,0 +1,18 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ExpressionSource, + P extends ExpressionHolder, + E extends ExpressionSource> + extends Expression { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Expressions.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Expressions.java new file mode 100644 index 00000000..adc662cf --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Expressions.java @@ -0,0 +1,227 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.spi.ExpressionFactory; + +import java.util.ServiceLoader; + +public abstract class Expressions { + + protected static ExpressionFactory factory; + + @SuppressWarnings( "unchecked" ) + protected static > ExpressionFactory getFactory() { + synchronized ( Expressions.class ) { + ServiceLoader sl = ServiceLoader.load( ExpressionFactory.class, Expressions.class.getClassLoader() ); + if ( sl.iterator().hasNext() ) { + factory = sl.iterator().next(); + } else { + throw new IllegalStateException( "No ExpressionFactory implementation available, unable to continue" ); + } + } + return factory; + } + + @SuppressWarnings( "unchecked" ) + public static > StringLiteral literal( String val ) { + return (StringLiteral) getFactory().literal( val ); + } + + @SuppressWarnings( "unchecked" ) + public static > NumberLiteral literal( Number val ) { + return (NumberLiteral) getFactory().literal( val ); + } + + @SuppressWarnings( "unchecked" ) + public static > CharacterLiteral literal( Character val ) { + return (CharacterLiteral) getFactory().literal( val ); + } + + @SuppressWarnings( "unchecked" ) + public static > BooleanLiteral literal( Boolean val ) { + return (BooleanLiteral) getFactory().literal( val ); + } + + @SuppressWarnings( "unchecked" ) + public static > Literal zeroLiteral( String klass ) { + return (Literal) getFactory().zeroLiteral( klass ); + } + + @SuppressWarnings( "unchecked" ) + public static > Literal zeroLiteral( Class klass ) { + return (Literal) getFactory().zeroLiteral( klass ); + } + + @SuppressWarnings( "unchecked" ) + public static > ThisLiteral thisLiteral() { + return (ThisLiteral) getFactory().thisLiteral(); + } + + @SuppressWarnings( "unchecked" ) + public static > NullLiteral nullLiteral() { + return (NullLiteral) getFactory().nullLiteral(); + } + + @SuppressWarnings( "unchecked" ) + public static > ClassLiteral classLiteral( String klass ) { + return (ClassLiteral) getFactory().classLiteral( klass ); + } + + @SuppressWarnings( "unchecked" ) + public static > ClassLiteral classLiteral( Class klass ) { + return (ClassLiteral) getFactory().classLiteral( klass ); + } + + @SuppressWarnings( "unchecked" ) + public static > Accessor classStatic( String klass ) { + return (Accessor) getFactory().classStatic( klass ); + } + + @SuppressWarnings( "unchecked" ) + public static > Accessor classStatic( Class klass ) { + return (Accessor) getFactory().classStatic( klass ); + } + + @SuppressWarnings( "unchecked" ) + public static > ConstructorExpression construct( String klass ) { + return (ConstructorExpression) getFactory().construct( klass ); + } + + @SuppressWarnings( "unchecked" ) + public static > ConstructorExpression construct( Class klass ) { + return (ConstructorExpression) getFactory().construct( klass ); + } + + @SuppressWarnings( "unchecked" ) + public static > ArrayConstructorExpression newArray( String klass ) { + return (ArrayConstructorExpression) getFactory().newArray( klass ); + } + + @SuppressWarnings( "unchecked" ) + public static > ArrayConstructorExpression newArray( Class klass ) { + return (ArrayConstructorExpression) getFactory().newArray( klass ); + } + + @SuppressWarnings( "unchecked" ) + public static > ArrayInit vec() { + return (ArrayInit) getFactory().vec(); + } + + @SuppressWarnings( "unchecked" ) + public static > Variable var( String variable ) { + return (Variable) getFactory().var( variable ); + } + + @SuppressWarnings( "unchecked" ) + public static > OperatorExpression operator( Op operator ) { + return (OperatorExpression) getFactory().operator( operator ); + } + + @SuppressWarnings( "unchecked" ) + public static > UnaryExpression operator( PrefixOp operator, Argument arg ) { + return (UnaryExpression) getFactory().operator( operator, arg ); + } + + @SuppressWarnings( "unchecked" ) + public static > TernaryExpression ternary() { + return (TernaryExpression) getFactory().ternary(); + } + + @SuppressWarnings( "unchecked" ) + public static > Argument cast( String klass, ExpressionSource expression ) { + return (Argument) getFactory().cast( klass, expression ); + } + + @SuppressWarnings( "unchecked" ) + public static > Argument cast( Class klass, ExpressionSource expression ) { + return (Argument) getFactory().cast( klass, expression ); + } + + @SuppressWarnings( "unchecked" ) + public static > ParenExpression paren( ExpressionSource inner ) { + return (ParenExpression) getFactory().paren( inner ); + } + + @SuppressWarnings( "unchecked" ) + public static > UnaryExpression not( Argument inner ) { + return (UnaryExpression) getFactory().not( inner ); + } + + @SuppressWarnings( "unchecked" ) + public static > AssignExpression assign( Assignment operator ) { + return (AssignExpression) getFactory().assign( operator ); + } + + @SuppressWarnings( "unchecked" ) + public static > DeclareExpression declare( String klass, String name ) { + return (DeclareExpression) getFactory().declare( klass, name ); + } + + @SuppressWarnings( "unchecked" ) + public static > DeclareExpression declare( Class klass, String name ) { + return (DeclareExpression) getFactory().declare( klass, name ); + } + + @SuppressWarnings( "unchecked" ) + public static > DeclareExpression declare( String klass, String name, ExpressionSource init ) { + return (DeclareExpression) getFactory().declare( klass, name, init ); + } + + @SuppressWarnings( "unchecked" ) + public static > DeclareExpression declare( Class klass, String name, ExpressionSource init ) { + return (DeclareExpression) getFactory().declare( klass, name, init ); + } + + @SuppressWarnings( "unchecked" ) + public static > MethodCallExpression invoke( String method ) { + return (MethodCallExpression) getFactory().invoke( method ); + } + + @SuppressWarnings( "unchecked" ) + public static > Field field( String field ) { + return (Field) getFactory().field( field ); + } + + @SuppressWarnings( "unchecked" ) + public static > Getter getter( String field, String klass ) { + return (Getter) getFactory().getter( field, klass ); + } + + @SuppressWarnings( "unchecked" ) + public static > Getter getter( String field, Class klass ) { + return (Getter) getFactory().getter( field, klass ); + } + + @SuppressWarnings( "unchecked" ) + public static > Setter setter( String field, String klass, ExpressionSource value ) { + return (Setter) getFactory().setter( field, klass, value ); + } + + @SuppressWarnings( "unchecked" ) + public static > Setter setter( String field, Class klass, ExpressionSource value ) { + return (Setter) getFactory().setter( field, klass, value ); + } + + @SuppressWarnings( "unchecked" ) + public static > Super sup() { + return (Super) getFactory().sup(); + } + + @SuppressWarnings( "unchecked" ) + public static > InstanceofExpression instanceOf( String klass, ExpressionSource expression ) { + return (InstanceofExpression) getFactory().instanceOf( klass, expression ); + } + + @SuppressWarnings( "unchecked" ) + public static > InstanceofExpression instanceOf( Class klass, ExpressionSource expression ) { + return (InstanceofExpression) getFactory().instanceOf( klass, expression ); + } + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Field.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Field.java new file mode 100644 index 00000000..dd16a495 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Field.java @@ -0,0 +1,22 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface Field, + P extends ExpressionHolder> + extends OrdinalArgument>, + NonPrimitiveExpression> { + + public String getFieldName(); + + public ExpressionSource,?> getSource(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Getter.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Getter.java new file mode 100644 index 00000000..8cda6708 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Getter.java @@ -0,0 +1,21 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface Getter, + P extends ExpressionHolder> + extends Accessor>, + MethodInvokeExpression>, + ExpressionChainLink> { + + public String getFieldName(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/InstanceofExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/InstanceofExpression.java new file mode 100644 index 00000000..00dcc4fe --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/InstanceofExpression.java @@ -0,0 +1,21 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface InstanceofExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression> { + + public String getTypeName(); + + public ExpressionSource,?> getExpression(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Literal.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Literal.java new file mode 100644 index 00000000..9c6ff565 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Literal.java @@ -0,0 +1,17 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface Literal, + P extends ExpressionHolder, + E extends Literal> + extends Argument { +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/LiteralBuilder.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/LiteralBuilder.java new file mode 100644 index 00000000..0d4afc69 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/LiteralBuilder.java @@ -0,0 +1,37 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface LiteralBuilder, + P extends ExpressionHolder> { + + public StringLiteral literal( String val ); + + public PrimitiveLiteral literal( Number val ); + + public PrimitiveLiteral literal( Character val ); + + public PrimitiveLiteral literal( Boolean val ); + + public Literal zeroLiteral( String klass ); + + public Literal zeroLiteral( Class klass ); + + public ThisLiteral thisLiteral(); + + public NullLiteral nullLiteral(); + + public ClassLiteral classLiteral( String klass ); + + public ClassLiteral classLiteral( Class klass ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodCallExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodCallExpression.java new file mode 100644 index 00000000..cec2bc5b --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodCallExpression.java @@ -0,0 +1,20 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface MethodCallExpression, + P extends ExpressionHolder> + extends MethodWithArgumentsInvokeExpression> { + + public MethodCallExpression setMethodName( String name ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodInvokeExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodInvokeExpression.java new file mode 100644 index 00000000..460e6791 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodInvokeExpression.java @@ -0,0 +1,26 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface MethodInvokeExpression, + P extends ExpressionHolder, + M extends MethodInvokeExpression> + extends Argument, + ChainableExpression, + NonPrimitiveExpression { + + public String getMethodName(); + + public M setTarget( ExpressionSource target ); + + public ExpressionSource getTarget(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodWithArgumentsInvokeExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodWithArgumentsInvokeExpression.java new file mode 100644 index 00000000..5f126cec --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/MethodWithArgumentsInvokeExpression.java @@ -0,0 +1,19 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface MethodWithArgumentsInvokeExpression, + P extends ExpressionHolder, + M extends MethodWithArgumentsInvokeExpression> + extends MethodInvokeExpression, + ArgumentHolder { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/NonPrimitiveExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/NonPrimitiveExpression.java new file mode 100644 index 00000000..676cfa79 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/NonPrimitiveExpression.java @@ -0,0 +1,19 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.JavaType; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface NonPrimitiveExpression, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends ExpressionSource, + ExpressionHolder { +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/NullLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/NullLiteral.java new file mode 100644 index 00000000..048d7066 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/NullLiteral.java @@ -0,0 +1,17 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface NullLiteral, + P extends ExpressionHolder> + extends Literal> { +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/NumberLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/NumberLiteral.java new file mode 100644 index 00000000..2cdad7ff --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/NumberLiteral.java @@ -0,0 +1,19 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface NumberLiteral, + P extends ExpressionHolder> + extends PrimitiveLiteral { + + public Number getValue(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Op.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Op.java new file mode 100644 index 00000000..1fa607b4 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Op.java @@ -0,0 +1,51 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +public enum Op { + + TIMES( "*" ), + DIVIDE( "/" ), + REMAINDER( "%" ), + PLUS( "+" ), + MINUS( "-" ), + LEFT_SHIFT( "<<" ), + RIGHT_SHIFT_SIGNED( ">>" ), + RIGHT_SHIFT_UNSIGNED( ">>>" ), + LESS( "<" ), + GREATER( ">" ), + LESS_EQUALS( "<=" ), + GREATER_EQUALS( ">=" ), + EQUALS( "==" ), + NOT_EQUALS( "!=" ), + BITWISE_XOR( "^" ), + BITWISE_AND( "&" ), + BITWISE_OR( "|" ), + OR( "||" ), + AND( "&&" ); + + private String op; + + Op( String o ) { + op = o; + } + + public String getOp() { + return op; + } + + public static Op build( String x ) { + for ( Op en : Op.values() ) { + if ( en.getOp().equals( x ) ) { + return en; + } + } + throw new IllegalStateException( "Unrecognized operation " + x ); + } +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/OperatorExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/OperatorExpression.java new file mode 100644 index 00000000..c91e9303 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/OperatorExpression.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface OperatorExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression>, + ArgumentHolder> { + + public OperatorExpression addArgument( Argument arg ); + + public String getOperator(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/OrdinalArgument.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/OrdinalArgument.java new file mode 100644 index 00000000..2d83ba3e --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/OrdinalArgument.java @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface OrdinalArgument, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends Argument, + Accessor, + NonPrimitiveExpression, + ExpressionChainLink { + + public PostFixExpression> inc(); + + public PostFixExpression> dec(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ParenExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ParenExpression.java new file mode 100644 index 00000000..20e3324c --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ParenExpression.java @@ -0,0 +1,21 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.spi.ExpressionFactory; + +public interface ParenExpression, + P extends ExpressionHolder> + extends ExpressionFactory>, + NonPrimitiveExpression>, + Argument> { + + public ExpressionSource,?> getExpression(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/PostFixExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PostFixExpression.java new file mode 100644 index 00000000..a4acebff --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PostFixExpression.java @@ -0,0 +1,18 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + + +public interface PostFixExpression, + P extends ExpressionHolder> + extends UnaryExpression { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/PostfixOp.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PostfixOp.java new file mode 100644 index 00000000..305367fb --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PostfixOp.java @@ -0,0 +1,34 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +public enum PostfixOp { + + INC( "++" ), + DEC( "--" ); + + private String op; + + PostfixOp( String o ) { + op = o; + } + + public String getOp() { + return op; + } + + public static PostfixOp build( String x ) { + for ( PostfixOp en : PostfixOp.values() ) { + if ( en.getOp().equals( x ) ) { + return en; + } + } + throw new IllegalStateException( "Unrecognized operation " + x ); + } +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/PrefixOp.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PrefixOp.java new file mode 100644 index 00000000..78f8d68c --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PrefixOp.java @@ -0,0 +1,38 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +public enum PrefixOp { + + INC( "++" ), + DEC( "--" ), + PLUS( "+" ), + MINUS( "-" ), + NOT( "!" ), + NEG( "~" ); + + private String op; + + PrefixOp( String o ) { + op = o; + } + + public String getOp() { + return op; + } + + public static PrefixOp build( String x ) { + for ( PrefixOp en : PrefixOp.values() ) { + if ( en.getOp().equals( x ) ) { + return en; + } + } + throw new IllegalStateException( "Unrecognized operation " + x ); + } +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/PrimitiveLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PrimitiveLiteral.java new file mode 100644 index 00000000..b3160665 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/PrimitiveLiteral.java @@ -0,0 +1,17 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface PrimitiveLiteral, + P extends ExpressionHolder> + extends Literal> { +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Setter.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Setter.java new file mode 100644 index 00000000..39f8a007 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Setter.java @@ -0,0 +1,20 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface Setter, + P extends ExpressionHolder> + extends ExpressionSource>, + MethodWithArgumentsInvokeExpression> { + + public ExpressionSource,?> getValue(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/StringLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/StringLiteral.java new file mode 100644 index 00000000..abb1cd6b --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/StringLiteral.java @@ -0,0 +1,22 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface StringLiteral, + P extends ExpressionHolder> + extends Literal>, + NonPrimitiveExpression>, + Accessor> { + + public String getValue(); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Super.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Super.java new file mode 100644 index 00000000..4292f420 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Super.java @@ -0,0 +1,19 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface Super, + P extends ExpressionHolder> + extends AccessBuilder>, + NonPrimitiveExpression> { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/TernaryExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/TernaryExpression.java new file mode 100644 index 00000000..7399576d --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/TernaryExpression.java @@ -0,0 +1,34 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface TernaryExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression> { + + public ExpressionSource,?> getCondition(); + + public TernaryExpression setCondition( ExpressionSource expression ); + + + public ExpressionSource,?> getThenExpression(); + + public TernaryExpression setThenExpression( ExpressionSource onTrue ); + + + public ExpressionSource,?> getElseExpression(); + + public TernaryExpression setElseExpression( ExpressionSource onFalse ); + + + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/ThisLiteral.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ThisLiteral.java new file mode 100644 index 00000000..9eb6cb8a --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/ThisLiteral.java @@ -0,0 +1,20 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ThisLiteral, + P extends ExpressionHolder> + extends Literal>, + NonPrimitiveExpression>, + Accessor>, + ExpressionChainLink { +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/UnaryExpression.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/UnaryExpression.java new file mode 100644 index 00000000..ff68ff46 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/UnaryExpression.java @@ -0,0 +1,21 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface UnaryExpression, + P extends ExpressionHolder> + extends Argument>, + NonPrimitiveExpression> { + + public ExpressionSource,?> getExpression(); + + public String getOperator(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/expressions/Variable.java b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Variable.java new file mode 100644 index 00000000..00f8b61d --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/expressions/Variable.java @@ -0,0 +1,18 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface Variable, + P extends ExpressionHolder> + extends OrdinalArgument> { + + String getName(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/source/BlockHolder.java b/api/src/main/java/org/jboss/forge/roaster/model/source/BlockHolder.java new file mode 100644 index 00000000..d00d71f8 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/source/BlockHolder.java @@ -0,0 +1,16 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.source; + +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.JavaType; + +public interface BlockHolder> { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/source/BlockSource.java b/api/src/main/java/org/jboss/forge/roaster/model/source/BlockSource.java new file mode 100644 index 00000000..e3b1812f --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/source/BlockSource.java @@ -0,0 +1,34 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.source; + +import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.statements.StatementSource; + +import java.util.List; + +public interface BlockSource, + P extends BlockHolder, + S extends BlockSource> + extends Block { + + List> getStatements(); + + BlockSource addStatement( StatementSource statement ); + + BlockSource addStatement( int index, StatementSource statement ); + + BlockSource addStatement( ExpressionSource statement ); + + BlockSource addStatement( int index, ExpressionSource statement ); + + BlockSource removeStatement( StatementSource statement ); + + BlockSource removeStatement( int index ); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/source/MethodBody.java b/api/src/main/java/org/jboss/forge/roaster/model/source/MethodBody.java new file mode 100644 index 00000000..bf8601c5 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/source/MethodBody.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.source; + +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.Method; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.source.MethodSource; + +/** + * The body of a method + */ +public interface MethodBody, T extends MethodSource> + extends BlockHolder, + Origin { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java b/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java index 73d0ffb5..a1af604c 100644 --- a/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java +++ b/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java @@ -7,11 +7,16 @@ package org.jboss.forge.roaster.model.source; -import java.util.List; - import org.jboss.forge.roaster.model.JavaClass; import org.jboss.forge.roaster.model.JavaType; import org.jboss.forge.roaster.model.Method; +import org.jboss.forge.roaster.model.expressions.Expression; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.ReturnStatement; +import org.jboss.forge.roaster.model.statements.Statement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +import java.util.List; /** * Represents a Java Method in source form. @@ -21,7 +26,9 @@ */ public interface MethodSource> extends Method>, AbstractableSource>, - MemberSource>, GenericCapableSource> + MemberSource>, + GenericCapableSource>, + BlockHolder { /** * Sets the synchronized keyword in this element. @@ -135,4 +142,23 @@ public interface MethodSource> extends Method removeParameter(JavaType type, String name); + /** + * @param body + * @return + */ + MethodSource setBody( StatementSource body ); + + /** + * @param body + * @return + */ + MethodSource setBody( BlockSource body ); + + /** + * Returns the body of this method as a Roaster Block + * @return + */ + BlockSource,?> getBodyAsBlock(); + + } \ No newline at end of file diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/AssertStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/AssertStatement.java new file mode 100644 index 00000000..a731b434 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/AssertStatement.java @@ -0,0 +1,27 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface AssertStatement, + P extends BlockHolder> + extends StatementSource> { + + public ExpressionSource,?> getAssertion(); + + public AssertStatement setAssertion( ExpressionSource expression ); + + + public ExpressionSource,?> getMessage(); + + public AssertStatement setMessage( ExpressionSource msg ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/AssignmentStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/AssignmentStatement.java new file mode 100644 index 00000000..e832f2f2 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/AssignmentStatement.java @@ -0,0 +1,34 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.AssignExpression; +import org.jboss.forge.roaster.model.expressions.Assignment; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface AssignmentStatement, + P extends BlockHolder> + extends ExpressionStatement> { + + public Accessor>, ?> getLeft(); + + public AssignmentStatement setLeft( Accessor left ); + + public String getAssignmentOperator(); + + public AssignmentStatement setAssignmentOperator( Assignment op ); + + public ExpressionSource>, ?> getRight(); + + public AssignmentStatement setRight( ExpressionSource right ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/BlockStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/BlockStatement.java new file mode 100644 index 00000000..f6732d65 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/BlockStatement.java @@ -0,0 +1,18 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface BlockStatement, + P extends BlockHolder> + extends BlockSource>, + StatementSource> { +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/BodyHolder.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/BodyHolder.java new file mode 100644 index 00000000..3d45286b --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/BodyHolder.java @@ -0,0 +1,19 @@ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface BodyHolder, + P extends BlockHolder, + B extends BodyHolder> + extends BlockHolder { + + public BlockStatement getBody(); + + public B setBody( BlockSource body ); + + public B setBody( StatementSource body ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/BranchingStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/BranchingStatement.java new file mode 100644 index 00000000..16f2a24d --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/BranchingStatement.java @@ -0,0 +1,17 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface BranchingStatement, + P extends BlockHolder, + S extends BranchingStatement> + extends ControlFlowStatement { +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/BreakStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/BreakStatement.java new file mode 100644 index 00000000..3aa4e910 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/BreakStatement.java @@ -0,0 +1,22 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface BreakStatement, + P extends BlockHolder> + extends BranchingStatement> { + + public String getBreakToLabel(); + + public BreakStatement setBreakToLabel( String label ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ContinueStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ContinueStatement.java new file mode 100644 index 00000000..17b2ee23 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ContinueStatement.java @@ -0,0 +1,22 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ContinueStatement, + P extends BlockHolder> + extends BranchingStatement> { + + public String getContinueToLabel(); + + public ContinueStatement setContinueToLabel( String label ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ControlFlowStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ControlFlowStatement.java new file mode 100644 index 00000000..fca667aa --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ControlFlowStatement.java @@ -0,0 +1,19 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ControlFlowStatement, + P extends BlockHolder, + S extends ControlFlowStatement> + extends StatementSource, + ExpressionHolder { +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/DeclareStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/DeclareStatement.java new file mode 100644 index 00000000..08dd7833 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/DeclareStatement.java @@ -0,0 +1,49 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; +import java.util.Map; + +public interface DeclareStatement, + P extends BlockHolder> + extends StatementSource> { + + + public List getVariableNames(); + + public String getVariableName(); + + public String getVariableType(); + + public Map,?>> getInitExpressions(); + + public ExpressionSource,?> getInitExpression(); + + public boolean isSingleDeclaration(); + + + public DeclareStatement setVariable( Class klass, String name ); + + public DeclareStatement setVariable( String klass, String name ); + + public DeclareStatement setVariable( Class klass, String name, ExpressionSource init ); + + public DeclareStatement setVariable( String klass, String name, ExpressionSource init ); + + public DeclareStatement addVariable( String name ); + + public DeclareStatement addVariable( String name, ExpressionSource init ); + + +} \ No newline at end of file diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/DoWhileStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/DoWhileStatement.java new file mode 100644 index 00000000..477805a2 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/DoWhileStatement.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface DoWhileStatement, + P extends BlockHolder> + extends LoopingStatement>, + BodyHolder> { + + public ExpressionSource,?> getCondition(); + + public DoWhileStatement setCondition( ExpressionSource expr ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/EvalExpressionStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/EvalExpressionStatement.java new file mode 100644 index 00000000..926423ea --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/EvalExpressionStatement.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface EvalExpressionStatement, + P extends BlockHolder> + extends ExpressionStatement> { + + public ExpressionSource,?> getExpr(); + + public EvalExpressionStatement setExpr( ExpressionSource expr ); + +} + + diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ExpressionStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ExpressionStatement.java new file mode 100644 index 00000000..29916465 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ExpressionStatement.java @@ -0,0 +1,22 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ExpressionStatement, + P extends BlockHolder, + S extends ExpressionStatement> + extends StatementSource, + ExpressionHolder { + + public ExpressionSource getExpression(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ForEachStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ForEachStatement.java new file mode 100644 index 00000000..5afbfc83 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ForEachStatement.java @@ -0,0 +1,32 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ForEachStatement, + P extends BlockHolder> + extends LoopingStatement>, + BodyHolder> { + + public String getIteratorName(); + + public String getIteratorType(); + + public ForEachStatement setIterator( String klass, String name ); + + public ForEachStatement setIterator( Class klass, String name ); + + + public ExpressionSource,?> getSource(); + + public ForEachStatement setSource( ExpressionSource expr ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ForStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ForStatement.java new file mode 100644 index 00000000..4c1e95a4 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ForStatement.java @@ -0,0 +1,36 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +public interface ForStatement, + P extends BlockHolder> + extends LoopingStatement>, + BodyHolder> { + + public DeclareExpression> getDeclaration(); + + public ForStatement setDeclaration( DeclareExpression declaration ); + + + public ExpressionSource,?> getCondition(); + + public ForStatement setCondition( ExpressionSource booleanExpression ); + + + public List,?>> getUpdates(); + + public ForStatement addUpdate( ExpressionSource expression ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/IfStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/IfStatement.java new file mode 100644 index 00000000..b29496f9 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/IfStatement.java @@ -0,0 +1,38 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface IfStatement, + P extends BlockHolder> + extends ControlFlowStatement>, + BodyHolder> { + + public ExpressionSource,?> getCondition(); + + public IfStatement setCondition( ExpressionSource condition ); + + + public BlockStatement> getThen(); + + public IfStatement setThen( StatementSource ifBlock ); + + public IfStatement setThen( BlockSource ifBlock ); + + + public BlockStatement> getElse(); + + public IfStatement setElse( StatementSource elseBlock ); + + public IfStatement setElse( BlockSource elseBlock ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/InvokeStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/InvokeStatement.java new file mode 100644 index 00000000..7f549953 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/InvokeStatement.java @@ -0,0 +1,37 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.expressions.MethodWithArgumentsInvokeExpression; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +public interface InvokeStatement, + P extends BlockHolder> + extends ExpressionStatement> { + + public String getMethod(); + + public InvokeStatement setMethod( String method ); + + + public Accessor,?>,?> getTarget(); + + public InvokeStatement setTarget( Accessor target ); + + + public List,?>,?>> getArguments(); + + public InvokeStatement addArgument( Argument argument ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/LoopingStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/LoopingStatement.java new file mode 100644 index 00000000..6032660d --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/LoopingStatement.java @@ -0,0 +1,17 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface LoopingStatement, + P extends BlockHolder, + S extends LoopingStatement> + extends ControlFlowStatement { +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ReturnStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ReturnStatement.java new file mode 100644 index 00000000..a2b714c6 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ReturnStatement.java @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ReturnStatement, + P extends BlockHolder> + extends BranchingStatement>, + ExpressionHolder { + + public ExpressionSource,?> getReturn(); + + public ReturnStatement setReturn( ExpressionSource expr ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/Statement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/Statement.java new file mode 100644 index 00000000..670e1642 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/Statement.java @@ -0,0 +1,29 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface Statement, + P extends BlockHolder, + S extends Statement> + extends Origin

, + ExpressionHolder { + + public void setOrigin( P origin ); + + public S setLabel( String label ); + + public String getLabel(); + + public boolean hasLabel(); +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/StatementSource.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/StatementSource.java new file mode 100644 index 00000000..89785cef --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/StatementSource.java @@ -0,0 +1,22 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + + +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface StatementSource, + P extends BlockHolder, + S extends StatementSource> + extends Statement { +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/Statements.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/Statements.java new file mode 100644 index 00000000..069774ea --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/Statements.java @@ -0,0 +1,137 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.spi.StatementFactory; + +import java.util.ServiceLoader; + +public abstract class Statements { + + protected static StatementFactory factory; + + @SuppressWarnings( "unchecked" ) + protected static > StatementFactory getFactory() { + synchronized ( Statements.class ) { + ServiceLoader sl = ServiceLoader.load( StatementFactory.class, Statements.class.getClassLoader() ); + if ( sl.iterator().hasNext() ) { + factory = sl.iterator().next(); + } else { + throw new IllegalStateException( "No StatementFactory implementation available, unable to continue" ); + } + } + return factory; + } + + @SuppressWarnings( "unchecked" ) + public static > ReturnStatement newReturn() { + return (ReturnStatement) getFactory().newReturn(); + } + + @SuppressWarnings( "unchecked" ) + public static > IfStatement> newIf() { + return (IfStatement>) getFactory().newIf(); + } + + @SuppressWarnings( "unchecked" ) + public static > AssignmentStatement> newAssign() { + return (AssignmentStatement>) getFactory().newAssign(); + } + + @SuppressWarnings( "unchecked" ) + public static > DeclareStatement> newDeclare() { + return (DeclareStatement>) getFactory().newDeclare(); + } + + @SuppressWarnings( "unchecked" ) + public static > ForStatement> newFor() { + return (ForStatement>) getFactory().newFor(); + } + + @SuppressWarnings( "unchecked" ) + public static > ForEachStatement> newForEach() { + return (ForEachStatement>) getFactory().newForEach(); + } + + @SuppressWarnings( "unchecked" ) + public static > InvokeStatement> newInvoke() { + return (InvokeStatement>) getFactory().newInvoke(); + } + + @SuppressWarnings( "unchecked" ) + public static > WhileStatement> newWhile() { + return (WhileStatement>) getFactory().newWhile(); + } + + @SuppressWarnings( "unchecked" ) + public static > DoWhileStatement> newDoWhile() { + return (DoWhileStatement>) getFactory().newDoWhile(); + } + + @SuppressWarnings( "unchecked" ) + public static > BlockStatement> newBlock() { + return (BlockStatement>) getFactory().newBlock(); + } + + @SuppressWarnings( "unchecked" ) + public static > BreakStatement> newBreak() { + return (BreakStatement>) getFactory().newBreak(); + } + + @SuppressWarnings( "unchecked" ) + public static > ContinueStatement> newContinue() { + return (ContinueStatement>) getFactory().newContinue(); + } + + @SuppressWarnings( "unchecked" ) + public static > ThrowStatement> newThrow() { + return (ThrowStatement>) getFactory().newThrow(); + } + + @SuppressWarnings( "unchecked" ) + public static > TryCatchStatement> newTryCatch() { + return (TryCatchStatement>) getFactory().newTryCatch(); + } + + @SuppressWarnings( "unchecked" ) + public static > SynchStatement> newSynchronized() { + return (SynchStatement>) getFactory().newSynchronized(); + } + + @SuppressWarnings( "unchecked" ) + public static > ThisStatement> newThis() { + return (ThisStatement>) getFactory().newThis(); + } + + @SuppressWarnings( "unchecked" ) + public static > SuperStatement> newSuper() { + return (SuperStatement>) getFactory().newSuper(); + } + + @SuppressWarnings( "unchecked" ) + public static > AssertStatement> newAssert() { + return (AssertStatement>) getFactory().newAssert(); + } + + @SuppressWarnings( "unchecked" ) + public static > SwitchStatement> newSwitch() { + return (SwitchStatement>) getFactory().newSwitch(); + } + + @SuppressWarnings( "unchecked" ) + public static > EvalExpressionStatement> newEval() { + return (EvalExpressionStatement>) getFactory().newEval(); + } + + public static > SwitchCaseStatement>> newCase() { + return (SwitchCaseStatement>>) getFactory().newCase(); + } + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/SuperStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/SuperStatement.java new file mode 100644 index 00000000..4f520323 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/SuperStatement.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +public interface SuperStatement, + P extends BlockHolder> + extends StatementSource> { + + public List,?>> getArguments(); + + public SuperStatement addArgument( Argument argument ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchCaseStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchCaseStatement.java new file mode 100644 index 00000000..17272c4e --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchCaseStatement.java @@ -0,0 +1,22 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface SwitchCaseStatement, + P extends SwitchStatement>> + extends StatementSource> { + + public ExpressionSource,?> getCaseExpression(); + + public SwitchCaseStatement setCaseExpression( ExpressionSource src ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchStatement.java new file mode 100644 index 00000000..0e6ce3f4 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/SwitchStatement.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +public interface SwitchStatement, + P extends BlockHolder> + extends ControlFlowStatement>, + BodyHolder> { + + public List,?>> getCaseExpressions(); + + public SwitchStatement addCase( ExpressionSource option ); + + public SwitchStatement addCase( SwitchCaseStatement option ); + + public SwitchStatement addDefault(); + + + public List,?>> getStatements(); + + public SwitchStatement addStatement( StatementSource arg ); + + + public ExpressionSource,?> getSwitch(); + + public SwitchStatement setSwitch( ExpressionSource expr ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/SynchStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/SynchStatement.java new file mode 100644 index 00000000..c2e9a8a0 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/SynchStatement.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface SynchStatement, + P extends BlockHolder> + extends ControlFlowStatement>, + BodyHolder> { + + public ExpressionSource,?> getSynchOn(); + + public SynchStatement setSynchOn( ExpressionSource expr ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ThisStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ThisStatement.java new file mode 100644 index 00000000..b5e837e7 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ThisStatement.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.List; + +public interface ThisStatement, + P extends BlockHolder> + extends StatementSource> { + + public List,?>> getArguments(); + + public ThisStatement addArgument( Argument argument ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/ThrowStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/ThrowStatement.java new file mode 100644 index 00000000..3b4c2dc6 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/ThrowStatement.java @@ -0,0 +1,22 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ThrowStatement, + P extends BlockHolder> + extends ControlFlowStatement> { + + public ExpressionSource,?> getThrowable(); + + public ThrowStatement setThrowable( ExpressionSource expr ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/TryCatchStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/TryCatchStatement.java new file mode 100644 index 00000000..d5a49c4d --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/TryCatchStatement.java @@ -0,0 +1,36 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.Map; + +public interface TryCatchStatement, + P extends BlockHolder> + extends ControlFlowStatement>, + BodyHolder> { + + public Map>,BlockStatement>> getCatches(); + + public TryCatchStatement addCatch( DeclareExpression declaration, + BlockStatement block ); + + public TryCatchStatement addCatch( DeclareExpression declaration, + StatementSource block ); + + + public BlockStatement> getFinally(); + + public TryCatchStatement setFinally( BlockStatement block ); + + public TryCatchStatement setFinally( StatementSource block ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/model/statements/WhileStatement.java b/api/src/main/java/org/jboss/forge/roaster/model/statements/WhileStatement.java new file mode 100644 index 00000000..c64a0845 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/model/statements/WhileStatement.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.statements; + +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface WhileStatement, + P extends BlockHolder> + extends LoopingStatement>, + BodyHolder> { + + public ExpressionSource,?> getCondition(); + + public WhileStatement setCondition( ExpressionSource expr ); + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactory.java b/api/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactory.java new file mode 100644 index 00000000..f71ad514 --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactory.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.spi; + + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.AccessBuilder; +import org.jboss.forge.roaster.model.expressions.BasicExpressionFactory; +import org.jboss.forge.roaster.model.expressions.NonPrimitiveExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface ExpressionFactory, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends AccessBuilder, + BasicExpressionFactory { + +} diff --git a/api/src/main/java/org/jboss/forge/roaster/spi/StatementFactory.java b/api/src/main/java/org/jboss/forge/roaster/spi/StatementFactory.java new file mode 100644 index 00000000..a814205a --- /dev/null +++ b/api/src/main/java/org/jboss/forge/roaster/spi/StatementFactory.java @@ -0,0 +1,112 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.spi; + +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.AssertStatement; +import org.jboss.forge.roaster.model.statements.AssignmentStatement; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.BreakStatement; +import org.jboss.forge.roaster.model.statements.EvalExpressionStatement; +import org.jboss.forge.roaster.model.statements.ContinueStatement; +import org.jboss.forge.roaster.model.statements.DeclareStatement; +import org.jboss.forge.roaster.model.statements.DoWhileStatement; +import org.jboss.forge.roaster.model.statements.ForEachStatement; +import org.jboss.forge.roaster.model.statements.ForStatement; +import org.jboss.forge.roaster.model.statements.IfStatement; +import org.jboss.forge.roaster.model.statements.InvokeStatement; +import org.jboss.forge.roaster.model.statements.ReturnStatement; +import org.jboss.forge.roaster.model.statements.SuperStatement; +import org.jboss.forge.roaster.model.statements.SwitchCaseStatement; +import org.jboss.forge.roaster.model.statements.SwitchStatement; +import org.jboss.forge.roaster.model.statements.SynchStatement; +import org.jboss.forge.roaster.model.statements.ThisStatement; +import org.jboss.forge.roaster.model.statements.ThrowStatement; +import org.jboss.forge.roaster.model.statements.TryCatchStatement; +import org.jboss.forge.roaster.model.statements.WhileStatement; + +public interface StatementFactory> { + + /** + * Creates a return statement + * @return + */ + public ReturnStatement newReturn(); + + /** + * Creates a variable assignment statement + * @return + */ + public AssignmentStatement newAssign(); + + /** + * Creates a variable declaration statement + * @return + */ + public DeclareStatement newDeclare(); + + /** + * Creates a method invocation statement + * @return + */ + public InvokeStatement newInvoke(); + + /** + * Creates an if statement + * @return + */ + public IfStatement newIf(); + + /** + * Creates a while statement + * @return + */ + public WhileStatement newWhile(); + + /** + * Creates a for statement + * @return + */ + public ForStatement newFor(); + + /** + * Creates an enhanced for statement + * @return + */ + public ForEachStatement newForEach(); + + public DoWhileStatement newDoWhile(); + + public BreakStatement newBreak(); + + public ContinueStatement newContinue(); + + public ThrowStatement newThrow(); + + public TryCatchStatement newTryCatch(); + + public SynchStatement newSynchronized(); + + public ThisStatement newThis(); + + public SuperStatement newSuper(); + + public AssertStatement newAssert(); + + public SwitchStatement newSwitch(); + + public EvalExpressionStatement newEval(); + + /** + * Creates a block statement + * @return + */ + public BlockStatement newBlock(); + + public SwitchCaseStatement newCase(); +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/ASTNode.java b/impl/src/main/java/org/jboss/forge/roaster/model/ASTNode.java new file mode 100644 index 00000000..16b5bd57 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/ASTNode.java @@ -0,0 +1,19 @@ +package org.jboss.forge.roaster.model; + + +import org.eclipse.jdt.core.dom.AST; + +public interface ASTNode { + + AST getAst(); + + J materialize( AST ast ); + + J getInternal(); + + void setInternal(J jdtNode); + + boolean isMaterialized(); + + void setAst( AST ast ); +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/BlockImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/BlockImpl.java new file mode 100644 index 00000000..cfad292c --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/BlockImpl.java @@ -0,0 +1,61 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.LabeledStatement; +import org.eclipse.jdt.core.dom.Statement; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.impl.statements.JdtStatementWrapper; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; + + +public abstract class BlockImpl, + P extends BlockHolder, + B extends Block> + extends NodeImpl + implements Block, + JdtStatementWrapper { + + protected org.eclipse.jdt.core.dom.Block jdtBlock; + + @Override + public boolean isMaterialized() { + return getInternal() != null; + } + + @Override + public org.eclipse.jdt.core.dom.Block getInternal() { + return jdtBlock; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.Block jdtNode) { + super.setInternal(jdtNode); + this.jdtBlock = jdtNode; + } + + public org.eclipse.jdt.core.dom.Statement wireAndGetStatement( org.jboss.forge.roaster.model.statements.Statement statement, B parent, AST ast ) { + ASTNode node = (ASTNode) statement; + statement.setOrigin( parent ); + node.setAst( ast ); + Statement stat = node.materialize( ast ); + + if ( statement.hasLabel() ) { + LabeledStatement labeledStatement = ast.newLabeledStatement(); + labeledStatement.setBody( stat ); + labeledStatement.setLabel( ast.newSimpleName( statement.getLabel() ) ); + return labeledStatement; + } else { + return stat; + } + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JDTHelper.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JDTHelper.java index 63d6c277..d489639b 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JDTHelper.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JDTHelper.java @@ -6,19 +6,23 @@ */ package org.jboss.forge.roaster.model.impl; +import java.util.Arrays; import java.util.List; +import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ArrayType; import org.eclipse.jdt.core.dom.BodyDeclaration; import org.eclipse.jdt.core.dom.EnumDeclaration; import org.eclipse.jdt.core.dom.ParameterizedType; import org.eclipse.jdt.core.dom.PrimitiveType; import org.eclipse.jdt.core.dom.QualifiedType; +import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.SimpleType; import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; import org.eclipse.jdt.core.dom.Type; import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.jdt.core.dom.WildcardType; +import org.jboss.forge.roaster.model.util.Types; /** * Help with Eclipse JDT common operations. @@ -86,4 +90,79 @@ public static List getInterfaces(final BodyDeclaration dec) return (List) dec.getStructuralProperty(desc); } + public static Type getType( Class klass, AST ast ) + { + return getType( klass.getName(), ast ); + } + + public static Type getType( String klass, AST ast ) + { + if ( Types.isPrimitive( klass ) ) { + return ast.newPrimitiveType( PrimitiveType.toCode( klass ) ); + } else if ( Types.isGeneric( klass ) ) { + String base = Types.stripGenerics( klass ); + ParameterizedType param = ast.newParameterizedType( getType( base, ast ) ); + for (String typeP : Types.getGenericsTypeParameter( klass ).split(",")) + { + param.typeArguments().add( getType( typeP, ast ) ); + } + return param; + } else if ( Types.isArray( klass ) ) { + String base = Types.stripArray( klass ); + return ast.newArrayType( getType( base, ast ), Types.getArrayDimension( klass ) ); + } else if ( Types.isQualified( klass ) ) { + return ast.newQualifiedType( ast.newSimpleType( ast.newName( Types.getPackage( klass ) ) ), ast.newSimpleName( Types.toSimpleName( klass ) ) ); + } else if ( Types.isSimpleName( klass ) ) { + return ast.newSimpleType( ast.newName( klass ) ); + } + return null; + } + + public static String getter( String fieldName, String type ) { + if ( boolean.class.getName().equals( type ) ) { + return "is" + capitalize( fieldName ); + } + return "get" + capitalize( fieldName ); + } + + public static String setter( String fieldName, String type ) { + return "set" + capitalize( fieldName ); + } + + public static String capitalize( String fieldName ) { + return fieldName.substring( 0, 1 ).toUpperCase() + fieldName.substring( 1 ); + } + + public static String fieldForGetter( String name ) { + if ( name.startsWith( "get" ) ) { + return firstCharToLowerCase( name.substring( 3 ) ); + } else if ( name.startsWith( "is" ) ) { + return firstCharToLowerCase( name.substring( 2 ) ); + } else { + return name; + } + } + + public static String fieldForSetter( String name ) { + if ( name.startsWith( "set" ) ) { + return firstCharToLowerCase( name.substring( 3 ) ); + } else { + return name; + } + } + + public static String firstCharToLowerCase( String word ) { + if ( word.length() <= 1 ) { + return word.toLowerCase(); + } + return word.substring( 0, 1 ).toLowerCase() + word.substring( 1 ); + } + + public static boolean isGetter( String name, List arguments ) { + return name.startsWith( "get" ) || name.startsWith( "is" ) && arguments.isEmpty(); + } + + public static boolean isSetter( String name, List arguments ) { + return name.startsWith( "set" ) && arguments.size() == 1; + } } diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaClassImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaClassImpl.java index 964dc198..d5f31b3e 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaClassImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaClassImpl.java @@ -182,4 +182,10 @@ else if (Types.isGeneric(type)) return this; } + + @Override + public JavaClassSource asJavaClassSource() + { + return this; + } } \ No newline at end of file diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java index a08b57af..a45e54a7 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java @@ -6,11 +6,6 @@ */ package org.jboss.forge.roaster.model.impl; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.Block; @@ -31,21 +26,36 @@ import org.jboss.forge.roaster.model.Visibility; import org.jboss.forge.roaster.model.ast.AnnotationAccessor; import org.jboss.forge.roaster.model.ast.ModifierAccessor; +import org.jboss.forge.roaster.model.impl.statements.BlockStatementImpl; +import org.jboss.forge.roaster.model.impl.statements.InnerBlockImpl; +import org.jboss.forge.roaster.model.impl.statements.JdtStatementWrapper; +import org.jboss.forge.roaster.model.impl.statements.StatementBuilder; import org.jboss.forge.roaster.model.source.AnnotationSource; import org.jboss.forge.roaster.model.source.Import; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; import org.jboss.forge.roaster.model.source.JavaClassSource; import org.jboss.forge.roaster.model.source.JavaDocSource; import org.jboss.forge.roaster.model.source.JavaSource; import org.jboss.forge.roaster.model.source.MethodSource; import org.jboss.forge.roaster.model.source.ParameterSource; import org.jboss.forge.roaster.model.source.TypeVariableSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.Statements; import org.jboss.forge.roaster.model.util.Strings; import org.jboss.forge.roaster.model.util.Types; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + + /** * @author Lincoln Baxter, III */ -public class MethodImpl> implements MethodSource +public class MethodImpl> implements MethodSource, BlockHolder { private final AnnotationAccessor> annotations = new AnnotationAccessor>(); private final ModifierAccessor modifiers = new ModifierAccessor(); @@ -55,6 +65,8 @@ public class MethodImpl> implements MethodSource private CompilationUnit cu = null; private final MethodDeclaration method; + private BlockStatement> body; + private void init(final O parent) { this.parent = parent; @@ -83,8 +95,7 @@ public MethodImpl(final O parent, final String method) JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List> methods = temp.getMethods(); MethodDeclaration newMethod = (MethodDeclaration) methods.get(0).getInternal(); - MethodDeclaration subtree = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod); - this.method = subtree; + this.method = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod); } @Override @@ -132,13 +143,13 @@ public AnnotationSource addAnnotation(final Class addAnnotation(final String className) { - return annotations.addAnnotation(this, method, className); + return annotations.addAnnotation( this, method, className ); } @Override public List> getAnnotations() { - return annotations.getAnnotations(this, method); + return annotations.getAnnotations( this, method ); } @Override @@ -150,13 +161,13 @@ public boolean hasAnnotation(final Class removeAnnotation(final Annotation annotation) { - return annotations.removeAnnotation(this, method, annotation); + return annotations.removeAnnotation( this, method, annotation ); } @Override @@ -212,7 +223,7 @@ public MethodSource setBody(final String body) Block block = ((MethodDeclaration) methods.get(0).getInternal()).getBody(); block = (Block) ASTNode.copySubtree(method.getAST(), block); - method.setBody(block); + method.setBody( block ); } return this; } @@ -286,7 +297,7 @@ public MethodSource setReturnType(final String typeName) @Override public MethodSource setReturnType(final JavaType type) { - return setReturnType(type.getName()); + return setReturnType( type.getName() ); } @Override @@ -390,7 +401,7 @@ public MethodSource setNative(boolean value) @Override public boolean isNative() { - return modifiers.hasModifier(method, ModifierKeyword.NATIVE_KEYWORD); + return modifiers.hasModifier( method, ModifierKeyword.NATIVE_KEYWORD ); } @Override @@ -504,13 +515,13 @@ public MethodSource setProtected() @Override public Visibility getVisibility() { - return Visibility.getFrom(this); + return Visibility.getFrom( this ); } @Override public MethodSource setVisibility(final Visibility scope) { - return Visibility.set(this, scope); + return Visibility.set( this, scope ); } /* @@ -721,13 +732,13 @@ public MethodSource removeTypeVariable(String name) @Override public MethodSource removeTypeVariable(TypeVariable typeVariable) { - return removeTypeVariable(typeVariable.getName()); + return removeTypeVariable( typeVariable.getName() ); } @Override public ParameterSource addParameter(Class type, String name) { - return addParameter(type.getCanonicalName(), name); + return addParameter( type.getCanonicalName(), name ); } @Override @@ -834,6 +845,7 @@ public boolean hasJavaDoc() public MethodSource removeJavaDoc() { method.setJavadoc(null); + return this; } @@ -848,4 +860,45 @@ public JavaDocSource> getJavaDoc() } return new JavaDocImpl>(this, javadoc); } + + + @Override + public MethodSource setBody( BlockSource statement ) + { + this.body = (BlockStatement>) statement; + + ( (BlockStatement>) statement ).setOrigin( this ); + JdtStatementWrapper,?> wrapper = (JdtStatementWrapper, ?>) body; + wrapper.setAst( cu.getAST() ); + Statement jdtStatement = wrapper.materialize( cu.getAST() ); + + method.setBody((Block) jdtStatement); + return this; + } + + @Override + public MethodSource setBody( StatementSource statement ) + { + BlockStatement> block = new BlockStatementImpl>(); + setBody( (BlockSource, ?>) block ); + block.addStatement( statement ); + return this; + } + + + public BlockSource,?> getBodyAsBlock() + { + if (body == null) { + body = new BlockStatementImpl(); + ((org.jboss.forge.roaster.model.ASTNode)body).setInternal(method.getBody()); + for (Object statement : method.getBody().statements()) { + StatementSource stat = StatementBuilder.asRoasterStatement((Statement) statement); + if (stat != null) { + body.addStatement(stat); + } + } + } + return body; + } + } diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/NodeImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/NodeImpl.java new file mode 100644 index 00000000..df4fe72d --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/NodeImpl.java @@ -0,0 +1,78 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.LabeledStatement; +import org.jboss.forge.roaster.Origin; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.Block; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Expression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.expressions.JdtExpressionWrapper; +import org.jboss.forge.roaster.model.impl.statements.JdtStatementWrapper; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.Statement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.Statements; + +public abstract class NodeImpl, + T, + J extends org.eclipse.jdt.core.dom.ASTNode> + implements Origin, + ASTNode { + + protected T origin; + protected AST ast; + protected String label; + + @Override + public T getOrigin() { + return origin; + } + + public void setOrigin( T origin ) { + this.origin = origin; + } + + public AST getAst() { + return ast; + } + + public void setAst( AST ast ) { + this.ast = ast; + } + + public String getLabel() { + return label; + } + + public boolean hasLabel() { + return label != null && label.length() > 0; + } + + @Override + public void setInternal(J jdtNode) { + setAst(jdtNode.getAST()); + } + + @Override + public String toString() { + return getInternal() != null ? getInternal().toString() : ""; + } + + @Override + public boolean isMaterialized() { + return getInternal() != null; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/ParameterImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/ParameterImpl.java index f8d01e70..da680d5c 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/ParameterImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/ParameterImpl.java @@ -17,6 +17,7 @@ import org.jboss.forge.roaster.model.ast.ModifierAccessor; import org.jboss.forge.roaster.model.source.AnnotationSource; import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.source.MethodSource; import org.jboss.forge.roaster.model.source.ParameterSource; /** diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AbstractMethodInvokeImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AbstractMethodInvokeImpl.java new file mode 100644 index 00000000..b1244711 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AbstractMethodInvokeImpl.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ArgumentHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public abstract class AbstractMethodInvokeImpl, + P extends ExpressionHolder, + M extends MethodInvokeExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends InvokeableImpl + implements MethodInvokeExpression, + ArgumentHolder { + + protected List> arguments = Collections.EMPTY_LIST; + + public AbstractMethodInvokeImpl( String method ) { + super( method ); + } + + @Override + public M addArgument( Argument argument ) { + if ( arguments.isEmpty() ) { + arguments = new LinkedList>(); + } + arguments.add( (Argument) argument ); + return (M) this; + } + + @Override + public ExpressionSource 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 new file mode 100644 index 00000000..7f56918f --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AccessorImpl.java @@ -0,0 +1,56 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.ArrayIndexer; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.NonPrimitiveExpression; +import org.jboss.forge.roaster.model.expressions.Setter; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class AccessorImpl, + P extends ExpressionHolder, + E extends NonPrimitiveExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends NonPrimitiveExpressionImpl + implements Accessor { + + public Field field( String fieldName ) { + return new DotAccessorImpl( this ).field( fieldName ); + } + + public Getter getter( String field, String klass ) { + return new DotAccessorImpl( this ).getter( field, klass ); + } + + public Getter getter( String field, Class klass ) { + return new DotAccessorImpl( this ).getter( field, klass ); + } + + public Setter setter( String fldName, String type, ExpressionSource value ) { + return new DotAccessorImpl( this ).setter( fldName, type, value ); + } + + public Setter setter( String fldName, Class type, ExpressionSource value ) { + return new DotAccessorImpl( this ).setter( fldName, type, value ); + } + + public MethodCallExpression invoke( String method ) { + return new DotAccessorImpl( this ).invoke( method ); + } + + @Override + public ArrayIndexer itemAt( ExpressionSource index ) { + return new DotAccessorImpl( this ).itemAt( index ); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayAccessImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayAccessImpl.java new file mode 100644 index 00000000..f5b0cb0c --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayAccessImpl.java @@ -0,0 +1,83 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ArrayAccess; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ArrayIndexer; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class ArrayAccessImpl, + P extends ExpressionHolder> + extends SimpleAccessorImpl,ArrayAccess> + implements ArrayIndexer { + + private ExpressionSource,?> index; + private ExpressionSource,?> target; + private ArrayAccess axx; + + public ArrayAccessImpl( ExpressionSource index ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) index; + this.index = cast; + } + + @Override + public ArrayAccess materialize( AST ast ) { + if (isMaterialized()) { + return axx; + } + axx = ast.newArrayAccess(); + axx.setIndex( wireAndGetExpression( index, this, ast ) ); + if ( target != null ) { + axx.setArray( wireAndGetExpression( target, this, ast ) ); + } + return axx; + } + + @Override + public ArrayAccess getInternal() { + return axx; + } + + @Override + public void setInternal(ArrayAccess jdtNode) { + super.setInternal(jdtNode); + this.axx = jdtNode; + } + + @Override + public ExpressionSource,?> chainExpression( ExpressionSource child ) { + ExpressionSource,?> cast = ( ExpressionSource,?> ) child; + this.target = cast; + return cast; + } + + @Override + public ExpressionSource, ?> getIndex() { + return index; + } + + @Override + public ArrayIndexer setIndex( ExpressionSource index ) { + this.index = (ExpressionSource, ?>) index; + return this; + } + + @Override + public ExpressionSource, ?> getTarget() { + return target; + } + + @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 new file mode 100644 index 00000000..842564e0 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayImpl.java @@ -0,0 +1,103 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ArrayCreation; +import org.eclipse.jdt.core.dom.ArrayInitializer; +import org.eclipse.jdt.core.dom.ArrayType; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ArrayConstructorExpression; +import org.jboss.forge.roaster.model.expressions.ArrayInit; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + + +public class ArrayImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,ArrayCreation> + implements ArrayConstructorExpression { + + private ArrayCreation array; + + private String type; + private List,?>> dims = Collections.EMPTY_LIST; + private ArrayInit> init; + + public ArrayImpl( String type ) { + this.type = type; + } + + @Override + public ArrayCreation materialize( AST ast ) { + if (isMaterialized()) { + return array; + } + array = ast.newArrayCreation(); + array.setType( (ArrayType) JDTHelper.getType( type + new String( new char[ getDimension() ] ).replace( "\0", "[]" ), ast ) ); + for ( ExpressionSource,?> dim : dims ) { + array.dimensions().add( wireAndGetExpression( dim, this, ast ) ); + } + if ( init != null ) { + array.setInitializer( (ArrayInitializer) wireAndGetExpression( init, this, ast ) ); + } + return array; + } + + @Override + public ArrayCreation getInternal() { + return array; + } + + @Override + public void setInternal(ArrayCreation jdtNode) { + super.setInternal(jdtNode); + this.array = jdtNode; + } + + @Override + public ArrayConstructorExpression addDimension( ExpressionSource dim ) { + if ( dims.isEmpty() ) { + dims = new ArrayList,?>>(); + } + dims.add( (ExpressionSource, ?>) dim ); + return this; + } + + @Override + public ArrayConstructorExpression init( ArrayInit array ) { + this.init = (ArrayInit>) array; + return this; + } + + public int getDimension() { + if ( ! dims.isEmpty() ) { + return dims.size(); + } else if ( init != null ) { + return init.getDimension(); + } + return 0; + } + + @Override + public ArrayInit> getInit() { + return init; + } + + @Override + public List, ?>> getDimensions() { + return Collections.unmodifiableList( dims ); + } + +} + diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayInitImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayInitImpl.java new file mode 100644 index 00000000..1189caea --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ArrayInitImpl.java @@ -0,0 +1,93 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ArrayInitializer; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ArrayInit; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class ArrayInitImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,ArrayInitializer> + implements ArrayInit { + + private List,?>> elements = Collections.EMPTY_LIST; + + private ArrayInitializer init; + + @Override + public ArrayInit addElement( ArrayInit subRow ) { + if ( elements.isEmpty() ) { + elements = new ArrayList( ); + } + elements.add( (ExpressionSource, ?>) subRow ); + return this; + } + + @Override + public ArrayInit addElement( ExpressionSource subElement ) { + if ( elements.isEmpty() ) { + elements = new ArrayList( ); + } + elements.add( (ExpressionSource, ?>) subElement ); + return this; + } + + @Override + public int size() { + return elements.size(); + } + + @Override + public int getDimension() { + if ( elements.isEmpty() ) { + return 0; + } else { + if ( elements.get( 0 ) instanceof ArrayInit ) { + return elements.size(); + } else { + return 1; + } + } + } + + @Override + public ArrayInitializer materialize( AST ast ) { + if (isMaterialized()) { + return init; + } + this.init = ast.newArrayInitializer(); + for ( ExpressionSource,?> src : elements ) { + this.init.expressions().add( wireAndGetExpression( src, this, ast ) ); + } + return init; + } + + @Override + public ArrayInitializer getInternal() { + return init; + } + + @Override + public void setInternal(ArrayInitializer jdtNode) { + super.setInternal(jdtNode); + this.init = jdtNode; + } + + @Override + public List, ?>> getElements() { + return Collections.unmodifiableList( elements ); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AssignImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AssignImpl.java new file mode 100644 index 00000000..15fefdf2 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/AssignImpl.java @@ -0,0 +1,93 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.Assignment; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.AssignExpression; +import org.jboss.forge.roaster.model.expressions.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; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BaseInvokeableImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BaseInvokeableImpl.java new file mode 100644 index 00000000..4bfb7379 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BaseInvokeableImpl.java @@ -0,0 +1,32 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class BaseInvokeableImpl, + P extends ExpressionHolder, + E extends MethodInvokeExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends AccessorImpl + implements MethodInvokeExpression { + + protected String method; + + public BaseInvokeableImpl( String method ) { + this.method = method; + } + + @Override + public String getMethodName() { + return method; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BasicExpressionFactoryImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BasicExpressionFactoryImpl.java new file mode 100644 index 00000000..6bc18fa2 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BasicExpressionFactoryImpl.java @@ -0,0 +1,240 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ArrayConstructorExpression; +import org.jboss.forge.roaster.model.expressions.ArrayInit; +import org.jboss.forge.roaster.model.expressions.AssignExpression; +import org.jboss.forge.roaster.model.expressions.Assignment; +import org.jboss.forge.roaster.model.expressions.BasicExpressionFactory; +import org.jboss.forge.roaster.model.expressions.CastExpression; +import org.jboss.forge.roaster.model.expressions.ClassLiteral; +import org.jboss.forge.roaster.model.expressions.ConstructorExpression; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.InstanceofExpression; +import org.jboss.forge.roaster.model.expressions.Literal; +import org.jboss.forge.roaster.model.expressions.NullLiteral; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.expressions.OperatorExpression; +import org.jboss.forge.roaster.model.expressions.ParenExpression; +import org.jboss.forge.roaster.model.expressions.PrefixOp; +import org.jboss.forge.roaster.model.expressions.PrimitiveLiteral; +import org.jboss.forge.roaster.model.expressions.StringLiteral; +import org.jboss.forge.roaster.model.expressions.Super; +import org.jboss.forge.roaster.model.expressions.TernaryExpression; +import org.jboss.forge.roaster.model.expressions.ThisLiteral; +import org.jboss.forge.roaster.model.expressions.UnaryExpression; +import org.jboss.forge.roaster.model.expressions.Variable; +import org.jboss.forge.roaster.model.impl.NodeImpl; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class BasicExpressionFactoryImpl, + P extends ExpressionHolder, + J extends org.eclipse.jdt.core.dom.Expression> + extends NodeImpl + implements BasicExpressionFactory { + + + public BasicExpressionFactoryImpl() { } + + @Override + public J materialize( AST ast ) { + throw new UnsupportedOperationException( "Method should not be called directly on this class" ); + } + + @Override + public J getInternal() { + throw new UnsupportedOperationException( "Method should not be called directly on this class" ); + } + + @Override + public StringLiteral literal( String val ) { + return new StringLiteralImpl( val ); + } + + @Override + public 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 new file mode 100644 index 00000000..ad77239c --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/BooleanLiteralImpl.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.BooleanLiteral; +import org.jboss.forge.roaster.model.expressions.PrimitiveLiteral; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class BooleanLiteralImpl, + P extends ExpressionHolder> + extends LiteralImpl,org.eclipse.jdt.core.dom.BooleanLiteral> + implements BooleanLiteral { + + private boolean val; + + private org.eclipse.jdt.core.dom.BooleanLiteral literal; + + public BooleanLiteralImpl( boolean val ) { + this.val = val; + } + + @Override + public org.eclipse.jdt.core.dom.BooleanLiteral materialize( AST ast ) { + if (isMaterialized()) { + return literal; + } + literal = ast.newBooleanLiteral(val); + return literal; + } + + @Override + public org.eclipse.jdt.core.dom.BooleanLiteral getInternal() { + return literal; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.BooleanLiteral jdtNode) { + super.setInternal(jdtNode); + this.literal = jdtNode; + } + + public Boolean getValue() { + return val; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CastImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CastImpl.java new file mode 100644 index 00000000..e16a6685 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CastImpl.java @@ -0,0 +1,70 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ParenthesizedExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.CastExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class CastImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,org.eclipse.jdt.core.dom.CastExpression> + implements CastExpression { + + private org.eclipse.jdt.core.dom.CastExpression cast; + + private String type; + private ExpressionSource,?> expression; + + public CastImpl( String klass, ExpressionSource expression ) { + this.type = klass; + this.expression = (ExpressionSource, ?>) expression; + } + + @Override + public org.eclipse.jdt.core.dom.CastExpression materialize( AST ast ) { + if (isMaterialized()) { + return cast; + } + cast = ast.newCastExpression(); + cast.setType( JDTHelper.getType( type, ast ) ); + if ( expression != null ) { + org.eclipse.jdt.core.dom.Expression expr = wireAndGetExpression( expression, this, ast ); + ParenthesizedExpression paren = ast.newParenthesizedExpression(); + paren.setExpression( expr ); + cast.setExpression( paren ); + } + return cast; + } + + @Override + public org.eclipse.jdt.core.dom.CastExpression getInternal() { + return cast; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.CastExpression jdtNode) { + super.setInternal(jdtNode); + this.cast = jdtNode; + } + + @Override + public String getType() { + return type; + } + + @Override + public ExpressionSource,?> getExpression() { + return expression; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CharacterLiteralImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CharacterLiteralImpl.java new file mode 100644 index 00000000..4031c400 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/CharacterLiteralImpl.java @@ -0,0 +1,53 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.CharacterLiteral; +import org.jboss.forge.roaster.model.expressions.PrimitiveLiteral; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class CharacterLiteralImpl, + P extends ExpressionHolder> + extends LiteralImpl,org.eclipse.jdt.core.dom.CharacterLiteral> + implements CharacterLiteral { + + private Character val; + + private org.eclipse.jdt.core.dom.CharacterLiteral literal; + + public CharacterLiteralImpl( Character val ) { + this.val = val; + } + + @Override + public org.eclipse.jdt.core.dom.CharacterLiteral materialize( AST ast ) { + if (isMaterialized()) { + return literal; + } + literal = ast.newCharacterLiteral(); + literal.setCharValue( val ); + return literal; + } + + @Override + public org.eclipse.jdt.core.dom.CharacterLiteral getInternal() { + return literal; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.CharacterLiteral jdtNode) { + super.setInternal(jdtNode); + this.literal = jdtNode; + } + + public Character getValue() { + return val; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ClassLiteralImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ClassLiteralImpl.java new file mode 100644 index 00000000..9c5e8bc5 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ClassLiteralImpl.java @@ -0,0 +1,54 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.TypeLiteral; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ClassLiteral; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class ClassLiteralImpl, + P extends ExpressionHolder> + extends AccessorImpl,TypeLiteral> + implements ClassLiteral { + + private String val; + + private TypeLiteral literal; + + public ClassLiteralImpl( String val ) { + this.val = val; + } + + @Override + public TypeLiteral materialize( AST ast ) { + if (isMaterialized()) { + return literal; + } + literal = ast.newTypeLiteral(); + literal.setType( JDTHelper.getType( val, ast ) ); + return literal; + } + + @Override + public TypeLiteral getInternal() { + return literal; + } + + @Override + public void setInternal(TypeLiteral jdtNode) { + super.setInternal(jdtNode); + this.literal = jdtNode; + } + + public String getValue() { + return val; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ConstructorImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ConstructorImpl.java new file mode 100644 index 00000000..4c8c315b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ConstructorImpl.java @@ -0,0 +1,84 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ClassInstanceCreation; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ConstructorExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionChainLink; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + + +public class ConstructorImpl, + P extends ExpressionHolder> + extends AccessorImpl,ClassInstanceCreation> + implements ConstructorExpression, + ExpressionChainLink { + + private ClassInstanceCreation constr; + + private String type; + private List,?>> arguments = Collections.EMPTY_LIST; + + public ConstructorImpl( String type ) { + this.type = type; + } + + @Override + public ConstructorExpression addArgument( Argument arg ) { + if ( arguments.isEmpty() ) { + arguments = new LinkedList,?>>(); + } + arguments.add( (Argument, ?>) arg ); + return this; + } + + @Override + public List, ?>> getArguments() { + return arguments; + } + + @Override + public ClassInstanceCreation materialize( AST ast ) { + if (isMaterialized()) { + return constr; + } + constr = ast.newClassInstanceCreation(); + constr.setType( JDTHelper.getType( type, ast ) ); + + for ( Argument,?> arg : arguments ) { + constr.arguments().add( wireAndGetExpression( arg, this, ast ) ); + } + return constr; + } + + @Override + public ClassInstanceCreation getInternal() { + return constr; + } + + @Override + public void setInternal(ClassInstanceCreation jdtNode) { + super.setInternal(jdtNode); + this.constr = jdtNode; + } + + @Override + public ExpressionSource chainExpression( ExpressionSource child ) { + throw new IllegalStateException( "This method should not be invoked [constructors start chains]" ); + } +} + diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DeclareExpressionImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DeclareExpressionImpl.java new file mode 100644 index 00000000..40e5882f --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DeclareExpressionImpl.java @@ -0,0 +1,133 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.VariableDeclarationExpression; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DeclareExpressionImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,VariableDeclarationExpression> + implements DeclareExpression { + + private VariableDeclarationExpression var; + + private String type; + private List names = new ArrayList(); + private Map,?>> initExpressions = new HashMap, ?>>(); + + public DeclareExpressionImpl( String type, String name ) { + this.type = type; + this.names.add( name ); + } + + public DeclareExpressionImpl( String klass, String name, ExpressionSource init ) { + this( klass, name ); + this.initExpressions.put( name, (ExpressionSource, ?>) init ); + } + + public DeclareExpression addDeclaration( String name, ExpressionSource expr ) { + addDeclaration( name ); + if ( expr != null ) { + initExpressions.put( name, (ExpressionSource, ?>) expr ); + } + return this; + } + + public DeclareExpression addDeclaration( String name ) { + this.names.add( name ); + return this; + } + + @Override + public VariableDeclarationExpression materialize( AST ast ) { + if (isMaterialized()) { + return var; + } + VariableDeclarationFragment frag = ast.newVariableDeclarationFragment(); + + frag.setName( ast.newSimpleName( getVariableName()) ); + var = ast.newVariableDeclarationExpression( frag ); + var.setType( JDTHelper.getType( getVariableType(), ast ) ); + if ( getInitExpression() != null ) { + frag.setInitializer( wireAndGetExpression( getInitExpression(), this, ast ) ); + } + + if ( names.size() > 1 ) { + for ( int j = 1; j < names.size(); j++ ) { + VariableDeclarationFragment moreFrag = ast.newVariableDeclarationFragment(); + String n = names.get( j ); + moreFrag.setName( ast.newSimpleName( n ) ); + if ( initExpressions.containsKey( n ) ) { + moreFrag.setInitializer( wireAndGetExpression( initExpressions.get( n ), this, ast ) ); + } + var.fragments().add( moreFrag ); + } + } + + return var; + } + + @Override + public VariableDeclarationExpression getInternal() { + return var; + } + + @Override + public void setInternal(VariableDeclarationExpression jdtNode) { + super.setInternal(jdtNode); + this.var = jdtNode; + } + + @Override + public String getVariableName() { + return names.isEmpty() ? null : names.get( 0 ); + } + + @Override + public String getVariableType() { + return type; + } + + @Override + public List getVariableNames() { + return names; + } + + @Override + public Map, ?>> getInitExpressions() { + return Collections.unmodifiableMap(initExpressions); + } + + @Override + public boolean isSingleDeclaration() { + return names.size() == 1; + } + + @Override + public int getNumVariables() { + return names.size(); + } + + @Override + public ExpressionSource, ?> getInitExpression() { + return getVariableName() != null ? initExpressions.get( getVariableName() ) : null; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DotAccessorImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DotAccessorImpl.java new file mode 100644 index 00000000..cbeb3798 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/DotAccessorImpl.java @@ -0,0 +1,121 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.ArrayIndexer; +import org.jboss.forge.roaster.model.expressions.ExpressionChainLink; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Expressions; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.NonPrimitiveExpression; +import org.jboss.forge.roaster.model.expressions.Setter; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.spi.ExpressionFactoryImpl; + +public class DotAccessorImpl, + P extends ExpressionHolder, + E extends NonPrimitiveExpression> + extends ExpressionImpl + implements Accessor +{ + + private ExpressionFactoryImpl factory = new ExpressionFactoryImpl(); + + private ExpressionSource owner; + + public DotAccessorImpl( ExpressionSource parent ) { + this.owner = parent; + } + + @Override + public P getOrigin() { + return null; + } + + @Override + public Field field( String fieldName ) { + Field field = factory.field( fieldName ); + swap( field, this.owner ); + return field; + } + + @Override + public Getter getter( String fieldName, String type ) { + Getter getter = factory.getter( fieldName, type ); + swap( getter, this.owner ); + return getter; + } + + @Override + public Getter getter( String field, Class klass ) { + return getter( field, klass.getName() ); + } + + @Override + public Setter setter( String fldName, String type, ExpressionSource value ) { + Setter setter = factory.setter( fldName, type, value ); + swap( setter, this.owner ); + return setter; + } + + @Override + public Setter setter( String fldName, Class type, ExpressionSource value ) { + return setter( fldName, type.getName(), value ); + } + + + /* + @Override + public Variable var( String varName ) { + Variable var = (Variable) Expressions.var( varName ); + swap( var, this.owner ); + return var; + } + */ + + + @Override + public MethodCallExpression invoke( String method ) { + MethodCallExpression invoke = (MethodCallExpression) Expressions.invoke( method ); + swap( invoke, this.owner ); + return invoke; + } + + @Override + public ArrayIndexer itemAt( ExpressionSource index ) { + ArrayIndexer accessor = factory.itemAt( index ); + swap( accessor, this.owner ); + return accessor; + } + + + private void swap( ExpressionHolder originalChild, ExpressionSource originalParent ) { + ExpressionChainLink sub = (ExpressionChainLink) originalChild; + ExpressionChainLink sup = (ExpressionChainLink) originalParent; + + sub.linkParent( sup.getParent() ); + sup.linkParent( originalChild ); + sub.chainExpression( originalParent ); + } + + @Override + public org.eclipse.jdt.core.dom.Expression materialize( AST ast ) { + throw new UnsupportedOperationException( "Method should not be called directly on this class" ); + } + + @Override + public org.eclipse.jdt.core.dom.Expression getInternal() { + throw new UnsupportedOperationException( "Method should not be called directly on this class" ); + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionBuilder.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionBuilder.java new file mode 100644 index 00000000..0d20c8b7 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionBuilder.java @@ -0,0 +1,283 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.ArrayAccess; +import org.eclipse.jdt.core.dom.ArrayCreation; +import org.eclipse.jdt.core.dom.ArrayInitializer; +import org.eclipse.jdt.core.dom.ArrayType; +import org.eclipse.jdt.core.dom.Assignment; +import org.eclipse.jdt.core.dom.BooleanLiteral; +import org.eclipse.jdt.core.dom.CastExpression; +import org.eclipse.jdt.core.dom.CharacterLiteral; +import org.eclipse.jdt.core.dom.ClassInstanceCreation; +import org.eclipse.jdt.core.dom.ConditionalExpression; +import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jdt.core.dom.FieldAccess; +import org.eclipse.jdt.core.dom.InfixExpression; +import org.eclipse.jdt.core.dom.InstanceofExpression; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.NumberLiteral; +import org.eclipse.jdt.core.dom.ParenthesizedExpression; +import org.eclipse.jdt.core.dom.PostfixExpression; +import org.eclipse.jdt.core.dom.PrefixExpression; +import org.eclipse.jdt.core.dom.QualifiedName; +import org.eclipse.jdt.core.dom.SimpleName; +import org.eclipse.jdt.core.dom.StringLiteral; +import org.eclipse.jdt.core.dom.SuperFieldAccess; +import org.eclipse.jdt.core.dom.SuperMethodInvocation; +import org.eclipse.jdt.core.dom.VariableDeclarationExpression; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ArrayConstructorExpression; +import org.jboss.forge.roaster.model.expressions.ArrayInit; +import org.jboss.forge.roaster.model.expressions.ConstructorExpression; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.Expressions; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.expressions.OperatorExpression; +import org.jboss.forge.roaster.model.expressions.OrdinalArgument; +import org.jboss.forge.roaster.model.expressions.PrefixOp; +import org.jboss.forge.roaster.model.impl.JDTHelper; + +import java.text.NumberFormat; +import java.text.ParseException; + +import static org.eclipse.jdt.core.dom.ASTNode.ARRAY_ACCESS; +import static org.eclipse.jdt.core.dom.ASTNode.ARRAY_CREATION; +import static org.eclipse.jdt.core.dom.ASTNode.ARRAY_INITIALIZER; +import static org.eclipse.jdt.core.dom.ASTNode.ASSIGNMENT; +import static org.eclipse.jdt.core.dom.ASTNode.BOOLEAN_LITERAL; +import static org.eclipse.jdt.core.dom.ASTNode.CAST_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.CHARACTER_LITERAL; +import static org.eclipse.jdt.core.dom.ASTNode.CLASS_INSTANCE_CREATION; +import static org.eclipse.jdt.core.dom.ASTNode.CONDITIONAL_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.FIELD_ACCESS; +import static org.eclipse.jdt.core.dom.ASTNode.INFIX_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.INSTANCEOF_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.METHOD_INVOCATION; +import static org.eclipse.jdt.core.dom.ASTNode.NULL_LITERAL; +import static org.eclipse.jdt.core.dom.ASTNode.NUMBER_LITERAL; +import static org.eclipse.jdt.core.dom.ASTNode.PARENTHESIZED_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.POSTFIX_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.PREFIX_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.QUALIFIED_NAME; +import static org.eclipse.jdt.core.dom.ASTNode.SIMPLE_NAME; +import static org.eclipse.jdt.core.dom.ASTNode.STRING_LITERAL; +import static org.eclipse.jdt.core.dom.ASTNode.SUPER_FIELD_ACCESS; +import static org.eclipse.jdt.core.dom.ASTNode.SUPER_METHOD_INVOCATION; +import static org.eclipse.jdt.core.dom.ASTNode.THIS_EXPRESSION; +import static org.eclipse.jdt.core.dom.ASTNode.VARIABLE_DECLARATION_EXPRESSION; +import static org.jboss.forge.roaster.model.expressions.Expressions.assign; +import static org.jboss.forge.roaster.model.expressions.Expressions.cast; +import static org.jboss.forge.roaster.model.expressions.Expressions.declare; +import static org.jboss.forge.roaster.model.expressions.Expressions.getter; +import static org.jboss.forge.roaster.model.expressions.Expressions.instanceOf; +import static org.jboss.forge.roaster.model.expressions.Expressions.invoke; +import static org.jboss.forge.roaster.model.expressions.Expressions.literal; +import static org.jboss.forge.roaster.model.expressions.Expressions.newArray; +import static org.jboss.forge.roaster.model.expressions.Expressions.nullLiteral; +import static org.jboss.forge.roaster.model.expressions.Expressions.operator; +import static org.jboss.forge.roaster.model.expressions.Expressions.paren; +import static org.jboss.forge.roaster.model.expressions.Expressions.setter; +import static org.jboss.forge.roaster.model.expressions.Expressions.sup; +import static org.jboss.forge.roaster.model.expressions.Expressions.ternary; +import static org.jboss.forge.roaster.model.expressions.Expressions.thisLiteral; +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.expressions.Expressions.vec; + +public class ExpressionBuilder { + + public static org.jboss.forge.roaster.model.expressions.ExpressionSource asRoasterExpression(org.eclipse.jdt.core.dom.Expression jdtExpression) + { + org.jboss.forge.roaster.model.expressions.ExpressionSource roasterExpr; + switch (jdtExpression.getNodeType()) { + //TODO FIXME : names are not necessarily vars, but it may not be possible to determine at this point + case SIMPLE_NAME: + roasterExpr = var( ((SimpleName) jdtExpression).getIdentifier() ); + break; + case QUALIFIED_NAME: + roasterExpr = var( ( (QualifiedName) jdtExpression ).getFullyQualifiedName() ); + break; + case STRING_LITERAL: + roasterExpr = literal( ( (StringLiteral) jdtExpression ).getLiteralValue() ); + break; + case BOOLEAN_LITERAL: + roasterExpr = literal( (( BooleanLiteral) jdtExpression).booleanValue() ); + break; + case CHARACTER_LITERAL: + roasterExpr = literal( ((CharacterLiteral) jdtExpression).charValue() ); + break; + case NULL_LITERAL: + roasterExpr = nullLiteral(); + break; + case NUMBER_LITERAL: + try { + roasterExpr = literal( NumberFormat.getInstance().parse( + ( (NumberLiteral) jdtExpression ).getToken() ) ); + } catch ( ParseException e ) { + // we know it's a valid number, so this should never happen + e.printStackTrace(); + roasterExpr = null; + } + break; + case CONDITIONAL_EXPRESSION: + ConditionalExpression cond = (ConditionalExpression) jdtExpression; + roasterExpr = ternary().setCondition( asRoasterExpression( cond.getExpression() ) ) + .setThenExpression( asRoasterExpression( cond.getThenExpression() ) ) + .setElseExpression( asRoasterExpression( cond.getElseExpression() ) ); + + break; + case CAST_EXPRESSION : + CastExpression cast = (CastExpression) jdtExpression; + roasterExpr = cast( cast.getType().toString(), asRoasterExpression( cast.getExpression() ) ); + break; + case INFIX_EXPRESSION : + InfixExpression infix = (InfixExpression) jdtExpression; + roasterExpr = operator( Op.build( infix.getOperator().toString() ) ) + .addArgument( (Argument) asRoasterExpression( infix.getLeftOperand() ) ) + .addArgument( (Argument) asRoasterExpression( infix.getRightOperand() ) ); + for ( Object o : infix.extendedOperands() ) { + (( OperatorExpression) roasterExpr).addArgument( (Argument) asRoasterExpression( (Expression) o ) ); + } + break; + case FIELD_ACCESS : + FieldAccess fax = (FieldAccess) jdtExpression; + Accessor axx = (Accessor) asRoasterExpression( fax.getExpression() ); + roasterExpr = axx.field( fax.getName().getIdentifier() ); + break; + case INSTANCEOF_EXPRESSION : + InstanceofExpression iof = (InstanceofExpression) jdtExpression; + roasterExpr = instanceOf( iof.getRightOperand().toString(), asRoasterExpression( iof.getLeftOperand() ) ); + break; + case THIS_EXPRESSION : + roasterExpr = thisLiteral(); + break; + case PREFIX_EXPRESSION: + PrefixExpression pref = (PrefixExpression) jdtExpression; + roasterExpr = operator( PrefixOp.build( pref.getOperator().toString() ), (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 new file mode 100644 index 00000000..aa3e595b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ExpressionImpl.java @@ -0,0 +1,34 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.NodeImpl; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class ExpressionImpl, + P extends ExpressionHolder, + E extends ExpressionSource, + J extends org.eclipse.jdt.core.dom.Expression> + extends NodeImpl + implements ExpressionSource { + + private ExpressionHolder parent; + + public ExpressionImpl() { } + + public ExpressionHolder getParent() { + return parent; + } + + public void wireParent( ExpressionHolder parent ) { + this.parent = parent; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/FieldImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/FieldImpl.java new file mode 100644 index 00000000..979ee4b8 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/FieldImpl.java @@ -0,0 +1,73 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.FieldAccess; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class FieldImpl, + P extends ExpressionHolder> + extends SimpleAccessorImpl,FieldAccess> + implements Field { + + private FieldAccess fld; + + private String fieldName; + private ExpressionSource,?> expression; + + public FieldImpl( String fieldName ) { + this.fieldName = fieldName; + } + + @Override + public FieldAccess materialize( AST ast ) { + if (isMaterialized()) { + return fld; + } + fld = ast.newFieldAccess(); + fld.setName( ast.newSimpleName( fieldName ) ); + if ( expression == null ) { + fld.setExpression( ast.newThisExpression() ); + } else { + fld.setExpression( wireAndGetExpression( expression, this, ast ) ); + } + return fld; + } + + @Override + public FieldAccess getInternal() { + return fld; + } + + @Override + public void setInternal( FieldAccess jdtNode) { + super.setInternal(jdtNode); + this.fld = jdtNode; + } + + @Override + public ExpressionSource,?> chainExpression( ExpressionSource child ) { + ExpressionSource,?> cast = ( ExpressionSource,?> ) child; + this.expression = cast; + return cast; + } + + @Override + public String getFieldName() { + return fieldName; + } + + @Override + public ExpressionSource, ?> 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 new file mode 100644 index 00000000..e20a2882 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/GetterImpl.java @@ -0,0 +1,72 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class GetterImpl, + P extends ExpressionHolder> + extends InvokeableImpl,MethodInvocation> + implements Getter { + + protected MethodInvocation invoke; + + private String fieldName; + + public GetterImpl( String fieldName, String type ) { + super( JDTHelper.getter( fieldName, type ) ); + this.fieldName = fieldName; + } + + @Override + public ExpressionSource, ?> getTarget() { + return target; + } + + @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(); + + invoke.setName(ast.newSimpleName(method)); + if ( target != null ) { + invoke.setExpression(wireAndGetExpression(target, this, ast)); + } + return invoke; + } + + @Override + public MethodInvocation getInternal() { + return invoke; + } + + @Override + public void setInternal(MethodInvocation jdtNode) { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } + + @Override + public String getFieldName() { + return fieldName; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InstanceofImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InstanceofImpl.java new file mode 100644 index 00000000..407f7044 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InstanceofImpl.java @@ -0,0 +1,65 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.InstanceofExpression; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class InstanceofImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,org.eclipse.jdt.core.dom.InstanceofExpression> + implements InstanceofExpression { + + private org.eclipse.jdt.core.dom.InstanceofExpression isA; + + private String typeName; + private ExpressionSource,?> expression; + + public InstanceofImpl( String klass, ExpressionSource expression ) { + this.typeName = klass; + this.expression = (ExpressionSource, ?>) expression; + } + + @Override + public org.eclipse.jdt.core.dom.InstanceofExpression materialize( AST ast ) { + if (isMaterialized()) { + return isA; + } + isA = ast.newInstanceofExpression(); + isA.setRightOperand( JDTHelper.getType( typeName, ast ) ); + if ( expression != null ) { + org.eclipse.jdt.core.dom.Expression expr = wireAndGetExpression( expression, this, ast ); + isA.setLeftOperand( expr ); + } + return isA; + } + + @Override + public org.eclipse.jdt.core.dom.InstanceofExpression getInternal() { + return isA; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.InstanceofExpression jdtNode) { + super.setInternal(jdtNode); + this.isA = jdtNode; + } + + public String getTypeName() { + return typeName; + } + + @Override + public ExpressionSource, ?> getExpression() { + return expression; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InvokeableImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InvokeableImpl.java new file mode 100644 index 00000000..3724903b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/InvokeableImpl.java @@ -0,0 +1,41 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class InvokeableImpl, + P extends ExpressionHolder, + E extends MethodInvokeExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends BaseInvokeableImpl + implements MethodInvokeExpression { + + protected ExpressionSource target; + + + public InvokeableImpl( String method ) { + super( method ); + } + + @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 new file mode 100644 index 00000000..771be4e3 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/JdtExpressionWrapper.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +public interface JdtExpressionWrapper, + P extends ExpressionHolder, + J extends org.eclipse.jdt.core.dom.Expression> + extends ASTNode { + + public org.eclipse.jdt.core.dom.Expression wireAndGetExpression( ExpressionSource expression, P parent, AST ast ); + +} \ No newline at end of file diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/LiteralImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/LiteralImpl.java new file mode 100644 index 00000000..d4a9f980 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/LiteralImpl.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.Expression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Literal; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class LiteralImpl, + P extends ExpressionHolder, + L extends Literal, + J extends Expression> + extends ExpressionImpl + implements Literal { + + public LiteralImpl() {} + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/MethodInvokeImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/MethodInvokeImpl.java new file mode 100644 index 00000000..6660bbb1 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/MethodInvokeImpl.java @@ -0,0 +1,67 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.jboss.forge.roaster.model.ExpressionHolder; +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> { + + protected MethodInvocation invoke; + + public MethodInvokeImpl( String method ) { + super( method ); + } + + @Override + public MethodCallExpression setMethodName( String name ) { + this.method = name; + return this; + } + + @Override + public MethodInvocation materialize( AST ast ) { + if (isMaterialized()) { + return invoke; + } + invoke = ast.newMethodInvocation(); + + invoke.setName(ast.newSimpleName(method)); + if ( target != null ) { + invoke.setExpression( wireAndGetExpression( target, this, ast ) ); + } + for ( Argument,?> arg : arguments ) { + invoke.arguments().add( wireAndGetExpression( arg, this, ast ) ); + } + return invoke; + } + + @Override + public MethodInvocation getInternal() { + return invoke; + } + + @Override + public void setInternal( MethodInvocation jdtNode ) { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NonPrimitiveExpressionImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NonPrimitiveExpressionImpl.java new file mode 100644 index 00000000..796b4493 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NonPrimitiveExpressionImpl.java @@ -0,0 +1,43 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.NonPrimitiveExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class NonPrimitiveExpressionImpl, + P extends ExpressionHolder, + E extends NonPrimitiveExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends ExpressionImpl + implements NonPrimitiveExpression, + JdtExpressionWrapper { + + private ExpressionHolder parent; + + public NonPrimitiveExpressionImpl() { } + + public ExpressionHolder getParent() { + return parent; + } + + public void linkParent( ExpressionHolder parent ) { + this.parent = parent; + } + + public org.eclipse.jdt.core.dom.Expression wireAndGetExpression( ExpressionSource expression, E parent, AST ast ) { + ASTNode node = (ASTNode) expression; + expression.setOrigin( parent ); + node.setAst( ast ); + return node.materialize( ast ); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NotImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NotImpl.java new file mode 100644 index 00000000..68066421 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NotImpl.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.PrefixOp; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class NotImpl, + P extends ExpressionHolder> + extends UnaryImpl { + + public NotImpl( 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 new file mode 100644 index 00000000..94cfb543 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NullLiteralImpl.java @@ -0,0 +1,47 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.NullLiteral; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class NullLiteralImpl, + P extends ExpressionHolder> + extends LiteralImpl,org.eclipse.jdt.core.dom.NullLiteral> + implements org.jboss.forge.roaster.model.expressions.NullLiteral { + + private org.eclipse.jdt.core.dom.NullLiteral literal; + + public NullLiteralImpl() {} + + @Override + public org.eclipse.jdt.core.dom.NullLiteral materialize( AST ast ) { + if (isMaterialized()) { + return literal; + } + literal = ast.newNullLiteral(); + return literal; + } + + @Override + public org.eclipse.jdt.core.dom.NullLiteral getInternal() { + return literal; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.NullLiteral jdtNode) { + super.setInternal(jdtNode); + this.literal = jdtNode; + } + + public Object getValue() { + return null; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NumberLiteralImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NumberLiteralImpl.java new file mode 100644 index 00000000..92e7b290 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/NumberLiteralImpl.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.NumberLiteral; +import org.jboss.forge.roaster.model.expressions.PrimitiveLiteral; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class NumberLiteralImpl, + P extends ExpressionHolder> + extends LiteralImpl,org.eclipse.jdt.core.dom.NumberLiteral> + implements NumberLiteral { + + private Number val; + + private org.eclipse.jdt.core.dom.NumberLiteral literal; + + public NumberLiteralImpl( Number val ) { + this.val = val; + } + + @Override + public org.eclipse.jdt.core.dom. NumberLiteral materialize( AST ast ) { + if (isMaterialized()) { + return literal; + } + literal = ast.newNumberLiteral( val.toString() ); + return literal; + } + + @Override + public org.eclipse.jdt.core.dom.NumberLiteral getInternal() { + return literal; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.NumberLiteral jdtNode) { + super.setInternal(jdtNode); + this.literal = jdtNode; + } + + public Number getValue() { + return val; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/OperatorImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/OperatorImpl.java new file mode 100644 index 00000000..ab42cb7d --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/OperatorImpl.java @@ -0,0 +1,108 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jdt.core.dom.InfixExpression; +import org.eclipse.jdt.core.dom.ParenthesizedExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.expressions.OperatorExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class OperatorImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,InfixExpression> + implements OperatorExpression { + + private InfixExpression expr; + + private Op operator; + private List,?>> argumentList = Collections.EMPTY_LIST; + + public OperatorImpl( Op op ) { + this.operator = op; + } + + @Override + public OperatorExpression addArgument( Argument arg ) { + if ( argumentList.isEmpty() ) { + argumentList = new LinkedList,?>>(); + } + argumentList.add( (Argument, ?>) arg ); + return this; + } + + public List,?>> getArguments() { + return Collections.unmodifiableList( argumentList ); + } + + public String getOperator() { + return operator.getOp(); + } + + @Override + public InfixExpression materialize( AST ast ) { + if (isMaterialized()) { + return expr; + } + expr = ast.newInfixExpression(); + + expr.setOperator( InfixExpression.Operator.toOperator( this.operator.getOp() ) ); + + for ( Argument,?> arg : argumentList ) { + wireArg( arg, ast ); + } + return expr; + } + + @Override + public InfixExpression getInternal() { + return expr; + } + + @Override + public void setInternal( InfixExpression jdtNode ) { + super.setInternal(jdtNode); + this.expr = jdtNode; + } + + protected void wireArg( Argument,?> arg, AST ast ) { + Expression child = wireAndGetExpression( arg, this, ast ); + + if ( child.getNodeType() == ASTNode.INFIX_EXPRESSION ) { + if ( ! expr.getOperator().equals( ((InfixExpression) child).getOperator() ) ) { + ParenthesizedExpression par = ast.newParenthesizedExpression(); + par.setExpression( child ); + child = par; + } + } + + InfixExpression infix = expr; + if ( "MISSING".equals( infix.getLeftOperand().toString() ) ) { + infix.setLeftOperand( child ); + } else if ( "MISSING".equals( infix.getRightOperand().toString() ) ) { + infix.setRightOperand( child ); + } else { + org.eclipse.jdt.core.dom.Expression prev = infix.getRightOperand(); + InfixExpression more = ast.newInfixExpression(); + more.setOperator( infix.getOperator() ); + infix.setRightOperand( more ); + prev.delete(); + more.setLeftOperand( prev ); + more.setRightOperand( child ); + } + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ParenImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ParenImpl.java new file mode 100644 index 00000000..ecb8a020 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/ParenImpl.java @@ -0,0 +1,68 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ParenthesizedExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.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 { + + ParenthesizedExpression paren; + + private ExpressionSource,?> inner; + + public ParenImpl( ExpressionSource child ) { + inner = (ExpressionSource, ?>) child; + } + + @Override + public ParenthesizedExpression materialize( AST ast ) { + if (isMaterialized()) { + return paren; + } + paren = ast.newParenthesizedExpression(); + + if ( inner != null ) { + paren.setExpression( wireAndGetExpression( inner, this, ast ) ); + } + return paren; + } + + @Override + public ParenthesizedExpression getInternal() { + return paren; + } + + @Override + public void setInternal(ParenthesizedExpression jdtNode) { + super.setInternal(jdtNode); + this.paren = jdtNode; + } + + @Override + public ExpressionSource, ?> getExpression() { + return inner; + } + + public org.eclipse.jdt.core.dom.Expression wireAndGetExpression( ExpressionSource,?> expression, ParenExpression parent, AST ast ) { + JdtExpressionWrapper,org.eclipse.jdt.core.dom.Expression> node = (JdtExpressionWrapper,org.eclipse.jdt.core.dom.Expression>) expression; + expression.setOrigin( parent ); + node.setAst( ast ); + return node.materialize( ast ); + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/PostFixImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/PostFixImpl.java new file mode 100644 index 00000000..4ac7f89a --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/PostFixImpl.java @@ -0,0 +1,69 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.PostfixExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.OrdinalArgument; +import org.jboss.forge.roaster.model.expressions.PostFixExpression; +import org.jboss.forge.roaster.model.expressions.PostfixOp; +import org.jboss.forge.roaster.model.expressions.UnaryExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class PostFixImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,PostfixExpression> + implements PostFixExpression { + + private PostfixExpression post; + + private PostfixOp op; + private OrdinalArgument,?> arg; + + public PostFixImpl( PostfixOp op, 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() ) ); + + if ( arg != null ) { + post.setOperand( wireAndGetExpression( arg, this, ast ) ); + } + return post; + } + + @Override + public PostfixExpression getInternal() { + return post; + } + + @Override + public void setInternal( PostfixExpression jdtNode ) { + super.setInternal(jdtNode); + this.post = jdtNode; + } + + @Override + public ExpressionSource, ?> getExpression() { + return arg; + } + + @Override + public String getOperator() { + return op.getOp(); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SelfArgumentImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SelfArgumentImpl.java new file mode 100644 index 00000000..f4770c49 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SelfArgumentImpl.java @@ -0,0 +1,50 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ThisExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.ThisLiteral; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class SelfArgumentImpl, + P extends ExpressionHolder> + extends AccessorImpl,ThisExpression> + implements ThisLiteral { + + public SelfArgumentImpl() {} + + private ThisExpression self; + + @Override + public ThisExpression materialize( AST ast ) { + if (isMaterialized()) { + return self; + } + self = ast.newThisExpression(); + return self; + } + + @Override + public ThisExpression getInternal() { + return self; + } + + @Override + public void setInternal(ThisExpression jdtNode) { + super.setInternal(jdtNode); + this.self = jdtNode; + } + + @Override + public ExpressionSource chainExpression( ExpressionSource child ) { + return (ExpressionSource) child; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SetterImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SetterImpl.java new file mode 100644 index 00000000..cc1fc5d8 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SetterImpl.java @@ -0,0 +1,71 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.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 { + + protected MethodInvocation invoke; + + 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 MethodInvocation materialize( AST ast ) { + if (isMaterialized()) { + return invoke; + } + invoke = ast.newMethodInvocation(); + + invoke.setName(ast.newSimpleName(method)); + + if ( target != null ) { + invoke.setExpression( wireAndGetExpression( target, this, ast ) ); + } + + for ( Argument,?> arg : arguments ) { + invoke.arguments().add( wireAndGetExpression( arg, this, ast ) ); + } + + return invoke; + } + + + @Override + public MethodInvocation getInternal() { + return invoke; + } + + @Override + public ExpressionSource, ?> getValue() { + if ( getArguments().isEmpty() ) { + return null; + } + return getArguments().get( 0 ); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SimpleAccessorImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SimpleAccessorImpl.java new file mode 100644 index 00000000..90120bc4 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SimpleAccessorImpl.java @@ -0,0 +1,33 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.Expression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.OrdinalArgument; +import org.jboss.forge.roaster.model.expressions.PostFixExpression; +import org.jboss.forge.roaster.model.expressions.PostfixOp; +import org.jboss.forge.roaster.model.source.JavaSource; + +public abstract class SimpleAccessorImpl, + P extends ExpressionHolder, + E extends OrdinalArgument, + J extends Expression> + extends AccessorImpl + implements OrdinalArgument { + + + public PostFixExpression> inc() { + return new PostFixImpl>( PostfixOp.INC, 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 new file mode 100644 index 00000000..e427ead9 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/StaticClassAccessorImpl.java @@ -0,0 +1,55 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.Name; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ClassLiteral; +import org.jboss.forge.roaster.model.expressions.ExpressionChainLink; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class StaticClassAccessorImpl, + P extends ExpressionHolder> + extends AccessorImpl,Name> + implements ExpressionChainLink { + + private Name klass; + + private String name; + + public StaticClassAccessorImpl( String name ) { + this.name = name; + } + + @Override + public Name materialize( AST ast ) { + if (isMaterialized()) { + return klass; + } + klass = ast.newName( name ); + return klass; + } + + @Override + public Name getInternal() { + return klass; + } + + @Override + public void setInternal(Name jdtNode) { + super.setInternal(jdtNode); + this.klass = jdtNode; + } + + @Override + public ExpressionSource chainExpression( ExpressionSource child ) { + throw new UnsupportedOperationException( "Should not be invoked on this" ); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/StringLiteralImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/StringLiteralImpl.java new file mode 100644 index 00000000..dc695759 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/StringLiteralImpl.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.StringLiteral; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class StringLiteralImpl, + P extends ExpressionHolder> + extends AccessorImpl,org.eclipse.jdt.core.dom.StringLiteral> + implements StringLiteral { + + private String val; + + private org.eclipse.jdt.core.dom.StringLiteral literal; + + public StringLiteralImpl( String val ) { + this.val = val; + } + + @Override + public org.eclipse.jdt.core.dom.StringLiteral materialize( AST ast ) { + if (isMaterialized()) { + return literal; + } + literal = ast.newStringLiteral(); + literal.setLiteralValue( val ); + return literal; + } + + @Override + public org.eclipse.jdt.core.dom.StringLiteral getInternal() { + return literal; + } + + @Override + public void setInternal( org.eclipse.jdt.core.dom.StringLiteral jdtNode ) { + super.setInternal( jdtNode ); + this.literal = jdtNode; + } + + public String getValue() { + return val; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperFieldImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperFieldImpl.java new file mode 100644 index 00000000..dd232a1b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperFieldImpl.java @@ -0,0 +1,67 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SuperFieldAccess; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class SuperFieldImpl, + P extends ExpressionHolder> + extends SimpleAccessorImpl,SuperFieldAccess> + implements Field { + + private SuperFieldAccess fld; + + private String fieldName; + private ExpressionSource,?> expression = new SuperImpl>(); + + public SuperFieldImpl( String fieldName ) { + this.fieldName = fieldName; + } + + @Override + public SuperFieldAccess materialize( AST ast ) { + if (isMaterialized()) { + return fld; + } + fld = ast.newSuperFieldAccess(); + fld.setName( ast.newSimpleName( fieldName ) ); + return fld; + } + + @Override + public SuperFieldAccess getInternal() { + return fld; + } + + @Override + public void setInternal(SuperFieldAccess jdtNode) { + super.setInternal(jdtNode); + this.fld = jdtNode; + } + + @Override + public ExpressionSource, ?> chainExpression( ExpressionSource child ) { + ExpressionSource,?> cast = ( ExpressionSource,?> ) child; + return cast; + } + + @Override + public String getFieldName() { + return fieldName; + } + + @Override + public ExpressionSource, ?> 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 new file mode 100644 index 00000000..12066cfa --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperGetterImpl.java @@ -0,0 +1,77 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SuperMethodInvocation; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.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 { + + protected SuperMethodInvocation invoke; + + private String fieldName; + + public SuperGetterImpl(String fieldName, String type) { + super(JDTHelper.getter(fieldName, type)); + target = new SuperImpl>(); + this.fieldName = fieldName; + } + + public ExpressionSource,?> chainExpression( ExpressionSource child ) { + ExpressionSource, ?> cast = (ExpressionSource, ?>) child; + setTarget( cast ); + return cast; + } + + + @Override + public Getter setTarget( ExpressionSource target) { + chainExpression( (ExpressionSource) target ); + return this; + } + + @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 getInternal() { + return invoke; + } + + @Override + public void setInternal(SuperMethodInvocation jdtNode) { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } + + @Override + public String getFieldName() { + return fieldName; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperImpl.java new file mode 100644 index 00000000..524af3a3 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperImpl.java @@ -0,0 +1,55 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.Setter; +import org.jboss.forge.roaster.model.expressions.Super; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.spi.ExpressionFactoryImpl; + +public class SuperImpl, + P extends ExpressionHolder> + extends ExpressionFactoryImpl,org.eclipse.jdt.core.dom.Expression> + implements Super { + + @Override + public Field> field( String fieldName ) { + return new SuperFieldImpl>( fieldName ); + + } + + @Override + public Getter> getter( String field, String klass ) { + return new SuperGetterImpl>( field, klass ); + } + + @Override + public Getter> getter( String field, Class klass ) { + return getter( field, klass.getName() ); + } + + @Override + public MethodCallExpression> invoke( String method ) { + return new SuperMethodInvokeImpl>( method ); + } + + @Override + public Setter> setter( String fldName, Class type, ExpressionSource value ) { + return setter( fldName, type.getName(), value ); + } + + @Override + public Setter> setter( String fldName, String type, ExpressionSource value ) { + return new SuperSetterImpl>( fldName, type, value ); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperMethodInvokeImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperMethodInvokeImpl.java new file mode 100644 index 00000000..dd9775ae --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperMethodInvokeImpl.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SuperMethodInvocation; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class SuperMethodInvokeImpl, + P extends ExpressionHolder> + extends AbstractMethodInvokeImpl,SuperMethodInvocation> + implements MethodCallExpression { + + protected SuperMethodInvocation invoke; + + public SuperMethodInvokeImpl( String method ) { + super( method ); + target = new SuperImpl>(); + } + + @Override + public SuperMethodInvocation materialize( AST ast ) { + if (isMaterialized()) { + return invoke; + } + invoke = ast.newSuperMethodInvocation(); + + invoke.setName( ast.newSimpleName( method ) ); + for ( Argument,?> arg : arguments ) { + invoke.arguments().add( wireAndGetExpression( arg, this, ast ) ); + } + return invoke; + } + + @Override + public SuperMethodInvocation getInternal() { + return invoke; + } + + @Override + public MethodCallExpression setMethodName( String name ) { + this.method = name; + return this; + } + + @Override + public void setInternal(SuperMethodInvocation jdtNode) { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperSetterImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperSetterImpl.java new file mode 100644 index 00000000..3359f16e --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/SuperSetterImpl.java @@ -0,0 +1,64 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.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 { + + private SuperMethodInvocation setter; + + private ExpressionSource,?> value; + + public SuperSetterImpl( String fieldName, String type, ExpressionSource value ) { + super( JDTHelper.setter( fieldName, type ) ); + this.value = (ExpressionSource, ?>) value; + } + + @Override + public SuperMethodInvocation materialize( AST ast ) { + if (isMaterialized()) { + return setter; + } + setter = ast.newSuperMethodInvocation(); + setter.setName( ast.newSimpleName( method ) ); + if ( value != null ) { + setter.arguments().add( wireAndGetExpression( value, this, ast ) ); + } + return setter; + } + + @Override + public SuperMethodInvocation getInternal() { + return setter; + } + + @Override + public void setInternal( SuperMethodInvocation jdtNode ) { + super.setInternal( jdtNode ); + this.setter = jdtNode; + } + + @Override + public ExpressionSource, ?> getValue() { + if ( getArguments().isEmpty() ) { + return null; + } + return getArguments().get( 0 ); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/TernaryImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/TernaryImpl.java new file mode 100644 index 00000000..b7caff17 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/TernaryImpl.java @@ -0,0 +1,92 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ConditionalExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.TernaryExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class TernaryImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,ConditionalExpression> + implements TernaryExpression { + + private ConditionalExpression ternary; + + private ExpressionSource,?> condition; + private ExpressionSource,?> onTrue; + private ExpressionSource,?> onFalse; + + public TernaryImpl() {} + + @Override + public TernaryExpression setCondition( ExpressionSource expression ) { + this.condition = (ExpressionSource, ?>) expression; + return this; + } + + @Override + public TernaryExpression setThenExpression( ExpressionSource onTrue ) { + this.onTrue = (ExpressionSource, ?>) onTrue; + return this; + } + + @Override + public TernaryExpression setElseExpression( ExpressionSource onFalse ) { + this.onFalse = (ExpressionSource, ?>) onFalse; + return this; + } + + @Override + public ExpressionSource,?> getCondition() { + return condition; + } + + @Override + public ExpressionSource,?> getThenExpression() { + return onTrue; + } + + @Override + public ExpressionSource,?> getElseExpression() { + return onFalse; + } + + @Override + public ConditionalExpression materialize( AST ast ) { + if (isMaterialized()) { + return ternary; + } + ternary = ast.newConditionalExpression(); + + if ( condition != null ) { + ternary.setExpression( wireAndGetExpression( condition, this, ast ) ); + } + if ( onTrue != null ) { + ternary.setThenExpression( wireAndGetExpression( onTrue, this, ast ) ); + } + if ( onFalse != null ) { + ternary.setElseExpression( wireAndGetExpression( onFalse, this, ast ) ); + } + return ternary; + } + + @Override + public ConditionalExpression getInternal() { + return ternary; + } + + @Override + public void setInternal(ConditionalExpression jdtNode) { + super.setInternal(jdtNode); + this.ternary = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/UnaryImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/UnaryImpl.java new file mode 100644 index 00000000..5da30ba9 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/UnaryImpl.java @@ -0,0 +1,68 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.PrefixExpression; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.PrefixOp; +import org.jboss.forge.roaster.model.expressions.UnaryExpression; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class UnaryImpl, + P extends ExpressionHolder> + extends NonPrimitiveExpressionImpl,PrefixExpression> + implements UnaryExpression { + + PrefixExpression expr; + + private Argument,?> arg; + private PrefixOp op; + + public UnaryImpl( PrefixOp op, 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() ) ); + + if ( arg != null ) { + expr.setOperand( wireAndGetExpression( arg, this, ast ) ); + } + return expr; + } + + @Override + public PrefixExpression getInternal() { + return expr; + } + + @Override + public void setInternal(PrefixExpression jdtNode) { + super.setInternal(jdtNode); + this.expr = jdtNode; + } + + @Override + public ExpressionSource, ?> getExpression() { + return arg; + } + + @Override + public String getOperator() { + return op.getOp(); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/VarArgumentImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/VarArgumentImpl.java new file mode 100644 index 00000000..d900fa1c --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/expressions/VarArgumentImpl.java @@ -0,0 +1,64 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.expressions; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.Name; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Variable; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class VarArgumentImpl, + P extends ExpressionHolder> + extends SimpleAccessorImpl,Name> + implements Variable { + + private Name var; + + private String name; + + private ExpressionSource,?> expression; + + public VarArgumentImpl( String name ) { + this.name = name; + } + + @Override + public Name materialize( AST ast ) { + if (isMaterialized()) { + return var; + } + var = ast.newName( name ); + return var; + } + + @Override + public Name getInternal() { + return var; + } + + @Override + public void setInternal(Name jdtNode) { + super.setInternal(jdtNode); + this.var = jdtNode; + } + + @Override + public String getName() { + return name; + } + + + @Override + public ExpressionSource,?> chainExpression( ExpressionSource child ) { + ExpressionSource,?> cast = ( ExpressionSource,?> ) child; + this.expression = cast; + return cast; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssertStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssertStatementImpl.java new file mode 100644 index 00000000..796c8415 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssertStatementImpl.java @@ -0,0 +1,78 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.AssertStatement; + +public class AssertStatementImpl, + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.AssertStatement> + implements AssertStatement { + + private org.eclipse.jdt.core.dom.AssertStatement assrt; + + private ExpressionSource,?> assertion; + private ExpressionSource,?> message; + + + @Override + public org.eclipse.jdt.core.dom.AssertStatement materialize( AST ast ) { + if (assrt != null) { + return assrt; + } + assrt = ast.newAssertStatement(); + if ( assertion != null ) { + assrt.setExpression( wireAndGetExpression( assertion, this, ast ) ); + } + if ( message != null ) { + assrt.setMessage( wireAndGetExpression( message, this, ast ) ); + } + return assrt; + } + + @Override + public ExpressionSource,?> getAssertion() { + return assertion; + } + + @Override + public AssertStatement setAssertion( ExpressionSource expression ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) expression; + this.assertion = cast; + cast.setOrigin( this ); + return this; + } + + @Override + public ExpressionSource,?> getMessage() { + return message; + } + + @Override + public AssertStatement setMessage( ExpressionSource msg ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) msg; + this.message = cast; + cast.setOrigin( this ); + return this; + } + + @Override + public org.eclipse.jdt.core.dom.AssertStatement getInternal() { + return assrt; + } + + @Override + public void setInternal( org.eclipse.jdt.core.dom.AssertStatement jdtNode ) { + super.setInternal(jdtNode); + this.assrt = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssignStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssignStatementImpl.java new file mode 100644 index 00000000..21a1c565 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/AssignStatementImpl.java @@ -0,0 +1,95 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ExpressionStatement; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.AssignExpression; +import org.jboss.forge.roaster.model.expressions.Assignment; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.expressions.AssignImpl; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.AssignmentStatement; + +public class AssignStatementImpl, + P extends BlockSource,P>> + extends StatementImpl,ExpressionStatement> + implements AssignmentStatement { + + private ExpressionStatement statement; + + private AssignExpression> inner; + + public AssignStatementImpl() { + this.inner = new AssignImpl>( Assignment.ASSIGN ); + } + + public AssignStatementImpl( AssignExpression axx ) { + this.inner = (AssignExpression>) axx; + } + + @Override + public AssignmentStatement setRight( ExpressionSource right ) { + inner.setRight( right ); + return this; + } + + @Override + public AssignmentStatement setLeft( Accessor left ) { + inner.setLeft( left ); + return this; + } + + @Override + public Accessor>, ?> getLeft() { + return inner.getLeft(); + } + + @Override + public ExpressionSource>, ?> getRight() { + return inner.getRight(); + } + + @Override + public String getAssignmentOperator() { + return inner.getOperator(); + } + + @Override + public AssignmentStatement setAssignmentOperator( Assignment op ) { + inner.setOperator( op ); + return this; + } + + public ExpressionStatement materialize( AST ast ) { + if (statement != null) { + return statement; + } + statement = ast.newExpressionStatement( wireAndGetExpression( inner, this, ast ) ); + return statement; + } + + @Override + public ExpressionStatement getInternal() { + return statement; + } + + @Override + public void setInternal( ExpressionStatement jdtNode ) { + super.setInternal(jdtNode); + this.statement = jdtNode; + } + + @Override + public ExpressionSource, ?> 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 new file mode 100644 index 00000000..231a5539 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BlockStatementImpl.java @@ -0,0 +1,37 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.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 { + + public BlockStatementImpl() { + } + + public BlockStatementImpl( StatementSource body ) { + addStatement( body ); + } + + @Override + public BlockStatement setLabel( String label ) { + this.label = label; + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BodiedStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BodiedStatementImpl.java new file mode 100644 index 00000000..6da8a155 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BodiedStatementImpl.java @@ -0,0 +1,58 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.LabeledStatement; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.BodyHolder; +import org.jboss.forge.roaster.model.statements.Statement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +public abstract class BodiedStatementImpl, + P extends BlockHolder, + S extends Statement & BodyHolder, + J extends org.eclipse.jdt.core.dom.Statement> + extends StatementImpl + implements BodyHolder, + JdtStatementWrapper { + + protected BodiedStatementImpl() { + } + + protected BlockStatement wrap( StatementSource body ) { + if ( body instanceof BlockStatement ) { + return (BlockStatement) body; + } else { + BlockStatement block = new BlockStatementImpl( body ); + return block; + } + } + + + public org.eclipse.jdt.core.dom.Statement wireAndGetStatement(Statement statement,S parent,AST ast) { + ASTNode node = (ASTNode) statement; + statement.setOrigin(parent); + node.setAst(ast); + org.eclipse.jdt.core.dom.Statement stat = node.materialize(ast); + + if ( statement.hasLabel() ) { + LabeledStatement labeledStatement = ast.newLabeledStatement(); + labeledStatement.setBody( stat ); + labeledStatement.setLabel( ast.newSimpleName( statement.getLabel() ) ); + return labeledStatement; + } else { + return stat; + } + } + + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BreakStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BreakStatementImpl.java new file mode 100644 index 00000000..993e6ed3 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/BreakStatementImpl.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BreakStatement; + +public class BreakStatementImpl, + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.BreakStatement> + implements BreakStatement { + + private org.eclipse.jdt.core.dom.BreakStatement ret; + + private String breakToLabel; + + public BreakStatementImpl() { } + + @Override + public org.eclipse.jdt.core.dom.BreakStatement materialize( AST ast ) { + if (ret != null) { + return ret; + } + ret = ast.newBreakStatement(); + if ( breakToLabel != null ) { + ret.setLabel( ast.newSimpleName( breakToLabel ) ); + } + return ret; + } + + @Override + public String getBreakToLabel() { + return breakToLabel; + } + + @Override + public BreakStatement setBreakToLabel( String label ) { + this.breakToLabel = label; + return this; + } + + @Override + public org.eclipse.jdt.core.dom.BreakStatement getInternal() { + return ret; + } + + @Override + public void setInternal( org.eclipse.jdt.core.dom.BreakStatement jdtNode ) { + super.setInternal(jdtNode); + this.ret = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ContinueStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ContinueStatementImpl.java new file mode 100644 index 00000000..48504d04 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ContinueStatementImpl.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.ContinueStatement; + +public class ContinueStatementImpl, + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.ContinueStatement> + implements ContinueStatement { + + private org.eclipse.jdt.core.dom.ContinueStatement ret; + + private String continueToLabel; + + public ContinueStatementImpl() { } + + @Override + public org.eclipse.jdt.core.dom.ContinueStatement materialize( AST ast ) { + if (ret != null) { + return ret; + } + ret = ast.newContinueStatement(); + if ( continueToLabel != null ) { + ret.setLabel( ast.newSimpleName( continueToLabel ) ); + } + return ret; + } + + @Override + public org.eclipse.jdt.core.dom.ContinueStatement getInternal() { + return ret; + } + + @Override + public void setInternal( org.eclipse.jdt.core.dom.ContinueStatement jdtNode ) { + super.setInternal(jdtNode); + this.ret = jdtNode; + } + + @Override + public String getContinueToLabel() { + return continueToLabel; + } + + @Override + public ContinueStatement setContinueToLabel( String label ) { + this.continueToLabel = label; + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DeclareStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DeclareStatementImpl.java new file mode 100644 index 00000000..e3b686d7 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DeclareStatementImpl.java @@ -0,0 +1,151 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.eclipse.jdt.core.dom.VariableDeclarationStatement; +import org.jboss.forge.roaster.model.expressions.BasicExpressionFactory; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.impl.expressions.BasicExpressionFactoryImpl; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.DeclareStatement; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DeclareStatementImpl, + P extends BlockHolder> + extends StatementImpl,VariableDeclarationStatement> + implements DeclareStatement { + + private VariableDeclarationStatement var; + + private BasicExpressionFactory> factory = new BasicExpressionFactoryImpl, org.eclipse.jdt.core.dom.Expression>(); + + private Map,?>> initializers = new HashMap, ?>>(); + private List names = new ArrayList(); + private String type; + + public DeclareStatementImpl() { } + + @Override + public List getVariableNames() { + return names; + } + + @Override + public String getVariableName() { + return names.get( 0 ); + } + + @Override + public String getVariableType() { + return type; + } + + @Override + public DeclareStatement setVariable( Class klass, String name ) { + return setVariable( klass.getName(), name ); + } + + @Override + public DeclareStatement setVariable( String klass, String name ) { + return setVariable( klass, name, null ); + } + + @Override + public DeclareStatement setVariable( Class klass, String name, ExpressionSource init ) { + return setVariable( klass.getName(), name, init ); + } + + @Override + public DeclareStatement setVariable( String klass, String name, ExpressionSource init ) { + this.type = klass; + names.add( name ); + if ( init != null ) { + initializers.put( name, (ExpressionSource, ?>) init ); + } + return this; + } + + @Override + public DeclareStatement addVariable( String name ) { + names.add( name ); + return this; + } + + @Override + public DeclareStatement addVariable( String name, ExpressionSource init ) { + addVariable( name ); + if ( init != null ) { + this.initializers.put( name, (ExpressionSource, ?>) init ); + } + return this; + } + + + @Override + public boolean isSingleDeclaration() { + return names.size() == 1; + } + + + @Override + public Map, ?>> getInitExpressions() { + return Collections.unmodifiableMap( initializers ); + } + + @Override + public ExpressionSource, ?> getInitExpression() { + return initializers.containsKey( getVariableName() ) ? initializers.get( getVariableName() ) : null; + } + + @Override + public VariableDeclarationStatement materialize( AST ast ) { + if (var != null) { + return var; + } + + List vdfs = new ArrayList( names.size() ); + for ( String name : names ) { + VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment(); + vdf.setName( ast.newSimpleName( name ) ); + if ( initializers.containsKey( name ) ) { + vdf.setInitializer( wireAndGetExpression( initializers.get( name ), this, ast ) ); + } + vdfs.add( vdf ); + } + + var = ast.newVariableDeclarationStatement( vdfs.get( 0 ) ); + if ( vdfs.size() > 1 ) { + for ( int j = 1; j < vdfs.size(); j++ ) { + var.fragments().add( vdfs.get( j ) ); + } + } + + var.setType( JDTHelper.getType( type, ast ) ); + return var; + } + + @Override + public VariableDeclarationStatement getInternal() { + return var; + } + + @Override + public void setInternal(VariableDeclarationStatement jdtNode) { + super.setInternal(jdtNode); + this.var = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DoWhileStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DoWhileStatementImpl.java new file mode 100644 index 00000000..7ac6ea3b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/DoWhileStatementImpl.java @@ -0,0 +1,88 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.DoStatement; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.DoWhileStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +public class DoWhileStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,DoStatement> + implements DoWhileStatement { + + private DoStatement rep; + + private BlockStatement> body; + private ExpressionSource,?> condition; + + public DoWhileStatementImpl() { } + + @Override + public BlockStatement> getBody() { + return body; + } + + @Override + public DoWhileStatement setBody( BlockSource body ) { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } + + @Override + public DoWhileStatement setBody( StatementSource statement ) { + this.body = wrap( statement ); + return this; + } + + @Override + public ExpressionSource, ?> getCondition() { + return condition; + } + + @Override + public DoWhileStatement setCondition( ExpressionSource expr ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) expr; + this.condition = cast; + return this; + } + + @Override + public DoStatement materialize( AST ast ) { + if (rep != null) { + return rep; + } + rep = ast.newDoStatement(); + + if ( body != null ) { + rep.setBody(wireAndGetStatement(body,this,ast)); + } + if ( condition != null ) { + rep.setExpression( wireAndGetExpression(condition,this,ast ) ); + } + return rep; + } + + @Override + public DoStatement getInternal() { + return rep; + } + + @Override + public void setInternal(DoStatement jdtNode) { + super.setInternal(jdtNode); + this.rep = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ExpressionStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ExpressionStatementImpl.java new file mode 100644 index 00000000..86f10a0f --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ExpressionStatementImpl.java @@ -0,0 +1,70 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.EvalExpressionStatement; + +public class ExpressionStatementImpl, + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.ExpressionStatement> + implements EvalExpressionStatement { + + private org.eclipse.jdt.core.dom.ExpressionStatement exprStatement; + + private ExpressionSource,?> expression; + + public ExpressionStatementImpl( ExpressionSource expr ) { + setExpr( expr ); + } + + + public ExpressionStatementImpl() { + } + + @Override + public org.eclipse.jdt.core.dom.ExpressionStatement materialize( AST ast ) { + if (isMaterialized()) { + return exprStatement; + } + exprStatement = ast.newExpressionStatement(wireAndGetExpression(expression,this,getAst())); + return exprStatement; + } + + @Override + public org.eclipse.jdt.core.dom.ExpressionStatement getInternal() { + return exprStatement; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.ExpressionStatement jdtNode) { + super.setInternal( jdtNode ); + this.exprStatement = jdtNode; + } + + @Override + public ExpressionSource, ?> getExpr() { + return expression; + } + + @Override + public EvalExpressionStatement setExpr( ExpressionSource expr ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) expr; + this.expression = cast; + cast.setOrigin( this ); + return this; + } + + @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 new file mode 100644 index 00000000..54205c4b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ForEachStatementImpl.java @@ -0,0 +1,117 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.EnhancedForStatement; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.ForEachStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +public class ForEachStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,EnhancedForStatement> + implements ForEachStatement { + + private EnhancedForStatement iter; + + private String name; + private String type; + private ExpressionSource,?> source; + private BlockStatement> body; + + public ForEachStatementImpl() { } + + @Override + public String getIteratorName() { + return name; + } + + @Override + public String getIteratorType() { + return type; + } + + @Override + public ForEachStatement setIterator(String klass, String name) { + this.name = name; + this.type = klass; + return this; + } + + @Override + public ForEachStatement setIterator( Class klass, String name ) { + return setIterator( klass.getName(), name ); + } + + @Override + public ExpressionSource, ?> getSource() { + return source; + } + + @Override + public ForEachStatement setSource( ExpressionSource expr ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) expr; + this.source = cast; + return this; + } + + @Override + public EnhancedForStatement materialize( AST ast ) { + if (iter != null) { + return iter; + } + iter = ast.newEnhancedForStatement(); + + iter.getParameter().setName(iter.getAST().newSimpleName(name)); + iter.getParameter().setType(JDTHelper.getType(type,iter.getAST())); + + if ( source != null ) { + iter.setExpression(wireAndGetExpression(source,this,ast)); + } + if ( body != null ) { + iter.setBody( wireAndGetStatement(body,this,ast) ); + } + return iter; + } + + @Override + public EnhancedForStatement getInternal() { + return iter; + } + + @Override + public void setInternal(EnhancedForStatement jdtNode) { + super.setInternal(jdtNode); + this.iter = jdtNode; + } + + @Override + public BlockStatement> getBody() { + return body; + } + + @Override + public ForEachStatement setBody(BlockSource body) { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } + + @Override + public ForEachStatement setBody(StatementSource statement ) { + this.body = wrap(statement); + return this; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ForStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ForStatementImpl.java new file mode 100644 index 00000000..44890f1b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ForStatementImpl.java @@ -0,0 +1,134 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.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; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.ForStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class ForStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,org.eclipse.jdt.core.dom.ForStatement> + implements ForStatement { + + private org.eclipse.jdt.core.dom.ForStatement iter; + + private DeclareExpression> declaration; + private List,?>> updaters = Collections.EMPTY_LIST; + private ExpressionSource,?> condition; + private BlockStatement> body; + + public ForStatementImpl() { } + + @Override + public DeclareExpression> getDeclaration() { + return declaration; + } + + @Override + public ForStatement setDeclaration( DeclareExpression declaration ) { + DeclareExpression> cast = (DeclareExpression>) declaration; + this.declaration = cast; + return this; + } + + @Override + public ExpressionSource, ?> getCondition() { + return condition; + } + + @Override + public ForStatement addUpdate( ExpressionSource expression ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) expression; + if ( updaters.isEmpty() ) { + updaters = new LinkedList,?>>(); + } + updaters.add( cast ); + return this; + } + + @Override + public ForStatement setCondition( ExpressionSource booleanExpression ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) booleanExpression; + this.condition = cast; + return this; + } + + @Override + public List, ?>> getUpdates() { + return Collections.unmodifiableList( updaters ); + } + + @Override + public BlockStatement> getBody() { + return body; + } + + @Override + public ForStatement setBody( BlockSource body ) { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } + + @Override + public ForStatement setBody( StatementSource statement ) { + this.body = wrap( statement ); + return this; + } + + @Override + public org.eclipse.jdt.core.dom.ForStatement materialize( AST ast ) { + if (iter != null) { + return iter; + } + + iter = ast.newForStatement(); + + if ( declaration != null ) { + iter.initializers().add( wireAndGetExpression( declaration, this, ast ) ); + } + + if ( condition != null ) { + iter.setExpression( wireAndGetExpression( condition, this, ast ) ); + } + + for ( ExpressionSource,?> updater : updaters ) { + iter.updaters().add( wireAndGetExpression( updater, this, ast ) ); + } + + if ( body != null ) { + iter.setBody( wireAndGetStatement( body, this, ast ) ); + } + + return iter; + } + + @Override + public org.eclipse.jdt.core.dom.ForStatement getInternal() { + return iter; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.ForStatement jdtNode) { + super.setInternal(jdtNode); + this.iter = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/IfStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/IfStatementImpl.java new file mode 100644 index 00000000..387c1002 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/IfStatementImpl.java @@ -0,0 +1,125 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.IfStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +public class IfStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,org.eclipse.jdt.core.dom.IfStatement> + implements IfStatement { + + private org.eclipse.jdt.core.dom.IfStatement iff; + + private ExpressionSource,?> condition; + private BlockStatement> thenBody; + private BlockStatement> elseBody; + + public IfStatementImpl() { } + + @Override + public ExpressionSource, ?> getCondition() { + return condition; + } + + @Override + public IfStatement setCondition( ExpressionSource condition ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) condition; + this.condition = cast; + return this; + } + + @Override + public BlockStatement> getThen() { + return thenBody; + } + + @Override + public IfStatement setThen(BlockSource body) { + BlockStatement> cast = (BlockStatement>) body; + this.thenBody = cast; + return this; + } + + @Override + public BlockStatement> getElse() { + return elseBody; + } + + @Override + public IfStatement setThen(StatementSource statement) { + this.thenBody = wrap(statement); + return this; + } + + @Override + public BlockStatement> getBody() { + return thenBody; + } + + @Override + public IfStatement setBody(BlockSource body) { + return setThen(body); + } + + @Override + public IfStatement setBody(StatementSource statement) { + return setThen(statement); + } + + @Override + public IfStatement setElse(BlockSource body) { + BlockStatement> cast = (BlockStatement>) body; + this.elseBody = cast; + return this; + } + + @Override + public IfStatement setElse(StatementSource statement) { + this.elseBody = wrap(statement); + return this; + } + + + @Override + public org.eclipse.jdt.core.dom.IfStatement materialize( AST ast ) { + if (iff != null) { + return iff; + } + iff = ast.newIfStatement(); + + if ( condition != null ) { + iff.setExpression(wireAndGetExpression(condition,this,ast)); + } + if ( thenBody != null ) { + iff.setThenStatement(wireAndGetStatement(thenBody,this,ast)); + } + if ( elseBody != null ) { + iff.setElseStatement(wireAndGetStatement(elseBody,this,ast)); + } + return iff; + } + + @Override + public org.eclipse.jdt.core.dom.IfStatement getInternal() { + return iff; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.IfStatement jdtNode) { + super.setInternal(jdtNode); + this.iff = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InnerBlockImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InnerBlockImpl.java new file mode 100644 index 00000000..2dddcac3 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InnerBlockImpl.java @@ -0,0 +1,106 @@ +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.LabeledStatement; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.BlockImpl; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.Statement; +import org.jboss.forge.roaster.model.statements.StatementSource; + +import java.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 ) ); + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InvokeStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InvokeStatementImpl.java new file mode 100644 index 00000000..a1dbe60b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/InvokeStatementImpl.java @@ -0,0 +1,110 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ExpressionStatement; +import org.jboss.forge.roaster.model.expressions.Accessor; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.ArgumentHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.expressions.MethodWithArgumentsInvokeExpression; +import org.jboss.forge.roaster.model.impl.expressions.MethodInvokeImpl; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.InvokeStatement; + +import java.util.Collections; +import java.util.List; + +public class InvokeStatementImpl, + P extends BlockHolder> + extends StatementImpl,ExpressionStatement> + implements InvokeStatement { + + private ExpressionStatement statement; + + private MethodInvokeExpression,?> inner; + + public InvokeStatementImpl() { + inner = new MethodInvokeImpl>( null ); + } + + public InvokeStatementImpl( MethodInvokeExpression expr ) { + this.inner = (MethodInvokeExpression,?>) expr; + } + + @Override + public String 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 new file mode 100644 index 00000000..e0a3da3b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/JdtStatementWrapper.java @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.Statement; + +public interface JdtStatementWrapper, + P extends BlockHolder, + J extends org.eclipse.jdt.core.dom.Statement> + extends ASTNode { + + // return type is not J because of LabeledStatements which wrap the actual Statement type + org.eclipse.jdt.core.dom.Statement wireAndGetStatement( Statement statement, + P parent, + AST ast ); +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ReturnStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ReturnStatementImpl.java new file mode 100644 index 00000000..d5f3967b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ReturnStatementImpl.java @@ -0,0 +1,61 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.ReturnStatement; + +public class ReturnStatementImpl, + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.ReturnStatement> + implements ReturnStatement { + + private org.eclipse.jdt.core.dom.ReturnStatement ret; + + private ExpressionSource,?> result; + + public ReturnStatementImpl() { } + + public ReturnStatement setReturn( ExpressionSource expression ) { + ExpressionSource,?> cast = (ExpressionSource,?>) expression; + result = cast; + cast.setOrigin( this ); + return this; + } + + public ExpressionSource,?> getReturn() { + return result; + } + + @Override + public org.eclipse.jdt.core.dom.ReturnStatement materialize( AST ast ) { + if (isMaterialized()) { + return ret; + } + ret = ast.newReturnStatement(); + + if ( result != null ) { + ret.setExpression( wireAndGetExpression( result, this, ast ) ); + } + return ret; + } + + @Override + public org.eclipse.jdt.core.dom.ReturnStatement getInternal() { + return ret; + } + + @Override + public void setInternal( org.eclipse.jdt.core.dom.ReturnStatement jdtNode ) { + super.setInternal(jdtNode); + this.ret = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementBuilder.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementBuilder.java new file mode 100644 index 00000000..64c40df8 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementBuilder.java @@ -0,0 +1,268 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AssertStatement; +import org.eclipse.jdt.core.dom.Block; +import org.eclipse.jdt.core.dom.BreakStatement; +import org.eclipse.jdt.core.dom.CatchClause; +import org.eclipse.jdt.core.dom.ConstructorInvocation; +import org.eclipse.jdt.core.dom.ContinueStatement; +import org.eclipse.jdt.core.dom.DoStatement; +import org.eclipse.jdt.core.dom.EnhancedForStatement; +import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jdt.core.dom.ExpressionStatement; +import org.eclipse.jdt.core.dom.ForStatement; +import org.eclipse.jdt.core.dom.IfStatement; +import org.eclipse.jdt.core.dom.LabeledStatement; +import org.eclipse.jdt.core.dom.ReturnStatement; +import org.eclipse.jdt.core.dom.Statement; +import org.eclipse.jdt.core.dom.SuperConstructorInvocation; +import org.eclipse.jdt.core.dom.SwitchCase; +import org.eclipse.jdt.core.dom.SwitchStatement; +import org.eclipse.jdt.core.dom.SynchronizedStatement; +import org.eclipse.jdt.core.dom.ThrowStatement; +import org.eclipse.jdt.core.dom.TryStatement; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.eclipse.jdt.core.dom.VariableDeclarationStatement; +import org.eclipse.jdt.core.dom.WhileStatement; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.expressions.AssignExpression; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.MethodInvokeExpression; +import org.jboss.forge.roaster.model.impl.BlockImpl; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.DeclareStatement; +import org.jboss.forge.roaster.model.statements.SuperStatement; +import org.jboss.forge.roaster.model.statements.ThisStatement; +import org.jboss.forge.roaster.model.statements.TryCatchStatement; + +import static org.eclipse.jdt.core.dom.ASTNode.ASSERT_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.BLOCK; +import static org.eclipse.jdt.core.dom.ASTNode.BREAK_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.CONSTRUCTOR_INVOCATION; +import static org.eclipse.jdt.core.dom.ASTNode.CONTINUE_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.DO_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.EMPTY_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.ENHANCED_FOR_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.EXPRESSION_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.FOR_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.IF_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.LABELED_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.RETURN_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.SUPER_CONSTRUCTOR_INVOCATION; +import static org.eclipse.jdt.core.dom.ASTNode.SWITCH_CASE; +import static org.eclipse.jdt.core.dom.ASTNode.SWITCH_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.SYNCHRONIZED_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.THROW_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.TRY_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.VARIABLE_DECLARATION_STATEMENT; +import static org.eclipse.jdt.core.dom.ASTNode.WHILE_STATEMENT; +import static org.jboss.forge.roaster.model.expressions.Expressions.declare; +import static org.jboss.forge.roaster.model.impl.expressions.ExpressionBuilder.asRoasterExpression; +import static org.jboss.forge.roaster.model.statements.Statements.newAssert; +import static org.jboss.forge.roaster.model.statements.Statements.newBlock; + +import static org.jboss.forge.roaster.model.statements.Statements.newBreak; +import static org.jboss.forge.roaster.model.statements.Statements.newCase; +import static org.jboss.forge.roaster.model.statements.Statements.newContinue; +import static org.jboss.forge.roaster.model.statements.Statements.newDeclare; +import static org.jboss.forge.roaster.model.statements.Statements.newDoWhile; +import static org.jboss.forge.roaster.model.statements.Statements.newFor; +import static org.jboss.forge.roaster.model.statements.Statements.newForEach; +import static org.jboss.forge.roaster.model.statements.Statements.newIf; +import static org.jboss.forge.roaster.model.statements.Statements.newReturn; +import static org.jboss.forge.roaster.model.statements.Statements.newSuper; +import static org.jboss.forge.roaster.model.statements.Statements.newSwitch; +import static org.jboss.forge.roaster.model.statements.Statements.newSynchronized; +import static org.jboss.forge.roaster.model.statements.Statements.newThis; +import static org.jboss.forge.roaster.model.statements.Statements.newThrow; +import static org.jboss.forge.roaster.model.statements.Statements.newTryCatch; +import static org.jboss.forge.roaster.model.statements.Statements.newWhile; + +public class StatementBuilder { + + public static org.jboss.forge.roaster.model.statements.StatementSource asRoasterStatement(Statement jdtStatement) + { + org.jboss.forge.roaster.model.statements.StatementSource roasterStmt = null; + switch (jdtStatement.getNodeType()) { + case RETURN_STATEMENT: + ReturnStatement returnStatement = (ReturnStatement) jdtStatement; + roasterStmt = newReturn() + .setReturn( asRoasterExpression( returnStatement.getExpression() ) ); + break; + case ASSERT_STATEMENT: + AssertStatement assertStatement = (AssertStatement) jdtStatement; + roasterStmt = newAssert() + .setAssertion( asRoasterExpression( assertStatement.getExpression() ) ) + .setMessage( asRoasterExpression( assertStatement.getMessage() ) ); + break; + case BLOCK: + Block block = (Block) jdtStatement; + roasterStmt = newBlock(); + ((BlockImpl) roasterStmt).setInternal( block ); + for ( Object o : block.statements() ) { + ((BlockStatement) roasterStmt).addStatement( StatementBuilder.asRoasterStatement( (Statement) o ) ); + } + break; + case VARIABLE_DECLARATION_STATEMENT: + VariableDeclarationStatement vds = (VariableDeclarationStatement) jdtStatement; + DeclareStatement declareStatement = newDeclare(); + for ( Object var : vds.fragments() ) { + VariableDeclarationFragment vdf = (VariableDeclarationFragment) var; + if ( vdf.getInitializer() != null ) { + declareStatement.setVariable( vds.getType().toString(), vdf.getName().toString(), asRoasterExpression( vdf.getInitializer() ) ); + } else { + declareStatement.setVariable( vds.getType().toString(), vdf.getName().toString() ); + } + } + roasterStmt = declareStatement; + break; + case EMPTY_STATEMENT: + roasterStmt = null; + break; + case EXPRESSION_STATEMENT : + ExpressionSource expr = asRoasterExpression( (( ExpressionStatement)jdtStatement).getExpression() ); + if ( expr instanceof MethodInvokeExpression ) { + roasterStmt = new InvokeStatementImpl( (MethodInvokeExpression) expr ); + } else if ( expr instanceof AssignExpression ) { + roasterStmt = new AssignStatementImpl( (AssignExpression) expr ); + } else { + roasterStmt = new ExpressionStatementImpl( expr ); + } + break; + case CONSTRUCTOR_INVOCATION : + ThisStatement call = newThis(); + ConstructorInvocation civ = (ConstructorInvocation) jdtStatement; + for ( Object o : civ.arguments() ) { + call.addArgument( (Argument) asRoasterExpression( (Expression) o ) ); + } + roasterStmt = call; + break; + case SUPER_CONSTRUCTOR_INVOCATION : + SuperStatement supCall = newSuper(); + SuperConstructorInvocation sup = (SuperConstructorInvocation) jdtStatement; + for ( Object o : sup.arguments() ) { + supCall.addArgument( (Argument) asRoasterExpression( (Expression) o ) ); + } + roasterStmt = supCall; + break; + case WHILE_STATEMENT : + WhileStatement whil = (WhileStatement) jdtStatement; + roasterStmt = newWhile() + .setCondition( asRoasterExpression( whil.getExpression() ) ) + .setBody( asRoasterStatement( whil.getBody() ) ); + + break; + case DO_STATEMENT : + DoStatement doWhil = (DoStatement) jdtStatement; + roasterStmt = newDoWhile() + .setCondition( asRoasterExpression( doWhil.getExpression() ) ) + .setBody( asRoasterStatement( doWhil.getBody() ) ); + + break; + case BREAK_STATEMENT: + BreakStatement brik = (BreakStatement) jdtStatement; + roasterStmt = brik.getLabel() != null ? newBreak().setBreakToLabel( brik.getLabel().toString() ) : newBreak(); + break; + case CONTINUE_STATEMENT: + ContinueStatement cont = (ContinueStatement) jdtStatement; + roasterStmt = newContinue().setContinueToLabel( cont.getLabel().toString() ); + break; + case LABELED_STATEMENT: + LabeledStatement lbl = (LabeledStatement) jdtStatement; + roasterStmt = asRoasterStatement( lbl.getBody() ); + roasterStmt.setLabel( lbl.getLabel().toString() ); + jdtStatement = ( (LabeledStatement) jdtStatement ).getBody(); + break; + case ENHANCED_FOR_STATEMENT: + EnhancedForStatement foreach = (EnhancedForStatement) jdtStatement; + roasterStmt = newForEach().setIterator( foreach.getParameter().getType().toString(), foreach.getParameter().getName().toString() ) + .setSource( asRoasterExpression( foreach.getExpression() ) ) + .setBody( asRoasterStatement( foreach.getBody() ) ); + break; + case TRY_STATEMENT: + TryStatement trial = (TryStatement) jdtStatement; + TryCatchStatement tryCatchStatement = newTryCatch() + .setBody( asRoasterStatement( trial.getBody() ) ) + .setFinally( asRoasterStatement( trial.getFinally() ) ); + for ( Object o : trial.catchClauses() ) { + CatchClause c = (CatchClause) o; + tryCatchStatement.addCatch( declare( c.getException().getType().toString(), c.getException().getName().toString() ), + asRoasterStatement( c.getBody() ) ); + } + roasterStmt = tryCatchStatement; + break; + case THROW_STATEMENT: + ThrowStatement trow = (ThrowStatement) jdtStatement; + roasterStmt = newThrow().setThrowable( asRoasterExpression( trow.getExpression() ) ); + break; + case IF_STATEMENT: + IfStatement iff = (IfStatement) jdtStatement; + org.jboss.forge.roaster.model.statements.IfStatement rstIf = newIf().setCondition( asRoasterExpression( iff.getExpression() ) ); + if ( iff.getThenStatement() != null ) { + rstIf.setThen( asRoasterStatement( iff.getThenStatement() ) ); + } + if ( iff.getElseStatement() != null ) { + rstIf.setElse( asRoasterStatement( iff.getElseStatement() ) ); + } + roasterStmt = rstIf; + break; + case FOR_STATEMENT: + ForStatement forst = (ForStatement) jdtStatement; + org.jboss.forge.roaster.model.statements.ForStatement rstFor = newFor(); + if ( forst.getExpression() != null ) { + rstFor.setCondition( asRoasterExpression( forst.getExpression() ) ); + } + if ( forst.getBody() != null ) { + rstFor.setBody( asRoasterStatement( forst.getBody() ) ); + } + for ( Object init : forst.initializers() ) { + rstFor.setDeclaration( (DeclareExpression) asRoasterExpression( (Expression) init ) ); + } + for ( Object updt : forst.updaters() ) { + rstFor.addUpdate( asRoasterExpression( (Expression) updt ) ); + } + roasterStmt = rstFor; + break; + case SYNCHRONIZED_STATEMENT: + SynchronizedStatement synch = (SynchronizedStatement) jdtStatement; + roasterStmt = newSynchronized().setSynchOn( asRoasterExpression( synch.getExpression() ) ) + .setBody( asRoasterStatement( synch.getBody() ) ); + break; + case SWITCH_STATEMENT: + SwitchStatement swicc = (SwitchStatement) jdtStatement; + org.jboss.forge.roaster.model.statements.SwitchStatement rstSwitch = newSwitch().setSwitch( asRoasterExpression( swicc.getExpression() ) ); + for ( Object o : swicc.statements() ) { + rstSwitch.addStatement( asRoasterStatement( (Statement) o ) ); + } + roasterStmt = rstSwitch; + break; + case SWITCH_CASE: + SwitchCase cas = (SwitchCase) jdtStatement; + if ( ! cas.isDefault() ) { + roasterStmt = newCase().setCaseExpression( asRoasterExpression( cas.getExpression() ) ); + } else { + roasterStmt = newCase(); + } + break; + default: + throw new UnsupportedOperationException(" Unknown statement type : " + jdtStatement.getNodeType() + " >> " + jdtStatement.toString() ); + } + if ( roasterStmt != null ) { + ( (org.jboss.forge.roaster.model.ASTNode) roasterStmt ).setInternal( jdtStatement ); + } + return roasterStmt; + } + + + +} + + diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementFactoryImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementFactoryImpl.java new file mode 100644 index 00000000..f7e24f8e --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementFactoryImpl.java @@ -0,0 +1,138 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.AssertStatement; +import org.jboss.forge.roaster.model.statements.AssignmentStatement; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.BreakStatement; +import org.jboss.forge.roaster.model.statements.EvalExpressionStatement; +import org.jboss.forge.roaster.model.statements.ContinueStatement; +import org.jboss.forge.roaster.model.statements.DeclareStatement; +import org.jboss.forge.roaster.model.statements.DoWhileStatement; +import org.jboss.forge.roaster.model.statements.ForEachStatement; +import org.jboss.forge.roaster.model.statements.ForStatement; +import org.jboss.forge.roaster.model.statements.IfStatement; +import org.jboss.forge.roaster.model.statements.InvokeStatement; +import org.jboss.forge.roaster.model.statements.ReturnStatement; +import org.jboss.forge.roaster.model.statements.SuperStatement; +import org.jboss.forge.roaster.model.statements.SwitchCaseStatement; +import org.jboss.forge.roaster.model.statements.SwitchStatement; +import org.jboss.forge.roaster.model.statements.SynchStatement; +import org.jboss.forge.roaster.model.statements.ThisStatement; +import org.jboss.forge.roaster.model.statements.ThrowStatement; +import org.jboss.forge.roaster.model.statements.TryCatchStatement; +import org.jboss.forge.roaster.model.statements.WhileStatement; +import org.jboss.forge.roaster.spi.StatementFactory; + +public class StatementFactoryImpl, + P extends BlockSource,P>> + implements StatementFactory { + + public StatementFactoryImpl() { } + + + public ReturnStatement newReturn() { + return new ReturnStatementImpl(); + } + + public AssignmentStatement newAssign() { + return new AssignStatementImpl(); + } + + public DeclareStatement newDeclare() { + return new DeclareStatementImpl(); + } + + public InvokeStatement newInvoke() { + return new InvokeStatementImpl(); + } + + public IfStatement newIf() { + return new IfStatementImpl(); + } + + public WhileStatement newWhile() { + return new WhileStatementImpl(); + } + + public DoWhileStatement newDoWhile() { + return new DoWhileStatementImpl(); + } + + public ForStatement newFor() { + return new ForStatementImpl(); + } + + public ForEachStatement newForEach() { + return new ForEachStatementImpl(); + } + + @Override + public BlockStatement newBlock() { + return new BlockStatementImpl(); + } + + @Override + public BreakStatement newBreak() { + return new BreakStatementImpl(); + } + + @Override + public ContinueStatement newContinue() { + return new ContinueStatementImpl(); + } + + @Override + public ThrowStatement newThrow() { + return new ThrowStatementImpl(); + } + + @Override + public TryCatchStatement newTryCatch() { + return new TryCatchStatementImpl(); + } + + @Override + public SynchStatement newSynchronized() { + return new SynchStatementImpl(); + } + + @Override + public ThisStatement newThis() { + return new ThisStatementImpl(); + } + + @Override + public SuperStatement newSuper() { + return new SuperStatementImpl(); + } + + @Override + public AssertStatement newAssert() { + return new AssertStatementImpl(); + } + + @Override + public SwitchStatement newSwitch() { + return new SwitchStatementImpl(); + } + + @Override + public EvalExpressionStatement newEval() { + return new ExpressionStatementImpl(); + } + + @Override + public SwitchCaseStatement newCase() { + return new SwitchCaseStatementImpl(); + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementImpl.java new file mode 100644 index 00000000..0420906d --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/StatementImpl.java @@ -0,0 +1,48 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.impl.NodeImpl; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.Statement; + +public abstract class StatementImpl, + P extends BlockHolder, + S extends Statement, + J extends org.eclipse.jdt.core.dom.Statement> + extends NodeImpl + implements Statement { + + protected StatementImpl() { + } + + @Override + public S setLabel( String label ) { + this.label = label; + return (S) this; + } + + protected > org.eclipse.jdt.core.dom.Expression wireAndGetExpression(ExpressionSource expression,X parent,AST ast) { + ASTNode node = (ASTNode) expression; + expression.setOrigin( parent ); + node.setAst( ast ); + return node.materialize( ast ); + } + + + @Override + public boolean isMaterialized() { + return getInternal() != null; + } + +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SuperStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SuperStatementImpl.java new file mode 100644 index 00000000..feb15973 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SuperStatementImpl.java @@ -0,0 +1,70 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SuperConstructorInvocation; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.SuperStatement; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class SuperStatementImpl, + P extends BlockHolder> + extends StatementImpl,SuperConstructorInvocation> + implements SuperStatement { + + private SuperConstructorInvocation invoke; + + private List,?>> argumentList = Collections.EMPTY_LIST; + + public SuperStatementImpl() { } + + @Override + public List,?>> getArguments() { + return Collections.unmodifiableList( argumentList ); + } + + @Override + public SuperStatement addArgument( Argument argument ) { + Argument,?> cast = (Argument, ?>) argument; + if ( argumentList.isEmpty() ) { + argumentList = new LinkedList,?>>(); + } + argumentList.add( cast ); + return this; + } + + @Override + public SuperConstructorInvocation materialize( AST ast ) { + if (invoke != null) { + return invoke; + } + invoke = ast.newSuperConstructorInvocation(); + + for ( Argument argument : argumentList ) { + invoke.arguments().add( wireAndGetExpression( argument, this, ast ) ); + } + return invoke; + } + + @Override + public SuperConstructorInvocation getInternal() { + return invoke; + } + + @Override + public void setInternal( SuperConstructorInvocation jdtNode ) { + super.setInternal( jdtNode ); + this.invoke = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchCaseStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchCaseStatementImpl.java new file mode 100644 index 00000000..23fe2f43 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchCaseStatementImpl.java @@ -0,0 +1,97 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SwitchCase; +import org.jboss.forge.roaster.model.ASTNode; +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.SwitchCaseStatement; +import org.jboss.forge.roaster.model.statements.SwitchStatement; + +public class SwitchCaseStatementImpl,P extends BlockHolder> + implements SwitchCaseStatement>, + ASTNode { + + private ExpressionSource>,?> option; + + private SwitchCase inner; + private AST ast; + + public SwitchCaseStatementImpl() {} + + public SwitchCaseStatementImpl setLabel( String label ) { return null; } + public String getLabel() { return null; } + public boolean hasLabel() { return false; } + + public void setOrigin( SwitchStatement origin ) {} + public SwitchStatement getOrigin() { return null; } + + @Override + public ExpressionSource>, ?> getCaseExpression() { + return option; + } + + @Override + public SwitchCaseStatement> setCaseExpression( ExpressionSource src ) { + this.option = (ExpressionSource>, ?>) src; + return this; + } + + @Override + public AST getAst() { + return null; + } + + @Override + public org.eclipse.jdt.core.dom.SwitchCase materialize( AST ast ) { + if ( inner != null ) { + return inner; + } + inner = ast.newSwitchCase(); + if ( option != null ) { + // case ... : + inner.setExpression( wireAndGetExpression( option, this, ast ) ); + } else { + // default: + inner.setExpression(null); + } + return inner; + } + + @Override + public org.eclipse.jdt.core.dom.SwitchCase getInternal() { + return inner; + } + + @Override + public void setInternal( org.eclipse.jdt.core.dom.SwitchCase jdtNode ) { + this.inner = jdtNode; + } + + @Override + public boolean isMaterialized() { + return inner != null; + } + + @Override + public void setAst( AST ast ) { + this.ast = ast; + } + + protected > org.eclipse.jdt.core.dom.Expression wireAndGetExpression(ExpressionSource expression,X parent,AST ast) { + ASTNode node = (ASTNode) expression; + expression.setOrigin( parent ); + node.setAst( ast ); + return node.materialize( ast ); + } + +} \ No newline at end of file diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchStatementImpl.java new file mode 100644 index 00000000..e1f0b6e6 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SwitchStatementImpl.java @@ -0,0 +1,148 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SwitchCase; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.Statement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.SwitchCaseStatement; +import org.jboss.forge.roaster.model.statements.SwitchStatement; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class SwitchStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,org.eclipse.jdt.core.dom.SwitchStatement> + implements SwitchStatement { + + private org.eclipse.jdt.core.dom.SwitchStatement opts; + + private ExpressionSource,?> expression; + private List,?>> statements = new LinkedList, ?>>(); + + public SwitchStatementImpl() { } + + + @Override + public org.eclipse.jdt.core.dom.SwitchStatement materialize( AST ast ) { + if (opts != null) { + return opts; + } + opts = ast.newSwitchStatement(); + opts.setExpression( wireAndGetExpression( expression, this, ast ) ); + + for ( Statement,?> stat : statements ) { + if ( SwitchCaseStatementImpl.class.isInstance( stat ) ) { + SwitchCaseStatementImpl mock = (SwitchCaseStatementImpl) stat; + mock.materialize( ast ); + opts.statements().add( mock.getInternal() ); + } else { + opts.statements().add( wireAndGetStatement( stat, this, ast ) ); + } + } + return opts; + } + + @Override + public org.eclipse.jdt.core.dom.SwitchStatement getInternal() { + return opts; + } + + @Override + public void setInternal( org.eclipse.jdt.core.dom.SwitchStatement jdtNode ) { + super.setInternal(jdtNode); + this.opts = jdtNode; + } + + @Override + public List, ?>> getCaseExpressions() { + List,?>> cases = new ArrayList, ?>>(); + for ( StatementSource,?> stat : statements ) { + if ( SwitchCaseStatementImpl.class.isInstance( stat ) ) { + cases.add( ( (SwitchCaseStatementImpl) stat ).getCaseExpression() ); + } + } + return cases; + } + + @Override + public SwitchStatement addCase( ExpressionSource option ) { + ExpressionSource,?> cast = (ExpressionSource,?>) option; + statements.add( new SwitchCaseStatementImpl().setCaseExpression( cast ) ); + return this; + } + + @Override + public SwitchStatement addCase( SwitchCaseStatement option ) { + statements.add( (StatementSource, ?>) option ); + return this; + } + + @Override + public List,?>> getStatements() { + return Collections.unmodifiableList( statements ); + } + + @Override + public SwitchStatement addDefault() { + statements.add( new SwitchCaseStatementImpl() ); + return this; + } + + @Override + public SwitchStatement addStatement( StatementSource arg ) { + StatementSource,?> cast = (StatementSource,?>) arg; + if ( statements.isEmpty() ) { + statements = new LinkedList, ?>>(); + } + statements.add( cast ); + return this; + } + + @Override + public ExpressionSource, ?> getSwitch() { + return this.expression; + } + + @Override + public org.jboss.forge.roaster.model.statements.SwitchStatement setSwitch( ExpressionSource expr ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) expr; + this.expression = cast; + return this; + } + + @Override + public BlockStatement> getBody() { + return null; + } + + @Override + public SwitchStatement setBody( BlockSource body ) { + addDefault(); + return addStatement( (BlockStatement) body ); + } + + @Override + public SwitchStatement setBody(StatementSource body) { + addDefault(); + return addStatement(wrap(body)); + } + + +} + + diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SynchStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SynchStatementImpl.java new file mode 100644 index 00000000..55bed127 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/SynchStatementImpl.java @@ -0,0 +1,88 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.SynchronizedStatement; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.SynchStatement; + +public class SynchStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,SynchronizedStatement> + implements SynchStatement { + + private SynchronizedStatement synch; + + private ExpressionSource,?> expr; + private BlockStatement> body; + + public SynchStatementImpl() { } + + @Override + public SynchronizedStatement materialize( AST ast ) { + if (synch != null) { + return synch; + } + synch = ast.newSynchronizedStatement(); + + if ( expr != null ) { + synch.setExpression( wireAndGetExpression( expr, this, ast ) ); + } + if ( body != null ) { + synch.setBody( (org.eclipse.jdt.core.dom.Block) wireAndGetStatement(body,this,ast)); + } + return synch; + } + + @Override + public SynchronizedStatement getInternal() { + return synch; + } + + @Override + public void setInternal(SynchronizedStatement jdtNode) { + super.setInternal(jdtNode); + this.synch = jdtNode; + } + + @Override + public ExpressionSource, ?> getSynchOn() { + return expr; + } + + @Override + public SynchStatement setSynchOn( ExpressionSource expr ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) expr; + this.expr = cast; + return this; + } + + @Override + public BlockStatement> getBody() { + return body; + } + + @Override + public SynchStatement setBody( BlockSource body ) { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } + + @Override + public SynchStatement setBody( StatementSource body ) { + this.body = wrap(body); + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThisStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThisStatementImpl.java new file mode 100644 index 00000000..9936de43 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThisStatementImpl.java @@ -0,0 +1,70 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ConstructorInvocation; +import org.jboss.forge.roaster.model.expressions.Argument; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.ThisStatement; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class ThisStatementImpl, + P extends BlockHolder> + extends StatementImpl,ConstructorInvocation> + implements ThisStatement { + + private ConstructorInvocation invoke; + + private List,?>> argumentList = Collections.EMPTY_LIST; + + public ThisStatementImpl() { } + + @Override + public List,?>> getArguments() { + return Collections.unmodifiableList( argumentList ); + } + + @Override + public ThisStatement addArgument( Argument argument ) { + Argument,?> cast = (Argument, ?>) argument; + if ( argumentList.isEmpty() ) { + argumentList = new LinkedList,?>>(); + } + argumentList.add( cast ); + return this; + } + + @Override + public ConstructorInvocation materialize( AST ast ) { + if (invoke != null) { + return invoke; + } + invoke = ast.newConstructorInvocation(); + + for ( Argument argument : argumentList ) { + invoke.arguments().add( wireAndGetExpression( argument, this, ast ) ); + } + return invoke; + } + + @Override + public ConstructorInvocation getInternal() { + return invoke; + } + + @Override + public void setInternal(ConstructorInvocation jdtNode) { + super.setInternal(jdtNode); + this.invoke = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThrowStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThrowStatementImpl.java new file mode 100644 index 00000000..b352b02b --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/ThrowStatementImpl.java @@ -0,0 +1,61 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.ThrowStatement; + +public class ThrowStatementImpl, + P extends BlockHolder> + extends StatementImpl,org.eclipse.jdt.core.dom.ThrowStatement> + implements ThrowStatement { + + private ExpressionSource,?> expression; + + private org.eclipse.jdt.core.dom.ThrowStatement throwStatement; + + public ThrowStatementImpl() {} + + @Override + public org.eclipse.jdt.core.dom.ThrowStatement materialize( AST ast ) { + if (throwStatement != null) { + return throwStatement; + } + throwStatement = ast.newThrowStatement(); + if ( expression != null ) { + throwStatement.setExpression( wireAndGetExpression( expression, this, getAst() ) ); + } + return throwStatement; + } + + @Override + public ExpressionSource, ?> getThrowable() { + return expression; + } + + @Override + public ThrowStatement setThrowable( ExpressionSource expr ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) expr; + this.expression = cast; + return this; + } + + @Override + public org.eclipse.jdt.core.dom.ThrowStatement getInternal() { + return throwStatement; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.ThrowStatement jdtNode) { + super.setInternal(jdtNode); + this.throwStatement = jdtNode; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/TryCatchStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/TryCatchStatementImpl.java new file mode 100644 index 00000000..8e1bd631 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/TryCatchStatementImpl.java @@ -0,0 +1,142 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.CatchClause; +import org.eclipse.jdt.core.dom.SingleVariableDeclaration; +import org.eclipse.jdt.core.dom.TryStatement; +import org.eclipse.jdt.core.dom.VariableDeclarationExpression; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.impl.JDTHelper; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.TryCatchStatement; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +public class TryCatchStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,TryStatement> + implements TryCatchStatement { + + private TryStatement tryCatch; + + private Map>,BlockStatement>> catches = Collections.EMPTY_MAP; + private BlockStatement> body; + private BlockStatement> fine; + + public TryCatchStatementImpl() { } + + @Override + public TryStatement materialize( AST ast ) { + if (tryCatch != null) { + return tryCatch; + } + tryCatch = ast.newTryStatement(); + + for ( DeclareExpression> declare : catches.keySet() ) { + CatchClause clause = ast.newCatchClause(); + clause.setException( varToSvd( (VariableDeclarationExpression) wireAndGetExpression( declare, this, ast ), ast ) ); + clause.setBody( (org.eclipse.jdt.core.dom.Block) wireAndGetStatement( catches.get( declare ), this, ast ) ); + tryCatch.catchClauses().add( clause ); + } + + if ( fine != null ) { + tryCatch.setFinally( (org.eclipse.jdt.core.dom.Block) wireAndGetStatement( fine, this, ast ) ); + } + if ( body != null ) { + tryCatch.setBody( (org.eclipse.jdt.core.dom.Block) wireAndGetStatement( body, this, ast ) ); + } + return tryCatch; + } + + @Override + public TryStatement getInternal() { + return tryCatch; + } + + @Override + public void setInternal(TryStatement jdtNode) { + super.setInternal(jdtNode); + this.tryCatch = jdtNode; + } + + private SingleVariableDeclaration varToSvd( VariableDeclarationExpression var, AST ast ) { + SingleVariableDeclaration svd = ast.newSingleVariableDeclaration(); + VariableDeclarationFragment frag = (VariableDeclarationFragment) var.fragments().get( 0 ); + svd.setName( ast.newSimpleName( frag.getName().getIdentifier() ) ); + svd.setType( JDTHelper.getType( var.getType().toString(), ast ) ); + return svd; + } + + @Override + public Map>, BlockStatement>> getCatches() { + return Collections.unmodifiableMap( catches ); + } + + @Override + public TryCatchStatement addCatch( DeclareExpression declaration, + BlockStatement block ) { + if ( catches.isEmpty() ) { + catches = new LinkedHashMap>, BlockStatement>>(); + } + DeclareExpression> castKey = (DeclareExpression>) declaration; + BlockStatement> castValue = (BlockStatement>) block; + catches.put( castKey, castValue ); + return this; + } + + @Override + public BlockStatement> getFinally() { + return fine; + } + + @Override + public TryCatchStatement addCatch( DeclareExpression declaration, + StatementSource block ) { + return addCatch(declaration, wrap(block)); + } + + @Override + public BlockStatement> getBody() { + return body; + } + + @Override + public TryCatchStatement setBody( BlockSource body ) { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } + + @Override + public TryCatchStatement setBody( StatementSource body ) { + this.body = wrap(body); + return this; + } + + @Override + public TryCatchStatement setFinally( BlockStatement block ) { + BlockStatement> cast = (BlockStatement>) block; + this.fine = cast; + return this; + } + + @Override + public TryCatchStatement setFinally( StatementSource block ) { + this.fine = wrap(block); + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/WhileStatementImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/WhileStatementImpl.java new file mode 100644 index 00000000..bccc772f --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/statements/WhileStatementImpl.java @@ -0,0 +1,87 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.model.impl.statements; + +import org.eclipse.jdt.core.dom.AST; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.source.BlockHolder; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.WhileStatement; + +public class WhileStatementImpl, + P extends BlockHolder> + extends BodiedStatementImpl,org.eclipse.jdt.core.dom.WhileStatement> + implements WhileStatement { + + private org.eclipse.jdt.core.dom.WhileStatement rep; + + private BlockStatement> body; + private ExpressionSource,?> condition; + + public WhileStatementImpl() { } + + @Override + public ExpressionSource, ?> getCondition() { + return condition; + } + + @Override + public WhileStatement setCondition( ExpressionSource expr ) { + ExpressionSource,?> cast = (ExpressionSource, ?>) expr; + this.condition = cast; + return this; + } + + @Override + public org.eclipse.jdt.core.dom.WhileStatement materialize( AST ast ) { + if (rep != null) { + return rep; + } + rep = ast.newWhileStatement(); + + if ( body != null ) { + rep.setBody( wireAndGetStatement( body, this, ast ) ); + } + if ( condition != null ) { + rep.setExpression( wireAndGetExpression( condition, this, ast ) ); + } + return rep; + } + + @Override + public org.eclipse.jdt.core.dom.WhileStatement getInternal() { + return rep; + } + + @Override + public void setInternal(org.eclipse.jdt.core.dom.WhileStatement jdtNode) { + super.setInternal(jdtNode); + this.rep = jdtNode; + } + + @Override + public BlockStatement> getBody() { + return body; + } + + @Override + public WhileStatement setBody( BlockSource body ) { + BlockStatement> cast = (BlockStatement>) body; + this.body = cast; + return this; + } + + @Override + public WhileStatement setBody( StatementSource body ) { + this.body = wrap( body ); + return this; + } +} diff --git a/impl/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactoryImpl.java b/impl/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactoryImpl.java new file mode 100644 index 00000000..5162f289 --- /dev/null +++ b/impl/src/main/java/org/jboss/forge/roaster/spi/ExpressionFactoryImpl.java @@ -0,0 +1,71 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.roaster.spi; + +import org.jboss.forge.roaster.model.ExpressionHolder; +import org.jboss.forge.roaster.model.expressions.ArrayIndexer; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.NonPrimitiveExpression; +import org.jboss.forge.roaster.model.expressions.Setter; +import org.jboss.forge.roaster.model.impl.expressions.ArrayAccessImpl; +import org.jboss.forge.roaster.model.impl.expressions.BasicExpressionFactoryImpl; +import org.jboss.forge.roaster.model.impl.expressions.FieldImpl; +import org.jboss.forge.roaster.model.impl.expressions.GetterImpl; +import org.jboss.forge.roaster.model.impl.expressions.MethodInvokeImpl; +import org.jboss.forge.roaster.model.impl.expressions.SetterImpl; +import org.jboss.forge.roaster.model.source.JavaSource; + +public class ExpressionFactoryImpl, + P extends ExpressionHolder, + E extends NonPrimitiveExpression, + J extends org.eclipse.jdt.core.dom.Expression> + extends BasicExpressionFactoryImpl + implements ExpressionFactory { + + + public ExpressionFactoryImpl() { } + + @Override + public Field field( String fieldName ) { + return new FieldImpl( fieldName ); + } + + @Override + public Getter getter( String fieldName, String type ) { + return new GetterImpl( fieldName, type ); + } + + @Override + public Getter getter( String fieldName, Class type ) { + return getter( fieldName, type.getName() ); + } + + @Override + public Setter setter( String fldName, String type, ExpressionSource value ) { + return new SetterImpl( fldName, type, value ); + } + + @Override + public Setter setter( String fldName, Class type, ExpressionSource value ) { + return setter( fldName, type.getName(), value ); + } + + @Override + public MethodCallExpression invoke( String method ) { + return new MethodInvokeImpl( method ); + } + + + @Override + public ArrayIndexer itemAt( ExpressionSource index ) { + return new ArrayAccessImpl( index ); + } +} diff --git a/impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.ExpressionFactory b/impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.ExpressionFactory new file mode 100644 index 00000000..b21e171f --- /dev/null +++ b/impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.ExpressionFactory @@ -0,0 +1 @@ +org.jboss.forge.roaster.spi.ExpressionFactoryImpl \ No newline at end of file diff --git a/impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.StatementFactory b/impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.StatementFactory new file mode 100644 index 00000000..bd4c0503 --- /dev/null +++ b/impl/src/main/resources/META-INF/services/org.jboss.forge.roaster.spi.StatementFactory @@ -0,0 +1 @@ +org.jboss.forge.roaster.model.impl.statements.StatementFactoryImpl \ No newline at end of file diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ConstructorStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ConstructorStatementsTest.java new file mode 100644 index 00000000..ae32d9f6 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ConstructorStatementsTest.java @@ -0,0 +1,45 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.jboss.forge.roaster.model.expressions.Expressions.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; +import static org.junit.Assert.assertEquals; + + +public class ConstructorStatementsTest +{ + + @Test + public void testEmptyConstructor() throws Exception + { + String target = "this(x);"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public Foo()" ); + + method.setBody( newThis().addArgument( var( "x" ) ) ); + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testSuperConstructor() throws Exception + { + String target = "super(x);"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public Foo()" ); + + method.setBody( newSuper().addArgument( var( "x" ) ) ); + assertEquals( target, method.getBody().trim() ); + } + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ControlFlowStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ControlFlowStatementsTest.java new file mode 100644 index 00000000..3b2aab35 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ControlFlowStatementsTest.java @@ -0,0 +1,192 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.jboss.forge.roaster.model.expressions.Expressions.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; +import static org.jboss.forge.roaster.model.statements.Statements.newAssert; +import static org.jboss.forge.roaster.model.statements.Statements.newAssign; +import static org.jboss.forge.roaster.model.statements.Statements.newBlock; +import static org.jboss.forge.roaster.model.statements.Statements.newBreak; +import static org.jboss.forge.roaster.model.statements.Statements.newContinue; +import static org.jboss.forge.roaster.model.statements.Statements.newIf; +import static org.jboss.forge.roaster.model.statements.Statements.newInvoke; +import static org.jboss.forge.roaster.model.statements.Statements.newReturn; +import static org.jboss.forge.roaster.model.statements.Statements.newSwitch; +import static org.jboss.forge.roaster.model.statements.Statements.newSynchronized; +import static org.jboss.forge.roaster.model.statements.Statements.newThrow; +import static org.jboss.forge.roaster.model.statements.Statements.newTryCatch; +import static org.jboss.forge.roaster.model.statements.Statements.newWhile; +import static org.junit.Assert.assertEquals; + + +public class ControlFlowStatementsTest +{ + @Test + public void testSimpleLabeledStatement() throws Exception + { + String target = "Out: return x;"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newReturn().setLabel( "Out" ).setReturn( var( "x" ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testBreak() throws Exception + { + String target = "if (true) {\n break;\n}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newBlock() + .addStatement( newIf().setCondition( literal( true ) ).setThen( newBreak() ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testContinue() throws Exception + { + String target = "while (true) {\n continue;\n}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newBlock() + .addStatement( newWhile().setCondition( literal( true ) ).setBody( newContinue() ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testBreakWithLabel() throws Exception + { + String target = "while (true) {\n break exit;\n}\n exit: return x;"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newBlock() + .addStatement( newWhile().setCondition( literal( true ) ).setBody( + newBreak().setBreakToLabel( "exit" ) ) ) + .addStatement( newReturn().setReturn( var( "x" ) ).setLabel( "exit" ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testContinueWithLabel() throws Exception + { + String target = "while (true) {\n test: x=x + 1;\n continue test;\n}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newBlock() + .addStatement( newWhile().setCondition( literal( true ) ).setBody( + newBlock() + .addStatement( + newAssign().setLeft( var( "x" ) ).setRight( operator( Op.PLUS ).addArgument( var( "x" ) ).addArgument( literal( 1 ) ) ) + .setLabel( "test" ) ) + .addStatement( newContinue().setContinueToLabel( "test" ) ) + ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + + @Test + public void testThrowable() throws Exception + { + String target = "throw new java.lang.UnsupportedOperationException(\"sorry\");"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newThrow().setThrowable( construct( UnsupportedOperationException.class ).addArgument( literal( "sorry" ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testTryCatch() throws Exception + { + String target = "try {\n" + + " x.foo();\n" + + "}\n catch (java.lang.NullPointerException npe) {\n" + + " npe.printStackTrace();\n" + + "}\ncatch (java.lang.IllegalStateException ise) {\n" + + " ise.printStackTrace();\n" + + "}\n finally {\n" + + " return;\n" + + "}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newTryCatch() + .setBody( newInvoke().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()" ); + + method.setBody( newSynchronized().setSynchOn( thisLiteral() ).setBody( newBlock().addStatement( var( "x" ).inc() ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testSwitchCase() throws Exception + { + String target = "switch (x) {\n" + + "case 0:\n" + + " return true;\n" + + "case 1:\n" + + "break;\n" + + "case 2:\n" + + "default :\n" + + "return false;\n" + + "}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newSwitch().setSwitch( var( "x" ) ) + .addCase( literal( 0 ) ).addStatement( newReturn().setReturn( literal( true ) ) ) + .addCase( literal( 1 ) ).addStatement( newBreak() ) + .addCase( literal( 2 ) ) + .addDefault() + .addStatement( newReturn().setReturn( literal( false ) ) ) + ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testAssertion() throws Exception + { + String target = "assert x > 0 : \"x GT 0\";"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newAssert() + .setAssertion( operator( Op.GREATER ).addArgument( var( "x" ) ).addArgument( literal( 0 ) ) ) + .setMessage( literal( "x GT 0" ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/DeclareStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/DeclareStatementsTest.java new file mode 100644 index 00000000..86627ab6 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/DeclareStatementsTest.java @@ -0,0 +1,108 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +import static org.jboss.forge.roaster.model.statements.Statements.*; +import static org.jboss.forge.roaster.model.expressions.Expressions.*; + + +public class DeclareStatementsTest +{ + + @Test + public void testDeclare() throws Exception + { + String target = "java.lang.Integer y=new java.lang.Integer();"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo( String x )" ); + + method.setBody( newDeclare().setVariable( Integer.class, "y", construct( Integer.class ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testDeclarePrimitive() throws Exception + { + String target = "int y;"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo( String x )" ); + + method.setBody( newDeclare().setVariable( int.class, "y" ) ); + + assertEquals( target, method.getBody().trim() ); + } + + + @Test + public void testDeclareAndInit() throws Exception + { + String target = "java.lang.Integer y=0;"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo( String x )" ); + + method.setBody( newDeclare().setVariable( Integer.class, "y", literal( 0 ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testDeclareManyAndInit() throws Exception + { + String target = "int x, y=0, z=3, w;"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo( String x )" ); + + method.setBody( newDeclare().setVariable( int.class, "x" ).addVariable( "y", literal( 0 ) ).addVariable( "z", literal( 3 ) ).addVariable( "w" ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testSimpleAssignment() throws Exception + { + String target = "this.name=x;"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo( String x )" ); + + method.setBody( newAssign().setLeft( field( "name" ) ).setRight( var( "x" ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testAssignmentExpr() throws Exception + { + String target = "this.name=this.name + \" Doe\";"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo( String x )" ); + + method.setBody( newAssign() + .setLeft( field( "name" ) ) + .setRight( operator( Op.PLUS ).addArgument( field( "name" ) ).addArgument( literal( " Doe" ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testDeclareWithGenerics() throws Exception + { + String target = "java.util.Map> y=new java.util.HashMap>();"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo( String x )" ); + + method.setBody( newDeclare() + .setVariable( "java.util.Map>", + "y", + construct( "java.util.HashMap>" ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ForStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ForStatementsTest.java new file mode 100644 index 00000000..e96a8fb6 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ForStatementsTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Assignment; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +import static org.jboss.forge.roaster.model.statements.Statements.*; +import static org.jboss.forge.roaster.model.expressions.Expressions.*; + +public class ForStatementsTest +{ + + @Test + public void testSimpleForWithPrint() throws Exception + { + + String target = "for (int j=0, k=0; j < 100; j++, k=k + 2) {\n java.lang.System.out.println(j);\n}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newFor() + .setDeclaration( declare( int.class, "j", literal( 0 ) ).addDeclaration( "k", literal( 0 ) ) ) + .setCondition( operator( Op.LESS ) + .addArgument( var( "j" ) ) + .addArgument( literal( 100 ) ) ) + .addUpdate( var( "j" ).inc() ) + .addUpdate( assign( Assignment.ASSIGN ) + .setLeft( var( "k" ) ) + .setRight( operator( Op.PLUS ) + .addArgument( var( "k" ) ) + .addArgument( literal( 2 ) ) ) ) + .setBody( newInvoke() + .setMethod( "println" ) + .setTarget( classStatic( System.class ).field( "out" ) ) + .addArgument( var( "j" ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testSimpleForEachWithPrint() throws Exception + { + + String target = "for (java.lang.String name : p.getNames()) {\n java.lang.System.out.println(name);\n}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newForEach() + .setIterator( String.class, "name" ) + .setSource( var( "p" ).getter( "names", String.class ) ) + .setBody( newInvoke() + .setTarget( classStatic( System.class ).field( "out" ) ) + .setMethod( "println" ) + .addArgument( var( "name" ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/IfStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/IfStatementsTest.java new file mode 100644 index 00000000..a6cd9a97 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/IfStatementsTest.java @@ -0,0 +1,95 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.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() ); + } + + @Test + public void testIfWithDotAccessor() throws Exception + { + String target = "if (this.x != y.z) {\n return false;\n}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newIf() + .setCondition( operator( Op.NOT_EQUALS ) + .addArgument( thisLiteral().field( "x" ) ) + .addArgument( var( "y" ).field( "z" ) ) ) + .setThen( newReturn().setReturn( literal( false ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testIfWithNegation() throws Exception + { + String target = "if (!x) {\n return false;\n}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newIf() + .setCondition( not( var( "x" ) ) ) + .setThen( newReturn().setReturn( literal( false ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testIfWithDoubleNegation() throws Exception + { + String target = "if (!!x) {\n return false;\n}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newIf() + .setCondition( not( not( var( "x" ) ) ) ) + .setThen( newReturn().setReturn( literal( false ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testIfWithInstanceof() throws Exception + { + String target = "if (x instanceof Foo) {\n return false;\n}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newIf() + .setCondition( instanceOf( "Foo", var( "x" ) ) ) + .setThen( newReturn().setReturn( literal( false ) ) ) ); + assertEquals( target, method.getBody().trim() ); + } + + + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/InvokeStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/InvokeStatementsTest.java new file mode 100644 index 00000000..70802987 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/InvokeStatementsTest.java @@ -0,0 +1,151 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.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()" ); + + method.setBody( newInvoke().setMethod( "random" ).setTarget( classStatic( Math.class ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testInvokeWithArg() throws Exception + { + String target = "java.lang.Math.round(2.4);"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo()" ); + + method.setBody( newInvoke().setTarget( classStatic( Math.class ) ).setMethod( "round" ).addArgument( literal( 2.4f ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testInvokeWithArgs() throws Exception + { + String target = "java.lang.Math.min(3,4);"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo()" ); + + method.setBody( newInvoke().addArgument( literal( 3 ) ).addArgument( literal( 4 ) ).setTarget( classStatic( Math.class ) ).setMethod( "min" ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testInvokeWithArgsRevOrder() throws Exception + { + String target = "java.lang.Math.min(3,4);"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo()" ); + + method.setBody( newInvoke().addArgument( literal( 3 ) ).addArgument( literal( 4 ) ).setTarget( classStatic( Math.class ) ).setMethod( "min" ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testNestedInvoke() throws Exception + { + String target = "java.lang.Math.min(java.lang.Math.max(java.lang.Math.round(3,4),6),4);"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo()" ); + + method.setBody( newInvoke().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() ); + } + + @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 ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testInvokeOnThis() throws Exception + { + String target = "this.toString();"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo()" ); + + method.setBody( newInvoke().setTarget( thisLiteral() ).setMethod( "toString" ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testInvokeOnLongChain() throws Exception + { + String target = "this.getName().foo().bar().baz.toString();"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo()" ); + + method.setBody( newInvoke().setMethod( "toString" ) + .setTarget( + thisLiteral().getter( "name", "String" ).invoke( "foo" ).dot().invoke( "bar" ).dot().field( "baz" ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testEval() { + String target = "3 + 4;"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo()" ); + + method.setBody( newEval().setExpr( operator( Op.PLUS ).addArgument( literal( 3 ) ).addArgument( literal( 4 ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testNewInstanceWithMethod() { + String target = "new java.lang.Integer(42).toString();"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo()" ); + + method.setBody( newEval().setExpr( construct( Integer.class ).addArgument( literal( 42 ) ).invoke( "toString" ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testNewInstance() { + String target = "new java.lang.Integer();"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void foo()" ); + + method.setBody( newEval().setExpr( construct( Integer.class ) ) ); + + assertEquals( target, method.getBody().trim() ); + } +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ModifyMethodBodyTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ModifyMethodBodyTest.java new file mode 100644 index 00000000..4cb6b88b --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ModifyMethodBodyTest.java @@ -0,0 +1,68 @@ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.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; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.jboss.forge.roaster.model.statements.InvokeStatement; +import org.jboss.forge.roaster.model.statements.ReturnStatement; +import org.jboss.forge.roaster.model.statements.Statement; +import org.junit.Test; + +import java.util.List; + +import static org.jboss.forge.roaster.model.expressions.Expressions.classStatic; +import static org.jboss.forge.roaster.model.expressions.Expressions.var; +import static org.jboss.forge.roaster.model.statements.Statements.newInvoke; +import static org.jboss.forge.roaster.model.statements.Statements.newReturn; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +public class ModifyMethodBodyTest { + + @Test + public void testReturnArg() throws Exception + { + String target = "return x;"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public String echo( String x )" ); + + method.setBody( newReturn().setReturn( var( "x" ) ) ); + + assertEquals( target, method.getBody().trim() ); + + BlockSource body = method.getBodyAsBlock(); + assertEquals( 1, body.getStatements().size() ); + assertTrue( body.getStatements().get( 0 ) instanceof ReturnStatement ); + } + + @Test + public void testModifySource() { + String source = "public class Bean {\n" + + "public String echo(String x) {\n" + + " return x;\n" + + "}\n" + + "}"; + + JavaClassSource javaClassSource = Roaster.parse(JavaClass.class, source).asJavaClassSource(); + List> methods = javaClassSource.getMethods(); + assertEquals(1, methods.size()); + MethodSource method = methods.get(0); + + BlockSource body = method.getBodyAsBlock(); + body.addStatement(0, newInvoke().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 ); + + assertTrue( ((JdtStatementWrapper)body).getInternal().toString().contains( "System.out.println" ) ); + } +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/RebuildStatementTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/RebuildStatementTest.java new file mode 100644 index 00000000..b38eaa87 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/RebuildStatementTest.java @@ -0,0 +1,1130 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ + +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.JavaClass; +import org.jboss.forge.roaster.model.expressions.ArrayConstructorExpression; +import org.jboss.forge.roaster.model.expressions.ArrayIndexer; +import org.jboss.forge.roaster.model.expressions.ArrayInit; +import org.jboss.forge.roaster.model.expressions.BooleanLiteral; +import org.jboss.forge.roaster.model.expressions.CastExpression; +import org.jboss.forge.roaster.model.expressions.CharacterLiteral; +import org.jboss.forge.roaster.model.expressions.ConstructorExpression; +import org.jboss.forge.roaster.model.expressions.DeclareExpression; +import org.jboss.forge.roaster.model.expressions.ExpressionSource; +import org.jboss.forge.roaster.model.expressions.Field; +import org.jboss.forge.roaster.model.expressions.Getter; +import org.jboss.forge.roaster.model.expressions.InstanceofExpression; +import org.jboss.forge.roaster.model.expressions.MethodCallExpression; +import org.jboss.forge.roaster.model.expressions.NullLiteral; +import org.jboss.forge.roaster.model.expressions.NumberLiteral; +import org.jboss.forge.roaster.model.expressions.OperatorExpression; +import org.jboss.forge.roaster.model.expressions.ParenExpression; +import org.jboss.forge.roaster.model.expressions.PostFixExpression; +import org.jboss.forge.roaster.model.expressions.Setter; +import org.jboss.forge.roaster.model.expressions.StringLiteral; +import org.jboss.forge.roaster.model.expressions.Super; +import org.jboss.forge.roaster.model.expressions.TernaryExpression; +import org.jboss.forge.roaster.model.expressions.ThisLiteral; +import org.jboss.forge.roaster.model.expressions.UnaryExpression; +import org.jboss.forge.roaster.model.expressions.Variable; +import org.jboss.forge.roaster.model.impl.expressions.StringLiteralImpl; +import org.jboss.forge.roaster.model.source.BlockSource; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.jboss.forge.roaster.model.statements.AssertStatement; +import org.jboss.forge.roaster.model.statements.AssignmentStatement; +import org.jboss.forge.roaster.model.statements.BlockStatement; +import org.jboss.forge.roaster.model.statements.BreakStatement; +import org.jboss.forge.roaster.model.statements.ContinueStatement; +import org.jboss.forge.roaster.model.statements.DeclareStatement; +import org.jboss.forge.roaster.model.statements.DoWhileStatement; +import org.jboss.forge.roaster.model.statements.ExpressionStatement; +import org.jboss.forge.roaster.model.statements.ForEachStatement; +import org.jboss.forge.roaster.model.statements.ForStatement; +import org.jboss.forge.roaster.model.statements.IfStatement; +import org.jboss.forge.roaster.model.statements.InvokeStatement; +import org.jboss.forge.roaster.model.statements.ReturnStatement; +import org.jboss.forge.roaster.model.statements.StatementSource; +import org.jboss.forge.roaster.model.statements.SuperStatement; +import org.jboss.forge.roaster.model.statements.SwitchCaseStatement; +import org.jboss.forge.roaster.model.statements.SwitchStatement; +import org.jboss.forge.roaster.model.statements.SynchStatement; +import org.jboss.forge.roaster.model.statements.ThisStatement; +import org.jboss.forge.roaster.model.statements.ThrowStatement; +import org.jboss.forge.roaster.model.statements.TryCatchStatement; +import org.jboss.forge.roaster.model.statements.WhileStatement; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class RebuildStatementTest { + + @Test + public void testRebuildReturnStatement() { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return x; " + + "} " + + "}"; + List statements = parseSource( source ); + + assertEquals(1, statements.size()); + assertTrue( statements.get( 0 ) instanceof ReturnStatement ); + assertEquals( "x", ((Variable) ((ReturnStatement) statements.get(0)).getReturn()).getName() ); + } + + @Test + public void testRebuildReturnStatementBoolean() { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return true; " + + "} " + + "}"; + List statements = parseSource( source ); + + assertEquals(1, statements.size()); + assertTrue( statements.get( 0 ) instanceof ReturnStatement ); + assertTrue( ( (ReturnStatement) statements.get( 0 ) ).getReturn() instanceof BooleanLiteral ); + } + + @Test + public void testRebuildReturnStatementWithCast() { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return (my.own.Klass) foo; " + + "} " + + "}"; + List statements = parseSource( source ); + + assertEquals(1, statements.size()); + assertTrue( statements.get( 0 ) instanceof ReturnStatement ); + assertTrue( ( (ReturnStatement) statements.get( 0 ) ).getReturn() instanceof CastExpression ); + CastExpression cast = (CastExpression) ( (ReturnStatement) statements.get( 0 ) ).getReturn(); + assertEquals( "my.own.Klass", cast.getType() ); + assertTrue( cast.getExpression() instanceof Variable ); + } + + @Test + public void testRebuildReturnNull() { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return null; " + + "} " + + "}"; + List statements = parseSource( source ); + + assertEquals(1, statements.size()); + assertTrue( statements.get( 0 ) instanceof ReturnStatement ); + assertTrue( ( (ReturnStatement) statements.get( 0 ) ).getReturn() instanceof NullLiteral ); + } + + @Test + public void testRebuildReturnWithInstanceOf() { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return x instanceof Test; " + + "} " + + "}"; + List statements = parseSource( source ); + + assertEquals(1, statements.size()); + assertTrue( statements.get( 0 ) instanceof ReturnStatement ); + assertTrue( ( (ReturnStatement) statements.get( 0 ) ).getReturn() instanceof InstanceofExpression ); + InstanceofExpression iof = (InstanceofExpression) ( (ReturnStatement) statements.get( 0 ) ).getReturn(); + assertEquals( "Test", iof.getTypeName() ); + assertTrue( iof.getExpression() instanceof Variable ); + } + + @Test + public void testRebuildReturnWithPrefix() { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return ++x; " + + "} " + + "}"; + List statements = parseSource( source ); + + assertEquals(1, statements.size()); + assertTrue( statements.get( 0 ) instanceof ReturnStatement ); + assertTrue( ( (ReturnStatement) statements.get( 0 ) ).getReturn() instanceof UnaryExpression ); + UnaryExpression una = (UnaryExpression) ( (ReturnStatement) statements.get( 0 ) ).getReturn(); + assertTrue( una.getExpression() instanceof Variable ); + assertEquals( "++", una.getOperator() ); + } + + @Test + public void testRebuildReturnWithPostfix() { + String source = "public class Bean { " + + "public String echo(String x) { " + + " return x--; " + + "} " + + "}"; + List statements = parseSource( source ); + + assertEquals(1, statements.size()); + assertTrue( statements.get( 0 ) instanceof ReturnStatement ); + assertTrue( ( (ReturnStatement) statements.get( 0 ) ).getReturn() instanceof UnaryExpression ); + PostFixExpression una = (PostFixExpression) ( (ReturnStatement) statements.get( 0 ) ).getReturn(); + assertTrue( una.getExpression() instanceof Variable ); + assertEquals( "--", una.getOperator() ); + } + + @Test + public void testRebuildVariableDeclarations() { + String source = "public class Bean { " + + "public String echo(String x) { " + + " int y = 1; " + + " String z; " + + " Object q; " + + " my.pack.Foo foo; " + + "" + + " return z; " + + "} " + + "}"; + List statements = parseSource( source ); + + String[] names = new String[] { "y", "z", "q", "foo" }; + String[] types = new String[] { "int", "String", "Object", "my.pack.Foo" }; + + assertEquals( 5, statements.size() ); + for ( int j = 0; j < 4; j++ ) { + assertTrue( statements.get( j ) instanceof DeclareStatement ); + DeclareStatement decl = ( (DeclareStatement) statements.get( j ) ); + assertEquals( names[j], decl.getVariableName() ); + assertEquals( types[j], decl.getVariableType() ); + } + } + + @Test + public void testRebuildSuperField() { + String source = "public class Bean { " + + "public String echo(String x) { " + + "" + + " return super.z; " + + "} " + + "}"; + List statements = parseSource( source ); + assertEquals(1, statements.size()); + assertTrue( statements.get( 0 ) instanceof ReturnStatement ); + assertTrue( ( (ReturnStatement) statements.get( 0 ) ).getReturn() instanceof Field ); + Field f = (Field) ( (ReturnStatement) statements.get( 0 ) ).getReturn(); + assertEquals( "z", f.getFieldName() ); + assertTrue( f.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 new file mode 100644 index 00000000..e3295c13 --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/ReturnStatementsTest.java @@ -0,0 +1,262 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.expressions.PrefixOp; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.JavaSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import java.util.UUID; + +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()" ); + + method.setBody( newReturn() ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testReturnArg() throws Exception + { + String target = "return x;"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public String echo( String x )" ); + + method.setBody( newReturn().setReturn( var( "x" ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testReturnExpr() throws Exception + { + String target = "return x + 5;"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public String echo( String x )" ); + + method.setBody( newReturn().setReturn( operator( Op.PLUS ).addArgument( var( "x" ) ).addArgument( literal( 5 ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testReturnInvoke() throws Exception + { + String target = "return java.util.UUID.randomUUID();"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public String foo()" ); + + method.setBody( newReturn().setReturn( invoke( "randomUUID" ).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" ) ) ); + + 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() ); + } + + @Test + public void testReturnArray() throws Exception + { + String target = "return new int[2][3];"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public int[][] foo()" ); + + method.setBody( newReturn().setReturn( newArray( int.class ).addDimension( literal( 2 ) ).addDimension( literal( 3 ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testReturnArrayWithImpliedInit() throws Exception + { + String target = "return new int[][]{{1,2},{3,4}};"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public int[][] foo()" ); + + method.setBody( newReturn().setReturn( newArray( int.class ).init( + vec().addElement( + vec().addElement( literal( 1 ) ).addElement( literal( 2 ) ) ) + .addElement( + vec().addElement( literal( 3 ) ).addElement( literal( 4 ) ) + ) + ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testReturnArrayAccess() throws Exception + { + String target = "return x[4][2].getY();"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public int[][] foo()" ); + + method.setBody( newReturn().setReturn( + var( "x" ).itemAt( literal( 4 ) ).itemAt( literal( 2 ) ).getter( "y", String.class ) + ) ); + + assertEquals( target, method.getBody().trim() ); + } + + + @Test + public void testReturnChain2() throws Exception + { + String target = "return this.foo.getBar().getRes();"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public String foo()" ); + + method.setBody( newReturn().setReturn( field( "foo" ).getter( "bar", String.class.getName() ).invoke( "getRes" ) ) ); + + assertEquals( target, method.getBody().trim() ); + } +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/SuperTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/SuperTest.java new file mode 100644 index 00000000..e6d749ba --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/SuperTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import 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; + +public class SuperTest +{ + @Test + public void testSuperField() throws Exception + { + String target = "return super.x;"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newReturn().setReturn( sup().field( "x" ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testSuperGetter() throws Exception + { + String target = "return super.getX();"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newReturn().setReturn( sup().getter( "x", String.class ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testSuperSetter() throws Exception + { + String target = "return super.setX(y);"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newReturn().setReturn( sup().setter( "x", String.class, var( "y" ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testSuperInvoke() throws Exception + { + String target = "return super.foo(x);"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newReturn().setReturn( sup().invoke( "foo" ).addArgument( var( "x" ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + +} diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/WhileStatementsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/WhileStatementsTest.java new file mode 100644 index 00000000..3c567a9f --- /dev/null +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/statements/WhileStatementsTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2014 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Eclipse Public License version 1.0, available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.jboss.forge.test.roaster.model.statements; + +import org.jboss.forge.roaster.Roaster; +import org.jboss.forge.roaster.model.expressions.Op; +import org.jboss.forge.roaster.model.source.JavaClassSource; +import org.jboss.forge.roaster.model.source.MethodSource; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +import static org.jboss.forge.roaster.model.statements.Statements.*; +import static org.jboss.forge.roaster.model.expressions.Expressions.*; + + +public class WhileStatementsTest +{ + @Test + public void testSimpleWhile() throws Exception + { + String target = "while (x < 0) {\n x=x + 1;\n}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newWhile() + .setCondition( operator( Op.LESS ) + .addArgument( var( "x" ) ) + .addArgument( literal( 0 ) ) ) + .setBody( newAssign() + .setLeft( var( "x" ) ) + .setRight( operator( Op.PLUS ).addArgument( var( "x" ) ).addArgument( literal( 1 ) ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + @Test + public void testSimpleWhile2() throws Exception + { + String target = "while (x < 0) {\n x=x + 1;\n}"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newWhile() + .setBody( newAssign() + .setLeft( var( "x" ) ) + .setRight( operator( Op.PLUS ).addArgument( var( "x" ) ).addArgument( literal( 1 ) ) ) ) + .setCondition( operator( Op.LESS ) + .addArgument( var( "x" ) ) + .addArgument( literal( 0 ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + + @Test + public void testSimpleDoWhile() throws Exception + { + String target = "do {\n x=x + 1;\n}\n while (x < 0);"; + MethodSource method = Roaster.create( JavaClassSource.class ).addMethod( "public void hello()" ); + + method.setBody( newDoWhile() + .setCondition( operator( Op.LESS ) + .addArgument( var( "x" ) ) + .addArgument( literal( 0 ) ) ) + .setBody( newAssign() + .setLeft( var( "x" ) ) + .setRight( operator( Op.PLUS ).addArgument( var( "x" ) ).addArgument( literal( 1 ) ) ) ) ); + + assertEquals( target, method.getBody().trim() ); + } + + +}