org.codehaus.groovy.ast.expr.ArrayExpression Java Examples

The following examples show how to use org.codehaus.groovy.ast.expr.ArrayExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void despreadList(final List<Expression> expressions, final boolean wrap) {
    List<Expression> spreadIndexes = new ArrayList<>();
    List<Expression> spreadExpressions = new ArrayList<>();
    List<Expression> normalArguments = new ArrayList<>();
    for (int i = 0, n = expressions.size(); i < n; i += 1) {
        Expression expr = expressions.get(i);
        if (!(expr instanceof SpreadExpression)) {
            normalArguments.add(expr);
        } else {
            spreadIndexes.add(new ConstantExpression(i - spreadExpressions.size(), true));
            spreadExpressions.add(((SpreadExpression) expr).getExpression());
        }
    }

    // load normal arguments as array
    visitTupleExpression(new ArgumentListExpression(normalArguments), wrap);
    // load spread expressions as array
    new TupleExpression(spreadExpressions).visit(this);
    // load insertion index
    new ArrayExpression(ClassHelper.int_TYPE, spreadIndexes, null).visit(this);

    controller.getOperandStack().remove(1);
    despreadList.call(controller.getMethodVisitor());
}
 
Example #2
Source File: VariableScopeVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void visitArrayExpression(ArrayExpression visitedArray) {
    final ClassNode visitedType = visitedArray.getElementType();
    final String visitedName = ElementUtils.getTypeName(visitedType);

    if (FindTypeUtils.isCaretOnClassNode(path, doc, cursorOffset)) {
        ASTNode currentNode = FindTypeUtils.findCurrentNode(path, doc, cursorOffset);
        addOccurrences(visitedType, (ClassNode) currentNode);
    } else if (leaf instanceof Variable) {
        String varName = removeParentheses(((Variable) leaf).getName());
        if (varName.equals(visitedName)) {
            occurrences.add(new FakeASTNode(visitedType, visitedName));
        }
    } else if (leaf instanceof ConstantExpression && leafParent instanceof PropertyExpression) {
        if (visitedName.equals(((PropertyExpression) leafParent).getPropertyAsString())) {
            occurrences.add(new FakeASTNode(visitedType, visitedName));
        }
    }
    super.visitArrayExpression(visitedArray);
}
 
Example #3
Source File: AnnotationCollectorTransform.java    From groovy with Apache License 2.0 6 votes vote down vote up
private Expression serialize(Expression e) {
    if (e instanceof AnnotationConstantExpression) {
        AnnotationConstantExpression ace = (AnnotationConstantExpression) e;
        return serialize((AnnotationNode) ace.getValue());
    } else if (e instanceof ListExpression) {
        boolean annotationConstant = false;
        ListExpression le = (ListExpression) e;
        List<Expression> list = le.getExpressions();
        List<Expression> newList = new ArrayList<>(list.size());
        for (Expression exp: list) {
            annotationConstant = annotationConstant || exp instanceof AnnotationConstantExpression;
            newList.add(serialize(exp));
        }
        ClassNode type = ClassHelper.OBJECT_TYPE;
        if (annotationConstant) type = type.makeArray();
        return new ArrayExpression(type, newList);
    }
    return e;
}
 
Example #4
Source File: ASTNodeVisitor.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public void visitArrayExpression(ArrayExpression node) {
	pushASTNode(node);
	try {
		super.visitArrayExpression(node);
	} finally {
		popASTNode();
	}
}
 
