use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class AutoCloneASTTransformation 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;
cNode.addInterface(CLONEABLE_TYPE);
boolean includeFields = memberHasValue(anno, "includeFields", true);
AutoCloneStyle style = getStyle(anno, "style");
List<String> excludes = getMemberStringList(anno, "excludes");
if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields))
return;
List<FieldNode> list = getInstancePropertyFields(cNode);
if (includeFields) {
list.addAll(getInstanceNonPropertyFields(cNode));
}
if (style == null)
style = AutoCloneStyle.CLONE;
switch(style) {
case COPY_CONSTRUCTOR:
createCloneCopyConstructor(cNode, list, excludes);
break;
case SERIALIZATION:
createCloneSerialization(cNode);
break;
case SIMPLE:
createSimpleClone(cNode, list, excludes);
break;
default:
createClone(cNode, list, excludes);
break;
}
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class NewifyASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
this.source = source;
if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
internalError("Expecting [AnnotationNode, AnnotatedClass] but got: " + Arrays.asList(nodes));
}
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(node.getClassNode())) {
internalError("Transformation called from wrong annotation: " + node.getClassNode().getName());
}
boolean autoFlag = determineAutoFlag(node.getMember("auto"));
Expression value = node.getMember("value");
if (parent instanceof ClassNode) {
newifyClass((ClassNode) parent, autoFlag, determineClasses(value, false));
} else if (parent instanceof MethodNode || parent instanceof FieldNode) {
newifyMethodOrField(parent, autoFlag, determineClasses(value, false));
} else if (parent instanceof DeclarationExpression) {
newifyDeclaration((DeclarationExpression) parent, autoFlag, determineClasses(value, true));
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class PackageScopeASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
boolean legacyMode = LEGACY_TYPE_NAME.equals(node.getClassNode().getName());
if (!MY_TYPE.equals(node.getClassNode()) && !legacyMode)
return;
Expression value = node.getMember("value");
if (parent instanceof ClassNode) {
List<groovy.transform.PackageScopeTarget> targets;
if (value == null)
targets = Collections.singletonList(legacyMode ? PackageScopeTarget.FIELDS : PackageScopeTarget.CLASS);
else
targets = determineTargets(value);
visitClassNode((ClassNode) parent, targets);
parent.getAnnotations();
} else {
if (value != null) {
addError("Error during " + MY_TYPE_NAME + " processing: " + TARGET_CLASS_NAME + " only allowed at class level.", parent);
return;
}
if (parent instanceof MethodNode) {
visitMethodNode((MethodNode) parent);
} else if (parent instanceof FieldNode) {
visitFieldNode((FieldNode) parent);
}
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class ReadWriteLockASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
final boolean isWriteLock;
if (READ_LOCK_TYPE.equals(node.getClassNode())) {
isWriteLock = false;
} else if (WRITE_LOCK_TYPE.equals(node.getClassNode())) {
isWriteLock = true;
} else {
throw new GroovyBugError("Internal error: expecting [" + READ_LOCK_TYPE.getName() + ", " + WRITE_LOCK_TYPE.getName() + "]" + " but got: " + node.getClassNode().getName());
}
String myTypeName = "@" + node.getClassNode().getNameWithoutPackage();
String value = getMemberStringValue(node, "value");
if (parent instanceof MethodNode) {
MethodNode mNode = (MethodNode) parent;
ClassNode cNode = mNode.getDeclaringClass();
String lockExpr = determineLock(value, cNode, mNode.isStatic(), myTypeName);
if (lockExpr == null)
return;
// get lock type
final Expression lockType;
if (isWriteLock) {
lockType = callX(varX(lockExpr, LOCK_TYPE), "writeLock");
} else {
lockType = callX(varX(lockExpr, LOCK_TYPE), "readLock");
}
Expression acquireLock = callX(lockType, "lock");
Expression releaseLock = callX(lockType, "unlock");
Statement originalCode = mNode.getCode();
mNode.setCode(block(stmt(acquireLock), new TryCatchStatement(originalCode, stmt(releaseLock))));
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class SingletonASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
if (parent instanceof ClassNode) {
ClassNode classNode = (ClassNode) parent;
String propertyName = getMemberStringValue(node, "property", "instance");
boolean isLazy = memberHasValue(node, "lazy", true);
boolean isStrict = !memberHasValue(node, "strict", false);
createField(classNode, propertyName, isLazy, isStrict);
}
}
Aggregations