use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class EqualsAndHashCodeASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode anno = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(anno.getClassNode()))
return;
if (parent instanceof ClassNode) {
ClassNode cNode = (ClassNode) parent;
if (!checkNotInterface(cNode, MY_TYPE_NAME))
return;
boolean callSuper = memberHasValue(anno, "callSuper", true);
boolean cacheHashCode = memberHasValue(anno, "cache", true);
boolean useCanEqual = !memberHasValue(anno, "useCanEqual", false);
if (callSuper && cNode.getSuperClass().getName().equals("java.lang.Object")) {
addError("Error during " + MY_TYPE_NAME + " processing: callSuper=true but '" + cNode.getName() + "' has no super class.", anno);
}
boolean includeFields = memberHasValue(anno, "includeFields", true);
List<String> excludes = getMemberStringList(anno, "excludes");
List<String> includes = getMemberStringList(anno, "includes");
final boolean allNames = memberHasValue(anno, "allNames", true);
if (!checkIncludeExcludeUndefinedAware(anno, excludes, includes, MY_TYPE_NAME))
return;
if (!checkPropertyList(cNode, includes, "includes", anno, MY_TYPE_NAME, includeFields))
return;
if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields))
return;
createHashCode(cNode, cacheHashCode, includeFields, callSuper, excludes, includes, allNames);
createEquals(cNode, includeFields, callSuper, useCanEqual, excludes, includes, allNames);
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class AntlrParserPlugin method packageDef.
// Top level control structures
//-------------------------------------------------------------------------
protected void packageDef(AST packageDef) {
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
AST node = packageDef.getFirstChild();
if (isType(ANNOTATIONS, node)) {
processAnnotations(annotations, node);
node = node.getNextSibling();
}
String name = qualifiedName(node);
// TODO should we check package node doesn't already exist? conflict?
PackageNode packageNode = setPackage(name, annotations);
configureAST(packageNode, packageDef);
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class AntlrParserPlugin method forStatement.
protected Statement forStatement(AST forNode) {
AST inNode = forNode.getFirstChild();
Expression collectionExpression;
Parameter forParameter;
if (isType(CLOSURE_LIST, inNode)) {
forStatementBeingDef = true;
ClosureListExpression clist = closureListExpression(inNode);
forStatementBeingDef = false;
int size = clist.getExpressions().size();
if (size != 3) {
throw new ASTRuntimeException(inNode, "3 expressions are required for the classic for loop, you gave " + size);
}
collectionExpression = clist;
forParameter = ForStatement.FOR_LOOP_DUMMY;
} else {
AST variableNode = inNode.getFirstChild();
AST collectionNode = variableNode.getNextSibling();
ClassNode type = ClassHelper.OBJECT_TYPE;
if (isType(VARIABLE_DEF, variableNode)) {
AST node = variableNode.getFirstChild();
// skip the final modifier if it's present
if (isType(MODIFIERS, node)) {
int modifiersMask = modifiers(node, new ArrayList<AnnotationNode>(), 0);
// only final modifier allowed
if ((modifiersMask & ~Opcodes.ACC_FINAL) != 0) {
throw new ASTRuntimeException(node, "Only the 'final' modifier is allowed in front of the for loop variable.");
}
node = node.getNextSibling();
}
type = makeTypeWithArguments(node);
variableNode = node.getNextSibling();
}
String variable = identifier(variableNode);
collectionExpression = expression(collectionNode);
forParameter = new Parameter(type, variable);
configureAST(forParameter, variableNode);
}
final AST node = inNode.getNextSibling();
Statement block;
if (isType(SEMI, node)) {
block = EmptyStatement.INSTANCE;
} else {
block = statement(node);
}
ForStatement forStatement = new ForStatement(forParameter, collectionExpression, block);
configureAST(forStatement, forNode);
return forStatement;
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class AntlrParserPlugin method fieldDef.
protected void fieldDef(AST fieldDef) {
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
AST node = fieldDef.getFirstChild();
int modifiers = 0;
if (isType(MODIFIERS, node)) {
modifiers = modifiers(node, annotations, modifiers);
node = node.getNextSibling();
}
if (classNode.isInterface()) {
modifiers |= Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
if ((modifiers & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
modifiers |= Opcodes.ACC_PUBLIC;
}
}
ClassNode type = null;
if (isType(TYPE, node)) {
type = makeTypeWithArguments(node);
node = node.getNextSibling();
}
String name = identifier(node);
node = node.getNextSibling();
Expression initialValue = null;
if (node != null) {
assertNodeType(ASSIGN, node);
initialValue = expression(node.getFirstChild());
}
if (classNode.isInterface() && initialValue == null && type != null) {
initialValue = getDefaultValueForPrimitive(type);
}
FieldNode fieldNode = new FieldNode(name, modifiers, type, classNode, initialValue);
fieldNode.addAnnotations(annotations);
configureAST(fieldNode, fieldDef);
if (!hasVisibility(modifiers)) {
// let's set the modifiers on the field
int fieldModifiers = 0;
int flags = Opcodes.ACC_STATIC | Opcodes.ACC_TRANSIENT | Opcodes.ACC_VOLATILE | Opcodes.ACC_FINAL;
if (!hasVisibility(modifiers)) {
modifiers |= Opcodes.ACC_PUBLIC;
fieldModifiers |= Opcodes.ACC_PRIVATE;
}
// let's pass along any other modifiers we need
fieldModifiers |= (modifiers & flags);
fieldNode.setModifiers(fieldModifiers);
fieldNode.setSynthetic(true);
// in the case that there is already a field, we would
// like to use that field, instead of the default field
// for the property
FieldNode storedNode = classNode.getDeclaredField(fieldNode.getName());
if (storedNode != null && !classNode.hasProperty(name)) {
fieldNode = storedNode;
// we remove it here, because addProperty will add it
// again and we want to avoid it showing up multiple
// times in the fields list.
classNode.getFields().remove(storedNode);
}
PropertyNode propertyNode = new PropertyNode(fieldNode, modifiers, null, null);
configureAST(propertyNode, fieldDef);
classNode.addProperty(propertyNode);
} else {
fieldNode.setModifiers(modifiers);
// if there is a property of that name, then a field of that
// name already exists, which means this new field here should
// be used instead of the field the property originally has.
PropertyNode pn = classNode.getProperty(name);
if (pn != null && pn.getField().isSynthetic()) {
classNode.getFields().remove(pn.getField());
pn.setField(fieldNode);
}
classNode.addField(fieldNode);
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class AntlrParserPlugin method enumConstantDef.
protected void enumConstantDef(AST node) {
enumConstantBeingDef = true;
assertNodeType(ENUM_CONSTANT_DEF, node);
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
AST element = node.getFirstChild();
if (isType(ANNOTATIONS, element)) {
processAnnotations(annotations, element);
element = element.getNextSibling();
}
String identifier = identifier(element);
Expression init = null;
element = element.getNextSibling();
if (element != null) {
init = expression(element);
ClassNode innerClass;
if (element.getNextSibling() == null) {
innerClass = getAnonymousInnerClassNode(init);
if (innerClass != null) {
init = null;
}
} else {
element = element.getNextSibling();
Expression next = expression(element);
innerClass = getAnonymousInnerClassNode(next);
}
if (innerClass != null) {
// we have to handle an enum constant with a class overriding
// a method in which case we need to configure the inner class
innerClass.setSuperClass(classNode.getPlainNodeReference());
innerClass.setModifiers(classNode.getModifiers() | Opcodes.ACC_FINAL);
// we use a ClassExpression for transportation to EnumVisitor
Expression inner = new ClassExpression(innerClass);
if (init == null) {
ListExpression le = new ListExpression();
le.addExpression(inner);
init = le;
} else {
if (init instanceof ListExpression) {
((ListExpression) init).addExpression(inner);
} else {
ListExpression le = new ListExpression();
le.addExpression(init);
le.addExpression(inner);
init = le;
}
}
// and remove the final modifier from classNode to allow the sub class
classNode.setModifiers(classNode.getModifiers() & ~Opcodes.ACC_FINAL);
} else if (isType(ELIST, element)) {
if (init instanceof ListExpression && !((ListExpression) init).isWrapped()) {
ListExpression le = new ListExpression();
le.addExpression(init);
init = le;
}
}
}
FieldNode enumField = EnumHelper.addEnumConstant(classNode, identifier, init);
enumField.addAnnotations(annotations);
configureAST(enumField, node);
enumConstantBeingDef = false;
}
Aggregations