Example #5
Source File: StaticTypesMethodReferenceExpressionWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private MethodNode addSyntheticMethodForConstructorReference(String syntheticMethodName, ClassNode returnType, Parameter[] parametersWithExactType) {
    ArgumentListExpression ctorArgs = args(parametersWithExactType);

    MethodNode syntheticMethodNode = controller.getClassNode().addSyntheticMethod(
            syntheticMethodName,
            Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC,
            returnType,
            parametersWithExactType,
            ClassNode.EMPTY_ARRAY,
            block(
                    returnS(
                            returnType.isArray()
                                    ?
                                    new ArrayExpression(
                                            ClassHelper.make(ArrayTypeUtils.elementType(returnType.getTypeClass())),
                                            null,
                                            ctorArgs.getExpressions()
                                    )
                                    :
                                    ctorX(returnType, ctorArgs)
                    )
            )
    );

    syntheticMethodNode.addAnnotation(new AnnotationNode(GENERATED_TYPE));
    syntheticMethodNode.addAnnotation(new AnnotationNode(COMPILE_STATIC_TYPE));

    return syntheticMethodNode;
}
 
Example #6
Source File: InvocationWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void loadArguments(final List<Expression> argumentList, final Parameter[] para) {
    if (para.length == 0) return;
    ClassNode lastParaType = para[para.length - 1].getOriginType();
    AsmClassGenerator acg = controller.getAcg();
    OperandStack operandStack = controller.getOperandStack();
    if (lastParaType.isArray() && (argumentList.size() > para.length
            || argumentList.size() == para.length - 1 || !lastIsArray(argumentList, para.length - 1))) {
        int stackLen = operandStack.getStackLength() + argumentList.size();
        MethodVisitor mv = controller.getMethodVisitor();
        controller.setMethodVisitor(mv);
        // varg call
        // first parameters as usual
        for (int i = 0, n = para.length - 1; i < n; i += 1) {
            argumentList.get(i).visit(acg);
            operandStack.doGroovyCast(para[i].getType());
        }
        // last parameters wrapped in an array
        List<Expression> lastParams = new LinkedList<>();
        for (int i = para.length - 1, n = argumentList.size(); i < n; i += 1) {
            lastParams.add(argumentList.get(i));
        }
        ArrayExpression array = new ArrayExpression(
                lastParaType.getComponentType(),
                lastParams
        );
        array.visit(acg);
        // adjust stack length
        while (operandStack.getStackLength() < stackLen) {
            operandStack.push(ClassHelper.OBJECT_TYPE);
        }
        if (argumentList.size() == para.length - 1) {
            operandStack.remove(1);
        }
    } else {
        for (int i = 0, n = argumentList.size(); i < n; i += 1) {
            argumentList.get(i).visit(acg);
            operandStack.doGroovyCast(para[i].getType());
        }
    }
}
 
Example #7
Source File: TraitComposer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Statement createSuperFallback(MethodNode forwarderMethod, ClassNode returnType) {
    ArgumentListExpression args = new ArgumentListExpression();
    Parameter[] forwarderMethodParameters = forwarderMethod.getParameters();
    for (final Parameter forwarderMethodParameter : forwarderMethodParameters) {
        args.addExpression(new VariableExpression(forwarderMethodParameter));
    }
    BinaryExpression instanceOfExpr = new BinaryExpression(new VariableExpression("this"), Token.newSymbol(Types.KEYWORD_INSTANCEOF, -1, -1), new ClassExpression(Traits.GENERATED_PROXY_CLASSNODE));
    MethodCallExpression superCall = new MethodCallExpression(
            new VariableExpression("super"),
            forwarderMethod.getName(),
            args
    );
    superCall.setImplicitThis(false);
    CastExpression proxyReceiver = new CastExpression(Traits.GENERATED_PROXY_CLASSNODE, new VariableExpression("this"));
    MethodCallExpression getProxy = new MethodCallExpression(proxyReceiver, "getProxyTarget", ArgumentListExpression.EMPTY_ARGUMENTS);
    getProxy.setImplicitThis(true);
    StaticMethodCallExpression proxyCall = new StaticMethodCallExpression(
            ClassHelper.make(InvokerHelper.class),
            "invokeMethod",
            new ArgumentListExpression(getProxy, new ConstantExpression(forwarderMethod.getName()), new ArrayExpression(ClassHelper.OBJECT_TYPE, args.getExpressions()))
    );
    IfStatement stmt = new IfStatement(
            new BooleanExpression(instanceOfExpr),
            new ExpressionStatement(new CastExpression(returnType,proxyCall)),
            new ExpressionStatement(superCall)
    );
    return stmt;
}
 
