Search in sources :

Example 36 with ClassNode

use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.

the class StaticTypesStatementWriter method writeForInLoop.

@Override
protected void writeForInLoop(final ForStatement loop) {
    controller.getAcg().onLineNumber(loop, "visitForLoop");
    writeStatementLabel(loop);
    CompileStack compileStack = controller.getCompileStack();
    MethodVisitor mv = controller.getMethodVisitor();
    OperandStack operandStack = controller.getOperandStack();
    compileStack.pushLoop(loop.getVariableScope(), loop.getStatementLabels());
    // Identify type of collection
    TypeChooser typeChooser = controller.getTypeChooser();
    Expression collectionExpression = loop.getCollectionExpression();
    ClassNode collectionType = typeChooser.resolveType(collectionExpression, controller.getClassNode());
    Parameter loopVariable = loop.getVariable();
    int size = operandStack.getStackLength();
    if (collectionType.isArray() && loopVariable.getOriginType().equals(collectionType.getComponentType())) {
        writeOptimizedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
    } else if (ENUMERATION_CLASSNODE.equals(collectionType)) {
        writeEnumerationBasedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
    } else {
        writeIteratorBasedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
    }
    operandStack.popDownTo(size);
    compileStack.pop();
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Parameter(org.codehaus.groovy.ast.Parameter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 37 with ClassNode

use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.

the class StaticTypesTypeChooser method resolveType.

@Override
public ClassNode resolveType(final Expression exp, final ClassNode current) {
    ASTNode target = exp instanceof VariableExpression ? getTarget((VariableExpression) exp) : exp;
    ClassNode inferredType = (ClassNode) target.getNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE);
    if (inferredType == null) {
        inferredType = (ClassNode) target.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
        if (inferredType == null && target instanceof VariableExpression && ((VariableExpression) target).getAccessedVariable() instanceof Parameter) {
            target = (Parameter) ((VariableExpression) target).getAccessedVariable();
            inferredType = ((Parameter) target).getOriginType();
        }
    }
    if (inferredType != null) {
        if (ClassHelper.VOID_TYPE == inferredType) {
            // we are in a case of a type inference failure, probably because code was generated
            // it is better to avoid using this
            inferredType = super.resolveType(exp, current);
        }
        return inferredType;
    }
    if (target instanceof VariableExpression && ((VariableExpression) target).isThisExpression()) {
        // AsmClassGenerator may create "this" expressions that the type checker knows nothing about
        return current;
    }
    return super.resolveType(exp, current);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ASTNode(org.codehaus.groovy.ast.ASTNode) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 38 with ClassNode

use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.

the class AnnotationConstantsVisitor method transformConstantExpression.

private Expression transformConstantExpression(Expression val, ClassNode returnType) {
    ClassNode returnWrapperType = ClassHelper.getWrapper(returnType);
    if (val instanceof ConstantExpression) {
        Expression result = revertType(val, returnWrapperType);
        if (result != null) {
            return result;
        }
        return val;
    }
    if (val instanceof CastExpression) {
        CastExpression castExp = (CastExpression) val;
        Expression castee = castExp.getExpression();
        if (castee instanceof ConstantExpression) {
            if (ClassHelper.getWrapper(castee.getType()).isDerivedFrom(returnWrapperType)) {
                return castee;
            }
            Expression result = revertType(castee, returnWrapperType);
            if (result != null) {
                return result;
            }
            return castee;
        }
    }
    return val;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 39 with ClassNode

use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.

the class ClassNodeResolver method findDecompiled.

/**
     * Search for classes using ASM decompiler
     */
private LookupResult findDecompiled(String name, CompilationUnit compilationUnit, GroovyClassLoader loader) {
    ClassNode node = ClassHelper.make(name);
    if (node.isResolved()) {
        return new LookupResult(null, node);
    }
    DecompiledClassNode asmClass = null;
    String fileName = name.replace('.', '/') + ".class";
    URL resource = loader.getResource(fileName);
    if (resource != null) {
        try {
            asmClass = new DecompiledClassNode(AsmDecompiler.parseClass(resource), new AsmReferenceResolver(this, compilationUnit));
            if (!asmClass.getName().equals(name)) {
                // this may happen under Windows because getResource is case insensitive under that OS!
                asmClass = null;
            }
        } catch (IOException e) {
        // fall through and attempt other search strategies
        }
    }
    if (asmClass != null) {
        if (isFromAnotherClassLoader(loader, fileName)) {
            return tryAsScript(name, compilationUnit, asmClass);
        }
        return new LookupResult(null, asmClass);
    }
    return null;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) DecompiledClassNode(org.codehaus.groovy.ast.decompiled.DecompiledClassNode) DecompiledClassNode(org.codehaus.groovy.ast.decompiled.DecompiledClassNode) AsmReferenceResolver(org.codehaus.groovy.ast.decompiled.AsmReferenceResolver) IOException(java.io.IOException) URL(java.net.URL)

Example 40 with ClassNode

use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.

the class StaticTypesStatementWriter method loadFromArray.

private void loadFromArray(MethodVisitor mv, BytecodeVariable variable, int array, int iteratorIdx) {
    OperandStack os = controller.getOperandStack();
    mv.visitVarInsn(ALOAD, array);
    mv.visitVarInsn(ILOAD, iteratorIdx);
    ClassNode varType = variable.getType();
    boolean primitiveType = ClassHelper.isPrimitiveType(varType);
    boolean isByte = ClassHelper.byte_TYPE.equals(varType);
    boolean isShort = ClassHelper.short_TYPE.equals(varType);
    boolean isInt = ClassHelper.int_TYPE.equals(varType);
    boolean isLong = ClassHelper.long_TYPE.equals(varType);
    boolean isFloat = ClassHelper.float_TYPE.equals(varType);
    boolean isDouble = ClassHelper.double_TYPE.equals(varType);
    boolean isChar = ClassHelper.char_TYPE.equals(varType);
    boolean isBoolean = ClassHelper.boolean_TYPE.equals(varType);
    if (primitiveType) {
        if (isByte) {
            mv.visitInsn(BALOAD);
        }
        if (isShort) {
            mv.visitInsn(SALOAD);
        }
        if (isInt || isChar || isBoolean) {
            mv.visitInsn(isChar ? CALOAD : isBoolean ? BALOAD : IALOAD);
        }
        if (isLong) {
            mv.visitInsn(LALOAD);
        }
        if (isFloat) {
            mv.visitInsn(FALOAD);
        }
        if (isDouble) {
            mv.visitInsn(DALOAD);
        }
    } else {
        mv.visitInsn(AALOAD);
    }
    os.push(varType);
    os.storeVar(variable);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode)

Aggregations

ClassNode (org.codehaus.groovy.ast.ClassNode)869 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)348 MethodNode (org.codehaus.groovy.ast.MethodNode)193 GenericsType (org.codehaus.groovy.ast.GenericsType)154 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)148 Expression (org.codehaus.groovy.ast.expr.Expression)146 Parameter (org.codehaus.groovy.ast.Parameter)135 FieldNode (org.codehaus.groovy.ast.FieldNode)126 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)118 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)117 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)113 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)103 ArrayList (java.util.ArrayList)95 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)92 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)75 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)74 LinkedList (java.util.LinkedList)71 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)71 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)62 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)61