Skip to content

Commit

Permalink
Improve generated default values for attributes of generic type
Browse files Browse the repository at this point in the history
  • Loading branch information
merks committed Feb 3, 2024
1 parent 602096e commit cd6a7e2
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 19 deletions.
Expand Up @@ -374,6 +374,11 @@ public interface GenFeature extends GenTypedElement
String getDefaultValue();
String getStaticDefaultValue();

/**
* @since 2.37
*/
boolean isStaticDefaultValueUncheckedCast();

boolean isContainer();
boolean isContains();
boolean isEffectiveContains();
Expand Down
Expand Up @@ -1400,12 +1400,45 @@ public String getStaticDefaultValue()
if (eType instanceof EDataType)
{
GenDataType genDataType = (GenDataType)findGenClassifier(eType);
return genDataType.getStaticValue(defaultString);
if (hasGenericType() && defaultString != null && !"java.lang.Class".equals(eType.getInstanceClassName()))
{
return getRawTypeCast() + genDataType.getStaticValue(defaultString, false);
}
else
{
return genDataType.getStaticValue(defaultString);
}
}

return "null";
}

public boolean isStaticDefaultValueUncheckedCast()
{
if (!getGenModel().useGenerics())
{
return false;
}
else if (isListDataType() && isSetDefaultValue())
{
return true;
}
else if (getEcoreFeature().getDefaultValueLiteral() == null)
{
return false;
}
else if (hasGenericType())
{
return true;
}
else
{
EClassifier eType = getEcoreFeature().getEType();
String instanceClassName = eType.getInstanceClassName();
return !"java.lang.Class".equals(instanceClassName) && instanceClassName != eType.getInstanceTypeName();
}
}