Example #8
Source File: ImmutablePropertyUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static Expression cloneArrayOrCloneableExpr(Expression fieldExpr, ClassNode type) {
    Expression smce = callX(
            REFLECTION_INVOKER_TYPE,
            "invoke",
            args(
                    fieldExpr,
                    constX("clone"),
                    new ArrayExpression(ClassHelper.OBJECT_TYPE.makeArray(), Collections.emptyList())
            )
    );
    return castX(type, smce);
}
 
Example #9
Source File: AnnotationCollectorTransform.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression serialize(AnnotationNode an) {
    ClassExpression type = new ClassExpression(an.getClassNode());
    type.setSourcePosition(an.getClassNode());

    MapExpression map = new MapExpression();
    for (Map.Entry<String, Expression> entry : an.getMembers().entrySet()) {
        Expression key = new ConstantExpression(entry.getKey());
        Expression val = serialize(entry.getValue());
        map.addMapEntryExpression(key, val);
    }

    return new ArrayExpression(ClassHelper.OBJECT_TYPE, Arrays.asList(type, map));
}
 
Example #10
Source File: DependencyTracker.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitArrayExpression(ArrayExpression expression) {
    super.visitArrayExpression(expression);
    addToCache(expression.getType());
}
 
Example #11
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
protected void loadArguments(final List<Expression> argumentList, final Parameter[] para) {
    if (para.length == 0) return;
    ClassNode lastParaType = para[para.length - 1].getOriginType();
    AsmClassGenerator acg = controller.getAcg();
    TypeChooser typeChooser = controller.getTypeChooser();
    OperandStack operandStack = controller.getOperandStack();
    int argumentListSize = argumentList.size();
    ClassNode lastArgType = argumentListSize > 0 ?
            typeChooser.resolveType(argumentList.get(argumentListSize -1), controller.getClassNode()) : null;
    if (lastParaType.isArray()
            && ((argumentListSize > para.length)
            || ((argumentListSize == (para.length - 1)) && !lastParaType.equals(lastArgType))
            || ((argumentListSize == para.length && lastArgType!=null && !lastArgType.isArray())
                && (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(lastArgType,lastParaType.getComponentType())))
                    || ClassHelper.GSTRING_TYPE.equals(lastArgType) && ClassHelper.STRING_TYPE.equals(lastParaType.getComponentType()))
            ) {
        int stackLen = operandStack.getStackLength() + argumentListSize;
        MethodVisitor mv = controller.getMethodVisitor();
        controller.setMethodVisitor(mv);
        // varg call
        // first parameters as usual
        for (int i = 0; i < para.length - 1; i += 1) {
            visitArgument(argumentList.get(i), para[i].getType());
        }
        // last parameters wrapped in an array
        List<Expression> lastParams = new ArrayList<>();
        for (int i = para.length - 1; i < argumentListSize; i += 1) {
            lastParams.add(argumentList.get(i));
        }
        ArrayExpression array = new ArrayExpression(lastParaType.getComponentType(), lastParams);
        array.visit(acg);
        // adjust stack length
        while (operandStack.getStackLength() < stackLen) {
            operandStack.push(ClassHelper.OBJECT_TYPE);
        }
        if (argumentListSize == para.length - 1) {
            operandStack.remove(1);
        }
    } else if (argumentListSize == para.length) {
        for (int i = 0; i < argumentListSize; i++) {
            visitArgument(argumentList.get(i), para[i].getType());
        }
    } else {
        // method call with default arguments
        ClassNode classNode = controller.getClassNode();
        Expression[] arguments = new Expression[para.length];
        for (int i = 0, j = 0, n = para.length; i < n; i += 1) {
            Parameter curParam = para[i];
            ClassNode curParamType = curParam.getType();
            Expression curArg = j < argumentListSize ? argumentList.get(j) : null;
            Expression initialExpression = curParam.getNodeMetaData(StaticTypesMarker.INITIAL_EXPRESSION);
            if (initialExpression == null && curParam.hasInitialExpression())
                initialExpression = curParam.getInitialExpression();
            if (initialExpression == null && curParam.getNodeMetaData(Verifier.INITIAL_EXPRESSION) != null) {
                initialExpression = curParam.getNodeMetaData(Verifier.INITIAL_EXPRESSION);
            }
            ClassNode curArgType = curArg == null ? null : typeChooser.resolveType(curArg, classNode);

            if (initialExpression != null && !compatibleArgumentType(curArgType, curParamType)) {
                // use default expression
                arguments[i] = initialExpression;
            } else {
                arguments[i] = curArg;
                j += 1;
            }
        }
        for (int i = 0, n = arguments.length; i < n; i += 1) {
            visitArgument(arguments[i], para[i].getType());
        }
    }
}
 
