-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
AbstractClasspathEntry.java
250 lines (213 loc) · 8.88 KB
/
AbstractClasspathEntry.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
/*
* Copyright 2016 the original author 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 org.gradle.plugins.ide.eclipse.model;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import groovy.util.Node;
import groovy.util.NodeList;
import org.gradle.internal.Cast;
import org.gradle.plugins.ide.eclipse.model.internal.PathUtil;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import static java.util.stream.Collectors.toMap;
// TODO: consider entryAttributes in equals, hashCode, and toString
/**
* Common superclass for all {@link ClasspathEntry} instances.
*/
public abstract class AbstractClasspathEntry implements ClasspathEntry {
private static final String NATIVE_LIBRARY_ATTRIBUTE = "org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY";
public static final String COMPONENT_NON_DEPENDENCY_ATTRIBUTE = "org.eclipse.jst.component.nondependency";
public static final String COMPONENT_DEPENDENCY_ATTRIBUTE = "org.eclipse.jst.component.dependency";
protected String path;
protected boolean exported;
protected Set<AccessRule> accessRules;
protected final Map<String, Object> entryAttributes;
public AbstractClasspathEntry(Node node) {
path = normalizePath((String) node.attribute("path"));
this.exported = isNodeExported(node);
accessRules = readAccessRules(node);
entryAttributes = readEntryAttributes(node);
Preconditions.checkNotNull(path);
Preconditions.checkNotNull(accessRules);
}
private boolean isNodeExported(Node node) {
Object value = node.attribute("exported");
if (value == null) {
return false;
} else if (value instanceof Boolean) {
return (Boolean) value;
} else {
return Boolean.parseBoolean((String) value);
}
}
public AbstractClasspathEntry(String path) {
Preconditions.checkNotNull(path);
this.path = normalizePath(path);
this.exported = false;
this.accessRules = new LinkedHashSet<>();
this.entryAttributes = new LinkedHashMap<>();
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public boolean isExported() {
return exported;
}
public void setExported(boolean exported) {
this.exported = exported;
}
public Set<AccessRule> getAccessRules() {
return accessRules;
}
public void setAccessRules(Set<AccessRule> accessRules) {
this.accessRules = accessRules;
}
public Map<String, Object> getEntryAttributes() {
return entryAttributes;
}
public String getNativeLibraryLocation() {
return (String) entryAttributes.get(NATIVE_LIBRARY_ATTRIBUTE);
}
public void setNativeLibraryLocation(String location) {
entryAttributes.put(NATIVE_LIBRARY_ATTRIBUTE, location);
}
@Override
public void appendNode(Node node) {
addClasspathEntry(node, ImmutableMap.of());
}
protected Node addClasspathEntry(Node node, Map<String, ?> attributes) {
ImmutableMap.Builder<String, Object> allAttributes = ImmutableMap.builder();
attributes.forEach((key, value) -> {
if (value != null && !String.valueOf(value).isEmpty()) {
allAttributes.put(key, value);
}
});
allAttributes.put("kind", getKind());
allAttributes.put("path", path);
if (exported && !(this instanceof SourceFolder)) {
allAttributes.put("exported", true);
}
Node entryNode = node.appendNode("classpathentry", allAttributes.build());
writeAccessRules(entryNode);
writeEntryAttributes(entryNode);
return entryNode;
}
protected String normalizePath(String path) {
return PathUtil.normalizePath(path);
}
private Set<AccessRule> readAccessRules(Node node) {
Set<AccessRule> accessRules = new LinkedHashSet<>();
NodeList accessRulesNodes = (NodeList) node.get("accessrules");
for (Object accessRulesNode : accessRulesNodes) {
NodeList accessRuleNodes = (NodeList) ((Node) accessRulesNode).get("accessrule");
for (Object accessRuleNode : accessRuleNodes) {
Node ruleNode = (Node) accessRuleNode;
accessRules.add(new AccessRule((String) ruleNode.attribute("kind"), (String) ruleNode.attribute("pattern")));
}
}
return accessRules;
}
private void writeAccessRules(Node node) {
if (accessRules == null || accessRules.isEmpty()) {
return;
}
Node accessRulesNode = getAttributesNode(node, "accessrules");
for (AccessRule rule : accessRules) {
accessRulesNode.appendNode("accessrule",
ImmutableMap.of(
"kind", rule.getKind(),
"pattern", rule.getPattern()));
}
}
private Map<String, Object> readEntryAttributes(Node node) {
Map<String, Object> attributes = new LinkedHashMap<>();
NodeList attributesNodes = (NodeList) node.get("attributes");
for (Object attributesEntry : attributesNodes) {
NodeList attributeNodes = (NodeList) ((Node) attributesEntry).get("attribute");
for (Object attributeEntry : attributeNodes) {
Node attributeNode = (Node) attributeEntry;
attributes.put((String) attributeNode.attribute("name"), attributeNode.attribute("value"));
}
}
return attributes;
}
public void writeEntryAttributes(Node node) {
Map<String, Object> effectiveEntryAttrs = getEffectiveEntryAttrs();
if (effectiveEntryAttrs.isEmpty()) {
return;
}
Node attributesNode = getAttributesNode(node, "attributes");
effectiveEntryAttrs.forEach((key, value) -> {
// If the attribute value is an Iterable, an <attribute> node is produced for each element in the Iterable.
// This is something that is supported by the classpath entry format and it allows users to define multi-value
// entries by putting a list as value into the 'entryAttributes' Map.
// For exmaple: entryAttributes['add-exports'] = ['java.base/jdk.internal.access=ALL-UNNAMED', 'java.base/jdk.internal.loader=ALL-UNNAMED']
if (value instanceof Iterable) {
Cast.<Iterable<?>>uncheckedCast(value).forEach(valueElement ->
attributesNode.appendNode("attribute", ImmutableMap.of(
"name", key,
"value", valueElement)
)
);
} else {
attributesNode.appendNode("attribute", ImmutableMap.of(
"name", key,
"value", value)
);
}
});
}
private Map<String, Object> getEffectiveEntryAttrs() {
return entryAttributes.entrySet().stream()
.filter(entry -> entry.getValue() != null)
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (existing, replacement) -> existing, LinkedHashMap::new));
}
private static Node getAttributesNode(Node node, String attributes) {
NodeList attributesNodes = (NodeList) node.get(attributes);
if (attributesNodes.isEmpty()) {
return node.appendNode(attributes);
}
return (Node) attributesNodes.get(0);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AbstractClasspathEntry that = (AbstractClasspathEntry) o;
return exported == that.exported
&& Objects.equal(path, that.path)
&& Objects.equal(accessRules, that.accessRules)
&& Objects.equal(getNativeLibraryLocation(), that.getNativeLibraryLocation());
}
@Override
public int hashCode() {
return Objects.hashCode(path, exported, accessRules, getNativeLibraryLocation());
}
@Override
public String toString() {
return "{path='" + path + "', nativeLibraryLocation='" + getNativeLibraryLocation() + "', exported=" + exported + ", accessRules=" + accessRules + "}";
}
}