use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class GenericsVisitor method checkGenericsUsage.
private void checkGenericsUsage(ClassNode n, ClassNode cn, Boolean isAnonInnerClass) {
if (n.isGenericsPlaceHolder())
return;
GenericsType[] nTypes = n.getGenericsTypes();
GenericsType[] cnTypes = cn.getGenericsTypes();
// raw type usage is always allowed
if (nTypes == null)
return;
// you can't parameterize a non-generified type
if (cnTypes == null) {
String message = "The class " + getPrintName(n) + " (supplied with " + plural("type parameter", nTypes.length) + ") refers to the class " + getPrintName(cn) + " which takes no parameters";
if (nTypes.length == 0) {
message += " (invalid Diamond <> usage?)";
}
addError(message, n);
return;
}
// parameterize a type by using all of the parameters only
if (nTypes.length != cnTypes.length) {
if (Boolean.FALSE.equals(isAnonInnerClass) && nTypes.length == 0) {
// allow Diamond for non-AIC cases from CCE
return;
}
String message;
if (Boolean.TRUE.equals(isAnonInnerClass) && nTypes.length == 0) {
message = "Cannot use diamond <> with anonymous inner classes";
} else {
message = "The class " + getPrintName(n) + " (supplied with " + plural("type parameter", nTypes.length) + ") refers to the class " + getPrintName(cn) + " which takes " + plural("parameter", cnTypes.length);
if (nTypes.length == 0) {
message += " (invalid Diamond <> usage?)";
}
}
addError(message, n);
return;
}
// check bounds
for (int i = 0; i < nTypes.length; i++) {
ClassNode nType = nTypes[i].getType();
ClassNode cnType = cnTypes[i].getType();
if (!nType.isDerivedFrom(cnType)) {
if (cnType.isInterface() && nType.implementsInterface(cnType))
continue;
addError("The type " + nTypes[i].getName() + " is not a valid substitute for the bounded parameter <" + getPrintName(cnTypes[i]) + ">", n);
}
}
}
use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class GenericsVisitor method checkWildcard.
private boolean checkWildcard(ClassNode cn) {
ClassNode sn = cn.getUnresolvedSuperClass(false);
if (sn == null)
return false;
GenericsType[] generics = sn.getGenericsTypes();
if (generics == null)
return false;
boolean error = false;
for (GenericsType generic : generics) {
if (generic.isWildcard()) {
addError("A supertype may not specify a wildcard type", sn);
error = true;
}
}
return error;
}
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;
}
Aggregations