-
Notifications
You must be signed in to change notification settings - Fork 3
/
EclipseJDTQualifiedNameResolver.java
382 lines (342 loc) · 12.4 KB
/
EclipseJDTQualifiedNameResolver.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*******************************************************************************
* Copyright (c) 2020, 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.query.ide.jdt.runtime.impl.namespace;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.acceleo.query.ide.jdt.Activator;
import org.eclipse.acceleo.query.ide.runtime.impl.namespace.EclipseQualifiedNameResolver;
import org.eclipse.acceleo.query.runtime.impl.namespace.ClassLoaderQualifiedNameResolver;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.osgi.container.Module;
/**
* Eclipse JDT {@link ClassLoaderQualifiedNameResolver}.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class EclipseJDTQualifiedNameResolver extends ClassLoaderQualifiedNameResolver {
/**
* The {@link IJavaProject}.
*/
private final IJavaProject project;
/**
* For workspace use, local project resolution only.
*/
private final boolean forWorkspace;
private final URI[] dependencyProjectEntries;
/**
* Constructor.String
*
* @param classLoader
* the {@link ClassLoader}
* @param project
* the {@link IProject}
* @param qualifierSeparator
* the qualifier name separator
* @param forWorkspace
* <code>true</code> for workspace use, local project resolution only
* @param dependencyProjectEntries
* dependency project entries {@link List}
*/
public EclipseJDTQualifiedNameResolver(ClassLoader classLoader, IProject project,
String qualifierSeparator, boolean forWorkspace, List<String> dependencyProjectEntries) {
super(createProjectClassLoader(classLoader, project, forWorkspace, dependencyProjectEntries),
qualifierSeparator);
this.project = JavaCore.create(project);
this.forWorkspace = forWorkspace;
final List<URI> uriList = new ArrayList<>();
for (String entry : dependencyProjectEntries) {
final IPath path = new Path(entry);
final URI urli = path.toFile().toURI();
uriList.add(urli);
}
this.dependencyProjectEntries = uriList.toArray(new URI[uriList.size()]);
}
/**
* Creates the class loader for the given {@link IProject}.
*
* @param classLoader
* the parent {@link ClassLoader}
* @param project
* the {@link IProject}
* @param forWorspace
* <code>true</code> for workspace use, local project resolution only
* @param dependencyProjectEntries
* dependency project entries
* @return the class loader for the given {@link IProject}
*/
protected static ClassLoader createProjectClassLoader(ClassLoader classLoader, IProject project,
boolean forWorspace, List<String> dependencyProjectEntries) {
ClassLoader res;
if (project.exists() && project.isOpen()) {
final IJavaProject javaProject = JavaCore.create(project);
if (javaProject != null && javaProject.exists()) {
try {
final String[] classPathEntries = getClassPathes(javaProject, forWorspace,
dependencyProjectEntries);
final List<URL> urlList = new ArrayList<URL>();
for (String entry : classPathEntries) {
final IPath path = new Path(entry);
final URL url = path.toFile().toURI().toURL();
urlList.add(url);
}
final URL[] urls = (URL[])urlList.toArray(new URL[urlList.size()]);
res = new URLClassLoader(urls, classLoader);
} catch (CoreException e) {
Activator.getPlugin().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
EclipseQualifiedNameResolver.CAN_T_LOAD_FROM_WORKSPACE, e));
res = EclipseQualifiedNameResolver.createProjectClassLoader(classLoader, project);
} catch (MalformedURLException e) {
Activator.getPlugin().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
EclipseQualifiedNameResolver.CAN_T_LOAD_FROM_WORKSPACE, e));
res = EclipseQualifiedNameResolver.createProjectClassLoader(classLoader, project);
}
} else {
res = EclipseQualifiedNameResolver.createProjectClassLoader(classLoader, project);
}
} else {
res = classLoader;
}
return res;
}
// copied from JavaRuntime.computeDefaultRuntimeClassPath()
private static String[] getClassPathes(IJavaProject javaProject, boolean forWorkspace,
List<String> dependencyProjectEntries) throws CoreException {
IRuntimeClasspathEntry[] unresolved = JavaRuntime.computeUnresolvedRuntimeClasspath(javaProject);
// 1. remove bootpath entries
// 2. resolve & translate to local file system paths
List<String> resolved = new ArrayList<>(unresolved.length);
for (int i = 0; i < unresolved.length; i++) {
IRuntimeClasspathEntry entry = unresolved[i];
if (entry.getClasspathProperty() == IRuntimeClasspathEntry.USER_CLASSES) {
IRuntimeClasspathEntry[] entries = JavaRuntime.resolveRuntimeClasspathEntry(entry,
javaProject);
for (int j = 0; j < entries.length; j++) {
String location = entries[j].getLocation();
if (location != null) {
if (isDependencyProjectEntry(javaProject, forWorkspace, entries[j])) {
dependencyProjectEntries.add(location);
}
resolved.add(location);
}
}
}
}
return resolved.toArray(new String[resolved.size()]);
}
/**
* Tells if the given {@link IRuntimeClasspathEntry} if from a project we depend on.
*
* @param javaProject
* the current {@link IJavaProject}
* @param forWorspace
* <code>true</code> for workspace use, local project resolution only
* @param entry
* the {@link IRuntimeClasspathEntry}
* @return <code>true</code> if the given {@link IRuntimeClasspathEntry} if from a project we depend on,
* <code>false</code> otherwise
*/
private static boolean isDependencyProjectEntry(IJavaProject javaProject, boolean forWorkspace,
IRuntimeClasspathEntry entry) {
final boolean res;
if (forWorkspace) {
if (entry.getType() == IRuntimeClasspathEntry.PROJECT && javaProject.getProject() != entry
.getResource()) {
res = true;
} else {
final IResource entryResource = entry.getResource();
if (entryResource != null && entryResource.exists() && entryResource.getProject() != null
&& !entryResource.getProject().equals(javaProject.getProject())) {
res = true;
} else {
res = false;
}
}
} else {
res = false;
}
return res;
}
@Override
protected Object load(String qualifiedName) {
final Object res;
if (!forWorkspace || !isInDependencyProject(super.getURI(qualifiedName))) {
res = super.load(qualifiedName);
} else {
res = null;
// we perform a dummy registration to prevent further loading
dummyRegistration(qualifiedName);
}
return res;
}
private boolean isInDependencyProject(URI uri) {
boolean res = false;
if (uri != null) {
for (URI entryURI : dependencyProjectEntries) {
if (!entryURI.relativize(uri).equals(uri)) {
res = true;
break;
}
}
}
return res;
}
@Override
public URI getSourceURI(String qualifiedName) {
URI res;
if (project != null) {
try {
final URI foundURI = getSourceURI(project, qualifiedName);
if (foundURI != null) {
res = foundURI;
} else {
res = super.getSourceURI(qualifiedName);
}
} catch (JavaModelException e) {
res = super.getSourceURI(qualifiedName);
}
} else {
res = super.getSourceURI(qualifiedName);
}
return res;
}
/**
* Gets the source {@link URI} corresponding to the given {@link Module} qualified name for the given
* {@link IJavaProject}.
*
* @param javaProject
* the {@link IJavaProject}
* @param qualifiedName
* the qualified name (e.g. <code>qualified::path::to::module</code>)
* @throws JavaModelException
* if the class path of the given {@link IJavaProject} can't be resolved.
* @return the source {@link URI} corresponding to the given {@link Module} qualified name for the given
* {@link IJavaProject} if any, <code>null</code> otherwise
*/
private URI getSourceURI(IJavaProject javaProject, String qualifiedName) throws JavaModelException {
URI res = null;
found: for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
final IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
final String srcFolder = entry.getPath().toString();
final List<String> relativePathes = getPossibleResourceNames(qualifiedName);
for (String relativePath : relativePathes) {
final URI uri = workspaceRoot.getFile(new Path(srcFolder + "/" + relativePath))
.getLocationURI();
final File file = new File(uri);
if (file.isFile() && file.exists()) {
res = file.toURI();
break found;
}
}
} else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
final IProject childProject = ResourcesPlugin.getWorkspace().getRoot().getProject(entry
.getPath().lastSegment());
if (childProject != null) {
final IJavaProject childJavaProject = JavaCore.create(childProject);
res = getSourceURI(childJavaProject, qualifiedName);
}
}
}
return res;
}
@Override
public URI getBinaryURI(URI sourceURI) {
URI res;
if (project != null) {
try {
final URI foundURI = getBinaryURI(project, sourceURI);
if (foundURI != null) {
res = foundURI;
} else {
res = super.getBinaryURI(sourceURI);
}
} catch (JavaModelException e) {
res = super.getBinaryURI(sourceURI);
}
} else {
res = super.getBinaryURI(sourceURI);
}
return res;
}
private URI getBinaryURI(IJavaProject javaProject, URI sourceURI) throws JavaModelException {
final URI res;
final IWorkspaceRoot workspaceRoot = javaProject.getProject().getWorkspace().getRoot();
final IFile sourceFile = workspaceRoot.getFileForLocation(new Path(sourceURI.getPath()));
final IClasspathEntry entry = getContainingEntry(javaProject, sourceFile);
if (entry != null) {
if (entry.getContentKind() == IPackageFragmentRoot.K_BINARY) {
res = sourceURI;
} else if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
// TODO check forWorkspace and the project to the class path entry
final IPath relativePath = sourceFile.getFullPath().makeRelativeTo(entry.getPath());
final IPath binaryPath = javaProject.getOutputLocation().append(relativePath);
final IFile binaryFile = workspaceRoot.getFile(binaryPath);
if (binaryFile.exists()) {
res = binaryFile.getLocationURI();
} else {
res = null;
}
} else {
throw new IllegalStateException("unknown classpath entry content kind.");
}
} else {
res = null;
}
return res;
}
private IClasspathEntry getContainingEntry(IJavaProject javaProject, final IFile sourceFile)
throws JavaModelException {
final IClasspathEntry res;
final IClasspathEntry foundEntry = javaProject.findContainingClasspathEntry(sourceFile);
if (foundEntry != null) {
res = foundEntry;
} else {
IClasspathEntry fallbackFound = null;
for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
if (entry.getPath().isPrefixOf(sourceFile.getFullPath())) {
fallbackFound = entry;
break;
}
}
res = fallbackFound;
}
return res;
}
/**
* Gets the {@link IProject}.
*
* @return the {@link IProject}
*/
protected IJavaProject getProject() {
return project;
}
}