Search in sources :

Example 1 with Iterator_TYPE

use of org.codehaus.groovy.ast.ClassHelper.Iterator_TYPE in project groovy by apache.

the class StaticTypesCallSiteWriter method writeListDotProperty.

private void writeListDotProperty(final Expression receiver, final String propertyName, final boolean safe) {
    // for lists, replace list.foo with:
    // def result = new ArrayList(list.size())
    // for (item in list) result.add(item.foo)
    // result
    ClassNode componentType = receiver.getNodeMetaData(StaticCompilationMetadataKeys.COMPONENT_TYPE);
    if (componentType == null) {
        componentType = OBJECT_TYPE;
    }
    CompileStack compileStack = controller.getCompileStack();
    MethodVisitor mv = controller.getMethodVisitor();
    Label exit = new Label();
    if (safe) {
        receiver.visit(controller.getAcg());
        Label doGet = new Label();
        mv.visitJumpInsn(IFNONNULL, doGet);
        controller.getOperandStack().remove(1);
        mv.visitInsn(ACONST_NULL);
        mv.visitJumpInsn(GOTO, exit);
        mv.visitLabel(doGet);
    }
    Variable tmpList = varX("tmpList", ClassHelper.make(ArrayList.class));
    int var = compileStack.defineTemporaryVariable(tmpList, false);
    Variable iterator = varX("iterator", Iterator_TYPE);
    int it = compileStack.defineTemporaryVariable(iterator, false);
    Variable nextVar = varX("next", componentType);
    final int next = compileStack.defineTemporaryVariable(nextVar, false);
    mv.visitTypeInsn(NEW, "java/util/ArrayList");
    mv.visitInsn(DUP);
    receiver.visit(controller.getAcg());
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "size", "()I", true);
    controller.getOperandStack().remove(1);
    mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "(I)V", false);
    mv.visitVarInsn(ASTORE, var);
    Label l1 = new Label();
    mv.visitLabel(l1);
    receiver.visit(controller.getAcg());
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "iterator", "()Ljava/util/Iterator;", true);
    controller.getOperandStack().remove(1);
    mv.visitVarInsn(ASTORE, it);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitVarInsn(ALOAD, it);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z", true);
    Label l3 = new Label();
    mv.visitJumpInsn(IFEQ, l3);
    mv.visitVarInsn(ALOAD, it);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;", true);
    mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(componentType));
    mv.visitVarInsn(ASTORE, next);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitVarInsn(ALOAD, var);
    PropertyExpression pexp = propX(bytecodeX(componentType, v -> v.visitVarInsn(ALOAD, next)), propertyName);
    pexp.visit(controller.getAcg());
    controller.getOperandStack().box();
    controller.getOperandStack().remove(1);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
    mv.visitInsn(POP);
    Label l5 = new Label();
    mv.visitLabel(l5);
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l3);
    mv.visitVarInsn(ALOAD, var);
    if (safe) {
        mv.visitLabel(exit);
    }
    controller.getOperandStack().push(ClassHelper.make(ArrayList.class));
    controller.getCompileStack().removeVar(next);
    controller.getCompileStack().removeVar(it);
    controller.getCompileStack().removeVar(var);
}
Also used : ASTORE(org.objectweb.asm.Opcodes.ASTORE) MethodVisitor(org.objectweb.asm.MethodVisitor) GETSTATIC(org.objectweb.asm.Opcodes.GETSTATIC) GeneralUtils.classX(org.codehaus.groovy.ast.tools.GeneralUtils.classX) Number_TYPE(org.codehaus.groovy.ast.ClassHelper.Number_TYPE) ClassHelper.isBigDecimalType(org.codehaus.groovy.ast.ClassHelper.isBigDecimalType) Variable(org.codehaus.groovy.ast.Variable) NEW(org.objectweb.asm.Opcodes.NEW) StaticTypeCheckingSupport.chooseBestMethod(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.chooseBestMethod) INVOKEINTERFACE(org.objectweb.asm.Opcodes.INVOKEINTERFACE) BytecodeHelper(org.codehaus.groovy.classgen.asm.BytecodeHelper) GeneralUtils.nullX(org.codehaus.groovy.ast.tools.GeneralUtils.nullX) Map(java.util.Map) GeneralUtils.propX(org.codehaus.groovy.ast.tools.GeneralUtils.propX) AALOAD(org.objectweb.asm.Opcodes.AALOAD) ClassHelper.isBigIntegerType(org.codehaus.groovy.ast.ClassHelper.isBigIntegerType) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ClassHelper.isWrapperLong(org.codehaus.groovy.ast.ClassHelper.isWrapperLong) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) GROOVY_OBJECT_TYPE(org.codehaus.groovy.ast.ClassHelper.GROOVY_OBJECT_TYPE) LIST_TYPE(org.codehaus.groovy.ast.ClassHelper.LIST_TYPE) Collection(java.util.Collection) GeneralUtils.args(org.codehaus.groovy.ast.tools.GeneralUtils.args) BeanUtils.capitalize(org.apache.groovy.util.BeanUtils.capitalize) IFNULL(org.objectweb.asm.Opcodes.IFNULL) MethodCallerMultiAdapter(org.codehaus.groovy.classgen.asm.MethodCallerMultiAdapter) ClassNodeUtils.getField(org.apache.groovy.ast.tools.ClassNodeUtils.getField) ClassHelper.getUnwrapper(org.codehaus.groovy.ast.ClassHelper.getUnwrapper) GeneralUtils.callX(org.codehaus.groovy.ast.tools.GeneralUtils.callX) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) GETFIELD(org.objectweb.asm.Opcodes.GETFIELD) GroovyBugError(org.codehaus.groovy.GroovyBugError) CallSiteWriter(org.codehaus.groovy.classgen.asm.CallSiteWriter) ExpressionUtils.isThisExpression(org.apache.groovy.ast.tools.ExpressionUtils.isThisExpression) StaticTypeCheckingSupport.findDGMMethodsByNameAndArguments(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findDGMMethodsByNameAndArguments) DUP(org.objectweb.asm.Opcodes.DUP) CLASS_Type(org.codehaus.groovy.ast.ClassHelper.CLASS_Type) List(java.util.List) StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf) CompileStack(org.codehaus.groovy.classgen.asm.CompileStack) OBJECT_TYPE(org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE) GeneralUtils.bytecodeX(org.codehaus.groovy.ast.tools.GeneralUtils.bytecodeX) ACC_PUBLIC(org.objectweb.asm.Opcodes.ACC_PUBLIC) IFNONNULL(org.objectweb.asm.Opcodes.IFNONNULL) STRING_TYPE(org.codehaus.groovy.ast.ClassHelper.STRING_TYPE) ClassHelper.getWrapper(org.codehaus.groovy.ast.ClassHelper.getWrapper) GOTO(org.objectweb.asm.Opcodes.GOTO) GeneralUtils.constX(org.codehaus.groovy.ast.tools.GeneralUtils.constX) GeneralUtils.isOrImplements(org.codehaus.groovy.ast.tools.GeneralUtils.isOrImplements) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClassHelper.int_TYPE(org.codehaus.groovy.ast.ClassHelper.int_TYPE) ClassHelper.isWrapperInteger(org.codehaus.groovy.ast.ClassHelper.isWrapperInteger) Label(org.objectweb.asm.Label) ACC_STATIC(org.objectweb.asm.Opcodes.ACC_STATIC) ArrayList(java.util.ArrayList) PUTFIELD(org.objectweb.asm.Opcodes.PUTFIELD) IFEQ(org.objectweb.asm.Opcodes.IFEQ) INVOKESTATIC(org.objectweb.asm.Opcodes.INVOKESTATIC) MethodNode(org.codehaus.groovy.ast.MethodNode) ACONST_NULL(org.objectweb.asm.Opcodes.ACONST_NULL) CLOSURE_TYPE(org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) GeneralUtils.varX(org.codehaus.groovy.ast.tools.GeneralUtils.varX) Expression(org.codehaus.groovy.ast.expr.Expression) CHECKCAST(org.objectweb.asm.Opcodes.CHECKCAST) POP(org.objectweb.asm.Opcodes.POP) Parameter(org.codehaus.groovy.ast.Parameter) MAP_TYPE(org.codehaus.groovy.ast.ClassHelper.MAP_TYPE) InvokerHelper(org.codehaus.groovy.runtime.InvokerHelper) ClassNode(org.codehaus.groovy.ast.ClassNode) OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) ClassHelper(org.codehaus.groovy.ast.ClassHelper) ClassHelper.isStringType(org.codehaus.groovy.ast.ClassHelper.isStringType) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) ClassHelper.isPrimitiveBoolean(org.codehaus.groovy.ast.ClassHelper.isPrimitiveBoolean) GeneralUtils.castX(org.codehaus.groovy.ast.tools.GeneralUtils.castX) StaticCompilationMetadataKeys(org.codehaus.groovy.transform.sc.StaticCompilationMetadataKeys) StaticTypesMarker(org.codehaus.groovy.transform.stc.StaticTypesMarker) ClassHelper.isGeneratedFunction(org.codehaus.groovy.ast.ClassHelper.isGeneratedFunction) ARRAYLENGTH(org.objectweb.asm.Opcodes.ARRAYLENGTH) StaticTypeCheckingSupport.isClassClassNodeWrappingConcreteType(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isClassClassNodeWrappingConcreteType) TypeChooser(org.codehaus.groovy.classgen.asm.TypeChooser) GeneralUtils.callThisX(org.codehaus.groovy.ast.tools.GeneralUtils.callThisX) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ASTNode(org.codehaus.groovy.ast.ASTNode) ClassHelper.isClassType(org.codehaus.groovy.ast.ClassHelper.isClassType) ClassHelper.isPrimitiveType(org.codehaus.groovy.ast.ClassHelper.isPrimitiveType) FieldNode(org.codehaus.groovy.ast.FieldNode) PUTSTATIC(org.objectweb.asm.Opcodes.PUTSTATIC) VariableSlotLoader(org.codehaus.groovy.classgen.asm.VariableSlotLoader) INVOKESPECIAL(org.objectweb.asm.Opcodes.INVOKESPECIAL) Iterator_TYPE(org.codehaus.groovy.ast.ClassHelper.Iterator_TYPE) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ALOAD(org.objectweb.asm.Opcodes.ALOAD) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) Variable(org.codehaus.groovy.ast.Variable) Label(org.objectweb.asm.Label) ArrayList(java.util.ArrayList) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) CompileStack(org.codehaus.groovy.classgen.asm.CompileStack) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 ClassNodeUtils.getField (org.apache.groovy.ast.tools.ClassNodeUtils.getField)1 ExpressionUtils.isThisExpression (org.apache.groovy.ast.tools.ExpressionUtils.isThisExpression)1 BeanUtils.capitalize (org.apache.groovy.util.BeanUtils.capitalize)1 GroovyBugError (org.codehaus.groovy.GroovyBugError)1 ASTNode (org.codehaus.groovy.ast.ASTNode)1 ClassHelper (org.codehaus.groovy.ast.ClassHelper)1 CLASS_Type (org.codehaus.groovy.ast.ClassHelper.CLASS_Type)1 CLOSURE_TYPE (org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE)1 GROOVY_OBJECT_TYPE (org.codehaus.groovy.ast.ClassHelper.GROOVY_OBJECT_TYPE)1 Iterator_TYPE (org.codehaus.groovy.ast.ClassHelper.Iterator_TYPE)1 LIST_TYPE (org.codehaus.groovy.ast.ClassHelper.LIST_TYPE)1 MAP_TYPE (org.codehaus.groovy.ast.ClassHelper.MAP_TYPE)1 Number_TYPE (org.codehaus.groovy.ast.ClassHelper.Number_TYPE)1 OBJECT_TYPE (org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE)1 STRING_TYPE (org.codehaus.groovy.ast.ClassHelper.STRING_TYPE)1 ClassHelper.getUnwrapper (org.codehaus.groovy.ast.ClassHelper.getUnwrapper)1