Example #12
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitArrayExpression(final ArrayExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    ClassNode elementType = expression.getElementType();
    String arrayTypeName = BytecodeHelper.getClassInternalName(elementType);
    List<Expression> sizeExpression = expression.getSizeExpression();

    int size = 0;
    int dimensions = 0;
    if (sizeExpression != null) {
        for (Expression element : sizeExpression) {
            if (element == ConstantExpression.EMPTY_EXPRESSION) break;
            dimensions += 1;
            // let's convert to an int
            element.visit(this);
            controller.getOperandStack().doGroovyCast(ClassHelper.int_TYPE);
        }
        controller.getOperandStack().remove(dimensions);
    } else {
        size = expression.getExpressions().size();
        BytecodeHelper.pushConstant(mv, size);
    }

    int storeIns = AASTORE;
    if (sizeExpression != null) {
        arrayTypeName = BytecodeHelper.getTypeDescription(expression.getType());
        mv.visitMultiANewArrayInsn(arrayTypeName, dimensions);
    } else if (ClassHelper.isPrimitiveType(elementType)) {
        int primType = 0;
        if (elementType == ClassHelper.boolean_TYPE) {
            primType = T_BOOLEAN;
            storeIns = BASTORE;
        } else if (elementType == ClassHelper.char_TYPE) {
            primType = T_CHAR;
            storeIns = CASTORE;
        } else if (elementType == ClassHelper.float_TYPE) {
            primType = T_FLOAT;
            storeIns = FASTORE;
        } else if (elementType == ClassHelper.double_TYPE) {
            primType = T_DOUBLE;
            storeIns = DASTORE;
        } else if (elementType == ClassHelper.byte_TYPE) {
            primType = T_BYTE;
            storeIns = BASTORE;
        } else if (elementType == ClassHelper.short_TYPE) {
            primType = T_SHORT;
            storeIns = SASTORE;
        } else if (elementType == ClassHelper.int_TYPE) {
            primType = T_INT;
            storeIns = IASTORE;
        } else if (elementType == ClassHelper.long_TYPE) {
            primType = T_LONG;
            storeIns = LASTORE;
        }
        mv.visitIntInsn(NEWARRAY, primType);
    } else {
        mv.visitTypeInsn(ANEWARRAY, arrayTypeName);
    }

    for (int i = 0; i < size; i += 1) {
        mv.visitInsn(DUP);
        BytecodeHelper.pushConstant(mv, i);
        Expression elementExpression = expression.getExpression(i);
        if (elementExpression == null) {
            ConstantExpression.NULL.visit(this);
        } else {
            elementExpression.visit(this);
            controller.getOperandStack().doGroovyCast(elementType);
        }
        mv.visitInsn(storeIns);
        controller.getOperandStack().remove(1);
    }

    controller.getOperandStack().push(expression.getType());
}
 
Example #13
Source File: CodeVisitorSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitArrayExpression(ArrayExpression expression) {
    visitListOfExpressions(expression.getExpressions());
    visitListOfExpressions(expression.getSizeExpression());
}
 
