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);
}
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;
}
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);
}
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();
}
}
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))));
}
Aggregations