use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class TypeSignatureParser method createWildcard.
private static GenericsType createWildcard(ClassNode[] upper, ClassNode lower) {
ClassNode base = ClassHelper.makeWithoutCaching("?");
base.setRedirect(ClassHelper.OBJECT_TYPE);
GenericsType t = new GenericsType(base, upper, lower);
t.setWildcard(true);
return t;
}
use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class SignatureCodecVersion1 method doEncode.
private void doEncode(final ClassNode node, DataOutputStream dos) throws IOException {
dos.writeUTF(node.getClass().getSimpleName());
if (node instanceof UnionTypeClassNode) {
UnionTypeClassNode union = (UnionTypeClassNode) node;
ClassNode[] delegates = union.getDelegates();
dos.writeInt(delegates.length);
for (ClassNode delegate : delegates) {
doEncode(delegate, dos);
}
return;
} else if (node instanceof WideningCategories.LowestUpperBoundClassNode) {
WideningCategories.LowestUpperBoundClassNode lub = (WideningCategories.LowestUpperBoundClassNode) node;
dos.writeUTF(lub.getLubName());
doEncode(lub.getUnresolvedSuperClass(), dos);
ClassNode[] interfaces = lub.getInterfaces();
if (interfaces == null) {
dos.writeInt(-1);
} else {
dos.writeInt(interfaces.length);
for (ClassNode anInterface : interfaces) {
doEncode(anInterface, dos);
}
}
return;
}
if (node.isArray()) {
dos.writeBoolean(true);
doEncode(node.getComponentType(), dos);
} else {
dos.writeBoolean(false);
dos.writeUTF(BytecodeHelper.getTypeDescription(node));
dos.writeBoolean(node.isUsingGenerics());
GenericsType[] genericsTypes = node.getGenericsTypes();
if (genericsTypes == null) {
dos.writeInt(-1);
} else {
dos.writeInt(genericsTypes.length);
for (GenericsType type : genericsTypes) {
dos.writeBoolean(type.isPlaceholder());
dos.writeBoolean(type.isWildcard());
doEncode(type.getType(), dos);
ClassNode lb = type.getLowerBound();
if (lb == null) {
dos.writeBoolean(false);
} else {
dos.writeBoolean(true);
doEncode(lb, dos);
}
ClassNode[] upperBounds = type.getUpperBounds();
if (upperBounds == null) {
dos.writeInt(-1);
} else {
dos.writeInt(upperBounds.length);
for (ClassNode bound : upperBounds) {
doEncode(bound, dos);
}
}
}
}
}
}
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 Java5 method configureWildcardType.
private ClassNode configureWildcardType(WildcardType wildcardType) {
ClassNode base = ClassHelper.makeWithoutCaching("?");
base.setRedirect(ClassHelper.OBJECT_TYPE);
//TODO: more than one lower bound for wildcards?
ClassNode[] lowers = configureTypes(wildcardType.getLowerBounds());
ClassNode lower = null;
// TODO: is it safe to remove this? What was the original intention?
if (lowers != null)
lower = lowers[0];
ClassNode[] upper = configureTypes(wildcardType.getUpperBounds());
GenericsType t = new GenericsType(base, upper, lower);
t.setWildcard(true);
ClassNode ref = ClassHelper.makeWithoutCaching(Object.class, false);
ref.setGenericsTypes(new GenericsType[] { t });
return ref;
}
Aggregations