use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class TraitASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode anno = (AnnotationNode) nodes[0];
if (!Traits.TRAIT_CLASSNODE.equals(anno.getClassNode()))
return;
unit = source;
init(nodes, source);
if (parent instanceof ClassNode) {
ClassNode cNode = (ClassNode) parent;
if (!checkNotInterface(cNode, Traits.TRAIT_TYPE_NAME))
return;
checkNoConstructor(cNode);
checkExtendsClause(cNode);
generateMethodsWithDefaultArgs(cNode);
replaceExtendsByImplements(cNode);
ClassNode helperClassNode = createHelperClass(cNode);
resolveHelperClassIfNecessary(helperClassNode);
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class MyIntegerAnnoTraceASTTransformation method visit.
public void visit(ASTNode[] nodes, final SourceUnit source) {
if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
throw new RuntimeException("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
}
AnnotatedNode parent = (AnnotatedNode) nodes[1];
File f = new File("temp/log.txt");
try {
ResourceGroovyMethods.append(f, parent.getClass().getSimpleName() + " " + parent.getAnnotations().get(0).getMember("value").getText() + " ");
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
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];
DelegateDescription delegate = null;
if (parent instanceof FieldNode) {
FieldNode fieldNode = (FieldNode) parent;
delegate = new DelegateDescription();
delegate.delegate = fieldNode;
delegate.annotation = node;
delegate.name = fieldNode.getName();
delegate.type = fieldNode.getType();
delegate.owner = fieldNode.getOwner();
delegate.getOp = varX(fieldNode);
delegate.origin = "field";
} else if (parent instanceof MethodNode) {
MethodNode methodNode = (MethodNode) parent;
delegate = new DelegateDescription();
delegate.delegate = methodNode;
delegate.annotation = node;
delegate.name = methodNode.getName();
delegate.type = methodNode.getReturnType();
delegate.owner = methodNode.getDeclaringClass();
delegate.getOp = callThisX(delegate.name);
delegate.origin = "method";
if (methodNode.getParameters().length > 0) {
addError("You can only delegate to methods that take no parameters, but " + delegate.name + " takes " + methodNode.getParameters().length + " parameters.", parent);
return;
}
}
if (delegate != null) {
if (delegate.type.equals(ClassHelper.OBJECT_TYPE) || delegate.type.equals(GROOVYOBJECT_TYPE)) {
addError(MY_TYPE_NAME + " " + delegate.origin + " '" + delegate.name + "' has an inappropriate type: " + delegate.type.getName() + ". Please add an explicit type but not java.lang.Object or groovy.lang.GroovyObject.", parent);
return;
}
if (delegate.type.equals(delegate.owner)) {
addError(MY_TYPE_NAME + " " + delegate.origin + " '" + delegate.name + "' has an inappropriate type: " + delegate.type.getName() + ". Delegation to own type not supported. Please use a different type.", parent);
return;
}
final List<MethodNode> delegateMethods = getAllMethods(delegate.type);
for (ClassNode next : delegate.type.getAllInterfaces()) {
delegateMethods.addAll(getAllMethods(next));
}
final boolean skipInterfaces = memberHasValue(node, MEMBER_INTERFACES, false);
final boolean includeDeprecated = memberHasValue(node, MEMBER_DEPRECATED, true) || (delegate.type.isInterface() && !skipInterfaces);
final boolean allNames = memberHasValue(node, MEMBER_ALL_NAMES, true);
delegate.excludes = getMemberStringList(node, MEMBER_EXCLUDES);
delegate.includes = getMemberStringList(node, MEMBER_INCLUDES);
delegate.excludeTypes = getMemberClassList(node, MEMBER_EXCLUDE_TYPES);
delegate.includeTypes = getMemberClassList(node, MEMBER_INCLUDE_TYPES);
checkIncludeExcludeUndefinedAware(node, delegate.excludes, delegate.includes, delegate.excludeTypes, delegate.includeTypes, MY_TYPE_NAME);
final List<MethodNode> ownerMethods = getAllMethods(delegate.owner);
for (MethodNode mn : delegateMethods) {
addDelegateMethod(delegate, ownerMethods, mn, includeDeprecated, allNames);
}
for (PropertyNode prop : getAllProperties(delegate.type)) {
if (prop.isStatic() || !prop.isPublic())
continue;
String name = prop.getName();
addGetterIfNeeded(delegate, prop, name, allNames);
addSetterIfNeeded(delegate, prop, name, allNames);
}
if (skipInterfaces)
return;
final Set<ClassNode> allInterfaces = getInterfacesAndSuperInterfaces(delegate.type);
final Set<ClassNode> ownerIfaces = delegate.owner.getAllInterfaces();
Map<String, ClassNode> genericsSpec = createGenericsSpec(delegate.owner);
genericsSpec = createGenericsSpec(delegate.type, genericsSpec);
for (ClassNode iface : allInterfaces) {
if (Modifier.isPublic(iface.getModifiers()) && !ownerIfaces.contains(iface)) {
final ClassNode[] ifaces = delegate.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);
delegate.owner.setInterfaces(newIfaces);
}
}
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
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 = 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));
}
createWriteExternal(cNode, excludes, list);
createReadExternal(cNode, excludes, list);
}
}
use of org.codehaus.groovy.ast.AnnotatedNode in project groovy by apache.
the class ExternalizeVerifierASTTransformation 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 (!hasNoargConstructor(cNode)) {
addError(MY_TYPE_NAME + ": An Externalizable class requires a no-arg constructor but none found", cNode);
}
if (!implementsExternalizable(cNode)) {
addError(MY_TYPE_NAME + ": An Externalizable class must implement the Externalizable interface", cNode);
}
boolean includeFields = memberHasValue(anno, "includeFields", true);
boolean checkPropertyTypes = memberHasValue(anno, "checkPropertyTypes", true);
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));
}
checkProps(list, excludes, checkPropertyTypes);
}
}
Aggregations