-
Notifications
You must be signed in to change notification settings - Fork 3
/
Services.java
223 lines (197 loc) · 6.68 KB
/
Services.java
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.aql.ide.ui.module.services;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.acceleo.Metamodel;
import org.eclipse.acceleo.Module;
import org.eclipse.acceleo.Template;
import org.eclipse.acceleo.Variable;
import org.eclipse.acceleo.aql.AcceleoUtil;
import org.eclipse.acceleo.aql.parser.AcceleoParser;
import org.eclipse.acceleo.query.ast.EClassifierTypeLiteral;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameQueryEnvironment;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameResolver;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EPackage;
/**
* Java services.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class Services {
/**
* The {@link IQualifiedNameQueryEnvironment}.
*/
private static IQualifiedNameQueryEnvironment queryEnvironment;
/**
* The workspace {@link IQualifiedNameResolver}.
*/
private static IQualifiedNameResolver workspaceResolver;
/**
* Sets the {@link IQualifiedNameQueryEnvironment}.
*
* @param queryEnvironment
* the {@link IQualifiedNameQueryEnvironment}
* @param workspaceResolver
* the workspace {@link IQualifiedNameQueryEnvironment}
*/
public static void initialize(IQualifiedNameQueryEnvironment queryEnvironment,
IQualifiedNameResolver workspaceResolver) {
Services.queryEnvironment = queryEnvironment;
Services.workspaceResolver = workspaceResolver;
}
/**
* Gets the qualified name for the given {@link Module}.
*
* @param module
* the {@link Module}
* @return the qualified name for the given {@link Module}
*/
public String getQualifiedName(Module module) {
final String res;
final String tmpQualifiedName = module.eResource().getURI().toString();
final int firstSeparatorIndex = tmpQualifiedName.indexOf(AcceleoParser.QUALIFIER_SEPARATOR);
if (firstSeparatorIndex >= 0) {
res = tmpQualifiedName.substring(firstSeparatorIndex + AcceleoParser.QUALIFIER_SEPARATOR
.length());
} else {
res = "";
}
return res;
}
/**
* Gets the Java package for the given {@link Module}.
*
* @param module
* the {@link Module}
* @return the Java package for the given {@link Module}
*/
public String getJavaPackage(Module module) {
final String res;
final String qualifiedName = getQualifiedName(module);
final int lastSeparatorIndex = qualifiedName.lastIndexOf(AcceleoParser.QUALIFIER_SEPARATOR);
if (lastSeparatorIndex >= 0) {
res = qualifiedName.substring(0, lastSeparatorIndex).replace(AcceleoParser.QUALIFIER_SEPARATOR,
".");
} else {
res = "";
}
return res;
}
/**
* Gets the current user name.
*
* @param object
* any object
* @return the current user name
*/
public String getUserName(Object object) {
return System.getProperty("user.name");
}
/**
* Gets the Java import statement for the given {@link EPackage}.
*
* @param ePkg
* the {@link EPackage}
* @return the Java import statement for the given {@link EPackage}
*/
public String getJavaImport(EPackage ePkg) {
return "import " + ePkg.getClass().getInterfaces()[0].getCanonicalName() + ";";
}
/**
* Gets the Java initialization statement for the given {@link EPackage}.
*
* @param ePkg
* the {@link EPackage}
* @return the Java initialization statement for the given {@link EPackage}
*/
public String getJavaInitialize(EPackage ePkg) {
return ePkg.getClass().getInterfaces()[0].getSimpleName() + ".eINSTANCE.getName();";
}
/**
* Gets the {@link Set} all {@link EPackage} used by the given {@link Module} and direct and indirect
* {@link Module} dependencies.
*
* @param module
* the {@link Module}
* @return the {@link Set} all {@link EPackage} used by the given {@link Module} and direct and indirect
* {@link Module} dependencies
*/
public Set<EPackage> getAllEPackages(Module module) {
final Set<EPackage> res = new LinkedHashSet<>();
final Set<String> qualifiedNames = new LinkedHashSet<>();
final String moduleQualifiedName = workspaceResolver.getQualifiedName(module);
qualifiedNames.add(moduleQualifiedName);
qualifiedNames.addAll(workspaceResolver.getDependOn(moduleQualifiedName));
for (String qualifiedName : qualifiedNames) {
final Object resolved = workspaceResolver.resolve(qualifiedName);
if (resolved instanceof Module) {
final Module child = (Module)resolved;
for (Metamodel metamodel : child.getMetamodels()) {
if (metamodel.getReferencedPackage() != null) {
res.add(metamodel.getReferencedPackage());
}
}
}
}
return res;
}
/**
* Gets the receiver qualified {@link Class} name for the given {@link Module}.
*
* @param module
* the {@link Module}
* @return the receiver qualified {@link Class} name for the given {@link Module}
*/
public String getReceiverQualifiedClassName(Module module) {
final String res;
final List<Template> mains = AcceleoUtil.getMainTemplates(module);
if (!mains.isEmpty()) {
final Template main = mains.get(0);
if (!main.getParameters().isEmpty()) {
final Variable receiver = main.getParameters().get(0);
if (receiver.getTypeAql() instanceof EClassifierTypeLiteral) {
final EClassifierTypeLiteral type = (EClassifierTypeLiteral)receiver.getTypeAql();
final EClassifier eClassifier = queryEnvironment.getEPackageProvider().getTypes(type
.getEPackageName(), type.getEClassifierName()).iterator().next();
res = eClassifier.getInstanceClassName();
} else {
res = "";
}
} else {
res = "";
}
} else {
res = "";
}
return res;
}
/**
* Gets the receiver {@link Class} name for the given {@link Module}.
*
* @param module
* the {@link Module}
* @return the receiver {@link Class} name for the given {@link Module}
*/
public String getReceiverClassName(Module module) {
final String res;
final String qualifiedClassName = getReceiverQualifiedClassName(module);
final int lastDotIndex = qualifiedClassName.lastIndexOf(".");
if (lastDotIndex >= 0) {
res = qualifiedClassName.substring(lastDotIndex + 1);
} else {
res = "";
}
return res;
}
}