use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class AntlrParserPlugin method annotationDef.
protected void annotationDef(AST classDef) {
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
AST node = classDef.getFirstChild();
int modifiers = Opcodes.ACC_PUBLIC;
if (isType(MODIFIERS, node)) {
modifiers = modifiers(node, annotations, modifiers);
checkNoInvalidModifier(classDef, "Annotation Definition", modifiers, Opcodes.ACC_SYNCHRONIZED, "synchronized");
node = node.getNextSibling();
}
modifiers |= Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_ANNOTATION;
String name = identifier(node);
node = node.getNextSibling();
ClassNode superClass = ClassHelper.OBJECT_TYPE;
GenericsType[] genericsType = null;
if (isType(TYPE_PARAMETERS, node)) {
genericsType = makeGenericsType(node);
node = node.getNextSibling();
}
ClassNode[] interfaces = ClassNode.EMPTY_ARRAY;
if (isType(EXTENDS_CLAUSE, node)) {
interfaces = interfaces(node);
node = node.getNextSibling();
}
boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
modifiers &= ~Opcodes.ACC_SYNTHETIC;
classNode = new ClassNode(dot(getPackageName(), name), modifiers, superClass, interfaces, null);
classNode.setSyntheticPublic(syntheticPublic);
classNode.addAnnotations(annotations);
classNode.setGenericsTypes(genericsType);
classNode.addInterface(ClassHelper.Annotation_TYPE);
configureAST(classNode, classDef);
assertNodeType(OBJBLOCK, node);
objectBlock(node);
output.addClass(classNode);
classNode = null;
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class AntlrParserPlugin method annotation.
protected AnnotationNode annotation(AST annotationNode) {
annotationBeingDef = true;
AST node = annotationNode.getFirstChild();
String name = qualifiedName(node);
AnnotationNode annotatedNode = new AnnotationNode(ClassHelper.make(name));
configureAST(annotatedNode, annotationNode);
while (true) {
node = node.getNextSibling();
if (isType(ANNOTATION_MEMBER_VALUE_PAIR, node)) {
AST memberNode = node.getFirstChild();
String param = identifier(memberNode);
Expression expression = expression(memberNode.getNextSibling());
if (annotatedNode.getMember(param) != null) {
throw new ASTRuntimeException(memberNode, "Annotation member '" + param + "' has already been associated with a value");
}
annotatedNode.setMember(param, expression);
} else {
break;
}
}
annotationBeingDef = false;
return annotatedNode;
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class AntlrParserPlugin method declarationExpression.
protected Expression declarationExpression(AST variableDef) {
AST node = variableDef.getFirstChild();
ClassNode type = null;
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
int modifiers = 0;
if (isType(MODIFIERS, node)) {
// force check of modifier conflicts
modifiers = modifiers(node, annotations, 0);
node = node.getNextSibling();
}
if (isType(TYPE, node)) {
type = makeTypeWithArguments(node);
node = node.getNextSibling();
}
Expression leftExpression;
Expression rightExpression = EmptyExpression.INSTANCE;
AST right;
if (isType(ASSIGN, node)) {
node = node.getFirstChild();
AST left = node.getFirstChild();
ArgumentListExpression alist = new ArgumentListExpression();
for (AST varDef = left; varDef != null; varDef = varDef.getNextSibling()) {
assertNodeType(VARIABLE_DEF, varDef);
DeclarationExpression de = (DeclarationExpression) declarationExpression(varDef);
alist.addExpression(de.getVariableExpression());
}
leftExpression = alist;
right = node.getNextSibling();
if (right != null)
rightExpression = expression(right);
} else {
String name = identifier(node);
VariableExpression ve = new VariableExpression(name, type);
ve.setModifiers(modifiers);
leftExpression = ve;
right = node.getNextSibling();
if (right != null) {
assertNodeType(ASSIGN, right);
rightExpression = expression(right.getFirstChild());
}
}
configureAST(leftExpression, node);
Token token = makeToken(Types.ASSIGN, variableDef);
DeclarationExpression expression = new DeclarationExpression(leftExpression, token, rightExpression);
expression.addAnnotations(annotations);
configureAST(expression, variableDef);
ExpressionStatement expressionStatement = new ExpressionStatement(expression);
configureAST(expressionStatement, variableDef);
return expression;
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class AntlrParserPlugin method methodDef.
protected void methodDef(AST methodDef) {
MethodNode oldNode = methodNode;
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
AST node = methodDef.getFirstChild();
GenericsType[] generics = null;
if (isType(TYPE_PARAMETERS, node)) {
generics = makeGenericsType(node);
node = node.getNextSibling();
}
int modifiers = Opcodes.ACC_PUBLIC;
if (isType(MODIFIERS, node)) {
modifiers = modifiers(node, annotations, modifiers);
checkNoInvalidModifier(methodDef, "Method", modifiers, Opcodes.ACC_VOLATILE, "volatile");
node = node.getNextSibling();
}
if (isAnInterface()) {
modifiers |= Opcodes.ACC_ABSTRACT;
}
ClassNode returnType = null;
if (isType(TYPE, node)) {
returnType = makeTypeWithArguments(node);
node = node.getNextSibling();
}
String name = identifier(node);
if (classNode != null && !classNode.isAnnotationDefinition()) {
if (classNode.getNameWithoutPackage().equals(name)) {
if (isAnInterface()) {
throw new ASTRuntimeException(methodDef, "Constructor not permitted within an interface.");
}
throw new ASTRuntimeException(methodDef, "Invalid constructor format. Remove '" + returnType.getName() + "' as the return type if you want a constructor, or use a different name if you want a method.");
}
}
node = node.getNextSibling();
Parameter[] parameters = Parameter.EMPTY_ARRAY;
ClassNode[] exceptions = ClassNode.EMPTY_ARRAY;
if (classNode == null || !classNode.isAnnotationDefinition()) {
assertNodeType(PARAMETERS, node);
parameters = parameters(node);
if (parameters == null)
parameters = Parameter.EMPTY_ARRAY;
node = node.getNextSibling();
if (isType(LITERAL_throws, node)) {
AST throwsNode = node.getFirstChild();
List<ClassNode> exceptionList = new ArrayList<ClassNode>();
throwsList(throwsNode, exceptionList);
exceptions = exceptionList.toArray(exceptions);
node = node.getNextSibling();
}
}
boolean hasAnnotationDefault = false;
Statement code = null;
boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
modifiers &= ~Opcodes.ACC_SYNTHETIC;
methodNode = new MethodNode(name, modifiers, returnType, parameters, exceptions, code);
if ((modifiers & Opcodes.ACC_ABSTRACT) == 0) {
if (node == null) {
throw new ASTRuntimeException(methodDef, "You defined a method without body. Try adding a body, or declare it abstract.");
}
assertNodeType(SLIST, node);
code = statementList(node);
} else if (node != null && classNode.isAnnotationDefinition()) {
code = statement(node);
hasAnnotationDefault = true;
} else if ((modifiers & Opcodes.ACC_ABSTRACT) > 0) {
if (node != null) {
throw new ASTRuntimeException(methodDef, "Abstract methods do not define a body.");
}
}
methodNode.setCode(code);
methodNode.addAnnotations(annotations);
methodNode.setGenericsTypes(generics);
methodNode.setAnnotationDefault(hasAnnotationDefault);
methodNode.setSyntheticPublic(syntheticPublic);
configureAST(methodNode, methodDef);
if (classNode != null) {
classNode.addMethod(methodNode);
} else {
output.addMethod(methodNode);
}
methodNode = oldNode;
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class AnnotationVisitor method checkCircularReference.
public void checkCircularReference(ClassNode searchClass, ClassNode attrType, Expression startExp) {
if (!isValidAnnotationClass(attrType))
return;
if (!(startExp instanceof AnnotationConstantExpression)) {
addError("Found '" + startExp.getText() + "' when expecting an Annotation Constant", startExp);
return;
}
AnnotationConstantExpression ace = (AnnotationConstantExpression) startExp;
AnnotationNode annotationNode = (AnnotationNode) ace.getValue();
if (annotationNode.getClassNode().equals(searchClass)) {
addError("Circular reference discovered in " + searchClass.getName(), startExp);
return;
}
ClassNode cn = annotationNode.getClassNode();
for (MethodNode method : cn.getMethods()) {
if (method.getReturnType().equals(searchClass)) {
addError("Circular reference discovered in " + cn.getName(), startExp);
}
ReturnStatement code = (ReturnStatement) method.getCode();
if (code == null)
continue;
checkCircularReference(searchClass, method.getReturnType(), code.getExpression());
}
}
Aggregations