Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EMF 2.7 Unable to support enumerations called Enum(Worked OK in EMF 2.4)
EMF 2.7 Unable to support enumerations called Enum [message #1042519] Tue, 16 April 2013 14:26 Go to next message
Rob Mising name is currently offline Rob Mising nameFriend
Messages: 118
Registered: July 2010
Senior Member
Hi Ed,

I have been updating our system from EMF 2.4 to EMF 2.7 and unfortunately we have stumbled accross a problem that I'm really hoping that you can assist with please.

If we have an XSD:

<xsd:schema xmlns="http://www.example.com/enumtest/" xmlns:tns1="http://www.example.com/enumtest/" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/enumtest/" elementFormDefault="qualified" attributeFormDefault="qualified">
	<xsd:simpleType name="Enum" ecore:name="Enum">
		<xsd:restriction base="xsd:string">
			<xsd:enumeration value="One" ecore:name="ONE"/>
			<xsd:enumeration value="Two" ecore:name="TWO"/>
			<xsd:enumeration value="Three" ecore:name="THREE"/>
		</xsd:restriction>
	</xsd:simpleType>
</xsd:schema>


And then use that to create an ecore/genmodel and then generate java code (Using the default templates), then it generates invalid code that will not compile - in the Factory java files.

In EMF 2.4 it worked OK, because it would generate code that used the fully qualified name for things like Enum:

public com.example.enumtest.Enum createEnumObjectFromString(EDataType eDataType, String initialValue) {
	return createEnumFromString(EnumtestPackage.Literals.ENUM, initialValue);
}


However in EMF 2.7 is will generate:

public Enum createEnumObjectFromString(EDataType eDataType, String initialValue) {
	return createEnumFromString(EnumtestPackage.Literals.ENUM, initialValue);
}



Any assistance you can give would be greatly appreciated.

Thanks

Rob
  • Attachment: EnumTest.xsd
    (Size: 0.64KB, Downloaded 167 times)
Re: EMF 2.7 Unable to support enumerations called Enum [message #1042597 is a reply to message #1042519] Tue, 16 April 2013 16:18 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Rob,

Open a bugzilla and I can look at addressing it in 2.9. It looks like
adding and explicit import to the file avoids the problem.

import com.example.enumtest.Enum;




On 16/04/2013 4:26 PM, Rob Mising name wrote:
> Hi Ed,
>
> I have been updating our system from EMF 2.4 to EMF 2.7 and unfortunately we have stumbled accross a problem that I'm really hoping that you can assist with please.
>
> If we have an XSD:
>
> <xsd:schema xmlns="http://www.example.com/enumtest/" xmlns:tns1="http://www.example.com/enumtest/" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/enumtest/" elementFormDefault="qualified" attributeFormDefault="qualified">
> <xsd:simpleType name="Enum" ecore:name="Enum">
> <xsd:restriction base="xsd:string">
> <xsd:enumeration value="One" ecore:name="ONE"/>
> <xsd:enumeration value="Two" ecore:name="TWO"/>
> <xsd:enumeration value="Three" ecore:name="THREE"/>
> </xsd:restriction>
> </xsd:simpleType>
> </xsd:schema>
>
> And then use that to create an ecore/genmodel and then generate java code (Using the default templates), then it generates invalid code that will not compile - in the Factory java files.
>
> In EMF 2.4 it worked OK, because it would generate code that used the fully qualified name for things like Enum:
>
> public com.example.enumtest.Enum createEnumObjectFromString(EDataType eDataType, String initialValue) {
> return createEnumFromString(EnumtestPackage.Literals.ENUM, initialValue);
> }
>
> However in EMF 2.7 is will generate:
>
> public Enum createEnumObjectFromString(EDataType eDataType, String initialValue) {
> return createEnumFromString(EnumtestPackage.Literals.ENUM, initialValue);
> }
>
>
> Any assistance you can give would be greatly appreciated.
>
> Thanks
>
> Rob


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EMF 2.7 Unable to support enumerations called Enum [message #1043074 is a reply to message #1042597] Wed, 17 April 2013 08:32 Go to previous messageGo to next message
Rob Mising name is currently offline Rob Mising nameFriend
Messages: 118
Registered: July 2010
Senior Member
Thanks Ed, I have raised:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=405854

If possible please could you point me at a diff if you do manage to fix it - I would like to see if there is an easy way for me to patch this one against 2.7
Re: EMF 2.7 Unable to support enumerations called Enum [message #1043168 is a reply to message #1043074] Wed, 17 April 2013 11:17 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Rob,

When I fix bugs, I always include a link to the commit, which shows the
diff.


On 17/04/2013 10:32 AM, Rob Mising name wrote:
> Thanks Ed, I have raised:
>
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=405854
>
> If possible please could you point me at a diff if you do manage to
> fix it - I would like to see if there is an easy way for me to patch
> this one against 2.7


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EMF 2.7 Unable to support enumerations called Enum [message #1043171 is a reply to message #1043168] Wed, 17 April 2013 11:22 Go to previous messageGo to next message
Rob Mising name is currently offline Rob Mising nameFriend
Messages: 118
Registered: July 2010
Senior Member
That would be brilliant - thanks Ed!

Rob
Re: EMF 2.7 Unable to support enumerations called Enum [message #1058293 is a reply to message #1042519] Mon, 13 May 2013 13:34 Go to previous messageGo to next message
Rob Mising name is currently offline Rob Mising nameFriend
Messages: 118
Registered: July 2010
Senior Member
Hi Ed,

I have been trying to work out what change caused this (To see if I can progromatically work around this) and I may be barking up the wrong tree, but could it be the change in:

org.eclipse.emf.codegen.ecore.genmodel.impl.GenBaseImpl, method: isPrimitiveType

Where it changed from:

  protected boolean isPrimitiveType(EClassifier eType)
  {
    try
    {
      Object result = eType.getInstanceClass();
      if (result == null)
      {
        return false;
      }
      Class instanceClass = (Class)result;
      return instanceClass.isPrimitive();
    }
    catch (Exception localException) {
    }
    return false;
  }


to:

  protected boolean isPrimitiveType(EClassifier eType)
  {
    return CodeGenUtil.isJavaPrimitiveType(eType.getInstanceClassName());
  }


Thanks

Rob
Re: EMF 2.7 Unable to support enumerations called Enum [message #1058317 is a reply to message #1058293] Mon, 13 May 2013 14:35 Go to previous messageGo to next message
Rob Mising name is currently offline Rob Mising nameFriend
Messages: 118
Registered: July 2010
Senior Member
Sorry, my mistake - continued looking and it appears to be a change made to:

org.eclipse.emf.codegen.util.ImportManager

This class has changed quite a bit between 2.4 and 2.7, I think the problem is in the method "basicGetImportedName", there are checks:

      String registeredName = shortNameToImportMap.get(shortName);
      
      if (registeredName == null)
      {
        // If no match, check for implicit java.lang import, but only on the first try (at the compilation unit level).
        // Failing that, check for matching package.
        //
        if (qualifiedNameChars == null && importName.equals("java.lang." + shortName))
        {
          if (javaLangImports != null && javaLangImports.contains(shortName))
          {
            imports.add(importName);
          }
          return baseName;
        }
        else if (importedPackages.contains(getPackageName(importName)) &&  (javaLangImports == null || !javaLangImports.contains(baseName)))
        {
          return baseName;
        }
      }


However the final "else if" (if I understand it correctly) needs to also check that it is not a class with the Java types?

Maybe use: CodeGenUtil.isJavaPrimitiveType ?

What do you think - could I be in the correct sort of area this time?

Thanks

Rob
Re: EMF 2.7 Unable to support enumerations called Enum [message #1058336 is a reply to message #1058317] Mon, 13 May 2013 15:09 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Rob,

That could be the case; I've not had time to look into the problem with
my recent travels. Please put your observations in the bugzilla and
hopefully I'll have time this week...


On 13/05/2013 4:35 PM, Rob Mising name wrote:
> Sorry, my mistake - continued looking and it appears to be a change
> made to:
>
> org.eclipse.emf.codegen.util.ImportManager
>
> This class has changed quite a bit between 2.4 and 2.7, I think the
> problem is in the method "basicGetImportedName", there are checks:
>
> String registeredName = shortNameToImportMap.get(shortName);
> if (registeredName == null)
> {
> // If no match, check for implicit java.lang import, but only
> on the first try (at the compilation unit level).
> // Failing that, check for matching package.
> //
> if (qualifiedNameChars == null &&
> importName.equals("java.lang." + shortName))
> {
> if (javaLangImports != null &&
> javaLangImports.contains(shortName))
> {
> imports.add(importName);
> }
> return baseName;
> }
> else if (importedPackages.contains(getPackageName(importName))
> && (javaLangImports == null || !javaLangImports.contains(baseName)))
> {
> return baseName;
> }
> }
>
>
> However the final "else if" (if I understand it correctly) needs to
> also check that it is not a class with the Java types?
>
> Maybe use: CodeGenUtil.isJavaPrimitiveType ?
>
> What do you think - could I be in the correct sort of area this time?
>
> Thanks
>
> Rob
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EMF 2.7 Unable to support enumerations called Enum [message #1058654 is a reply to message #1042519] Tue, 14 May 2013 15:35 Go to previous messageGo to next message
Rob Mising name is currently offline Rob Mising nameFriend
Messages: 118
Registered: July 2010
Senior Member
Hi Ed,

Thank you for fixing that. For my patch I have tried to copy what you have done but using templates. This is because it is quite hard for us to override just a single EMF file to apply a patch on top of the original EMF features/plugins.

So I have added the following to the FactoryClass.javajet:

<%// PATCH for https://bugs.eclipse.org/bugs/show_bug.cgi?id=405854 %>
<%List<String> result = new ArrayList<String>();%>
<%for (GenEnum genEnum : genPackage.getGenEnums()) { String name = genEnum.getName(); if (org.eclipse.emf.codegen.util.CodeGenUtil.isJavaDefaultType(name)) { result.add(name); } }%>
<%genModel.getImportManager().addJavaLangImports(result);%>
<%// END PATCH %>


I hope this is OK.

Thanks

Rob
Re: EMF 2.7 Unable to support enumerations called Enum [message #1058660 is a reply to message #1058654] Tue, 14 May 2013 15:49 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Rob,

Yes, that looks reasonable. It does appear that the factory
implementation is the only generated artifact that runs into problems...


On 14/05/2013 5:35 PM, Rob Mising name wrote:
> Hi Ed,
>
> Thank you for fixing that. For my patch I have tried to copy what you
> have done but using templates. This is because it is quite hard for
> us to override just a single EMF file to apply a patch on top of the
> original EMF features/plugins.
>
> So I have added the following to the FactoryClass.javajet:
>
> <%// PATCH for https://bugs.eclipse.org/bugs/show_bug.cgi?id=405854 %>
> <%List<String> result = new ArrayList<String>();%>
> <%for (GenEnum genEnum : genPackage.getGenEnums()) { String name =
> genEnum.getName(); if
> (org.eclipse.emf.codegen.util.CodeGenUtil.isJavaDefaultType(name)) {
> result.add(name); } }%>
> <%genModel.getImportManager().addJavaLangImports(result);%>
> <%// END PATCH %>
>
>
> I hope this is OK.
>
> Thanks
>
> Rob
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[POLL] Still Using Java 1.5?
Next Topic:[Teneo][Hibernate] Hints on general usage or lifecycle
Goto Forum:
  


Current Time: Tue Apr 23 10:08:31 GMT 2024

Powered by FUDForum. Page generated in 0.03513 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top