protected boolean isMapEntryFeature()
{
return getGenClass().isMapEntry() && ("key".equals(getName()) || "value".equals(getName()));
Expand Down Expand Up @@ -2600,6 +2633,7 @@ public boolean hasEDefault()
return
getEcoreFeature() instanceof EAttribute &&
(getEffectiveComplianceLevel().getValue() < GenJDKLevel.JDK50 ||
((EAttribute)getEcoreFeature()).getDefaultValueLiteral() != null ||
(getEcoreFeature().getEType() != null &&
getEcoreFeature().getEType().getETypeParameters().isEmpty() &&
getEcoreFeature().getEGenericType().getETypeParameter() == null &&
Expand Down
Expand Up @@ -1055,7 +1055,7 @@ public String generate(Object argument)
stringBuffer.append(genFeature.getAPITags(genModel.getIndentation(stringBuffer)));
}
stringBuffer.append(TEXT_54);
if (genModel.useGenerics() && genFeature.isListDataType() && genFeature.isSetDefaultValue()) {
if (genFeature.isStaticDefaultValueUncheckedCast()) {
stringBuffer.append(TEXT_73);
}
if (isJDK50 && genFeature.hasAPIDeprecatedTag()) {
Expand Down
Expand Up @@ -290,7 +290,7 @@ public interface <%=genClass.getInterfaceName()%><%=genClass.getTypeParameters()
* @generated
* @ordered
*/
<%if (genModel.useGenerics() && genFeature.isListDataType() && genFeature.isSetDefaultValue()) {%>
<%if (genFeature.isStaticDefaultValueUncheckedCast()) {%>
@SuppressWarnings("unchecked")
<%}%>
<%if (isJDK50 && genFeature.hasAPIDeprecatedTag()) {%>
Expand Down
22 changes: 11 additions & 11 deletions tests/org.eclipse.emf.test.tools/META-INF/MANIFEST.MF
Expand Up @@ -2,31 +2,31 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.emf.test.tools;singleton:=true
Bundle-Version: 2.19.0.qualifier
Bundle-Version: 2.20.0.qualifier
Bundle-ClassPath: .
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Export-Package: org.eclipse.emf.test.tools;version="2.19.0",
org.eclipse.emf.test.tools.ant;version="2.19.0",
org.eclipse.emf.test.tools.codegen;version="2.19.0",
org.eclipse.emf.test.tools.converter;version="2.19.0",
org.eclipse.emf.test.tools.importer;version="2.19.0",
org.eclipse.emf.test.tools.merger;version="2.19.0",
org.eclipse.emf.test.tools.merger.facade;version="2.19.0"
Export-Package: org.eclipse.emf.test.tools;version="2.20.0",
org.eclipse.emf.test.tools.ant;version="2.20.0",
org.eclipse.emf.test.tools.codegen;version="2.20.0",
org.eclipse.emf.test.tools.converter;version="2.20.0",
org.eclipse.emf.test.tools.importer;version="2.20.0",
org.eclipse.emf.test.tools.merger;version="2.20.0",
org.eclipse.emf.test.tools.merger.facade;version="2.20.0"
Require-Bundle: org.apache.ant;bundle-version="[1.7.0,2.0.0)",
org.eclipse.ant.core;bundle-version="[3.2.0,4.0.0)",
org.eclipse.core.runtime;bundle-version="[3.9.0,4.0.0)",
org.eclipse.core.resources;bundle-version="[3.8.0,4.0.0)",
org.eclipse.emf.codegen.ecore;bundle-version="[2.33.0,3.0.0)",
org.eclipse.emf.ecore.xmi;bundle-version="[2.18.0,3.0.0)",
org.eclipse.emf.codegen.ecore;bundle-version="[2.37.0,3.0.0)",
org.eclipse.emf.ecore.xmi;bundle-version="[2.37.0,3.0.0)",
org.eclipse.emf.importer;bundle-version="[2.13.0,3.0.0)",
org.eclipse.jdt.core;bundle-version="[3.9.0,4.0.0)",
org.junit;bundle-version="[4.12.0,5.0.0)",
org.eclipse.text;bundle-version="[3.5.0,4.0.0)",
org.eclipse.emf.test.common;bundle-version="[1.11.0,2.0.0)",
org.eclipse.emf.ant;bundle-version="[2.12.0,3.0.0)",
org.eclipse.emf.importer.rose;bundle-version="[2.12.0,3.0.0)",
org.eclipse.emf.importer.rose;bundle-version="[2.13.0,3.0.0)",
org.eclipse.xsd.ecore.importer;bundle-version="[2.10.0,3.0.0)",
org.eclipse.emf.importer.ecore;bundle-version="[2.11.0,3.0.0)",
org.eclipse.emf.importer.java;bundle-version="[2.12.0,3.0.0)",
Expand Down
27 changes: 23 additions & 4 deletions tests/org.eclipse.emf.test.tools/data/Defaults.ecore
@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="defaults"
nsURI="http:///org.eclipse.emf.test.models/Defaults.ecore" nsPrefix="def">
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="defaults" nsURI="http:///org.eclipse.emf.test.models/Defaults.ecore"
nsPrefix="def">
<eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
<details key="prefix" value="Defaults"/>
<details key="basePackage" value="org.eclipse.emf.test.tools.genmodel"/>
Expand Down Expand Up @@ -367,10 +366,30 @@
<details key="staticDefaultValue" value="true"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="duration" defaultValueLiteral="1 s">
<eAnnotations source="http://www.eclipse.org/emf/GenModelTest">
<details key="staticDefaultValue" value="(Quantity&lt;Time>)DefaultsFactory.eINSTANCE.createFromString(DefaultsPackage.eINSTANCE.getEQuantity(), &quot;1 s&quot;)"/>
</eAnnotations>
<eGenericType eClassifier="#//EQuantity">
<eTypeArguments eClassifier="#//ETime"/>
</eGenericType>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="period" eType="#//ETimeQuantity"
defaultValueLiteral="1 s">
<eAnnotations source="http://www.eclipse.org/emf/GenModelTest">
<details key="staticDefaultValue" value="(Quantity&lt;Time>)DefaultsFactory.eINSTANCE.createFromString(DefaultsPackage.eINSTANCE.getETimeQuantity(), &quot;1 s&quot;)"/>
</eAnnotations>
</eStructuralFeatures>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="MyClass"/>
<eClassifiers xsi:type="ecore:EDataType" name="MyInt" instanceClassName="int"/>
<eClassifiers xsi:type="ecore:EDataType" name="MyIntegerObject" instanceClassName="java.lang.Integer"/>
<eClassifiers xsi:type="ecore:EDataType" name="MyDate" instanceClassName="java.util.Date"/>
<eClassifiers xsi:type="ecore:EDataType" name="UUID" instanceClassName="java.util.UUID"/>
<eClassifiers xsi:type="ecore:EDataType" name="EQuantity" instanceClassName="javax.measure.Quantity">
<eTypeParameters name="dimension"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EDataType" name="ETime" instanceClassName="javax.measure.quantity.Time"
serializable="false"/>
<eClassifiers xsi:type="ecore:EDataType" name="ETimeQuantity" instanceTypeName="javax.measure.Quantity&lt;javax.measure.quantity.Time>"/>
</ecore:EPackage>
2 changes: 1 addition & 1 deletion tests/org.eclipse.emf.test.tools/pom.xml
Expand Up @@ -12,7 +12,7 @@

<groupId>org.eclipse.emf</groupId>
<artifactId>org.eclipse.emf.test.tools</artifactId>
<version>2.19.0-SNAPSHOT</version>
<version>2.20.0-SNAPSHOT</version>
<packaging>eclipse-test-plugin</packaging>

</project>

0 comments on commit cd6a7e2

Please sign in to comment.