Search in sources :

Example 11 with ClassNode

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)));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Example 12 with ClassNode

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());
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode)

Example 13 with ClassNode

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;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) HashSet(java.util.HashSet)

Example 14 with ClassNode

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;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement)

Example 15 with ClassNode

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);
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST)

Aggregations

ClassNode (org.codehaus.groovy.ast.ClassNode)850 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)329 MethodNode (org.codehaus.groovy.ast.MethodNode)192 GenericsType (org.codehaus.groovy.ast.GenericsType)149 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)148 Expression (org.codehaus.groovy.ast.expr.Expression)140 Parameter (org.codehaus.groovy.ast.Parameter)133 FieldNode (org.codehaus.groovy.ast.FieldNode)125 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)114 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)112 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)107 ArrayList (java.util.ArrayList)95 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)95 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)86 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)73 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)71 LinkedList (java.util.LinkedList)70 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)69 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)59 MethodVisitor (org.objectweb.asm.MethodVisitor)57