use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class GeneralUtils method createConstructorStatementDefault.
public static Statement createConstructorStatementDefault(FieldNode fNode) {
final String name = fNode.getName();
final ClassNode fType = fNode.getType();
final Expression fieldExpr = propX(varX("this"), name);
Expression initExpr = fNode.getInitialValueExpression();
Statement assignInit;
if (initExpr == null || (initExpr instanceof ConstantExpression && ((ConstantExpression) initExpr).isNullExpression())) {
if (ClassHelper.isPrimitiveType(fType)) {
assignInit = EmptyStatement.INSTANCE;
} else {
assignInit = assignS(fieldExpr, ConstantExpression.EMPTY_EXPRESSION);
}
} else {
assignInit = assignS(fieldExpr, initExpr);
}
fNode.setInitialValueExpression(null);
Expression value = findArg(name);
return ifElseS(equalsNullX(value), assignInit, assignS(fieldExpr, castX(fType, value)));
}
use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class GeneralUtils method getterX.
/**
* This method is similar to {@link #propX(Expression, Expression)} but will make sure that if the property
* being accessed is defined inside the classnode provided as a parameter, then a getter call is generated
* instead of a field access.
* @param annotatedNode the class node where the property node is accessed from
* @param receiver the object having the property
* @param pNode the property being accessed
* @return a method call expression or a property expression
*/
public static Expression getterX(ClassNode annotatedNode, Expression receiver, PropertyNode pNode) {
ClassNode owner = pNode.getDeclaringClass();
if (annotatedNode.equals(owner)) {
String getterName = "get" + MetaClassHelper.capitalize(pNode.getName());
boolean existingExplicitGetter = annotatedNode.getMethod(getterName, Parameter.EMPTY_ARRAY) != null;
if (ClassHelper.boolean_TYPE.equals(pNode.getOriginType()) && !existingExplicitGetter) {
getterName = "is" + MetaClassHelper.capitalize(pNode.getName());
}
return callX(receiver, getterName);
}
return propX(receiver, pNode.getName());
}
use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class GeneralUtils method getInterfacesAndSuperInterfaces.
public static Set<ClassNode> getInterfacesAndSuperInterfaces(ClassNode type) {
Set<ClassNode> res = new HashSet<ClassNode>();
if (type.isInterface()) {
res.add(type);
return res;
}
ClassNode next = type;
while (next != null) {
res.addAll(next.getAllInterfaces());
next = next.getSuperClass();
}
return res;
}
use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class AntlrParserPlugin method buildAST.
public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduction cst) throws ParserException {
setClassLoader(classLoader);
makeModule();
try {
convertGroovy(ast);
if (output.getStatementBlock().isEmpty() && output.getMethods().isEmpty() && output.getClasses().isEmpty()) {
output.addStatement(ReturnStatement.RETURN_NULL_OR_VOID);
}
// set the script source position
ClassNode scriptClassNode = output.getScriptClassDummy();
if (scriptClassNode != null) {
List<Statement> statements = output.getStatementBlock().getStatements();
if (!statements.isEmpty()) {
Statement firstStatement = statements.get(0);
Statement lastStatement = statements.get(statements.size() - 1);
scriptClassNode.setSourcePosition(firstStatement);
scriptClassNode.setLastColumnNumber(lastStatement.getLastColumnNumber());
scriptClassNode.setLastLineNumber(lastStatement.getLastLineNumber());
}
}
} catch (ASTRuntimeException e) {
throw new ASTParserException(e.getMessage() + ". File: " + sourceUnit.getName(), e);
}
return output;
}
use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class AntlrParserPlugin method asExpression.
protected Expression asExpression(AST node) {
AST leftNode = node.getFirstChild();
Expression leftExpression = expression(leftNode);
AST rightNode = leftNode.getNextSibling();
ClassNode type = makeTypeWithArguments(rightNode);
return CastExpression.asExpression(type, leftExpression);
}
Aggregations