Example #14
Source File: TransformingCodeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitArrayExpression(final ArrayExpression expression) {
    super.visitArrayExpression(expression);
    trn.visitArrayExpression(expression);
}
 
Example #15
Source File: ListExpressionTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
private Expression transformArrayConstructor(final ListExpression expr, final MethodNode target) {
    ArrayExpression aex = new ArrayExpression(target.getDeclaringClass().getComponentType(), transformArguments(expr));
    aex.setSourcePosition(expr);
    return aex;
}
 
Example #16
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static boolean isGroovyConstructorCompatible(final Expression rightExpression) {
    return rightExpression instanceof ListExpression
            || rightExpression instanceof MapExpression
            || rightExpression instanceof ArrayExpression;
}
 
Example #17
Source File: SynchronizedASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static Expression zeroLengthObjectArray() {
    return new ArrayExpression(ClassHelper.OBJECT_TYPE, null, Collections.singletonList((Expression) constX(0)));
}
 
Example #18
Source File: SecureASTCustomizer.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitArrayExpression(final ArrayExpression expression) {
    assertExpressionAuthorized(expression);
    visitListOfExpressions(expression.getExpressions());
    visitListOfExpressions(expression.getSizeExpression());
}
 
Example #19
Source File: ContextualClassCodeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitArrayExpression(final ArrayExpression expression) {
    pushContext(expression);
    super.visitArrayExpression(expression);
    popContext();
}
 
Example #20
Source File: ASTFinder.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitArrayExpression(final ArrayExpression expression) {
    super.visitArrayExpression(expression);
    tryFind(ArrayExpression.class, expression);
}
 
Example #21
Source File: MarkupBuilderCodeTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
private Expression transformMethodCall(final MethodCallExpression exp) {
    String name = exp.getMethodAsString();
    if (exp.isImplicitThis() && "include".equals(name)) {
        return tryTransformInclude(exp);
    } else if (exp.isImplicitThis() && name.startsWith(":")) {
        List<Expression> args;
        if (exp.getArguments() instanceof ArgumentListExpression) {
            args = ((ArgumentListExpression) exp.getArguments()).getExpressions();
        } else {
            args = Collections.singletonList(exp.getArguments());
        }
        Expression newArguments = transform(new ArgumentListExpression(new ConstantExpression(name.substring(1)), new ArrayExpression(ClassHelper.OBJECT_TYPE, args)));
        MethodCallExpression call = new MethodCallExpression(
                new VariableExpression("this"),
                "methodMissing",
                newArguments
        );
        call.setImplicitThis(true);
        call.setSafe(exp.isSafe());
        call.setSpreadSafe(exp.isSpreadSafe());
        call.setSourcePosition(exp);
        return call;
    } else if (name!=null && name.startsWith("$")) {
        MethodCallExpression reformatted = new MethodCallExpression(
                exp.getObjectExpression(),
                name.substring(1),
                exp.getArguments()
        );
        reformatted.setImplicitThis(exp.isImplicitThis());
        reformatted.setSafe(exp.isSafe());
        reformatted.setSpreadSafe(exp.isSpreadSafe());
        reformatted.setSourcePosition(exp);
        // wrap in a stringOf { ... } closure call
        ClosureExpression clos = new ClosureExpression(Parameter.EMPTY_ARRAY, new ExpressionStatement(reformatted));
        clos.setVariableScope(new VariableScope());
        MethodCallExpression stringOf = new MethodCallExpression(new VariableExpression("this"),
                "stringOf",
                clos);
        stringOf.setImplicitThis(true);
        stringOf.setSourcePosition(reformatted);
        return stringOf;
    }
    return super.transform(exp);
}
 
Example #22
Source File: FindTypeUsagesVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void visitArrayExpression(ArrayExpression expression) {
    addIfEquals(expression);
    super.visitArrayExpression(expression);
}
 
