Package asteroid.utils
Class ExpressionUtils
- java.lang.Object
-
- asteroid.utils.ExpressionUtils
-
public final class ExpressionUtils extends Object
Utility classes to deal withExpressioninstances- Since:
- 0.3.0
- See Also:
Utils
-
-
Constructor Summary
Constructors Constructor Description ExpressionUtils()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BinaryExpressionasBinaryExpr(Expression expr)Unsafe cast of a given @{link Expression} to aBinaryExpression<T> TasBinaryExpr(Expression expr, T defaultValue, Closure<T> func)Executes a given computation if the expression is of typeBinaryExpressionif not, then a default value is provided<U extends Expression>
UgetArgumentByIndexAs(ArgumentListExpression args, Integer index, Class<U> asType)Return the element in the specified index from theArgumentListExpressionpassed as argument as the expected type.<U extends Expression>
UgetArgumentByIndexAs(MethodCallExpression methodCallExpr, Integer index, Class<U> asType)Return the element in the specified index from theArgumentListExpressionpassed as argument as the expected type.ArgumentListExpressiongetArgumentList(MethodCallExpression methodCallExpr)Given aMethodCallExpressionit returns a list of arguments<U extends Expression>
UgetFirstArgumentAs(ArgumentListExpression args, Class<U> asType)Return the first element of theArgumentListExpressionpassed as parameters as the expected type.<U extends Expression>
UgetFirstArgumentAs(MethodCallExpression methodCallExpr, Class<U> asType)Return the first element of theArgumentListExpressionpassed as parameters as the expected type.<U extends Expression>
UgetLastArgumentAs(ArgumentListExpression args, Class<U> asType)Return the last element of theArgumentListExpressionpassed as parameters as the expected type.<U extends Expression>
UgetLastArgumentAs(MethodCallExpression methodCallExpr, Class<U> asType)Return the last element of theArgumentListExpressionpassed as parameters as the expected type.StringgetText(Expression expression)Extracts the String value of a given expressionbooleanhasArguments(MethodCallExpression methodCallExpr)Given aMethodCallExpressionit returns whether it has arguments or notbooleanisBinaryExpr(Expression expr)Checks whether an expression is of typeBinaryExpressionbooleanisBinaryExpr(Expression expr, int operator)Checks whether an expression is of typeBinaryExpressionusing a specific binary operatorbooleanisListExpr(Expression expr)Checks whether an expression is of typeListExpressionbooleanisMethodCallExpr(Expression expr)Checks whether an expression is of typeMethodCallExpression<T> TleftExprAs(BinaryExpression binaryExpr, Class<T> type)From aBinaryExpressiongets the left expression as if it were of a given type<T> TrightExprAs(BinaryExpression binaryExpr, Class<T> type)From aBinaryExpressiongets the right expression as if it were of a given type
-
-
-
Method Detail
-
hasArguments
public boolean hasArguments(MethodCallExpression methodCallExpr)
Given aMethodCallExpressionit returns whether it has arguments or not- Parameters:
methodCallExpr- a method call we want the arguments from- Returns:
- true if the method has any argument false otherwise
- Since:
- 0.3.0
-
getArgumentList
public ArgumentListExpression getArgumentList(MethodCallExpression methodCallExpr)
Given aMethodCallExpressionit returns a list of arguments- Parameters:
methodCallExpr- a method call we want the arguments from- Returns:
- a list of expressions within a
ArgumentListExpression - Since:
- 0.1.4
-
getFirstArgumentAs
public <U extends Expression> U getFirstArgumentAs(MethodCallExpression methodCallExpr, Class<U> asType)
Return the first element of theArgumentListExpressionpassed as parameters as the expected type.- Parameters:
methodCallExpr- a method call we want the arguments fromasType- the expected type- Returns:
- the first argument casted as the expected type
- Since:
- 0.3.0
-
getFirstArgumentAs
public <U extends Expression> U getFirstArgumentAs(ArgumentListExpression args, Class<U> asType)
Return the first element of theArgumentListExpressionpassed as parameters as the expected type.- Parameters:
args- the list of argumentsasType- the expected type- Returns:
- the first argument casted as the expected type
- Since:
- 0.1.4
-
getArgumentByIndexAs
public <U extends Expression> U getArgumentByIndexAs(MethodCallExpression methodCallExpr, Integer index, Class<U> asType)
Return the element in the specified index from theArgumentListExpressionpassed as argument as the expected type.- Parameters:
methodCallExpr- a method call we want the arguments fromindex- the index where the expression is locatedasType- theClassof the expected expression- Returns:
- the expression found in the expression list using the provided index or null if it couldn't be found
- Since:
- 0.3.0
-
getArgumentByIndexAs
public <U extends Expression> U getArgumentByIndexAs(ArgumentListExpression args, Integer index, Class<U> asType)
Return the element in the specified index from theArgumentListExpressionpassed as argument as the expected type.- Parameters:
args- the argument list expression we want the argument fromindex- the index where the expression is locatedasType- theClassof the expected expression- Returns:
- the expression found in the expression list using the provided index or null if it couldn't be found
- Since:
- 0.2.8
-
getLastArgumentAs
public <U extends Expression> U getLastArgumentAs(MethodCallExpression methodCallExpr, Class<U> asType)
Return the last element of theArgumentListExpressionpassed as parameters as the expected type.- Parameters:
asType- the expected type- Returns:
- the last argument casted as the expected type
- Since:
- 0.3.0
-
getLastArgumentAs
public <U extends Expression> U getLastArgumentAs(ArgumentListExpression args, Class<U> asType)
Return the last element of theArgumentListExpressionpassed as parameters as the expected type.- Parameters:
args- the list of argumentsasType- the expected type- Returns:
- the last argument casted as the expected type
- Since:
- 0.1.4
-
isMethodCallExpr
public boolean isMethodCallExpr(Expression expr)
Checks whether an expression is of typeMethodCallExpression- Parameters:
expr- the evaluated expression- Returns:
- true if the expression is an instance of
MethodCallExpression - Since:
- 0.3.0
-
isListExpr
public boolean isListExpr(Expression expr)
Checks whether an expression is of typeListExpression- Parameters:
expr- the evaluated expression- Returns:
- true if the expression is an instance of
ListExpression - Since:
- 0.3.0
-
isBinaryExpr
public boolean isBinaryExpr(Expression expr)
Checks whether an expression is of typeBinaryExpression- Parameters:
expr- the evaluated expression- Returns:
- true if the expression is an instance of
BinaryExpression - Since:
- 0.3.0
-
isBinaryExpr
public boolean isBinaryExpr(Expression expr, int operator)
Checks whether an expression is of typeBinaryExpressionusing a specific binary operator- Parameters:
expr- the evaluated expressionoperator- int value taken fromTypes- Returns:
- true if the expression is an instance of
BinaryExpression - Since:
- 0.3.0
-
asBinaryExpr
public <T> T asBinaryExpr(Expression expr, T defaultValue, Closure<T> func)
Executes a given computation if the expression is of typeBinaryExpressionif not, then a default value is provided- Parameters:
expr- the expression we want to compute overdefaultValue- a value in case the expression is not of typeBinaryExpressionfunc- function executed in case expression is not of typeBinaryExpression- Returns:
- the result of the computation or the default value
- Since:
- 0.3.0
-
asBinaryExpr
public BinaryExpression asBinaryExpr(Expression expr)
Unsafe cast of a given @{link Expression} to aBinaryExpression- Parameters:
expr- the expression we want to cast- Returns:
- the expression cast as a
BinaryExpression - Since:
- 0.3.0
-
leftExprAs
public <T> T leftExprAs(BinaryExpression binaryExpr, Class<T> type)
From aBinaryExpressiongets the left expression as if it were of a given type- Parameters:
binaryExpr- the binary expression we want the left expr fromtype- the type we would like to cast the left expr to- Returns:
- the left expression cast to a given type
- Since:
- 0.3.0
-
rightExprAs
public <T> T rightExprAs(BinaryExpression binaryExpr, Class<T> type)
From aBinaryExpressiongets the right expression as if it were of a given type- Parameters:
binaryExpr- the binary expression we want the right expr fromtype- the type we would like to cast the right expr to- Returns:
- the right expression cast to a given type
- Since:
- 0.3.0
-
getText
public String getText(Expression expression)
Extracts the String value of a given expression- Parameters:
expression- expression to get text from- Returns:
- a
Stringof a givenExpression - Since:
- 0.3.2
-
-