use of org.codehaus.groovy.ast.GenericsType in project groovy-core by groovy.
the class EnumHelper method makeEnumNode.
public static ClassNode makeEnumNode(String name, int modifiers, ClassNode[] interfaces, ClassNode outerClass) {
modifiers = modifiers | Opcodes.ACC_FINAL | Opcodes.ACC_ENUM;
ClassNode enumClass;
if (outerClass == null) {
enumClass = new ClassNode(name, modifiers, null, interfaces, MixinNode.EMPTY_ARRAY);
} else {
name = outerClass.getName() + "$" + name;
enumClass = new InnerClassNode(outerClass, name, modifiers, null, interfaces, MixinNode.EMPTY_ARRAY);
}
// set super class and generics info
// "enum X" -> class X extends Enum<X>
GenericsType gt = new GenericsType(enumClass);
ClassNode superClass = ClassHelper.makeWithoutCaching("java.lang.Enum");
superClass.setGenericsTypes(new GenericsType[] { gt });
enumClass.setSuperClass(superClass);
superClass.setRedirect(ClassHelper.Enum_Type);
return enumClass;
}
use of org.codehaus.groovy.ast.GenericsType in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method inferMapExpressionType.
protected ClassNode inferMapExpressionType(final MapExpression map) {
ClassNode mapType = LINKEDHASHMAP_CLASSNODE.getPlainNodeReference();
List<MapEntryExpression> entryExpressions = map.getMapEntryExpressions();
if (entryExpressions.isEmpty())
return mapType;
GenericsType[] genericsTypes = mapType.getGenericsTypes();
if (genericsTypes == null || genericsTypes.length < 2 || (genericsTypes.length == 2 && OBJECT_TYPE.equals(genericsTypes[0].getType()) && OBJECT_TYPE.equals(genericsTypes[1].getType()))) {
List<ClassNode> keyTypes = new LinkedList<ClassNode>();
List<ClassNode> valueTypes = new LinkedList<ClassNode>();
for (MapEntryExpression entryExpression : entryExpressions) {
keyTypes.add(getType(entryExpression.getKeyExpression()));
valueTypes.add(getType(entryExpression.getValueExpression()));
}
// to be used in generics, type must be boxed
ClassNode keyType = getWrapper(lowestUpperBound(keyTypes));
// to be used in generics, type must be boxed
ClassNode valueType = getWrapper(lowestUpperBound(valueTypes));
if (!OBJECT_TYPE.equals(keyType) || !OBJECT_TYPE.equals(valueType)) {
ClassNode inferred = mapType.getPlainNodeReference();
inferred.setGenericsTypes(new GenericsType[] { new GenericsType(wrapTypeIfNecessary(keyType)), new GenericsType(wrapTypeIfNecessary(valueType)) });
return inferred;
}
}
return mapType;
}
use of org.codehaus.groovy.ast.GenericsType in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method extractGenericsConnectionsForSuperClassAndInterfaces.
private void extractGenericsConnectionsForSuperClassAndInterfaces(final Map<String, GenericsType> resolvedPlaceholders, final Map<String, GenericsType> connections) {
for (GenericsType value : new HashSet<GenericsType>(connections.values())) {
if (!value.isPlaceholder() && !value.isWildcard()) {
ClassNode valueType = value.getType();
List<ClassNode> deepNodes = new LinkedList<ClassNode>();
ClassNode unresolvedSuperClass = valueType.getUnresolvedSuperClass();
if (unresolvedSuperClass != null && unresolvedSuperClass.isUsingGenerics()) {
deepNodes.add(unresolvedSuperClass);
}
for (ClassNode node : valueType.getUnresolvedInterfaces()) {
if (node.isUsingGenerics()) {
deepNodes.add(node);
}
}
if (!deepNodes.isEmpty()) {
for (GenericsType genericsType : resolvedPlaceholders.values()) {
ClassNode lowerBound = genericsType.getLowerBound();
if (lowerBound != null) {
for (ClassNode deepNode : deepNodes) {
if (lowerBound.equals(deepNode)) {
extractGenericsConnections(connections, deepNode, lowerBound);
}
}
}
ClassNode[] upperBounds = genericsType.getUpperBounds();
if (upperBounds != null) {
for (ClassNode upperBound : upperBounds) {
for (ClassNode deepNode : deepNodes) {
if (upperBound.equals(deepNode)) {
extractGenericsConnections(connections, deepNode, upperBound);
}
}
}
}
}
}
}
}
}
use of org.codehaus.groovy.ast.GenericsType in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method getTypeForSpreadExpression.
private ClassNode getTypeForSpreadExpression(ClassNode testClass, ClassNode objectExpressionType, PropertyExpression pexp) {
if (!pexp.isSpreadSafe())
return null;
MethodCallExpression mce = new MethodCallExpression(new VariableExpression("_", testClass), "iterator", ArgumentListExpression.EMPTY_ARGUMENTS);
mce.visit(this);
ClassNode callType = getType(mce);
if (!implementsInterfaceOrIsSubclassOf(callType, Iterator_TYPE))
return null;
GenericsType[] types = callType.getGenericsTypes();
ClassNode contentType = OBJECT_TYPE;
if (types != null && types.length == 1)
contentType = types[0].getType();
PropertyExpression subExp = new PropertyExpression(new VariableExpression("{}", contentType), pexp.getPropertyAsString());
AtomicReference<ClassNode> result = new AtomicReference<ClassNode>();
if (existsProperty(subExp, true, new PropertyLookupVisitor(result))) {
ClassNode intf = LIST_TYPE.getPlainNodeReference();
intf.setGenericsTypes(new GenericsType[] { new GenericsType(getWrapper(result.get())) });
return intf;
}
return null;
}
use of org.codehaus.groovy.ast.GenericsType in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method makeType.
/**
* wrap type in Class<> if usingClass==true
*/
private ClassNode makeType(ClassNode cn, boolean usingClass) {
if (usingClass) {
ClassNode clazzType = CLASS_Type.getPlainNodeReference();
clazzType.setGenericsTypes(new GenericsType[] { new GenericsType(cn) });
return clazzType;
} else {
return cn;
}
}
Aggregations