Example #23
Source File: ElementUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static ClassNode getDeclaringClass(ASTNode node) {
    if (node instanceof ClassNode) {
        return (ClassNode) node;
    } else if (node instanceof AnnotationNode) {
        return ((AnnotationNode) node).getClassNode();
    } else if (node instanceof MethodNode) {
        return ((MethodNode) node).getDeclaringClass();
    } else if (node instanceof FieldNode) {
        return ((FieldNode) node).getDeclaringClass();
    } else if (node instanceof PropertyNode) {
        return ((PropertyNode) node).getDeclaringClass();
    } else if (node instanceof Parameter) {
        return ((Parameter) node).getDeclaringClass();
    } else if (node instanceof ForStatement) {
        return ((ForStatement) node).getVariableType().getDeclaringClass();
    } else if (node instanceof CatchStatement) {
        return ((CatchStatement) node).getVariable().getDeclaringClass();
    } else if (node instanceof ImportNode) {
        return ((ImportNode) node).getDeclaringClass();
    } else if (node instanceof ClassExpression) {
        return ((ClassExpression) node).getType().getDeclaringClass();
    } else if (node instanceof VariableExpression) {
        return ((VariableExpression) node).getDeclaringClass();
    } else if (node instanceof DeclarationExpression) {
        DeclarationExpression declaration = ((DeclarationExpression) node);
        if (declaration.isMultipleAssignmentDeclaration()) {
            return declaration.getTupleExpression().getDeclaringClass();
        } else {
            return declaration.getVariableExpression().getDeclaringClass();
        }
    } else if (node instanceof ConstantExpression) {
        return ((ConstantExpression) node).getDeclaringClass();
    } else if (node instanceof MethodCallExpression) {
        return ((MethodCallExpression) node).getType();
    } else if (node instanceof ConstructorCallExpression) {
        return ((ConstructorCallExpression) node).getType();
    } else if (node instanceof ArrayExpression) {
        return ((ArrayExpression) node).getDeclaringClass();
    }

    throw new IllegalStateException("Not implemented yet - GroovyRefactoringElement.getDeclaringClass() ..looks like the type: " + node.getClass().getName() + " isn't handled at the moment!"); // NOI18N
}
 
Example #24
Source File: ElementUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static String getNameWithoutPackage(ASTNode node) {
    if (node instanceof FakeASTNode) {
        node = ((FakeASTNode) node).getOriginalNode();
    }

    String name = null;
    if (node instanceof ClassNode) {
        name = ((ClassNode) node).getNameWithoutPackage();
    } else if (node instanceof AnnotationNode) {
        return ((AnnotationNode) node).getText();
    } else if (node instanceof MethodNode) {
        name = ((MethodNode) node).getName();
        if ("<init>".equals(name)) { // NOI18N
            name = getDeclaringClassNameWithoutPackage(node);
        }
    } else if (node instanceof FieldNode) {
        name = ((FieldNode) node).getName();
    } else if (node instanceof PropertyNode) {
        name = ((PropertyNode) node).getName();
    } else if (node instanceof Parameter) {
        name = ((Parameter) node).getName();
    } else if (node instanceof ForStatement) {
        name = ((ForStatement) node).getVariableType().getNameWithoutPackage();
    } else if (node instanceof CatchStatement) {
        name = ((CatchStatement) node).getVariable().getName();
    } else if (node instanceof ImportNode) {
        name = ((ImportNode) node).getType().getNameWithoutPackage();
    } else if (node instanceof ClassExpression) {
        name = ((ClassExpression) node).getType().getNameWithoutPackage();
    } else if (node instanceof VariableExpression) {
        name = ((VariableExpression) node).getName();
    } else if (node instanceof DeclarationExpression) {
        DeclarationExpression declaration = ((DeclarationExpression) node);
        if (declaration.isMultipleAssignmentDeclaration()) {
            name = declaration.getTupleExpression().getType().getNameWithoutPackage();
        } else {
            name = declaration.getVariableExpression().getType().getNameWithoutPackage();
        }
    } else if (node instanceof ConstantExpression) {
        name = ((ConstantExpression) node).getText();
    } else if (node instanceof MethodCallExpression) {
        name = ((MethodCallExpression) node).getMethodAsString();
    } else if (node instanceof ConstructorCallExpression) {
        name = ((ConstructorCallExpression) node).getType().getNameWithoutPackage();
    } else if (node instanceof ArrayExpression) {
        name = ((ArrayExpression) node).getElementType().getNameWithoutPackage();
    }


    if (name != null) {
        return normalizeTypeName(name, null);
    }
    throw new IllegalStateException("Not implemented yet - GroovyRefactoringElement.getName() needs to be improve for type: " + node.getClass().getSimpleName()); // NOI18N
}
 
