use of org.codehaus.groovy.syntax.Token in project groovy by apache.
the class StaticTypeCheckingVisitor method getOpWithoutEqual.
private Token getOpWithoutEqual(Expression exp) {
// should never happen
if (!(exp instanceof BinaryExpression))
return null;
Token op = ((BinaryExpression) exp).getOperation();
int typeWithoutEqual = TokenUtil.removeAssignment(op.getType());
return new Token(typeWithoutEqual, op.getText(), /* will do */
op.getStartLine(), op.getStartColumn());
}
use of org.codehaus.groovy.syntax.Token in project groovy by apache.
the class NAryOperationRewriter method transformPostfixExpression.
private Expression transformPostfixExpression(final PostfixExpression exp) {
if (isInternalFieldAccess(exp.getExpression())) {
Token operation = exp.getOperation();
sourceUnit.addError(new SyntaxException("Postfix expressions on trait fields/properties are not supported in traits.", operation.getStartLine(), operation.getStartColumn()));
return exp;
} else {
return super.transform(exp);
}
}
use of org.codehaus.groovy.syntax.Token in project groovy-core by groovy.
the class NAryOperationRewriter method transformPrefixExpression.
private Expression transformPrefixExpression(final PrefixExpression exp) {
if (isInternalFieldAccess(exp.getExpression())) {
Token operation = exp.getOperation();
sourceUnit.addError(new SyntaxException("Prefix expressions on trait fields/properties are not supported in traits.", operation.getStartLine(), operation.getStartColumn()));
return exp;
} else {
return super.transform(exp);
}
}
use of org.codehaus.groovy.syntax.Token in project grails-core by grails.
the class ControllerActionTransformer method initializePrimitiveOrTypeWrapperParameter.
protected void initializePrimitiveOrTypeWrapperParameter(final ClassNode classNode, final BlockStatement wrapper, final Parameter param, final String requestParameterName) {
final ClassNode paramTypeClassNode = param.getType();
final String methodParamName = param.getName();
final Expression defaultValueExpression;
if (paramTypeClassNode.equals(ClassHelper.Boolean_TYPE)) {
defaultValueExpression = new ConstantExpression(false);
} else if (PRIMITIVE_CLASS_NODES.contains(paramTypeClassNode)) {
defaultValueExpression = new ConstantExpression(0);
} else {
defaultValueExpression = new ConstantExpression(null);
}
final ConstantExpression paramConstantExpression = new ConstantExpression(requestParameterName);
final Expression paramsTypeConversionMethodArguments = new ArgumentListExpression(paramConstantExpression, /*, defaultValueExpression*/
new ConstantExpression(null));
final String conversionMethodName;
if (TYPE_WRAPPER_CLASS_TO_CONVERSION_METHOD_NAME.containsKey(paramTypeClassNode)) {
conversionMethodName = TYPE_WRAPPER_CLASS_TO_CONVERSION_METHOD_NAME.get(paramTypeClassNode);
} else {
conversionMethodName = paramTypeClassNode.getName();
}
Expression getParamsExpression = buildGetPropertyExpression(new VariableExpression("this"), "params", classNode);
final MethodCallExpression retrieveConvertedValueExpression = new MethodCallExpression(getParamsExpression, conversionMethodName, paramsTypeConversionMethodArguments);
// choose any
Class<?> defaultValueClass = null;
if ("char".equals(conversionMethodName)) {
// TypeConvertingMap.'char' method has 2 different signatures, choose the one with "Character 'char'(String name, Integer defaultValue)" signature
defaultValueClass = Integer.class;
}
applyMethodTarget(retrieveConvertedValueExpression, TypeConvertingMap.class, null, defaultValueClass);
final Expression paramsContainsKeyMethodArguments = new ArgumentListExpression(paramConstantExpression);
final BooleanExpression containsKeyExpression = new BooleanExpression(applyDefaultMethodTarget(new MethodCallExpression(getParamsExpression, "containsKey", paramsContainsKeyMethodArguments), Map.class));
final Token equalsToken = Token.newSymbol(Types.EQUALS, 0, 0);
final VariableExpression convertedValueExpression = new VariableExpression("___converted_" + methodParamName, new ClassNode(Object.class));
final DeclarationExpression declareConvertedValueExpression = new DeclarationExpression(convertedValueExpression, equalsToken, new EmptyExpression());
Statement declareVariableStatement = new ExpressionStatement(declareConvertedValueExpression);
wrapper.addStatement(declareVariableStatement);
final VariableExpression methodParamExpression = new VariableExpression(methodParamName, paramTypeClassNode);
final DeclarationExpression declareParameterVariableStatement = new DeclarationExpression(methodParamExpression, equalsToken, new EmptyExpression());
declareVariableStatement = new ExpressionStatement(declareParameterVariableStatement);
wrapper.addStatement(declareVariableStatement);
final Expression assignmentExpression = new BinaryExpression(convertedValueExpression, equalsToken, new TernaryExpression(containsKeyExpression, retrieveConvertedValueExpression, defaultValueExpression));
wrapper.addStatement(new ExpressionStatement(assignmentExpression));
Expression rejectValueMethodCallExpression = getRejectValueExpression(classNode, methodParamName);
BlockStatement ifConvertedValueIsNullBlockStatement = new BlockStatement();
ifConvertedValueIsNullBlockStatement.addStatement(new ExpressionStatement(rejectValueMethodCallExpression));
ifConvertedValueIsNullBlockStatement.addStatement(new ExpressionStatement(new BinaryExpression(methodParamExpression, equalsToken, defaultValueExpression)));
final BooleanExpression isConvertedValueNullExpression = new BooleanExpression(new BinaryExpression(convertedValueExpression, Token.newSymbol(Types.COMPARE_EQUAL, 0, 0), new ConstantExpression(null)));
final ExpressionStatement assignConvertedValueToParamStatement = new ExpressionStatement(new BinaryExpression(methodParamExpression, equalsToken, convertedValueExpression));
final Statement ifStatement = new IfStatement(isConvertedValueNullExpression, ifConvertedValueIsNullBlockStatement, assignConvertedValueToParamStatement);
wrapper.addStatement(new IfStatement(new BooleanExpression(containsKeyExpression), ifStatement, new ExpressionStatement(new EmptyExpression())));
}
use of org.codehaus.groovy.syntax.Token in project groovy-core by groovy.
the class BinaryExpression method newInitializationExpression.
/**
* Creates variable initialization expression in which the specified expression
* is written into the specified variable name.
*/
public static BinaryExpression newInitializationExpression(String variable, ClassNode type, Expression rhs) {
VariableExpression lhs = new VariableExpression(variable);
if (type != null) {
lhs.setType(type);
}
Token operator = Token.newPlaceholder(Types.ASSIGN);
return new BinaryExpression(lhs, operator, rhs);
}
Aggregations