use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.
the class MopWriter method getMopMethodName.
/**
* creates a MOP method name from a method
*
* @param method the method to be called by the mop method
* @param useThis if true, then it is a call on "this", "super" else
* @return the mop method name
*/
public static String getMopMethodName(MethodNode method, boolean useThis) {
ClassNode declaringNode = method.getDeclaringClass();
int distance = 0;
for (; declaringNode != null; declaringNode = declaringNode.getSuperClass()) {
distance++;
}
return (useThis ? "this" : "super") + "$" + distance + "$" + method.getName();
}
use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.
the class ImportCustomizer method addImport.
private void addImport(final String className) {
final ClassNode node = ClassHelper.make(className);
imports.add(new Import(ImportType.regular, node.getNameWithoutPackage(), node));
}
use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.
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);
createHelperClass(cNode);
}
}
use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.
the class TraitComposer method doCreateSuperForwarder.
/**
* Creates a method to dispatch to "super traits" in a "stackable" fashion. The generated method looks like this:
* <p>
* <code>ReturnType trait$super$method(Class clazz, Arg1 arg1, Arg2 arg2, ...) {
* if (SomeTrait.is(A) { return SomeOtherTrait$Trait$Helper.method(this, arg1, arg2) }
* super.method(arg1,arg2)
* }</code>
* </p>
* @param targetNode
* @param forwarderMethod
* @param interfacesToGenerateForwarderFor
* @param genericsSpec
*/
private static void doCreateSuperForwarder(ClassNode targetNode, MethodNode forwarderMethod, ClassNode[] interfacesToGenerateForwarderFor, Map<String, ClassNode> genericsSpec) {
Parameter[] parameters = forwarderMethod.getParameters();
Parameter[] superForwarderParams = new Parameter[parameters.length];
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
ClassNode originType = parameter.getOriginType();
superForwarderParams[i] = new Parameter(correctToGenericsSpecRecurse(genericsSpec, originType), parameter.getName());
}
for (int i = 0; i < interfacesToGenerateForwarderFor.length; i++) {
final ClassNode current = interfacesToGenerateForwarderFor[i];
final ClassNode next = i < interfacesToGenerateForwarderFor.length - 1 ? interfacesToGenerateForwarderFor[i + 1] : null;
String forwarderName = Traits.getSuperTraitMethodName(current, forwarderMethod.getName());
if (targetNode.getDeclaredMethod(forwarderName, superForwarderParams) == null) {
ClassNode returnType = correctToGenericsSpecRecurse(genericsSpec, forwarderMethod.getReturnType());
Statement delegate = next == null ? createSuperFallback(forwarderMethod, returnType) : createDelegatingForwarder(forwarderMethod, next);
MethodNode methodNode = targetNode.addMethod(forwarderName, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, returnType, superForwarderParams, ClassNode.EMPTY_ARRAY, delegate);
methodNode.setGenericsTypes(forwarderMethod.getGenericsTypes());
}
}
}
use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.
the class TraitComposer method createSuperForwarder.
/**
* Creates, if necessary, a super forwarder method, for stackable traits.
* @param forwarder a forwarder method
* @param genericsSpec
*/
private static void createSuperForwarder(ClassNode targetNode, MethodNode forwarder, final Map<String, ClassNode> genericsSpec) {
List<ClassNode> interfaces = new ArrayList<ClassNode>(Traits.collectAllInterfacesReverseOrder(targetNode, new LinkedHashSet<ClassNode>()));
String name = forwarder.getName();
Parameter[] forwarderParameters = forwarder.getParameters();
LinkedHashSet<ClassNode> traits = new LinkedHashSet<ClassNode>();
List<MethodNode> superForwarders = new LinkedList<MethodNode>();
for (ClassNode node : interfaces) {
if (Traits.isTrait(node)) {
MethodNode method = node.getDeclaredMethod(name, forwarderParameters);
if (method != null) {
// a similar method exists, we need a super bridge
// trait$super$foo(Class currentTrait, ...)
traits.add(node);
superForwarders.add(method);
}
}
}
for (MethodNode superForwarder : superForwarders) {
doCreateSuperForwarder(targetNode, superForwarder, traits.toArray(new ClassNode[traits.size()]), genericsSpec);
}
}
Aggregations