consumer) {
for (var entry : eclipseClasspath.getEntries()) {
if (entry instanceof AbstractClasspathEntry abstractClasspathEntry) {
consumer.accept(abstractClasspathEntry);
}
}
}
/**
* Avoids ambiguous call to ({@link XmlFileContentMerger#whenMerged(Closure)} versus
* {@link XmlFileContentMerger#whenMerged(Action)}) in Kotlin build scripts.
*
* The {@code Closure} variant will use the build script itself as {@code this}, and Kotlin will consider any
* type ascription as a cast of {@code this} (instead of the argument of the {@code Action>}). This results in
* a mysterious {@link ClassCastException}, since the class generated from the build script doesn't extend from
* {@link Classpath}. Using this helper method selects the correct call and applies the cast properly.
*
* @param file The Eclipse classpath file.
* @param consumer The lambda to run on when the classpath file is merged.
*/
public static void whenClasspathFileMerged(XmlFileContentMerger file, Consumer consumer) {
file.whenMerged(untypedClasspath -> {
var classpath = (Classpath) untypedClasspath;
consumer.accept(classpath);
});
}
public static void patchGradleUsedByScope(AbstractClasspathEntry entry, Consumer> consumer) {
var entryAttributes = entry.getEntryAttributes();
var usedByValue = entryAttributes.get(GRADLE_USED_BY_SCOPE_ATTRIBUTE);
Set usedBySet;
if (usedByValue instanceof String usedByString) {
usedBySet = new LinkedHashSet<>(List.of(usedByString.split(GRADLE_USED_BY_SCOPE_SEPARATOR)));
} else {
usedBySet = new LinkedHashSet<>();
}
consumer.accept(usedBySet);
if (usedBySet.isEmpty()) {
entryAttributes.remove(GRADLE_USED_BY_SCOPE_ATTRIBUTE);
} else {
var newUsedByString = String.join(GRADLE_USED_BY_SCOPE_SEPARATOR, usedBySet);
entryAttributes.put(GRADLE_USED_BY_SCOPE_ATTRIBUTE, newUsedByString);
}
}
}