| 1 |
/* https://raw.githubusercontent.com/tsaglam/JavaCodeEcorification/master/src/main/java/jce/generators/EFactoryImplementationGenerator.xtend |
| 2 |
with some mods to include ««« inline comments |
| 3 |
*/ |
| 4 |
package jce.generators |
| 5 |
|
| 6 |
import java.io.File |
| 7 |
import java.util.List |
| 8 |
import jce.properties.EcorificationProperties |
| 9 |
import jce.util.PathHelper |
| 10 |
import org.eclipse.core.resources.IProject |
| 11 |
|
| 12 |
/** |
| 13 |
* Generator class for the generation of Ecore factory implementation classes. |
| 14 |
* @author Timur Saglam |
| 15 |
*/ |
| 16 |
class EFactoryImplementationGenerator extends ClassGenerator { |
| 17 |
extension PathHelper nameUtil |
| 18 |
|
| 19 |
/** |
| 20 |
* Basic constructor, sets the properties. |
| 21 |
*/ |
| 22 |
new(EcorificationProperties properties) { |
| 23 |
super(properties) |
| 24 |
nameUtil = new PathHelper('.') |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Creates a Ecore Factory in a package path with a specific name. |
| 29 |
*/ |
| 30 |
def public void create(String path, List<String> packageTypes, IProject project) { |
| 31 |
val currentPackage = path.replace(File.separatorChar, '.') // path to package declaration |
| 32 |
val interfacePackage = currentPackage.cutLastSegment |
| 33 |
val packageName = interfacePackage.getLastSegment.toFirstUpper |
| 34 |
val content = createFactoryContent(currentPackage, packageName, interfacePackage, packageTypes) |
| 35 |
createClass(path, '''«packageName»FactoryImpl.java''', content, project) |
| 36 |
monitor.beginTask(''' Created «packageName»FactoryImpl.java''', 0) // detailed logging |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Creates the content of an Ecore factory. |
| 41 |
*/ |
| 42 |
def private String createFactoryContent(String currentPackage, String packageName, String interfacePackage, List<String> packageTypes) ''' |
| 43 |
package «currentPackage»; |
| 44 |
|
| 45 |
import org.eclipse.emf.ecore.EClass; |
| 46 |
import org.eclipse.emf.ecore.EObject; |
| 47 |
import org.eclipse.emf.ecore.EPackage; |
| 48 |
import org.eclipse.emf.ecore.impl.EFactoryImpl; |
| 49 |
import org.eclipse.emf.ecore.plugin.EcorePlugin; |
| 50 |
|
| 51 |
import «interfacePackage».«packageName»Factory; |
| 52 |
import «interfacePackage».«packageName»Package; |
| 53 |
«FOR type : packageTypes» |
| 54 |
import «interfacePackage.cutFirstSegment».«type»; |
| 55 |
«ENDFOR» |
| 56 |
|
| 57 |
/** |
| 58 |
* An implementation of the model <b>Factory</b>. |
| 59 |
* @generated |
| 60 |
*/ |
| 61 |
public class «packageName»FactoryImpl extends EFactoryImpl implements «packageName»Factory { |
| 62 |
/** |
| 63 |
* Creates the default factory implementation. |
| 64 |
* <!-- begin-user-doc --> |
| 65 |
* <!-- end-user-doc --> |
| 66 |
* @generated |
| 67 |
*/ |
| 68 |
public static «packageName»Factory init() { |
| 69 |
try { |
| 70 |
«packageName»Factory the«packageName»Factory = («packageName»Factory)EPackage.Registry.INSTANCE.getEFactory(«packageName»Package.eNS_URI); |
| 71 |
if (the«packageName»Factory != null) { |
| 72 |
return the«packageName»Factory; |
| 73 |
} |
| 74 |
} |
| 75 |
catch (Exception exception) { |
| 76 |
EcorePlugin.INSTANCE.log(exception); |
| 77 |
} |
| 78 |
return new «packageName»FactoryImpl(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Creates an instance of the factory. |
| 83 |
* @generated |
| 84 |
*/ |
| 85 |
public «packageName»FactoryImpl() { |
| 86 |
super(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @generated |
| 91 |
*/ |
| 92 |
@Override |
| 93 |
public EObject create(EClass eClass) { |
| 94 |
switch (eClass.getClassifierID()) { |
| 95 |
«FOR type : packageTypes» |
| 96 |
case «packageName»Package.«constantName(type)»: return create«type»(); |
| 97 |
«ENDFOR» |
| 98 |
default: |
| 99 |
throw new IllegalArgumentException("The class '" + eClass.getName() + "' is not a valid classifier"); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
«FOR type : packageTypes SEPARATOR blankLine» |
| 104 |
«createFactoryMethod(type)» |
| 105 |
«ENDFOR» |
| 106 |
|
| 107 |
/** |
| 108 |
* @generated |
| 109 |
*/ |
| 110 |
public «packageName»Package get«packageName»Package() { |
| 111 |
return («packageName»Package)getEPackage(); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @deprecated |
| 116 |
* @generated |
| 117 |
*/ |
| 118 |
@Deprecated |
| 119 |
public static «packageName»Package getPackage() { |
| 120 |
return «packageName»Package.eINSTANCE; |
| 121 |
} |
| 122 |
|
| 123 |
} //«packageName»FactoryImpl |
| 124 |
''' |
| 125 |
|
| 126 |
/** |
| 127 |
* Creates the content of an Ecore factory method. |
| 128 |
*/ |
| 129 |
def private String createFactoryMethod(String className) ''' |
| 130 |
/** |
| 131 |
* @generated |
| 132 |
*/ |
| 133 |
public «className» create«className»() { |
| 134 |
return new «className»(); // origin code instance |
| 135 |
} |
| 136 |
''' |
| 137 |
|
| 138 |
/** |
| 139 |
* Makes a constant name out of a type name (MyType => MY_TYPE, ABCTest => ABC_TEST) |
| 140 |
*/ |
| 141 |
def private String constantName(String typeName) { |
| 142 |
var constantName = "" // empty result string |
| 143 |
var wasLowerCase = true // last word was lower case |
| 144 |
for (String word : typeName.split("(?=\\p{Upper})")) { // iterate over every word |
| 145 |
if(containsLowerCase(word) || wasLowerCase) { // if has or comes after lower case letter. |
| 146 |
constantName += '_' // add separator |
| 147 |
} |
| 148 |
constantName += word.toUpperCase() |
| 149 |
wasLowerCase = containsLowerCase(word) |
| 150 |
} |
| 151 |
return constantName.substring(1) |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Checks whether a String contains lower case characters. |
| 156 |
*/ |
| 157 |
def private boolean containsLowerCase(String string) { |
| 158 |
return !string.equals(string.toUpperCase()) |
| 159 |
} |
| 160 |
} |