use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.
the class ASTTransformationCollectorCodeVisitor method addCollectedAnnotations.
private boolean addCollectedAnnotations(List<AnnotationNode> collected, AnnotationNode aliasNode, AnnotatedNode origin) {
ClassNode classNode = aliasNode.getClassNode();
boolean ret = false;
for (AnnotationNode annotation : classNode.getAnnotations()) {
if (annotation.getClassNode().getName().equals(AnnotationCollector.class.getName())) {
Expression processorExp = annotation.getMember("processor");
AnnotationCollectorTransform act = null;
assertStringConstant(processorExp);
if (processorExp != null) {
String className = (String) ((ConstantExpression) processorExp).getValue();
Class klass = loadTransformClass(className, aliasNode);
if (klass != null) {
try {
act = (AnnotationCollectorTransform) klass.newInstance();
} catch (InstantiationException e) {
source.getErrorCollector().addErrorAndContinue(new ExceptionMessage(e, true, source));
} catch (IllegalAccessException e) {
source.getErrorCollector().addErrorAndContinue(new ExceptionMessage(e, true, source));
}
}
} else {
act = new AnnotationCollectorTransform();
}
if (act != null)
collected.addAll(act.visit(annotation, aliasNode, origin, source));
ret = true;
}
}
return ret;
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.
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 = getMemberList(anno, "excludes");
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 CLONE:
createClone(cNode, list, excludes);
break;
case SIMPLE:
createSimpleClone(cNode, list, excludes);
break;
}
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.
the class DelegateASTTransformation 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 FieldNode) {
FieldNode fieldNode = (FieldNode) parent;
final ClassNode type = fieldNode.getType();
final ClassNode owner = fieldNode.getOwner();
if (type.equals(ClassHelper.OBJECT_TYPE) || type.equals(GROOVYOBJECT_TYPE)) {
addError(MY_TYPE_NAME + " field '" + fieldNode.getName() + "' has an inappropriate type: " + type.getName() + ". Please add an explicit type but not java.lang.Object or groovy.lang.GroovyObject.", parent);
return;
}
if (type.equals(owner)) {
addError(MY_TYPE_NAME + " field '" + fieldNode.getName() + "' has an inappropriate type: " + type.getName() + ". Delegation to own type not supported. Please use a different type.", parent);
return;
}
final List<MethodNode> fieldMethods = getAllMethods(type);
for (ClassNode next : type.getAllInterfaces()) {
fieldMethods.addAll(getAllMethods(next));
}
final boolean skipInterfaces = memberHasValue(node, MEMBER_INTERFACES, false);
final boolean includeDeprecated = memberHasValue(node, MEMBER_DEPRECATED, true) || (type.isInterface() && !skipInterfaces);
List<String> excludes = getMemberList(node, MEMBER_EXCLUDES);
List<String> includes = getMemberList(node, MEMBER_INCLUDES);
List<ClassNode> excludeTypes = getClassList(node, MEMBER_EXCLUDE_TYPES);
List<ClassNode> includeTypes = getClassList(node, MEMBER_INCLUDE_TYPES);
checkIncludeExclude(node, excludes, includes, excludeTypes, includeTypes, MY_TYPE_NAME);
final List<MethodNode> ownerMethods = getAllMethods(owner);
for (MethodNode mn : fieldMethods) {
addDelegateMethod(node, fieldNode, owner, ownerMethods, mn, includeDeprecated, includes, excludes, includeTypes, excludeTypes);
}
for (PropertyNode prop : getAllProperties(type)) {
if (prop.isStatic() || !prop.isPublic())
continue;
String name = prop.getName();
addGetterIfNeeded(fieldNode, owner, prop, name, includes, excludes);
addSetterIfNeeded(fieldNode, owner, prop, name, includes, excludes);
}
if (skipInterfaces)
return;
final Set<ClassNode> allInterfaces = getInterfacesAndSuperInterfaces(type);
final Set<ClassNode> ownerIfaces = owner.getAllInterfaces();
Map<String, ClassNode> genericsSpec = createGenericsSpec(fieldNode.getDeclaringClass());
genericsSpec = createGenericsSpec(fieldNode.getType(), genericsSpec);
for (ClassNode iface : allInterfaces) {
if (Modifier.isPublic(iface.getModifiers()) && !ownerIfaces.contains(iface)) {
final ClassNode[] ifaces = owner.getInterfaces();
final ClassNode[] newIfaces = new ClassNode[ifaces.length + 1];
for (int i = 0; i < ifaces.length; i++) {
newIfaces[i] = correctToGenericsSpecRecurse(genericsSpec, ifaces[i]);
}
newIfaces[ifaces.length] = correctToGenericsSpecRecurse(genericsSpec, iface);
owner.setInterfaces(newIfaces);
}
}
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.
the class ExternalizeMethodsASTTransformation 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(EXTERNALIZABLE_TYPE);
boolean includeFields = memberHasValue(anno, "includeFields", true);
List<String> excludes = getMemberList(anno, "excludes");
List<FieldNode> list = getInstancePropertyFields(cNode);
if (includeFields) {
list.addAll(getInstanceNonPropertyFields(cNode));
}
createWriteExternal(cNode, excludes, list);
createReadExternal(cNode, excludes, list);
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.
the class FieldASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
sourceUnit = source;
if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
throw new GroovyBugError("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
}
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(node.getClassNode()))
return;
if (parent instanceof DeclarationExpression) {
DeclarationExpression de = (DeclarationExpression) parent;
ClassNode cNode = de.getDeclaringClass();
if (!cNode.isScript()) {
addError("Annotation " + MY_TYPE_NAME + " can only be used within a Script.", parent);
return;
}
candidate = de;
// GROOVY-4548: temp fix to stop CCE until proper support is added
if (de.isMultipleAssignmentDeclaration()) {
addError("Annotation " + MY_TYPE_NAME + " not supported with multiple assignment notation.", parent);
return;
}
VariableExpression ve = de.getVariableExpression();
variableName = ve.getName();
// set owner null here, it will be updated by addField
fieldNode = new FieldNode(variableName, ve.getModifiers(), ve.getType(), null, de.getRightExpression());
fieldNode.setSourcePosition(de);
cNode.addField(fieldNode);
// GROOVY-4833 : annotations that are not Groovy transforms should be transferred to the generated field
// GROOVY-6112 : also copy acceptable Groovy transforms
final List<AnnotationNode> annotations = de.getAnnotations();
for (AnnotationNode annotation : annotations) {
// GROOVY-6337 HACK: in case newly created field is @Lazy
if (annotation.getClassNode().equals(LAZY_TYPE)) {
LazyASTTransformation.visitField(annotation, fieldNode);
}
final ClassNode annotationClassNode = annotation.getClassNode();
if (notTransform(annotationClassNode) || acceptableTransform(annotation)) {
fieldNode.addAnnotation(annotation);
}
}
super.visitClass(cNode);
// GROOVY-5207 So that Closures can see newly added fields
// (not super efficient for a very large class with many @Fields but we chose simplicity
// and understandability of this solution over more complex but efficient alternatives)
VariableScopeVisitor scopeVisitor = new VariableScopeVisitor(source);
scopeVisitor.visitClass(cNode);
}
}
Aggregations