-
Notifications
You must be signed in to change notification settings - Fork 45
/
SARLClasspathContainer.java
178 lines (155 loc) · 5.05 KB
/
SARLClasspathContainer.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
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2016 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sarl.eclipse.buildpath;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.naming.NameNotFoundException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.JavaCore;
import org.osgi.framework.Bundle;
import io.sarl.eclipse.util.BundleUtil;
/** Classpath container dedicated to the SARL environment.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
public class SARLClasspathContainer implements IClasspathContainer {
/** Names of the reference libraries that are required to compile the SARL
* code and the generated Java code.
*/
public static final String[] SARL_REFERENCE_LIBRARIES = {
"org.eclipse.xtext.xbase.lib", //$NON-NLS-1$
"com.google.guava", //$NON-NLS-1$
"javax.inject", //$NON-NLS-1$
"io.sarl.lang.core", //$NON-NLS-1$
"io.sarl.util", //$NON-NLS-1$
"io.sarl.core", //$NON-NLS-1$
};
/** URL of the Javadoc for SARL API.
*/
public static final String JAVADOC_URL = "http://www.sarl.io/docs/api/"; //$NON-NLS-1$
private IClasspathEntry[] entries;
private final IPath containerPath;
/**
* @param containerPath - the path of the container, e.g. the project.
*/
public SARLClasspathContainer(IPath containerPath) {
this.containerPath = containerPath;
}
/** Replies the list of the symbolic names of the bundle dependencies.
*
* @return the bundle symbolic names of the dependencies.
*/
@SuppressWarnings("static-method")
public Set<String> getBundleDependencies() {
final Set<String> deps = new HashSet<>();
deps.addAll(Arrays.asList(SARL_REFERENCE_LIBRARIES));
return deps;
}
@Override
public synchronized IClasspathEntry[] getClasspathEntries() {
if (this.entries == null) {
try {
final List<IClasspathEntry> newEntries = new ArrayList<>();
updateEntries(newEntries);
this.entries = newEntries.toArray(new IClasspathEntry[newEntries.size()]);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return this.entries;
}
/** Compute the entries of the container.
*
* @param entries the list of entries to update..
* @throws Exception if something is going wrong.
*/
protected void updateEntries(List<IClasspathEntry> entries) throws Exception {
for (final String referenceLibrary : SARL_REFERENCE_LIBRARIES) {
entries.add(newLibrary(referenceLibrary));
}
}
/** Create the classpath library linked to the bundle with the given name.
*
* @param libraryName the bundle name.
* @return the classpath entry.
* @throws Exception if something is going wrong.
*/
@SuppressWarnings("static-method")
protected IClasspathEntry newLibrary(String libraryName) throws Exception {
// Retreive the bundle
final Bundle bundle = Platform.getBundle(libraryName);
if (bundle == null) {
throw new NameNotFoundException("No bundle found for: " + libraryName); //$NON-NLS-1$
}
final IPath bundlePath = BundleUtil.getBundlePath(bundle);
final IPath sourceBundlePath = BundleUtil.getSourceBundlePath(bundle, bundlePath);
IClasspathAttribute[] extraAttributes = null;
if (libraryName.startsWith("io.sarl")) { //$NON-NLS-1$
final IPath javadocPath = BundleUtil.getJavadocBundlePath(bundle, bundlePath);
final IClasspathAttribute attr;
if (javadocPath == null) {
attr = JavaCore.newClasspathAttribute(
IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME,
JAVADOC_URL);
} else {
attr = JavaCore.newClasspathAttribute(
IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME,
javadocPath.makeAbsolute().toOSString());
}
extraAttributes = new IClasspathAttribute[] {attr};
}
return JavaCore.newLibraryEntry(
bundlePath,
sourceBundlePath,
null,
new IAccessRule[] {},
extraAttributes,
false);
}
/** Reset the container.
*/
public synchronized void reset() {
this.entries = null;
}
@Override
public String getDescription() {
return Messages.SARLClasspathContainer_0;
}
@Override
public int getKind() {
return K_SYSTEM;
}
@Override
public IPath getPath() {
return this.containerPath;
}
}