Skip to content

Commit

Permalink
classToExtend, interfacesToImplement/Extend
Browse files Browse the repository at this point in the history
in JvmGenericType
  • Loading branch information
LorenzoBettini committed Dec 23, 2023
1 parent 4a95d67 commit 0cb0120
Show file tree
Hide file tree
Showing 8 changed files with 564 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*******************************************************************************/
package org.eclipse.xtext.common.types;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -52,12 +54,6 @@ protected JvmGenericType getObjectUnderTest() {
assertTrue(Iterables.isEmpty(interfaces));
}

private JvmTypeReference createReferenceTo(JvmType type) {
JvmParameterizedTypeReference result = TypesFactory.eINSTANCE.createJvmParameterizedTypeReference();
result.setType(type);
return result;
}

@Test public void testGetExtendedInterfaces_02() {
JvmGenericType interfaceType = TypesFactory.eINSTANCE.createJvmGenericType();
interfaceType.setInterface(true);
Expand Down Expand Up @@ -141,4 +137,125 @@ public JvmType apply(JvmTypeReference from) {
genericType.getMembers().add(operation);
assertEquals(constructor, Iterables.getOnlyElement(genericType.getDeclaredConstructors()));
}

@Test public void testSetClassToExtendsUpdatesSuperTypes() {
JvmTypeReference classToExtend = createTypeReference();
genericType.setClassToExtend(classToExtend);
assertNotNull(genericType.getClassToExtend());
assertSame(classToExtend, genericType.getSuperTypes().get(0));
// if we unset the class to extend...
genericType.setClassToExtend(null);
assertNull(genericType.getClassToExtend());
// ... it must also be removed from supertypes
assertEquals(0, genericType.getSuperTypes().size());
}

@Test public void testUpdateSuperTypesSetsClassToExtend() {
JvmTypeReference classToExtend = createTypeReference();
JvmTypeReference anotherType = createTypeReference();
genericType.setClassToExtend(classToExtend);
assertNotNull(genericType.getClassToExtend());
assertSame(classToExtend, genericType.getSuperTypes().get(0));

genericType.getSuperTypes().add(anotherType);
assertEquals(2, genericType.getSuperTypes().size());
// if we remove a super type that is not the class to extend...
genericType.getSuperTypes().remove(anotherType);
assertEquals(1, genericType.getSuperTypes().size());
// ... the extended class is still there
assertNotNull(genericType.getClassToExtend());
// ... otherwise ...
genericType.getSuperTypes().clear();
// ... the class to extend is unset as well ...
assertNull(genericType.getClassToExtend());
}

@Test public void testUpdateInterfacesToImplementUpdatesSuperTypes() {
JvmTypeReference interface1 = createTypeReference();
JvmTypeReference interface2 = createTypeReference();
genericType.getInterfacesToImplement().addAll(List.of(interface1, interface2));
assertEquals(2, genericType.getInterfacesToImplement().size());
var superTypes = genericType.getSuperTypes();
assertEquals(genericType.getInterfacesToImplement(), superTypes);
// call it twice to make sure it doesn't change
var superTypes2 = genericType.getSuperTypes();
assertEquals(genericType.getInterfacesToImplement(), superTypes2);
// remove from interface to implement ...
genericType.getInterfacesToImplement().remove(0);
assertEquals(1, genericType.getInterfacesToImplement().size());
// ... and the interface must be removed from supertypes as well
assertEquals(genericType.getInterfacesToImplement(), genericType.getSuperTypes());
}

@Test public void testUpdateSuperTypesUpdatesInterfacesToImplement() {
JvmTypeReference interface1 = createTypeReference();
JvmTypeReference interface2 = createTypeReference();
JvmTypeReference anotherType = createTypeReference();
genericType.getInterfacesToImplement().addAll(List.of(interface1, interface2));
assertEquals(2, genericType.getInterfacesToImplement().size());
var superTypes = genericType.getSuperTypes();
assertEquals(genericType.getInterfacesToImplement(), superTypes);

genericType.getSuperTypes().add(anotherType);
assertEquals(3, genericType.getSuperTypes().size());
// if we remove a super type that is not an interface to implement...
genericType.getSuperTypes().remove(anotherType);
assertEquals(2, genericType.getSuperTypes().size());
// ... the interface to implement is still there
assertEquals(2, genericType.getInterfacesToImplement().size());
// ... otherwise ...
genericType.getSuperTypes().clear();
// ... the interface to implement is removed as well ...
assertEquals(0, genericType.getInterfacesToImplement().size());
}

@Test public void testUpdateInterfacesToExtendUpdatesSuperTypes() {
JvmTypeReference interface1 = createTypeReference();
JvmTypeReference interface2 = createTypeReference();
genericType.getInterfacesToExtend().addAll(List.of(interface1, interface2));
assertEquals(2, genericType.getInterfacesToExtend().size());
var superTypes = genericType.getSuperTypes();
assertEquals(genericType.getInterfacesToExtend(), superTypes);
// call it twice to make sure it doesn't change
var superTypes2 = genericType.getSuperTypes();
assertEquals(genericType.getInterfacesToExtend(), superTypes2);
// remove from interface to extend ...
genericType.getInterfacesToExtend().remove(0);
assertEquals(1, genericType.getInterfacesToExtend().size());
// ... and the interface must be removed from supertypes as well
assertEquals(genericType.getInterfacesToExtend(), genericType.getSuperTypes());
}

@Test public void testUpdateSuperTypesUpdatesInterfacesToExtend() {
JvmTypeReference interface1 = createTypeReference();
JvmTypeReference interface2 = createTypeReference();
JvmTypeReference anotherType = createTypeReference();
genericType.getInterfacesToExtend().addAll(List.of(interface1, interface2));
assertEquals(2, genericType.getInterfacesToExtend().size());
var superTypes = genericType.getSuperTypes();
assertEquals(genericType.getInterfacesToExtend(), superTypes);

genericType.getSuperTypes().add(anotherType);
assertEquals(3, genericType.getSuperTypes().size());
// if we remove a super type that is not an interface to extend...
genericType.getSuperTypes().remove(anotherType);
assertEquals(2, genericType.getSuperTypes().size());
// ... the interface to implement is still there
assertEquals(2, genericType.getInterfacesToExtend().size());
// ... otherwise ...
genericType.getSuperTypes().clear();
// ... the interface to extend is removed as well ...
assertEquals(0, genericType.getInterfacesToExtend().size());
}

private JvmTypeReference createTypeReference() {
return createReferenceTo(
TypesFactory.eINSTANCE.createJvmGenericType());
}

private JvmTypeReference createReferenceTo(JvmType type) {
JvmParameterizedTypeReference result = TypesFactory.eINSTANCE.createJvmParameterizedTypeReference();
result.setType(type);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package org.eclipse.xtext.common.types;

import org.eclipse.emf.common.util.EList;

/**
* <!-- begin-user-doc -->
Expand All @@ -21,6 +22,9 @@
* <li>{@link org.eclipse.xtext.common.types.JvmGenericType#isInterface <em>Interface</em>}</li>
* <li>{@link org.eclipse.xtext.common.types.JvmGenericType#isStrictFloatingPoint <em>Strict Floating Point</em>}</li>
* <li>{@link org.eclipse.xtext.common.types.JvmGenericType#isAnonymous <em>Anonymous</em>}</li>
* <li>{@link org.eclipse.xtext.common.types.JvmGenericType#getClassToExtend <em>Class To Extend</em>}</li>
* <li>{@link org.eclipse.xtext.common.types.JvmGenericType#getInterfacesToImplement <em>Interfaces To Implement</em>}</li>
* <li>{@link org.eclipse.xtext.common.types.JvmGenericType#getInterfacesToExtend <em>Interfaces To Extend</em>}</li>
* </ul>
*
* @see org.eclipse.xtext.common.types.TypesPackage#getJvmGenericType()
Expand Down Expand Up @@ -95,4 +99,50 @@ public interface JvmGenericType extends JvmDeclaredType, JvmTypeParameterDeclara
*/
void setAnonymous(boolean value);

/**
* Returns the value of the '<em><b>Class To Extend</b></em>' reference.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @return the value of the '<em>Class To Extend</em>' reference.
* @see #setClassToExtend(JvmTypeReference)
* @see org.eclipse.xtext.common.types.TypesPackage#getJvmGenericType_ClassToExtend()
* @model
* @generated
*/
JvmTypeReference getClassToExtend();

/**
* Sets the value of the '{@link org.eclipse.xtext.common.types.JvmGenericType#getClassToExtend <em>Class To Extend</em>}' reference.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @param value the new value of the '<em>Class To Extend</em>' reference.
* @see #getClassToExtend()
* @generated
*/
void setClassToExtend(JvmTypeReference value);

/**
* Returns the value of the '<em><b>Interfaces To Implement</b></em>' reference list.
* The list contents are of type {@link org.eclipse.xtext.common.types.JvmTypeReference}.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @return the value of the '<em>Interfaces To Implement</em>' reference list.
* @see org.eclipse.xtext.common.types.TypesPackage#getJvmGenericType_InterfacesToImplement()
* @model resolveProxies="false"
* @generated
*/
EList<JvmTypeReference> getInterfacesToImplement();

/**
* Returns the value of the '<em><b>Interfaces To Extend</b></em>' reference list.
* The list contents are of type {@link org.eclipse.xtext.common.types.JvmTypeReference}.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @return the value of the '<em>Interfaces To Extend</em>' reference list.
* @see org.eclipse.xtext.common.types.TypesPackage#getJvmGenericType_InterfacesToExtend()
* @model resolveProxies="false"
* @generated
*/
EList<JvmTypeReference> getInterfacesToExtend();

} // JvmGenericType
Original file line number Diff line number Diff line change
Expand Up @@ -1498,14 +1498,41 @@ public interface TypesPackage extends EPackage
*/
int JVM_GENERIC_TYPE__ANONYMOUS = JVM_DECLARED_TYPE_FEATURE_COUNT + 3;

/**
* The feature id for the '<em><b>Class To Extend</b></em>' reference.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
* @ordered
*/
int JVM_GENERIC_TYPE__CLASS_TO_EXTEND = JVM_DECLARED_TYPE_FEATURE_COUNT + 4;

/**
* The feature id for the '<em><b>Interfaces To Implement</b></em>' reference list.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
* @ordered
*/
int JVM_GENERIC_TYPE__INTERFACES_TO_IMPLEMENT = JVM_DECLARED_TYPE_FEATURE_COUNT + 5;

/**
* The feature id for the '<em><b>Interfaces To Extend</b></em>' reference list.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
* @ordered
*/
int JVM_GENERIC_TYPE__INTERFACES_TO_EXTEND = JVM_DECLARED_TYPE_FEATURE_COUNT + 6;

/**
* The number of structural features of the '<em>Jvm Generic Type</em>' class.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
* @ordered
*/
int JVM_GENERIC_TYPE_FEATURE_COUNT = JVM_DECLARED_TYPE_FEATURE_COUNT + 4;
int JVM_GENERIC_TYPE_FEATURE_COUNT = JVM_DECLARED_TYPE_FEATURE_COUNT + 7;

/**
* The meta object id for the '{@link org.eclipse.xtext.common.types.impl.JvmTypeReferenceImpl <em>Jvm Type Reference</em>}' class.
Expand Down Expand Up @@ -3336,6 +3363,39 @@ public interface TypesPackage extends EPackage
*/
EAttribute getJvmGenericType_Anonymous();

/**
* Returns the meta object for the reference '{@link org.eclipse.xtext.common.types.JvmGenericType#getClassToExtend <em>Class To Extend</em>}'.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @return the meta object for the reference '<em>Class To Extend</em>'.
* @see org.eclipse.xtext.common.types.JvmGenericType#getClassToExtend()
* @see #getJvmGenericType()
* @generated
*/
EReference getJvmGenericType_ClassToExtend();

/**
* Returns the meta object for the reference list '{@link org.eclipse.xtext.common.types.JvmGenericType#getInterfacesToImplement <em>Interfaces To Implement</em>}'.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @return the meta object for the reference list '<em>Interfaces To Implement</em>'.
* @see org.eclipse.xtext.common.types.JvmGenericType#getInterfacesToImplement()
* @see #getJvmGenericType()
* @generated
*/
EReference getJvmGenericType_InterfacesToImplement();

/**
* Returns the meta object for the reference list '{@link org.eclipse.xtext.common.types.JvmGenericType#getInterfacesToExtend <em>Interfaces To Extend</em>}'.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @return the meta object for the reference list '<em>Interfaces To Extend</em>'.
* @see org.eclipse.xtext.common.types.JvmGenericType#getInterfacesToExtend()
* @see #getJvmGenericType()
* @generated
*/
EReference getJvmGenericType_InterfacesToExtend();

/**
* Returns the meta object for class '{@link org.eclipse.xtext.common.types.JvmTypeReference <em>Jvm Type Reference</em>}'.
* <!-- begin-user-doc -->
Expand Down Expand Up @@ -4669,6 +4729,30 @@ interface Literals
*/
EAttribute JVM_GENERIC_TYPE__ANONYMOUS = eINSTANCE.getJvmGenericType_Anonymous();

/**
* The meta object literal for the '<em><b>Class To Extend</b></em>' reference feature.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
EReference JVM_GENERIC_TYPE__CLASS_TO_EXTEND = eINSTANCE.getJvmGenericType_ClassToExtend();

/**
* The meta object literal for the '<em><b>Interfaces To Implement</b></em>' reference list feature.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
EReference JVM_GENERIC_TYPE__INTERFACES_TO_IMPLEMENT = eINSTANCE.getJvmGenericType_InterfacesToImplement();

/**
* The meta object literal for the '<em><b>Interfaces To Extend</b></em>' reference list feature.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
EReference JVM_GENERIC_TYPE__INTERFACES_TO_EXTEND = eINSTANCE.getJvmGenericType_InterfacesToExtend();

/**
* The meta object literal for the '{@link org.eclipse.xtext.common.types.impl.JvmTypeReferenceImpl <em>Jvm Type Reference</em>}' class.
* <!-- begin-user-doc -->
Expand Down
Loading

0 comments on commit 0cb0120

Please sign in to comment.