use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.
the class CategoryASTTransformation method visit.
@Override
public void visit(final ASTNode[] nodes, final SourceUnit sourceUnit) {
if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof ClassNode)) {
sourceUnit.addError(new SyntaxException("@Category can only be added to a ClassNode but got: " + (nodes.length == 2 ? nodes[1] : "nothing"), nodes[0].getLineNumber(), nodes[0].getColumnNumber()));
}
ClassNode sourceClass = (ClassNode) nodes[1];
// the declared type of "self"
ClassNode targetClass;
Expression value = ((AnnotationNode) nodes[0]).getMember("value");
if (value instanceof ClassExpression) {
targetClass = value.getType();
} else {
// TODO: ClassHelper.make((Class<?>)groovy.lang.Category.class.getMethod("value").getDefaultValue());
targetClass = ClassHelper.OBJECT_TYPE;
sourceUnit.addErrorAndContinue(new SyntaxException("@Category must define 'value' which is the class to apply this category to", nodes[0]));
}
if (ensureNoInstanceFieldOrProperty(sourceClass, sourceUnit)) {
transformReferencesToThis(targetClass, sourceClass, sourceUnit);
new VariableScopeVisitor(sourceUnit, true).visitClass(sourceClass);
}
}
use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.
the class ClassCodeVisitorSupport method addError.
@Override
public void addError(String error, ASTNode node) {
SourceUnit source = getSourceUnit();
source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException(error + '\n', node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
}
use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.
the class MacroGroovyMethods method getClosureArgument.
protected static ClosureExpression getClosureArgument(SourceUnit source, MethodCallExpression call) {
TupleExpression tupleArguments = getMacroArguments(source, call);
int size = tupleArguments == null ? -1 : tupleArguments.getExpressions().size();
if (size < 1) {
source.addError(new SyntaxException("Call arguments should have at least one argument" + '\n', tupleArguments));
return null;
}
Expression result = tupleArguments.getExpression(size - 1);
if (!(result instanceof ClosureExpression)) {
source.addError(new SyntaxException("Last call argument should be a closure" + '\n', result));
return null;
}
return (ClosureExpression) result;
}
use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.
the class StaticInvocationWriter method writeDirectMethodCall.
@Override
protected boolean writeDirectMethodCall(final MethodNode target, final boolean implicitThis, final Expression receiver, final TupleExpression args) {
if (target == null)
return false;
ClassNode classNode = controller.getClassNode();
if (target instanceof ExtensionMethodNode) {
ExtensionMethodNode emn = (ExtensionMethodNode) target;
MethodVisitor mv = controller.getMethodVisitor();
MethodNode node = emn.getExtensionMethodNode();
Parameter[] parameters = node.getParameters();
ClassNode returnType = node.getReturnType();
List<Expression> argumentList = new ArrayList<>();
if (emn.isStaticExtension()) {
argumentList.add(nullX());
} else {
Expression fixedReceiver = null;
if (isThisOrSuper(receiver) && classNode.getOuterClass() != null && controller.isInGeneratedFunction()) {
ClassNode current = classNode.getOuterClass();
fixedReceiver = varX("thisObject", current);
// adjust for multiple levels of nesting if needed
while (current.getOuterClass() != null && !classNode.equals(current)) {
FieldNode thisField = current.getField("this$0");
current = current.getOuterClass();
if (thisField != null) {
fixedReceiver = propX(fixedReceiver, "this$0");
fixedReceiver.setType(current);
}
}
}
argumentList.add(fixedReceiver != null ? fixedReceiver : receiver);
}
argumentList.addAll(args.getExpressions());
loadArguments(argumentList, parameters);
String owner = BytecodeHelper.getClassInternalName(node.getDeclaringClass());
String desc = BytecodeHelper.getMethodDescriptor(returnType, parameters);
mv.visitMethodInsn(INVOKESTATIC, owner, target.getName(), desc, false);
controller.getOperandStack().remove(argumentList.size());
if (isPrimitiveVoid(returnType)) {
returnType = ClassHelper.OBJECT_TYPE;
mv.visitInsn(ACONST_NULL);
}
controller.getOperandStack().push(returnType);
return true;
}
if (target == StaticTypeCheckingVisitor.CLOSURE_CALL_VARGS) {
// wrap arguments into an array
Expression arr = new ArrayExpression(ClassHelper.OBJECT_TYPE, args.getExpressions());
return super.writeDirectMethodCall(target, implicitThis, receiver, args(arr));
}
if (!target.isPublic() && controller.isInGeneratedFunction() && target.getDeclaringClass() != classNode) {
if (!tryBridgeMethod(target, receiver, implicitThis, args, classNode)) {
// replace call with an invoker helper call
MethodNode methodNode = target.isStatic() ? INVOKERHELPER_INVOKESTATICMETHOD : INVOKERHELPER_INVOKEMETHOD;
MethodCallExpression mce = callX(classX(INVOKERHELPER_CLASSNODE), methodNode.getName(), args(target.isStatic() ? classX(target.getDeclaringClass()) : receiver, constX(target.getName()), new ArrayExpression(ClassHelper.OBJECT_TYPE, args.getExpressions())));
mce.setMethodTarget(methodNode);
mce.visit(controller.getAcg());
}
return true;
}
if (target.isPrivate() && tryPrivateMethod(target, implicitThis, receiver, args, classNode)) {
return true;
}
Expression fixedReceiver = null;
boolean fixedImplicitThis = implicitThis;
if (target.isProtected()) {
ClassNode node = receiver == null ? ClassHelper.OBJECT_TYPE : controller.getTypeChooser().resolveType(receiver, controller.getClassNode());
if (!implicitThis && !isThisOrSuper(receiver) && !samePackageName(node, classNode) && StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(node, target.getDeclaringClass())) {
controller.getSourceUnit().addError(new SyntaxException("Method " + target.getName() + " is protected in " + target.getDeclaringClass().toString(false), receiver != null ? receiver : args));
} else if (!node.isDerivedFrom(target.getDeclaringClass()) && tryBridgeMethod(target, receiver, implicitThis, args, classNode)) {
return true;
}
} else if (target.isPublic() && receiver != null) {
if (implicitThis && controller.isInGeneratedFunction() && !classNode.isDerivedFrom(target.getDeclaringClass()) && !classNode.implementsInterface(target.getDeclaringClass())) {
ClassNode thisType = controller.getThisType();
// GROOVY-7242
if (isTrait(thisType.getOuterClass()))
thisType = ClassHelper.dynamicType();
fixedReceiver = varX("thisObject", thisType);
// account for multiple levels of inner types
while (thisType.getOuterClass() != null && !target.getDeclaringClass().equals(thisType)) {
FieldNode thisField = thisType.getField("this$0");
thisType = thisType.getOuterClass();
if (thisField != null) {
fixedReceiver = propX(fixedReceiver, "this$0");
fixedReceiver.setType(thisType);
fixedImplicitThis = false;
}
}
}
}
if (receiver != null && !isSuperExpression(receiver)) {
// in order to avoid calls to castToType, which is the dynamic behaviour, we make sure that we call CHECKCAST instead then replace the top operand type
return super.writeDirectMethodCall(target, fixedImplicitThis, new CheckcastReceiverExpression(fixedReceiver != null ? fixedReceiver : receiver, target), args);
}
return super.writeDirectMethodCall(target, implicitThis, receiver, args);
}
use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.
the class TraitASTTransformation method processField.
private void processField(final FieldNode field, final MethodNode initializer, final MethodNode staticInitializer, final ClassNode fieldHelper, final ClassNode helper, final ClassNode staticFieldHelper, final ClassNode trait, final Set<String> knownFields) {
if (field.isProtected()) {
sourceUnit.addError(new SyntaxException("Cannot have protected field in a trait (" + trait.getName() + "#" + field.getName() + ")", field.getLineNumber(), field.getColumnNumber()));
return;
}
Expression initialExpression = field.getInitialExpression();
MethodNode selectedMethod = field.isStatic() ? staticInitializer : initializer;
ClassNode target = field.isStatic() && staticFieldHelper != null ? staticFieldHelper : fieldHelper;
if (initialExpression != null) {
VariableExpression thisObject = varX(selectedMethod.getParameters()[0]);
ExpressionStatement initCode = new ExpressionStatement(initialExpression);
processBody(thisObject, initCode, trait, helper, fieldHelper, knownFields);
if (field.isFinal()) {
String baseName = field.isStatic() ? Traits.STATIC_INIT_METHOD : Traits.INIT_METHOD;
MethodNode fieldInitializer = new MethodNode(baseName + Traits.remappedFieldName(trait, field.getName()), ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, field.getOriginType(), new Parameter[] { createSelfParameter(trait, field.isStatic()) }, ClassNode.EMPTY_ARRAY, returnS(initCode.getExpression()));
helper.addMethod(fieldInitializer);
} else {
BlockStatement code = (BlockStatement) selectedMethod.getCode();
MethodCallExpression mce;
if (field.isStatic()) {
if (staticFieldHelper != null) {
target = staticFieldHelper;
}
mce = callX(classX(InvokerHelper.class), "invokeStaticMethod", args(thisObject, constX(Traits.helperSetterName(field)), initCode.getExpression()));
} else {
mce = callX(castX(createReceiverType(field.isStatic(), fieldHelper), thisObject), Traits.helperSetterName(field), castX(field.getOriginType(), initCode.getExpression()));
}
mce.setImplicitThis(false);
mce.setSourcePosition(initialExpression);
code.addStatement(stmt(mce));
}
}
// define setter/getter helper methods (setter added even for final fields for legacy compatibility)
target.addMethod(Traits.helperSetterName(field), ACC_PUBLIC | ACC_ABSTRACT, field.getOriginType(), new Parameter[] { new Parameter(field.getOriginType(), "val") }, ClassNode.EMPTY_ARRAY, null);
target.addMethod(Traits.helperGetterName(field), ACC_PUBLIC | ACC_ABSTRACT, field.getOriginType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
// dummy fields are only used to carry annotations if instance field
// and to differentiate from static fields otherwise
int mods = field.getModifiers() & Traits.FIELD_PREFIX_MASK;
String dummyFieldName = String.format("$0x%04x", mods) + Traits.remappedFieldName(field.getOwner(), field.getName());
FieldNode dummyField = new FieldNode(dummyFieldName, ACC_PUBLIC | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, field.getOriginType(), fieldHelper, null);
// copy annotations from field to dummy field
List<AnnotationNode> copied = new LinkedList<>();
List<AnnotationNode> notCopied = new LinkedList<>();
GeneralUtils.copyAnnotatedNodeAnnotations(field, copied, notCopied);
dummyField.addAnnotations(copied);
fieldHelper.addField(dummyField);
// retain legacy field (will be given lower precedence than above)
dummyFieldName = (field.isStatic() ? Traits.STATIC_FIELD_PREFIX : Traits.FIELD_PREFIX) + (field.isPublic() ? Traits.PUBLIC_FIELD_PREFIX : Traits.PRIVATE_FIELD_PREFIX) + Traits.remappedFieldName(field.getOwner(), field.getName());
dummyField = new FieldNode(dummyFieldName, ACC_PUBLIC | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, field.getOriginType(), fieldHelper, null);
// copy annotations from field to legacy dummy field
copied = new LinkedList<>();
notCopied = new LinkedList<>();
GeneralUtils.copyAnnotatedNodeAnnotations(field, copied, notCopied);
dummyField.addAnnotations(copied);
fieldHelper.addField(dummyField);
}
Aggregations