use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.
the class MacroGroovyMethods method getMacroArguments.
protected static TupleExpression getMacroArguments(SourceUnit source, MethodCallExpression call) {
Expression macroCallArguments = call.getArguments();
if (macroCallArguments == null) {
source.addError(new SyntaxException("Call should have arguments" + '\n', call));
return null;
}
if (!(macroCallArguments instanceof TupleExpression)) {
source.addError(new SyntaxException("Call should have TupleExpression as arguments" + '\n', macroCallArguments));
return null;
}
TupleExpression tupleArguments = (TupleExpression) macroCallArguments;
if (tupleArguments.getExpressions() == null) {
source.addError(new SyntaxException("Call arguments should have expressions" + '\n', tupleArguments));
return null;
}
return tupleArguments;
}
use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.
the class SyntaxErrorReportable method collectSyntaxError.
default void collectSyntaxError(GinqSyntaxError ginqSyntaxError) {
SourceUnit sourceUnit = getSourceUnit();
SyntaxException e = new SyntaxException(ginqSyntaxError.getMessage(), ginqSyntaxError, ginqSyntaxError.getLine(), ginqSyntaxError.getColumn());
sourceUnit.getErrorCollector().addFatalError(new SyntaxErrorMessage(e, sourceUnit));
}
use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.
the class TraitASTTransformation method createHelperClass.
private ClassNode createHelperClass(final ClassNode cNode) {
ClassNode helper = new InnerClassNode(cNode, Traits.helperClassName(cNode), ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_SYNTHETIC, OBJECT_TYPE, ClassNode.EMPTY_ARRAY, null);
// GROOVY-7242, GROOVY-7456, etc.
helper.setStaticClass(true);
cNode.setModifiers(ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE);
checkInnerClasses(cNode);
MethodNode initializer = createInitMethod(false, cNode, helper);
MethodNode staticInitializer = createInitMethod(true, cNode, helper);
// apply the verifier to have the property nodes generated
generatePropertyMethods(cNode);
// prepare fields
List<FieldNode> fields = new ArrayList<>();
Set<String> fieldNames = new HashSet<>();
boolean hasStatic = false;
for (FieldNode field : cNode.getFields()) {
if (!"metaClass".equals(field.getName()) && (!field.isSynthetic() || field.getName().indexOf('$') < 0)) {
fields.add(field);
fieldNames.add(field.getName());
if (field.isStatic()) {
hasStatic = true;
}
}
}
ClassNode fieldHelper = null;
ClassNode staticFieldHelper = null;
if (!fields.isEmpty()) {
fieldHelper = new InnerClassNode(cNode, Traits.fieldHelperClassName(cNode), ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE | ACC_SYNTHETIC, OBJECT_TYPE);
fieldHelper.setStaticClass(true);
if (hasStatic) {
staticFieldHelper = new InnerClassNode(cNode, Traits.staticFieldHelperClassName(cNode), ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE | ACC_SYNTHETIC, OBJECT_TYPE);
staticFieldHelper.setStaticClass(true);
}
}
// add fields
for (FieldNode field : fields) {
processField(field, initializer, staticInitializer, fieldHelper, helper, staticFieldHelper, cNode, fieldNames);
}
// add methods
List<MethodNode> methods = new ArrayList<>(cNode.getMethods());
List<MethodNode> nonPublicAPIMethods = new LinkedList<>();
List<Statement> staticInitStatements = null;
for (final MethodNode methodNode : methods) {
boolean declared = methodNode.getDeclaringClass() == cNode;
if (declared) {
if (!methodNode.isSynthetic() && (methodNode.isProtected() || (!methodNode.isPrivate() && !methodNode.isPublic()))) {
sourceUnit.addError(new SyntaxException("Cannot have protected/package-private method in a trait (" + cNode.getName() + "#" + methodNode.getTypeDescriptor() + ")", methodNode.getLineNumber(), methodNode.getColumnNumber()));
return null;
}
if (!methodNode.isAbstract()) {
MethodNode newMethod = processMethod(cNode, helper, methodNode, fieldHelper, fieldNames);
if (methodNode.isStaticConstructor()) {
staticInitStatements = getCodeAsBlock(newMethod).getStatements();
} else {
// add non-abstract methods; abstract methods covered from trait interface
helper.addMethod(newMethod);
}
}
if (methodNode.isPrivate() || methodNode.isStatic()) {
nonPublicAPIMethods.add(methodNode);
}
}
}
// remove methods which should not appear in the trait interface
for (MethodNode privateMethod : nonPublicAPIMethods) {
cNode.removeMethod(privateMethod);
}
// copy statements from static and instance init blocks
if (staticInitStatements != null) {
BlockStatement toBlock = getBlockStatement(staticInitializer, staticInitializer.getCode());
for (Statement next : staticInitStatements) {
toBlock.addStatement(next);
}
}
List<Statement> initStatements = cNode.getObjectInitializerStatements();
Statement toCode = initializer.getCode();
BlockStatement toBlock = getBlockStatement(initializer, toCode);
for (Statement next : initStatements) {
Parameter selfParam = createSelfParameter(cNode, false);
toBlock.addStatement(processBody(varX(selfParam), next, cNode, helper, fieldHelper, fieldNames));
}
initStatements.clear();
// clear properties to avoid generation of methods
cNode.getProperties().clear();
// copy annotations
copyClassAnnotations(cNode, helper);
markAsGenerated(cNode, helper);
// reuse the full list of fields
fields = new ArrayList<>(cNode.getFields());
for (FieldNode field : fields) {
cNode.removeField(field.getName());
}
// visit AST xforms
registerASTTransformations(helper);
sourceUnit.getAST().addClass(helper);
if (fieldHelper != null) {
sourceUnit.getAST().addClass(fieldHelper);
if (staticFieldHelper != null) {
sourceUnit.getAST().addClass(staticFieldHelper);
}
}
// resolve scope (for closures)
resolveScope(helper);
if (fieldHelper != null) {
resolveScope(fieldHelper);
if (staticFieldHelper != null) {
resolveScope(staticFieldHelper);
}
}
return helper;
}
use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.
the class NAryOperationRewriter method transformPostfixExpression.
private Expression transformPostfixExpression(final PostfixExpression exp) {
if (isInternalFieldAccess(exp.getExpression())) {
Token operation = exp.getOperation();
sourceUnit.addError(new SyntaxException("Postfix expressions on trait fields/properties are not supported in traits.", operation.getStartLine(), operation.getStartColumn()));
return exp;
} else {
return super.transform(exp);
}
}
use of org.codehaus.groovy.syntax.SyntaxException in project midpoint by Evolveum.
the class GroovyScriptEvaluator method getSandboxError.
private String getSandboxError(MultipleCompilationErrorsException e) {
List<?> errors = e.getErrorCollector().getErrors();
if (errors == null) {
return null;
}
for (Object error : errors) {
if (!(error instanceof SyntaxErrorMessage)) {
continue;
}
SyntaxException cause = ((SyntaxErrorMessage) error).getCause();
if (cause == null) {
continue;
}
String causeMessage = cause.getMessage();
if (causeMessage == null) {
continue;
}
int i = causeMessage.indexOf(SANDBOX_ERROR_PREFIX);
if (i < 0) {
continue;
}
return causeMessage.substring(i + SANDBOX_ERROR_PREFIX.length());
}
return null;
}
Aggregations