{
try
{
// Initialize the SDO runtime
DataObjectUtil.initRuntime();
EPackage.Registry packageRegistry = new EPackageRegistryImpl(EPackage.Registry.INSTANCE);
// Create an EPackage for the generated SDO
if (packageURI == null)
packageURI = "http://" + javaPackage;
EPackage implEPackage = EcoreFactory.eINSTANCE.createEPackage();
implEPackage.setNsURI(packageURI);
String shortName = shortName(packageURI);
implEPackage.setName(shortName);
implEPackage.setNsPrefix(shortName.toLowerCase());
packageRegistry.put(packageURI, implEPackage);
// Create EClasses for all the given Java interfaces
Map eClasses = new HashMap();
for (Iterator iter = javaInterfaces.iterator(); iter.hasNext();)
{
String interfaceName = (String)iter.next();
Class instanceClass = Class.forName(interfaceName, true, classLoader);
EClass implEClass = EcoreFactory.eINSTANCE.createEClass();
String className = shortName(instanceClass.getName());
implEClass.setName(className);
implEClass.setInstanceClass(instanceClass);
eClasses.put(instanceClass, implEClass);
implEPackage.getEClassifiers().add(implEClass);
}
// Populate the EClasses with EAttributes and EReferences for their properties
for (Iterator iter = implEPackage.getEClassifiers().iterator(); iter.hasNext();)
{
EClass implEClass = (EClass)iter.next();
Class instanceClass = implEClass.getInstanceClass();
Method[] methods = instanceClass.getMethods();
for (int m = 0; m < methods.length; m++)
{
Method method = methods[m];
String propertyName = null;
if (method.getName().startsWith("get"))
propertyName = method.getName().substring(3);
else if (method.getName().startsWith("is"))
propertyName = method.getName().substring(2);
if (propertyName != null)
{
if (propertyName.length() > 1)
propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);
Class propertyClass = method.getReturnType();
EClass propertyEClass = (EClass)eClasses.get(propertyClass);
if (propertyEClass != null)
{
// The property is another SDO, create an EReference to represent the property
EReference reference = EcoreFactory.eINSTANCE.createEReference();
reference.setName(propertyName);
reference.setContainment(true);
reference.setEType(propertyEClass);
implEClass.getEStructuralFeatures().add(reference);
}
else
{
// The property is a List<T> and T is an SDO, created a 0..many EReference to represent the property
if (propertyClass == List.class)
{
Type genericType = method.getGenericReturnType();
if (genericType instanceof ParameterizedType)
{
ParameterizedType parameterizedType = (ParameterizedType)genericType;
Type[] targs = parameterizedType.getActualTypeArguments();
if (targs.length != 0 && eClasses.containsKey(targs[0]))
{
propertyEClass = (EClass)eClasses.get(targs[0]);
if (propertyEClass != null)
{
EReference reference = EcoreFactory.eINSTANCE.createEReference();
reference.setName(propertyName);
reference.setContainment(true);
reference.setEType(propertyEClass);
reference.setUpperBound(-1);
implEClass.getEStructuralFeatures().add(reference);
}
}
}
continue;
}
// The property is a regular Java type / not an SDO, create an EAttribute to represent it
EAttribute attribute = EcoreFactory.eINSTANCE.createEAttribute();
attribute.setName(propertyName);
EDataType dataType = (EDataType)TypeHelper.INSTANCE.getType(propertyClass);
attribute.setEType(dataType);
implEClass.getEStructuralFeatures().add(attribute);
}
}
}
}
generatePackages(packageRegistry.values(), packageURI, shortName, targetDirectory, javaPackage, prefix, genOptions);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}