Example #25
Source File: ElementUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Returns type for the given ASTNode. For example if FieldNode is passed
 * as a parameter, it returns type of the given field etc. If the Method call
 * is passed as a parameter, the method tried to interfere proper type and return it
 *
 * @param node where we want to know declared type
 * @return type of the given node
 * @throws IllegalStateException if an implementation is missing for the given ASTNode type
 */
public static ClassNode getType(ASTNode node) {
    if (node instanceof FakeASTNode) {
        node = ((FakeASTNode) node).getOriginalNode();
    }

    if (node instanceof ClassNode) {
        ClassNode clazz = ((ClassNode) node);
        if (clazz.getComponentType() != null) {
            return clazz.getComponentType();
        } else {
            return clazz;
        }
    } else if (node instanceof AnnotationNode) {
        return ((AnnotationNode) node).getClassNode();
    } else if (node instanceof FieldNode) {
        return ((FieldNode) node).getType();
    } else if (node instanceof PropertyNode) {
        return ((PropertyNode) node).getType();
    } else if (node instanceof MethodNode) {
        return ((MethodNode) node).getReturnType();
    } else if (node instanceof Parameter) {
       return ((Parameter) node).getType();
    } else if (node instanceof ForStatement) {
        return ((ForStatement) node).getVariableType();
    } else if (node instanceof CatchStatement) {
        return ((CatchStatement) node).getVariable().getOriginType();
    } else if (node instanceof ImportNode) {
        return ((ImportNode) node).getType();
    } else if (node instanceof ClassExpression) {
        return ((ClassExpression) node).getType();
    } else if (node instanceof VariableExpression) {
        return ((VariableExpression) node).getType();
    } else if (node instanceof DeclarationExpression) {
        DeclarationExpression declaration = ((DeclarationExpression) node);
        if (declaration.isMultipleAssignmentDeclaration()) {
            return declaration.getTupleExpression().getType();
        } else {
            return declaration.getVariableExpression().getType();
        }
    } else if (node instanceof ConstructorCallExpression) {
        return ((ConstructorCallExpression) node).getType();
    } else if (node instanceof ArrayExpression) {
        return ((ArrayExpression) node).getElementType();
    }
    throw new IllegalStateException("Not implemented yet - GroovyRefactoringElement.getType() needs to be improve!"); // NOI18N
}
 
Example #26
Source File: FindTypeUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static OffsetRange getArrayExpressionRange(ArrayExpression expression, BaseDocument doc, int cursorOffset) {
    return getRange(expression.getElementType(), doc, cursorOffset);
}
 
Example #27
Source File: FindTypeUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isCaretOnArrayExpressionType(ArrayExpression expression, BaseDocument doc, int cursorOffset) {
    if (getArrayExpressionRange(expression, doc, cursorOffset) != OffsetRange.NONE) {
        return true;
    }
    return false;
}
 
Example #28
Source File: PathFinderVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void visitArrayExpression(ArrayExpression node) {
    if (isInside(node, line, column)) {
        super.visitArrayExpression(node);
    }
}
 
Example #29
Source File: GroovyCodeVisitor.java    From groovy with Apache License 2.0 votes vote down vote up
void visitArrayExpression(ArrayExpression expression);