Search in sources :

Example 16 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.

the class StaticInvocationWriter method writeInvokeConstructor.

@Override
public void writeInvokeConstructor(final ConstructorCallExpression call) {
    MethodNode mn = call.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
    if (mn == null) {
        super.writeInvokeConstructor(call);
        return;
    }
    if (writeAICCall(call))
        return;
    ConstructorNode cn;
    if (mn instanceof ConstructorNode) {
        cn = (ConstructorNode) mn;
    } else {
        cn = new ConstructorNode(mn.getModifiers(), mn.getParameters(), mn.getExceptions(), mn.getCode());
        cn.setDeclaringClass(mn.getDeclaringClass());
    }
    TupleExpression args = makeArgumentList(call.getArguments());
    if (cn.isPrivate()) {
        ClassNode classNode = controller.getClassNode();
        ClassNode declaringClass = cn.getDeclaringClass();
        if (declaringClass != classNode) {
            MethodNode bridge = null;
            if (call.getNodeMetaData(StaticTypesMarker.PV_METHODS_ACCESS) != null) {
                Map<MethodNode, MethodNode> bridgeMethods = declaringClass.getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_BRIDGE_METHODS);
                bridge = bridgeMethods != null ? bridgeMethods.get(cn) : null;
            }
            if (bridge instanceof ConstructorNode) {
                ArgumentListExpression newArgs = args(nullX());
                for (Expression arg : args) {
                    newArgs.addExpression(arg);
                }
                cn = (ConstructorNode) bridge;
                args = newArgs;
            } else {
                controller.getSourceUnit().addError(new SyntaxException("Cannot call private constructor for " + declaringClass.toString(false) + " from class " + classNode.toString(false), call));
            }
        }
    }
    String ownerDescriptor = prepareConstructorCall(cn);
    int before = controller.getOperandStack().getStackLength();
    loadArguments(args.getExpressions(), cn.getParameters());
    finnishConstructorCall(cn, ownerDescriptor, controller.getOperandStack().getStackLength() - before);
}
Also used : DecompiledClassNode(org.codehaus.groovy.ast.decompiled.DecompiledClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) ExtensionMethodNode(org.codehaus.groovy.transform.stc.ExtensionMethodNode) MethodNode(org.codehaus.groovy.ast.MethodNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ExpressionUtils.isSuperExpression(org.apache.groovy.ast.tools.ExpressionUtils.isSuperExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) TemporaryVariableExpression(org.codehaus.groovy.transform.sc.TemporaryVariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 17 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.

the class ErrorCollector method getSyntaxError.

/**
 * Returns the specified error's underlying SyntaxException, or null if it isn't one.
 */
public SyntaxException getSyntaxError(final int index) {
    SyntaxException exception = null;
    Message message = getError(index);
    if (message instanceof SyntaxErrorMessage) {
        exception = ((SyntaxErrorMessage) message).getCause();
    }
    return exception;
}
Also used : LocatedMessage(org.codehaus.groovy.control.messages.LocatedMessage) WarningMessage(org.codehaus.groovy.control.messages.WarningMessage) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 18 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.

the class CompileUnit method addClass.

/**
 * Adds a class to the unit.
 */
public void addClass(ClassNode node) {
    node = node.redirect();
    String name = node.getName();
    ClassNode stored = classes.get(name);
    if (stored != null && stored != node) {
        // we have a duplicate class!
        // One possibility for this is, that we declared a script and a
        // class in the same file and named the class like the file
        SourceUnit nodeSource = node.getModule().getContext();
        SourceUnit storedSource = stored.getModule().getContext();
        String txt = "Invalid duplicate class definition of class " + node.getName() + " : ";
        if (nodeSource == storedSource) {
            // same class in same source
            txt += "The source " + nodeSource.getName() + " contains at least two definitions of the class " + node.getName() + ".\n";
            if (node.isScriptBody() || stored.isScriptBody()) {
                txt += "One of the classes is an explicit generated class using the class statement, the other is a class generated from" + " the script body based on the file name. Solutions are to change the file name or to change the class name.\n";
            }
        } else {
            txt += "The sources " + nodeSource.getName() + " and " + storedSource.getName() + " each contain a class with the name " + node.getName() + ".\n";
        }
        nodeSource.addErrorAndContinue(new SyntaxException(txt, node));
    }
    classes.put(name, node);
    ClassNode cn = classesToCompile.remove(name);
    if (cn != null)
        cn.setRedirect(node);
}
Also used : SyntaxException(org.codehaus.groovy.syntax.SyntaxException) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 19 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.

the class StaticTypesTransformation method visit.

@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
    AnnotationNode annotationInformation = (AnnotationNode) nodes[0];
    Map<String, Expression> members = annotationInformation.getMembers();
    Expression extensions = members.get("extensions");
    AnnotatedNode node = (AnnotatedNode) nodes[1];
    StaticTypeCheckingVisitor visitor = null;
    if (node instanceof ClassNode) {
        ClassNode classNode = (ClassNode) node;
        visitor = newVisitor(source, classNode);
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        visitor.initialize();
        visitor.visitClass(classNode);
    } else if (node instanceof MethodNode) {
        MethodNode methodNode = (MethodNode) node;
        visitor = newVisitor(source, methodNode.getDeclaringClass());
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        visitor.setMethodsToBeVisited(Collections.singleton(methodNode));
        visitor.initialize();
        visitor.visitMethod(methodNode);
    } else if (!(node instanceof PropertyNode)) {
        source.addError(new SyntaxException(STATIC_ERROR_PREFIX + "Unimplemented node type", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()));
    }
    if (visitor != null) {
        visitor.performSecondPass();
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) StaticTypeCheckingVisitor(org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor) PropertyNode(org.codehaus.groovy.ast.PropertyNode) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 20 with SyntaxException

use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.

the class MacroGroovyMethods method macro.

@Macro
public static Expression macro(MacroContext macroContext, PropertyExpression phaseExpression, ConstantExpression asIsConstantExpression, ClosureExpression closureExpression) {
    if (closureExpression.getParameters() != null && closureExpression.getParameters().length > 0) {
        macroContext.getSourceUnit().addError(new SyntaxException("Macro closure arguments are not allowed" + '\n', closureExpression));
        return macroContext.getCall();
    }
    final String source;
    try {
        source = ClosureUtils.convertClosureToSource(macroContext.getSourceUnit().getSource(), closureExpression);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    BlockStatement closureBlock = (BlockStatement) closureExpression.getCode();
    Boolean asIs = (Boolean) asIsConstantExpression.getValue();
    return callX(propX(classX(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"), "macro", args(phaseExpression != null ? phaseExpression : constX(null), asIsConstantExpression, constX(source), buildSubstitutions(macroContext.getSourceUnit(), closureExpression), classX(ClassHelper.makeWithoutCaching(MacroBuilder.getMacroValue(closureBlock, asIs).getClass(), false))));
}
Also used : SyntaxException(org.codehaus.groovy.syntax.SyntaxException) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) MacroBuilder(org.codehaus.groovy.macro.runtime.MacroBuilder) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) Macro(org.codehaus.groovy.macro.runtime.Macro)

Aggregations

SyntaxException (org.codehaus.groovy.syntax.SyntaxException)56 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)19 ClassNode (org.codehaus.groovy.ast.ClassNode)15 Expression (org.codehaus.groovy.ast.expr.Expression)12 MethodNode (org.codehaus.groovy.ast.MethodNode)11 SourceUnit (org.codehaus.groovy.control.SourceUnit)10 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)8 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)8 FieldNode (org.codehaus.groovy.ast.FieldNode)7 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)7 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)7 ArrayList (java.util.ArrayList)6 Parameter (org.codehaus.groovy.ast.Parameter)6 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)6 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)6 Token (org.codehaus.groovy.syntax.Token)6 LinkedList (java.util.LinkedList)5 PropertyNode (org.codehaus.groovy.ast.PropertyNode)5 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)5 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)5