File: Xtend.xtend

package info (click to toggle)
cloc 2.06-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,064 kB
  • sloc: perl: 30,146; cpp: 1,219; python: 623; ansic: 334; asm: 267; makefile: 244; sh: 186; sql: 144; java: 136; ruby: 111; cs: 104; pascal: 52; lisp: 50; haskell: 35; f90: 35; cobol: 35; objc: 25; php: 22; javascript: 15; fortran: 9; ml: 8; xml: 7; tcl: 2
file content (160 lines) | stat: -rw-r--r-- 5,036 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* https://raw.githubusercontent.com/tsaglam/JavaCodeEcorification/master/src/main/java/jce/generators/EFactoryImplementationGenerator.xtend
   with some mods to include ««« inline comments
   */
package jce.generators

import java.io.File
import java.util.List
import jce.properties.EcorificationProperties
import jce.util.PathHelper
import org.eclipse.core.resources.IProject

/**
 * Generator class for the generation of Ecore factory implementation classes.
 * @author Timur Saglam
 */
class EFactoryImplementationGenerator extends ClassGenerator {
	extension PathHelper nameUtil

	/**
	 * Basic constructor, sets the properties.
	 */
	new(EcorificationProperties properties) {
		super(properties)
		nameUtil = new PathHelper('.')
	}

	/**
	 * Creates a Ecore Factory in a package path with a specific name. 
	 */
	def public void create(String path, List<String> packageTypes, IProject project) {
		val currentPackage = path.replace(File.separatorChar, '.') // path to package declaration
		val interfacePackage = currentPackage.cutLastSegment
		val packageName = interfacePackage.getLastSegment.toFirstUpper
		val content = createFactoryContent(currentPackage, packageName, interfacePackage, packageTypes)
		createClass(path, '''«packageName»FactoryImpl.java''', content, project)
		monitor.beginTask(''' Created «packageName»FactoryImpl.java''', 0) // detailed logging
	}

	/**
	 * Creates the content of an Ecore factory.
	 */
	def private String createFactoryContent(String currentPackage, String packageName, String interfacePackage, List<String> packageTypes) '''
		package «currentPackage»;
		
		import org.eclipse.emf.ecore.EClass;
		import org.eclipse.emf.ecore.EObject;
		import org.eclipse.emf.ecore.EPackage;
		import org.eclipse.emf.ecore.impl.EFactoryImpl;
		import org.eclipse.emf.ecore.plugin.EcorePlugin;
		
		import «interfacePackage».«packageName»Factory;
		import «interfacePackage».«packageName»Package;
		«FOR type : packageTypes»
			import «interfacePackage.cutFirstSegment».«type»;
		«ENDFOR»
		
		/**
		 * An implementation of the model <b>Factory</b>.
		 * @generated
		 */
		public class «packageName»FactoryImpl extends EFactoryImpl implements «packageName»Factory {
		    /**
		     * Creates the default factory implementation.
		     * <!-- begin-user-doc -->
		     * <!-- end-user-doc -->
		     * @generated
		     */
		    public static «packageName»Factory init() {
		        try {
		            «packageName»Factory the«packageName»Factory = («packageName»Factory)EPackage.Registry.INSTANCE.getEFactory(«packageName»Package.eNS_URI);
		            if (the«packageName»Factory != null) {
		                return the«packageName»Factory;
		            }
		        }
		        catch (Exception exception) {
		            EcorePlugin.INSTANCE.log(exception);
		        }
		        return new «packageName»FactoryImpl();
		    }
		
		    /**
		     * Creates an instance of the factory.
		     * @generated
		     */
		    public «packageName»FactoryImpl() {
		        super();
		    }
		
		    /**
		     * @generated
		     */
		    @Override
		    public EObject create(EClass eClass) {
		        switch (eClass.getClassifierID()) {
					«FOR type : packageTypes»
						case «packageName»Package.«constantName(type)»: return create«type»();
					«ENDFOR»
					default:
					    throw new IllegalArgumentException("The class '" + eClass.getName() + "' is not a valid classifier");
					     }
					 }
			
			«FOR type : packageTypes SEPARATOR blankLine»
				«createFactoryMethod(type)»
			«ENDFOR»
			
			/**
			 * @generated
			 */
			public «packageName»Package get«packageName»Package() {
			return («packageName»Package)getEPackage();
			}
		
		    /**
		     * @deprecated
		     * @generated
		     */
		    @Deprecated
		    public static «packageName»Package getPackage() {
		        return «packageName»Package.eINSTANCE;
		    }
		    
		} //«packageName»FactoryImpl
	'''

	/**
	 * Creates the content of an Ecore factory method.
	 */
	def private String createFactoryMethod(String className) '''
		/**
		 * @generated
		 */
		public «className» create«className»() {
			return new «className»(); // origin code instance
		}	
	'''

	/**
	 * Makes a constant name out of a type name (MyType => MY_TYPE, ABCTest => ABC_TEST)
	 */
	def private String constantName(String typeName) {
		var constantName = "" // empty result string
		var wasLowerCase = true // last word was lower case
		for (String word : typeName.split("(?=\\p{Upper})")) { // iterate over every word
			if(containsLowerCase(word) || wasLowerCase) { // if has or comes after lower case letter.
				constantName += '_' // add separator
			}
			constantName += word.toUpperCase()
			wasLowerCase = containsLowerCase(word)
		}
		return constantName.substring(1)
	}

	/**
	 * Checks whether a String contains lower case characters. 
	 */
	def private boolean containsLowerCase(String string) {
		return !string.equals(string.toUpperCase())
	}
}