use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class DelegateASTTransformation method genericPlaceholderNames.
private static List<String> genericPlaceholderNames(MethodNode candidate) {
GenericsType[] candidateGenericsTypes = candidate.getGenericsTypes();
List<String> names = new ArrayList<String>();
if (candidateGenericsTypes != null) {
for (GenericsType gt : candidateGenericsTypes) {
names.add(gt.getName());
}
}
return names;
}
use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class AntlrParserPlugin method annotationDef.
protected void annotationDef(AST classDef) {
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
AST node = classDef.getFirstChild();
int modifiers = Opcodes.ACC_PUBLIC;
if (isType(MODIFIERS, node)) {
modifiers = modifiers(node, annotations, modifiers);
checkNoInvalidModifier(classDef, "Annotation Definition", modifiers, Opcodes.ACC_SYNCHRONIZED, "synchronized");
node = node.getNextSibling();
}
modifiers |= Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_ANNOTATION;
String name = identifier(node);
node = node.getNextSibling();
ClassNode superClass = ClassHelper.OBJECT_TYPE;
GenericsType[] genericsType = null;
if (isType(TYPE_PARAMETERS, node)) {
genericsType = makeGenericsType(node);
node = node.getNextSibling();
}
ClassNode[] interfaces = ClassNode.EMPTY_ARRAY;
if (isType(EXTENDS_CLAUSE, node)) {
interfaces = interfaces(node);
node = node.getNextSibling();
}
boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
modifiers &= ~Opcodes.ACC_SYNTHETIC;
classNode = new ClassNode(dot(getPackageName(), name), modifiers, superClass, interfaces, null);
classNode.setSyntheticPublic(syntheticPublic);
classNode.addAnnotations(annotations);
classNode.setGenericsTypes(genericsType);
classNode.addInterface(ClassHelper.Annotation_TYPE);
configureAST(classNode, classDef);
assertNodeType(OBJBLOCK, node);
objectBlock(node);
output.addClass(classNode);
classNode = null;
}
use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class AntlrParserPlugin method makeGenericsArgumentType.
private GenericsType makeGenericsArgumentType(AST typeArgument) {
GenericsType gt;
AST rootNode = typeArgument.getFirstChild();
if (isType(WILDCARD_TYPE, rootNode)) {
ClassNode base = ClassHelper.makeWithoutCaching("?");
if (rootNode.getNextSibling() != null) {
int boundType = getBoundType(rootNode.getNextSibling());
ClassNode[] gts = makeGenericsBounds(rootNode, boundType);
if (boundType == TYPE_UPPER_BOUNDS) {
gt = new GenericsType(base, gts, null);
} else {
gt = new GenericsType(base, null, gts[0]);
}
} else {
gt = new GenericsType(base, null, null);
}
gt.setName("?");
gt.setWildcard(true);
} else {
ClassNode argument = makeTypeWithArguments(rootNode);
gt = new GenericsType(argument);
}
configureAST(gt, typeArgument);
return gt;
}
use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class AntlrParserPlugin method methodDef.
protected void methodDef(AST methodDef) {
MethodNode oldNode = methodNode;
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
AST node = methodDef.getFirstChild();
GenericsType[] generics = null;
if (isType(TYPE_PARAMETERS, node)) {
generics = makeGenericsType(node);
node = node.getNextSibling();
}
int modifiers = Opcodes.ACC_PUBLIC;
if (isType(MODIFIERS, node)) {
modifiers = modifiers(node, annotations, modifiers);
checkNoInvalidModifier(methodDef, "Method", modifiers, Opcodes.ACC_VOLATILE, "volatile");
node = node.getNextSibling();
}
if (isAnInterface()) {
modifiers |= Opcodes.ACC_ABSTRACT;
}
ClassNode returnType = null;
if (isType(TYPE, node)) {
returnType = makeTypeWithArguments(node);
node = node.getNextSibling();
}
String name = identifier(node);
if (classNode != null && !classNode.isAnnotationDefinition()) {
if (classNode.getNameWithoutPackage().equals(name)) {
if (isAnInterface()) {
throw new ASTRuntimeException(methodDef, "Constructor not permitted within an interface.");
}
throw new ASTRuntimeException(methodDef, "Invalid constructor format. Remove '" + returnType.getName() + "' as the return type if you want a constructor, or use a different name if you want a method.");
}
}
node = node.getNextSibling();
Parameter[] parameters = Parameter.EMPTY_ARRAY;
ClassNode[] exceptions = ClassNode.EMPTY_ARRAY;
if (classNode == null || !classNode.isAnnotationDefinition()) {
assertNodeType(PARAMETERS, node);
parameters = parameters(node);
if (parameters == null)
parameters = Parameter.EMPTY_ARRAY;
node = node.getNextSibling();
if (isType(LITERAL_throws, node)) {
AST throwsNode = node.getFirstChild();
List<ClassNode> exceptionList = new ArrayList<ClassNode>();
throwsList(throwsNode, exceptionList);
exceptions = exceptionList.toArray(exceptions);
node = node.getNextSibling();
}
}
boolean hasAnnotationDefault = false;
Statement code = null;
boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
modifiers &= ~Opcodes.ACC_SYNTHETIC;
methodNode = new MethodNode(name, modifiers, returnType, parameters, exceptions, code);
if ((modifiers & Opcodes.ACC_ABSTRACT) == 0) {
if (node == null) {
throw new ASTRuntimeException(methodDef, "You defined a method without body. Try adding a body, or declare it abstract.");
}
assertNodeType(SLIST, node);
code = statementList(node);
} else if (node != null && classNode.isAnnotationDefinition()) {
code = statement(node);
hasAnnotationDefault = true;
} else if ((modifiers & Opcodes.ACC_ABSTRACT) > 0) {
if (node != null) {
throw new ASTRuntimeException(methodDef, "Abstract methods do not define a body.");
}
}
methodNode.setCode(code);
methodNode.addAnnotations(annotations);
methodNode.setGenericsTypes(generics);
methodNode.setAnnotationDefault(hasAnnotationDefault);
methodNode.setSyntheticPublic(syntheticPublic);
configureAST(methodNode, methodDef);
if (classNode != null) {
classNode.addMethod(methodNode);
} else {
output.addMethod(methodNode);
}
methodNode = oldNode;
}
use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class InitializerStrategy method createBuilderMethodForField.
private MethodNode createBuilderMethodForField(ClassNode builder, List<FieldNode> fields, String prefix, int fieldPos) {
String fieldName = fields.get(fieldPos).getName();
String setterName = getSetterName(prefix, fieldName);
GenericsType[] gtypes = new GenericsType[fields.size()];
List<Expression> argList = new ArrayList<Expression>();
for (int i = 0; i < fields.size(); i++) {
gtypes[i] = i == fieldPos ? new GenericsType(ClassHelper.make(SET.class)) : makePlaceholder(i);
argList.add(i == fieldPos ? propX(varX("this"), constX(fieldName)) : varX(fields.get(i).getName()));
}
ClassNode returnType = makeClassSafeWithGenerics(builder, gtypes);
FieldNode fNode = fields.get(fieldPos);
Map<String, ClassNode> genericsSpec = createGenericsSpec(fNode.getDeclaringClass());
extractSuperClassGenerics(fNode.getType(), builder, genericsSpec);
ClassNode correctedType = correctToGenericsSpecRecurse(genericsSpec, fNode.getType());
return new MethodNode(setterName, ACC_PUBLIC, returnType, params(param(correctedType, fieldName)), NO_EXCEPTIONS, block(stmt(assignX(propX(varX("this"), constX(fieldName)), varX(fieldName, correctedType))), returnS(ctorX(returnType, args(argList)))));
}
Aggregations