/* @Nullable */ private ArrayTypeReference doTryConvertToArray( ParameterizedTypeReference typeReference, RecursionGuard<JvmTypeParameter> recursionGuard) { JvmType type = typeReference.getType(); if (recursionGuard.tryNext((JvmTypeParameter) type)) { List<LightweightTypeReference> superTypes = typeReference.getSuperTypes(); for (int i = 0, size = superTypes.size(); i < size; i++) { LightweightTypeReference superType = superTypes.get(i); if (superType.isArray()) { return (ArrayTypeReference) superType; } ArrayTypeReference result = doTryConvertToArray(typeReference); if (result != null) { return result; } else { JvmType rawSuperType = superType.getType(); if (rawSuperType != null && rawSuperType.eClass() == TypesPackage.Literals.JVM_TYPE_PARAMETER) { result = doTryConvertToArray((ParameterizedTypeReference) superType, recursionGuard); if (result != null) return result; } } } } return null; }
protected void doVisitCompoundTypeReference(CompoundTypeReference reference) { List<LightweightTypeReference> components = reference.getMultiTypeComponents(); if (components.isEmpty()) doVisitTypeReference(reference); else for (LightweightTypeReference component : components) { component.accept(this); } }
@Override public boolean isOwnedBy(ITypeReferenceOwner owner) { if (super.isOwnedBy(owner)) { if (lowerBound != null && !lowerBound.isOwnedBy(owner)) return false; for (LightweightTypeReference parameterType : expose(upperBounds)) { if (!parameterType.isOwnedBy(owner)) { return false; } } return true; } return false; }
@Override public boolean isVisible(IVisibilityHelper visibilityHelper) { if (upperBounds != null && !upperBounds.isEmpty()) { for (LightweightTypeReference upperBound : upperBounds) { if (!upperBound.isVisible(visibilityHelper)) { return false; } } } if (lowerBound != null && !lowerBound.isVisible(visibilityHelper)) { return false; } return true; }
public LightweightTypeReference convertToList(ArrayTypeReference type) { LightweightTypeReference componentType = type.getComponentType(); LightweightTypeReference wrapper = componentType.getWrapperTypeIfPrimitive(); ITypeReferenceOwner owner = type.getOwner(); JvmType listType = type.getServices() .getTypeReferences() .findDeclaredType(List.class, owner.getContextResourceSet()); if (listType == null) { return owner.newUnknownTypeReference(List.class.getName()); } ParameterizedTypeReference result = owner.newParameterizedTypeReference(listType); result.addTypeArgument(wrapper); return result; }
@Override /* @Nullable */ public LightweightTypeReference getSuperType(Class<?> rawType) { if (isUnbounded()) { if (Object.class.equals(rawType)) { return internalFindTopLevelType(rawType); } return null; } List<LightweightTypeReference> nonNullUpperBounds = expose(getUpperBounds()); for (LightweightTypeReference upperBound : nonNullUpperBounds) { LightweightTypeReference result = upperBound.getSuperType(rawType); if (result != null) return result; } return null; }
@Override /* @Nullable */ public LightweightTypeReference getSuperType(JvmType rawType) { if (isUnbounded()) { if (Object.class.getName().equals(rawType.getIdentifier())) { return getOwner().newParameterizedTypeReference(rawType); } return null; } List<LightweightTypeReference> nonNullUpperBounds = expose(getUpperBounds()); for (LightweightTypeReference upperBound : nonNullUpperBounds) { LightweightTypeReference result = upperBound.getSuperType(rawType); if (result != null) return result; } return null; }
@Override public boolean hasTypeArguments() { if (lowerBound != null) return lowerBound.hasTypeArguments(); if (upperBounds != null && upperBounds.size() == 1) { return upperBounds.get(0).hasTypeArguments(); } return super.hasTypeArguments(); }
@Override public List<LightweightTypeReference> getTypeArguments() { if (lowerBound != null) return lowerBound.getTypeArguments(); if (upperBounds != null && upperBounds.size() == 1) { return upperBounds.get(0).getTypeArguments(); } return super.getTypeArguments(); }
public void addUpperBound(LightweightTypeReference upperBound) { if (upperBound == null) { throw new NullPointerException("upperBound may not be null"); } if (!upperBound.isOwnedBy(getOwner())) { throw new IllegalArgumentException("upperBound is not valid in current context"); } if (upperBound instanceof WildcardTypeReference) { throw new IllegalArgumentException("Wildcards are not supported as upper bounds"); } if (upperBound.isPrimitive() || upperBound.isPrimitiveVoid()) { throw new IllegalArgumentException("Constraints may not be primitives"); } if (upperBounds == null) upperBounds = Lists.newArrayListWithCapacity(2); upperBounds.add(upperBound); resolved = resolved && upperBound.isResolved(); }
@Override public JvmTypeReference toTypeReference() { TypesFactory typesFactory = getTypesFactory(); JvmWildcardTypeReference result = typesFactory.createJvmWildcardTypeReference(); if (upperBounds != null && !upperBounds.isEmpty()) { for (LightweightTypeReference typeArgument : upperBounds) { JvmUpperBound constraint = typesFactory.createJvmUpperBound(); constraint.setTypeReference(typeArgument.getWrapperTypeIfPrimitive().toTypeReference()); result.getConstraints().add(constraint); } } if (lowerBound != null) { JvmLowerBound constraint = typesFactory.createJvmLowerBound(); constraint.setTypeReference(lowerBound.getWrapperTypeIfPrimitive().toTypeReference()); result.getConstraints().add(constraint); } return result; }
public void setLowerBound(LightweightTypeReference lowerBound) { if (lowerBound == null) { throw new NullPointerException("lowerBound may not be null"); } if (!lowerBound.isOwnedBy(getOwner())) { throw new IllegalArgumentException("lowerBound is not valid in current context"); } if (lowerBound instanceof WildcardTypeReference) { throw new IllegalArgumentException("Wildcards are not supported as lower bounds"); } if (lowerBound.isPrimitive() || lowerBound.isPrimitiveVoid()) { throw new IllegalArgumentException("Constraints may not be primitives"); } if (this.lowerBound != null && this.lowerBound != lowerBound) { throw new IllegalStateException("only one lower bound is supported"); } this.lowerBound = lowerBound; resolved = resolved && lowerBound.isResolved(); }
/* @Nullable */ private ArrayTypeReference doTryConvertToArray(ParameterizedTypeReference typeReference) { LightweightTypeReference parameterizedIterable = typeReference.getSuperType(Iterable.class); if (parameterizedIterable != null) { ITypeReferenceOwner owner = typeReference.getOwner(); if (parameterizedIterable.isRawType()) { // return Object[] List<LightweightTypeReference> superTypes = parameterizedIterable.getSuperTypes(); if (superTypes.isEmpty()) { return null; } LightweightTypeReference objectType = superTypes.get(0); ArrayTypeReference array = owner.newArrayTypeReference(objectType); return array; } else { LightweightTypeReference componentType = parameterizedIterable.getTypeArguments().get(0).getUpperBoundSubstitute(); ArrayTypeReference array = owner.newArrayTypeReference(componentType); return array; } } return null; }
@Override protected WildcardTypeReference doCopyInto(ITypeReferenceOwner owner) { WildcardTypeReference result = owner.newWildcardTypeReference(); if (upperBounds != null && !upperBounds.isEmpty()) { for (LightweightTypeReference upperBound : upperBounds) { LightweightTypeReference copiedUpperBound = upperBound.copyInto(owner).getInvariantBoundSubstitute(); if (!(copiedUpperBound.isPrimitive() || copiedUpperBound.isPrimitiveVoid())) { result.addUpperBound(copiedUpperBound); } } } if (lowerBound != null) { LightweightTypeReference copiedLowerBound = lowerBound.copyInto(owner).getInvariantBoundSubstitute(); if (!(copiedLowerBound.isPrimitive() || copiedLowerBound.isPrimitiveVoid())) { result.setLowerBound(copiedLowerBound); } } return result; }
@Override public JvmTypeReference toJavaCompliantTypeReference(IVisibilityHelper helper) { TypesFactory typesFactory = getTypesFactory(); JvmWildcardTypeReference result = typesFactory.createJvmWildcardTypeReference(); if (upperBounds != null && !upperBounds.isEmpty()) { List<LightweightTypeReference> nonInterfaceTypes = getNonInterfaceTypes(upperBounds); JvmTypeReference upperBound = toJavaCompliantTypeReference( nonInterfaceTypes != null ? nonInterfaceTypes : upperBounds, helper); JvmUpperBound constraint = typesFactory.createJvmUpperBound(); constraint.setTypeReference(upperBound); result.getConstraints().add(constraint); } if (lowerBound != null) { JvmLowerBound constraint = typesFactory.createJvmLowerBound(); constraint.setTypeReference(lowerBound.toJavaCompliantTypeReference()); result.getConstraints().add(constraint); } return result; }