@Test public void testFindTypeByName_javaUtilList_07() { String typeName = List.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); assertEquals(1, type.getSuperTypes().size()); JvmParameterizedTypeReference superType = (JvmParameterizedTypeReference) type.getSuperTypes().get(0); assertFalse(superType.getType().eIsProxy()); assertEquals("java.util.Collection<E>", superType.getIdentifier()); assertEquals(1, type.getTypeParameters().size()); JvmType rawType = superType.getType(); assertFalse(rawType.eIsProxy()); assertEquals(1, superType.getArguments().size()); JvmTypeReference superTypeParameter = superType.getArguments().get(0); JvmType parameterType = superTypeParameter.getType(); assertFalse(parameterType.eIsProxy()); assertSame(parameterType, type.getTypeParameters().get(0)); }
@Test public void test_twoListParamsNoResult_03() { JvmOperation twoListParamsNoResult = getMethodFromParameterizedMethods( "twoListParamsNoResult(java.util.List,java.util.List)"); JvmFormalParameter firstParam = twoListParamsNoResult.getParameters().get(0); JvmTypeReference paramType = firstParam.getParameterType(); JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) paramType; assertEquals(1, parameterized.getArguments().size()); JvmTypeReference typeParameter = parameterized.getArguments().get(0); JvmType referencedType = typeParameter.getType(); assertFalse(referencedType.eIsProxy()); assertTrue(referencedType instanceof JvmTypeParameter); JvmTypeParameter typeVar = (JvmTypeParameter) referencedType; assertEquals("T", typeVar.getName()); assertSame(twoListParamsNoResult, typeVar.getDeclarator()); }
@Test public void test_twoListWildcardsNoResult_03() { JvmOperation twoListWildcardsNoResult = getMethodFromParameterizedMethods( "twoListWildcardsNoResult(java.util.List,java.util.List)"); JvmFormalParameter firstParam = twoListWildcardsNoResult.getParameters().get(0); JvmTypeReference paramType = firstParam.getParameterType(); JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) paramType; assertEquals(1, parameterized.getArguments().size()); JvmTypeReference typeParameter = parameterized.getArguments().get(0); assertTrue(typeParameter instanceof JvmWildcardTypeReference); JvmWildcardTypeReference wildcard = (JvmWildcardTypeReference) typeParameter; assertEquals(1, wildcard.getConstraints().size()); assertTrue(wildcard.getConstraints().get(0) instanceof JvmUpperBound); JvmUpperBound upperBound = (JvmUpperBound) wildcard.getConstraints().get(0); assertNotNull(upperBound.getTypeReference()); JvmType upperBoundType = upperBound.getTypeReference().getType(); assertFalse(upperBoundType.eIsProxy()); assertEquals("java.lang.Object", upperBoundType.getIdentifier()); }
@Test public void test_twoListWildcardsListResult_03() { JvmOperation twoListWildcardsListResult = getMethodFromParameterizedMethods( "twoListWildcardsListResult(java.util.List,java.util.List)"); JvmTypeReference returnType = twoListWildcardsListResult.getReturnType(); JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) returnType; assertEquals(1, parameterized.getArguments().size()); JvmTypeReference typeParameter = parameterized.getArguments().get(0); assertTrue(typeParameter instanceof JvmWildcardTypeReference); JvmWildcardTypeReference wildcard = (JvmWildcardTypeReference) typeParameter; assertEquals(1, wildcard.getConstraints().size()); assertTrue(wildcard.getConstraints().get(0) instanceof JvmUpperBound); JvmUpperBound upperBound = (JvmUpperBound) wildcard.getConstraints().get(0); assertNotNull(upperBound.getTypeReference()); JvmType upperBoundType = upperBound.getTypeReference().getType(); assertFalse(upperBoundType.eIsProxy()); assertEquals("java.lang.Object", upperBoundType.getIdentifier()); }
@Test public void test_ParameterizedTypes_02() { String typeName = ParameterizedTypes.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation methodS = getMethodFromType(type, ParameterizedTypes.class, "methodS(S)"); JvmTypeReference listS = methodS.getReturnType(); assertFalse(listS.toString(), listS.eIsProxy()); assertEquals("java.util.List<? extends S>", listS.getIdentifier()); JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) listS; assertEquals(1, listType.getArguments().size()); JvmTypeReference typeArgument = listType.getArguments().get(0); assertTrue(typeArgument instanceof JvmWildcardTypeReference); JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument; assertEquals("? extends S", wildcardTypeArgument.getIdentifier()); assertEquals(1, wildcardTypeArgument.getConstraints().size()); JvmUpperBound upperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0); JvmTypeParameter s = type.getTypeParameters().get(0); assertSame(s, upperBound.getTypeReference().getType()); }
@Test public void test_ParameterizedTypes_03() { String typeName = ParameterizedTypes.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation methodT = getMethodFromType(type, ParameterizedTypes.class, "methodT(T)"); JvmTypeReference listS = methodT.getReturnType(); assertFalse(listS.toString(), listS.eIsProxy()); assertEquals("java.util.List<? extends java.lang.Object & super T>", listS.getIdentifier()); JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) listS; assertEquals(1, listType.getArguments().size()); JvmTypeReference typeArgument = listType.getArguments().get(0); assertTrue(typeArgument instanceof JvmWildcardTypeReference); JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument; assertEquals("? extends java.lang.Object & super T", wildcardTypeArgument.getIdentifier()); assertEquals(2, wildcardTypeArgument.getConstraints().size()); JvmUpperBound uperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0); assertEquals("java.lang.Object", uperBound.getTypeReference().getIdentifier()); JvmLowerBound lowerBound = (JvmLowerBound) wildcardTypeArgument.getConstraints().get(1); JvmTypeParameter t = type.getTypeParameters().get(1); assertSame(t, lowerBound.getTypeReference().getType()); }
@Test public void test_ParameterizedTypes_05() { String typeName = ParameterizedTypes.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation methodZ = getMethodFromType(type, ParameterizedTypes.class, "methodZ(java.util.List)"); assertEquals(1, methodZ.getParameters().size()); assertEquals(1, methodZ.getTypeParameters().size()); assertEquals("Z", methodZ.getTypeParameters().get(0).getIdentifier()); JvmTypeReference listZ = methodZ.getReturnType(); assertFalse(listZ.toString(), listZ.getType().eIsProxy()); assertEquals("java.util.List<? extends java.lang.Object & super Z>", listZ.getIdentifier()); JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) listZ; assertEquals(1, listType.getArguments().size()); JvmTypeReference typeArgument = listType.getArguments().get(0); assertTrue(typeArgument instanceof JvmWildcardTypeReference); JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument; assertEquals("? extends java.lang.Object & super Z", wildcardTypeArgument.getIdentifier()); assertEquals(2, wildcardTypeArgument.getConstraints().size()); JvmUpperBound upperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0); assertEquals("java.lang.Object", upperBound.getTypeReference().getIdentifier()); JvmLowerBound lowerBound = (JvmLowerBound) wildcardTypeArgument.getConstraints().get(1); assertEquals("Z", lowerBound.getTypeReference().getIdentifier()); JvmTypeParameter z = methodZ.getTypeParameters().get(0); assertSame(z, lowerBound.getTypeReference().getType()); }
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ public void setOuter(JvmParameterizedTypeReference newOuter) { if (newOuter != outer) { NotificationChain msgs = null; if (outer != null) msgs = ((InternalEObject)outer).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - TypesPackage.JVM_INNER_TYPE_REFERENCE__OUTER, null, msgs); if (newOuter != null) msgs = ((InternalEObject)newOuter).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - TypesPackage.JVM_INNER_TYPE_REFERENCE__OUTER, null, msgs); msgs = basicSetOuter(newOuter, msgs); if (msgs != null) msgs.dispatch(); } else if (eNotificationRequired()) eNotify(new ENotificationImpl(this, Notification.SET, TypesPackage.JVM_INNER_TYPE_REFERENCE__OUTER, newOuter, newOuter)); }
@Test public void test_ParameterizedTypes_W_01() { String typeName = ParameterizedTypes.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmTypeParameter typeParameterW = type.getTypeParameters().get(4); assertEquals("W", typeParameterW.getIdentifier()); assertSame(type, typeParameterW.getDeclarator()); assertEquals(2, typeParameterW.getConstraints().size()); JvmTypeConstraint firstTypeConstraint = typeParameterW.getConstraints().get(0); assertTrue(firstTypeConstraint instanceof JvmUpperBound); JvmUpperBound firstUpperBound = (JvmUpperBound) firstTypeConstraint; assertNotNull(firstUpperBound.getTypeReference()); assertFalse(firstUpperBound.getTypeReference().toString(), firstUpperBound.getTypeReference().eIsProxy()); assertEquals("java.lang.Comparable<S>", firstUpperBound.getTypeReference().getIdentifier()); JvmParameterizedTypeReference comparableType = (JvmParameterizedTypeReference) firstUpperBound .getTypeReference(); assertEquals(1, comparableType.getArguments().size()); JvmTypeReference typeArgument = comparableType.getArguments().get(0); assertEquals("S", typeArgument.getIdentifier()); JvmTypeParameter s = type.getTypeParameters().get(0); assertSame(s, typeArgument.getType()); }
@Override public JvmTypeReference doVisitParameterizedTypeReference(JvmParameterizedTypeReference reference, Resource resource) { JvmType type = reference.getType(); if (type != null && !type.eIsProxy()) { if (type instanceof JvmTypeParameterDeclarator) { if (!((JvmTypeParameterDeclarator) type).getTypeParameters().isEmpty()) { JvmParameterizedTypeReference result = factory.createJvmParameterizedTypeReference(); result.setType(type); return result; } } else if (type instanceof JvmTypeParameter) { return getRawTypeFromConstraints(((JvmTypeParameter) type).getConstraints(), resource); } } return reference; }
@Test public void test_ParameterizedTypes_Inner_05() { String typeName = ParameterizedTypes.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodVArray_01()"); JvmTypeReference listT = methodV.getReturnType(); assertEquals("java.util.List<? extends V>[]", listT.getIdentifier()); JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) ((JvmGenericArrayTypeReference) listT) .getComponentType(); assertEquals(1, listType.getArguments().size()); JvmTypeReference typeArgument = listType.getArguments().get(0); assertTrue(typeArgument instanceof JvmWildcardTypeReference); JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument; assertEquals("? extends V", wildcardTypeArgument.getIdentifier()); assertEquals(1, wildcardTypeArgument.getConstraints().size()); JvmUpperBound upperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0); JvmTypeParameter v = type.getTypeParameters().get(3); assertSame(v, upperBound.getTypeReference().getType()); }
@Test public void test_ParameterizedTypes_Inner_07() { String typeName = ParameterizedTypes.Inner.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodZArray_01()"); JvmTypeReference listZ = methodV.getReturnType(); assertEquals("java.util.List<Z[][]>", listZ.getIdentifier()); JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) listZ; assertEquals(1, listType.getArguments().size()); JvmTypeReference typeArgument = listType.getArguments().get(0); JvmType argumentType = typeArgument.getType(); assertTrue(argumentType instanceof JvmArrayType); assertTrue(((JvmArrayType) argumentType).getComponentType() instanceof JvmArrayType); JvmComponentType componentType = ((JvmArrayType) ((JvmArrayType) argumentType).getComponentType()) .getComponentType(); JvmTypeParameter z = type.getTypeParameters().get(2); assertSame(z, componentType); }
@Test public void test_ParameterizedTypes_Inner_08() { String typeName = ParameterizedTypes.Inner.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodZArray_02()"); JvmTypeReference listZ = methodV.getReturnType(); assertEquals("java.util.List<Z[]>[]", listZ.getIdentifier()); JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) ((JvmGenericArrayTypeReference) listZ) .getComponentType(); assertEquals(1, listType.getArguments().size()); JvmTypeReference typeArgument = listType.getArguments().get(0); JvmType argumentType = typeArgument.getType(); assertTrue(argumentType instanceof JvmArrayType); JvmComponentType componentType = ((JvmArrayType) argumentType).getComponentType(); JvmTypeParameter z = type.getTypeParameters().get(2); assertSame(z, componentType); }
@Test public void test_ParameterizedTypes_Inner_Y_01() { String typeName = ParameterizedTypes.Inner.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmTypeParameter typeParameterY = type.getTypeParameters().get(1); assertEquals("Y", typeParameterY.getIdentifier()); assertSame(type, typeParameterY.getDeclarator()); assertEquals(1, typeParameterY.getConstraints().size()); JvmTypeConstraint typeConstraint = typeParameterY.getConstraints().get(0); assertTrue(typeConstraint instanceof JvmUpperBound); JvmUpperBound upperBound = (JvmUpperBound) typeConstraint; assertNotNull(upperBound.getTypeReference()); assertEquals("java.util.List<X>", upperBound.getTypeReference().getIdentifier()); JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) upperBound.getTypeReference(); assertEquals(1, listType.getArguments().size()); JvmTypeReference typeArgument = listType.getArguments().get(0); assertEquals("X", typeArgument.getIdentifier()); JvmTypeParameter x = type.getTypeParameters().get(0); assertSame(x, typeArgument.getType()); }
@Test public void test_ParameterizedTypes_Inner_Z_01() { String typeName = ParameterizedTypes.Inner.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmTypeParameter typeParameterZ = type.getTypeParameters().get(2); assertEquals("Z", typeParameterZ.getIdentifier()); assertSame(type, typeParameterZ.getDeclarator()); assertEquals(1, typeParameterZ.getConstraints().size()); JvmTypeConstraint typeConstraint = typeParameterZ.getConstraints().get(0); assertTrue(typeConstraint instanceof JvmUpperBound); JvmUpperBound upperBound = (JvmUpperBound) typeConstraint; assertNotNull(upperBound.getTypeReference()); assertEquals("java.util.List<W>", upperBound.getTypeReference().getIdentifier()); JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) upperBound.getTypeReference(); assertEquals(1, listType.getArguments().size()); JvmTypeReference typeArgument = listType.getArguments().get(0); assertEquals("W", typeArgument.getIdentifier()); JvmTypeParameter w = ((JvmTypeParameterDeclarator) type.getDeclaringType()).getTypeParameters().get(4); assertSame(w, typeArgument.getType()); }
private static void computeFor(JvmParameterizedTypeReference typeReference, char innerClassDelimiter, NameType nameType, StringBuilder result) { if (typeReference.eClass() == TypesPackage.Literals.JVM_INNER_TYPE_REFERENCE) { JvmParameterizedTypeReference outer = ((JvmInnerTypeReference) typeReference).getOuter(); if (outer != null) { computeFor(outer, innerClassDelimiter, nameType, result); if (result.length() != 0) { JvmType type = typeReference.getType(); result.append(innerClassDelimiter); result.append(type.getSimpleName()); } else { appendType(typeReference, innerClassDelimiter, nameType, result); } } else { appendType(typeReference, innerClassDelimiter, nameType, result); } } else { appendType(typeReference, innerClassDelimiter, nameType, result); } if (typeReference.eIsSet(TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__ARGUMENTS)) { result.append("<"); appendArguments(result, typeReference.getArguments(), innerClassDelimiter, nameType); result.append(">"); } }
@Check public void checkTypeArgsAgainstTypeParameters(JvmParameterizedTypeReference typeRef) { JvmType type = typeRef.getType(); if(type instanceof JvmGenericType && !type.eIsProxy()) { int numTypeParameters = ((JvmGenericType) type).getTypeParameters().size(); if(typeRef.getArguments().size() > 0) { if (numTypeParameters == 0) { errorTypeIsNotGeneric(type, typeRef); } else if(numTypeParameters != typeRef.getArguments().size()) { StringBuilder message = new StringBuilder(64); message.append("Incorrect number of arguments for type "); message = proxyAwareUIStrings.appendTypeSignature(type, message); message.append("; it cannot be parameterized with arguments "); message = proxyAwareUIStrings.appendTypeArguments(typeRef, message); if (message != null) { error(message.toString(), IssueCodes.INVALID_NUMBER_OF_TYPE_ARGUMENTS, typeRef); } } } else if(numTypeParameters > 0) { warnRawType(type, typeRef); } } else if (type != null && !type.eIsProxy() && !typeRef.getArguments().isEmpty()) { errorTypeIsNotGeneric(type, typeRef); } }
protected void warning(String message, String issueCode, JvmParameterizedTypeReference typeReference) { EObject rootContainer = EcoreUtil.getRootContainer(typeReference); Resource resource = rootContainer.eResource(); if (resource.getContents().get(0) == rootContainer) { super.warning(message, typeReference, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, -1, issueCode); } else { EObject container = typeReference; EObject sourceElement = jvmModelAssociations.getPrimarySourceElement(container); container = container.eContainer(); while(sourceElement == null && container != null) { sourceElement = jvmModelAssociations.getPrimarySourceElement(container); container = container.eContainer(); } if (sourceElement != null) { warning(message, sourceElement, sourceElement.eClass().getEStructuralFeature("name"), -1, issueCode); } else { super.warning(message, typeReference, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, -1, issueCode); } } }
protected void error(String message, String issueCode, JvmParameterizedTypeReference typeReference) { EObject rootContainer = EcoreUtil.getRootContainer(typeReference); Resource resource = rootContainer.eResource(); if (resource.getContents().get(0) == rootContainer) { super.error(message, typeReference, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, -1, issueCode); } else { EObject container = typeReference; EObject sourceElement = jvmModelAssociations.getPrimarySourceElement(container); container = container.eContainer(); while(sourceElement == null && container != null) { sourceElement = jvmModelAssociations.getPrimarySourceElement(container); container = container.eContainer(); } if (sourceElement != null) { error(message, sourceElement, sourceElement.eClass().getEStructuralFeature("name"), -1, issueCode); } else { super.error(message, typeReference, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, -1, issueCode); } } }
private LightweightTypeReference getSuperTypeByName(String typeName, boolean interfaceType) { JvmTypeReference superType = getSuperTypeByName(typeName, interfaceType, type, new RecursionGuard<JvmType>()); if (superType != null) { JvmType rawType = superType.getType(); if (isRawType()) { return createRawTypeReference(rawType); } if (superType.eClass() == TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE) { if (((JvmParameterizedTypeReference) superType).getArguments().isEmpty()) { return getOwner().newParameterizedTypeReference(rawType); } } LightweightTypeReference unresolved = getOwner().toLightweightTypeReference(rawType); TypeParameterSubstitutor<?> substitutor = createSubstitutor(); LightweightTypeReference result = substitutor.substitute(unresolved); return result; } return null; }
@Override /* @Nullable */ public LightweightTypeReference getSuperType(JvmType rawType) { Object rawResult = internalGetSuperType(rawType); if (rawResult instanceof ParameterizedTypeReference) { return (LightweightTypeReference) rawResult; } else if (rawResult != null) { JvmTypeReference superType = (JvmTypeReference) rawResult; if (isRawType()) { return createRawTypeReference(rawType); } if (superType.eClass() == TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE) { if (((JvmParameterizedTypeReference) superType).getArguments().isEmpty()) { return getOwner().newParameterizedTypeReference(rawType); } } LightweightTypeReference unresolved = getOwner().toLightweightTypeReference(rawType); TypeParameterSubstitutor<?> substitutor = createSubstitutor(); LightweightTypeReference result = substitutor.substitute(unresolved); return result; } return null; }
private LightweightTypeReference createUnknownTypeReference(JvmParameterizedTypeReference reference) { List<INode> nodes = NodeModelUtils.findNodesForFeature(reference, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE); if (nodes.isEmpty()) { Set<EObject> sourceElements = owner.getServices().getJvmModelAssociations().getSourceElements(reference); EObject firstSource = Iterables.getFirst(sourceElements, null); if (firstSource instanceof JvmParameterizedTypeReference) { nodes = NodeModelUtils.findNodesForFeature(firstSource, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE); } } if (nodes.size() == 1) { String name = nodes.get(0).getText().trim(); if (name != null && name.length() != 0) { int lastDot = name.lastIndexOf('.'); int lastDollar = name.lastIndexOf('$'); int lastDotOrDollar = Math.max(lastDot, lastDollar); if (lastDotOrDollar != -1 && lastDotOrDollar != name.length() - 1) { String shortName = name.substring(lastDotOrDollar + 1); if (shortName.length() != 0) { name = shortName; } } return owner.newUnknownTypeReference(name); } } return owner.newUnknownTypeReference(); }
@Override public JvmTypeReference doVisitParameterizedTypeReference(JvmParameterizedTypeReference reference, Pair<Resource, Set<JvmType>> context) { JvmType type = reference.getType(); if (type != null && !type.eIsProxy()) { if (type instanceof JvmTypeParameterDeclarator) { if (!((JvmTypeParameterDeclarator) type).getTypeParameters().isEmpty()) { JvmParameterizedTypeReference result = factory.createJvmParameterizedTypeReference(); result.setType(type); return result; } } else if (type instanceof JvmTypeParameter) { Set<JvmType> recursionGuard = context.getSecond(); if (recursionGuard.add(type)) { return getRawTypeFromConstraints(((JvmTypeParameter) type).getConstraints(), context); } else { return createObjectReference(context.getFirst()); } } } return reference; }
protected LightweightTypeReference getStricterConstraint(final UnboundTypeReference typeParameter, LightweightTypeReference hint) { final JvmTypeParameter parameter = typeParameter.getTypeParameter(); List<JvmTypeConstraint> constraints = parameter.getConstraints(); for(JvmTypeConstraint constraint: constraints) { JvmTypeReference constraintReference = constraint.getTypeReference(); if (constraintReference != null) { final boolean[] recursive = new boolean[] { false }; LightweightTypeReferenceFactory factory = new LightweightTypeReferenceFactory(hint.getOwner()) { @Override public LightweightTypeReference doVisitParameterizedTypeReference(JvmParameterizedTypeReference reference) { JvmType type = reference.getType(); if (type == parameter) {// recursively bound recursive[0] = true; } return super.doVisitParameterizedTypeReference(reference); } }; LightweightTypeReference lightweightReference = factory.toLightweightReference(constraintReference); if (!recursive[0] && hint.isAssignableFrom(lightweightReference)) { hint = lightweightReference; } } } return hint; }
public IScope createTypeScope(EObject context, EReference reference) { if (context.eClass() == TypesPackage.Literals.JVM_INNER_TYPE_REFERENCE) { JvmInnerTypeReference casted = (JvmInnerTypeReference) context; JvmParameterizedTypeReference outerType = casted.getOuter(); JvmType outerRawType = outerType.getType(); if (outerRawType instanceof JvmDeclaredType) { Iterable<JvmDeclaredType> nestedTypes = ((JvmDeclaredType) outerRawType).getAllNestedTypes(); List<IEObjectDescription> descriptions = Lists.newArrayList(); for(JvmDeclaredType nestedType: nestedTypes) { descriptions.add(EObjectDescription.create(nestedType.getSimpleName(), nestedType)); } return new SimpleScope(descriptions); } return IScope.NULLSCOPE; } else { final IScope delegateScope = getDelegate().getScope(context, reference); return delegateScope; } }
@Override public JvmTypeReference getEquivalent() { if (equivalent == null) { JvmType rawType = getType(); if (rawType != null && rawType instanceof JvmDeclaredType) { JvmParameterizedTypeReference result = !isInstanceContext() ? createEquivalentWithWildcards(rawType, isProcedure()) : createEquivalentWithoutWildcards(rawType, isProcedure()); boolean wasDeliver = eDeliver(); try { eSetDeliver(false); setEquivalent(result); } finally { eSetDeliver(wasDeliver); } } else { equivalent = null; } } return equivalent; }
protected JvmTypeReference getJavaLangObjectTypeRef(JvmType rawType, TypesFactory typesFactory) { ResourceSet rs = EcoreUtil2.getResourceSet(rawType); JvmParameterizedTypeReference refToObject = typesFactory.createJvmParameterizedTypeReference(); if (rs != null) { EObject javaLangObject = rs.getEObject(javaLangObjectURI, true); if (javaLangObject instanceof JvmType) { JvmType objectDeclaration = (JvmType) javaLangObject; refToObject.setType(objectDeclaration); return refToObject; } } JvmGenericType proxy = typesFactory.createJvmGenericType(); ((InternalEObject)proxy).eSetProxyURI(javaLangObjectURI); refToObject.setType(proxy); return refToObject; }
protected JvmTypeReference toTypeReference(final JvmType type, final int arrayDimensions) { if ((type == null)) { return null; } JvmParameterizedTypeReference _createJvmParameterizedTypeReference = TypesFactory.eINSTANCE.createJvmParameterizedTypeReference(); final Procedure1<JvmParameterizedTypeReference> _function = (JvmParameterizedTypeReference it) -> { it.setType(type); }; JvmTypeReference resultTypeRef = ObjectExtensions.<JvmParameterizedTypeReference>operator_doubleArrow(_createJvmParameterizedTypeReference, _function); ExclusiveRange _doubleDotLessThan = new ExclusiveRange(0, arrayDimensions, true); for (final Integer i : _doubleDotLessThan) { { final JvmGenericArrayTypeReference arrayRef = TypesFactory.eINSTANCE.createJvmGenericArrayTypeReference(); arrayRef.setComponentType(resultTypeRef); resultTypeRef = arrayRef; } } return resultTypeRef; }
@Test public void test_ParameterizedTypes_06() { String typeName = ParameterizedTypes.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation methodMap = getMethodFromType(type, ParameterizedTypes.class, "methodMap(java.util.Map)"); assertEquals(1, methodMap.getParameters().size()); assertEquals(1, methodMap.getTypeParameters().size()); assertEquals("Z", methodMap.getTypeParameters().get(0).getIdentifier()); JvmType z = methodMap.getReturnType().getType(); assertSame(methodMap.getTypeParameters().get(0), z); JvmTypeReference mapType = methodMap.getParameters().get(0).getParameterType(); assertEquals("java.util.Map<? extends java.lang.Object & super Z, ? extends S>", mapType.getIdentifier()); JvmParameterizedTypeReference parameterizedMapType = (JvmParameterizedTypeReference) mapType; assertEquals(2, parameterizedMapType.getArguments().size()); JvmWildcardTypeReference extendsS = (JvmWildcardTypeReference) parameterizedMapType.getArguments().get(1); assertEquals(1, extendsS.getConstraints().size()); JvmType s = type.getTypeParameters().get(0); assertSame(s, extendsS.getConstraints().get(0).getTypeReference().getType()); }
@Test public void test_ParameterizedTypes_U_01() { String typeName = ParameterizedTypes.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmTypeParameter typeVariable = type.getTypeParameters().get(2); assertEquals("U", typeVariable.getIdentifier()); assertSame(type, typeVariable.getDeclarator()); assertEquals(1, typeVariable.getConstraints().size()); JvmTypeConstraint typeConstraint = typeVariable.getConstraints().get(0); assertTrue(typeConstraint instanceof JvmUpperBound); JvmUpperBound upperBound = (JvmUpperBound) typeConstraint; assertNotNull(upperBound.getTypeReference()); assertFalse(upperBound.getTypeReference().toString(), upperBound.getTypeReference().eIsProxy()); assertEquals("java.util.List<S>", upperBound.getTypeReference().getIdentifier()); JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) upperBound.getTypeReference(); assertEquals(1, listType.getArguments().size()); JvmTypeReference typeArgument = listType.getArguments().get(0); JvmTypeParameter s = type.getTypeParameters().get(0); assertSame(s, typeArgument.getType()); }
public JvmTypeReference getArgument(JvmTypeReference left, int index) { if (left.getType() instanceof JvmGenericType) { List<JvmTypeParameter> typeParameters = ((JvmGenericType) left.getType()).getTypeParameters(); if (typeParameters.size() <= index) { throw new IllegalArgumentException("The type " + left.getType().getIdentifier() + " cannot be parameterized with more than " + typeParameters.size() + " type arguments."); } if (left instanceof JvmParameterizedTypeReference) { List<JvmTypeReference> arguments = ((JvmParameterizedTypeReference) left).getArguments(); if (arguments.size() == typeParameters.size()) { return arguments.get(index); } } final JvmTypeParameter jvmTypeParameter = typeParameters.get(index); return createTypeRef(jvmTypeParameter); } throw new IllegalArgumentException(left.getType().getIdentifier() + " is not generic."); }