Skip to content

Commit

Permalink
replace reflections by classgraph
Browse files Browse the repository at this point in the history
* Classgraph is used in other Eclipse projects like Xtext, too, and is
actively maintained.
* Now finds the sevntu YML files again (which the previous reflections
based code did not find at all).

fixes #515
fixes #682
  • Loading branch information
Bananeweizen committed Apr 1, 2024
1 parent cf32964 commit 37c95ac
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 57 deletions.
6 changes: 6 additions & 0 deletions config/checkstyle_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@
<message key="import.illegal"
value="Use org.assertj.core.api.Assertions instead."/>
</module>
<module name="IllegalImport">
<property name="illegalPkgs"
value="org.reflections"/>
<message key="import.illegal"
value="Use io.github.classgraph.ClassGraph instead."/>
</module>
<module name="ImportControl">
<property name="id" value="ImportControlMain"/>
<property name="file" value="${checkstyle.importcontrol.file}"/>
Expand Down
3 changes: 2 additions & 1 deletion net.sf.eclipsecs.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Bundle-ClassPath: .,
Bundle-Activator: net.sf.eclipsecs.core.CheckstylePlugin
Bundle-ActivationPolicy: lazy
Bundle-Vendor: %Bundle-Vendor
Require-Bundle: net.sf.eclipsecs.checkstyle;visibility:=reexport
Require-Bundle: net.sf.eclipsecs.checkstyle;visibility:=reexport,
io.github.classgraph.classgraph;bundle-version="4.8.168"
Eclipse-BuddyPolicy: registered
Export-Package: net.sf.eclipsecs.core,
net.sf.eclipsecs.core.builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -43,14 +44,12 @@
import java.util.ResourceBundle.Control;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;
import org.yaml.snakeyaml.Yaml;

import com.puppycrawl.tools.checkstyle.PackageNamesLoader;
Expand All @@ -60,6 +59,9 @@
import com.puppycrawl.tools.checkstyle.meta.ModulePropertyDetails;
import com.puppycrawl.tools.checkstyle.meta.XmlMetaReader;

import io.github.classgraph.ClassGraph;
import io.github.classgraph.Resource;
import io.github.classgraph.ScanResult;
import net.sf.eclipsecs.core.CheckstylePlugin;
import net.sf.eclipsecs.core.config.ConfigProperty;
import net.sf.eclipsecs.core.config.Module;
Expand All @@ -82,9 +84,8 @@ public final class MetadataFactory {
/** Map containing the public - internal DTD mapping. */
private static final Map<String, String> PUBLIC2INTERNAL_DTD_MAP = new HashMap<>();

/** Pattern for eclipse extension configuration files. */
private static final Pattern ECLIPSE_EXTENSION_CONFIG_FILE
= Pattern.compile(".*eclipse-metadata.*\\.yml");
/** eclipse extension configuration file name. */
private static final String ECLIPSE_EXTENSION_CONFIG_FILE = "eclipse-metadata.yml";

/** Pattern for dot. */
private static final String DOT_PATTERN = "\\.";
Expand Down Expand Up @@ -298,24 +299,45 @@ private static void createMetadataMap() {
.forEach(moduleDetail -> sModuleDetailsRepo.put(moduleDetail.getName(), moduleDetail));
}

/*
/**
* Fetch third party checks extension metadata from YML files.
*
* @implNote The class graph scanner is configured to use all known packages, but only to the
* second package element. That seems to be a good compromise between scanning far too
* many classes and potentially missing a YML file when using the packages of the
* registered check classes only.
*/
private static void loadThirdPartyModuleExtensionMetadata() {
var rootPackages = sPackageNameSet.stream()
.map(pack -> {
int secondDot = StringUtils.ordinalIndexOf(pack, ".", 2);
if (secondDot < 0) {
return pack;
}
return pack.substring(0, secondDot);
})
.distinct()
.toArray(String[]::new);
Set<String> eclipseMetaDataFiles = new HashSet<>();

for (String packageName : sPackageNameSet) {
final String topMostPackageName = packageName.split(DOT_PATTERN)[0];
Reflections reflections = new Reflections(topMostPackageName, new ResourcesScanner());
eclipseMetaDataFiles.addAll(reflections.getResources(ECLIPSE_EXTENSION_CONFIG_FILE));
try (ScanResult scanResult = new ClassGraph()
.acceptPackages(rootPackages)
.scan()) {
try {
scanResult.getResourcesWithLeafName(ECLIPSE_EXTENSION_CONFIG_FILE)
.forEachInputStreamThrowingIOException((Resource res, InputStream inputStream) -> {
eclipseMetaDataFiles
.add(new String(inputStream.readAllBytes(), StandardCharsets.UTF_8));
});
} catch (IOException ex) {
CheckstyleLog.log(ex, "Cannot read metadata YML");
}
}
eclipseMetaDataFiles.forEach(MetadataFactory::loadThirdPartyData);
}

private static void loadThirdPartyData(String metadataFile) {
InputStream inputStream = CheckstylePlugin.getDefault().getAddonExtensionClassLoader()
.getResourceAsStream(metadataFile);
Map<String, List<Map<String, Object>>> objects = new Yaml().load(inputStream);
private static void loadThirdPartyData(String metadataContent) {
Map<String, List<Map<String, Object>>> objects = new Yaml().load(metadataContent);
for (Map<String, Object> obj : objects.get("ruleGroups")) {
Map<String, String> ruleGroupData = new HashMap<>();
ruleGroupData.put("name", (String) obj.get("name"));
Expand Down
92 changes: 51 additions & 41 deletions net.sf.eclipsecs.target/net.sf.eclipsecs.target.target
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde?>
<!-- generated with https://github.com/eclipse-cbi/targetplatform-dsl -->
<target name="Eclipse Checkstyle" sequenceNumber="1710065411">
<target name="Eclipse Checkstyle" sequenceNumber="1711958226">
<locations>
<location includeMode="slicer" includeAllPlatforms="false" includeSource="true" includeConfigurePhase="true" type="InstallableUnit">
<unit id="org.eclipse.jdt.feature.group" version="3.18.1300.v20220831-1800"/>
Expand Down Expand Up @@ -37,54 +37,64 @@
<repository location="https://download.eclipse.org/tools/orbit/downloads/drops/R20220830213456/repository"/>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="SnakeYaml">
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.2</version>
<type>jar</type>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.2</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="Javassist">
<dependencies>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.29.2-GA</version>
<type>jar</type>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.29.2-GA</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="DOM4J">
<dependencies>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.4</version>
<type>jar</type>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.4</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="Classgraph">
<dependencies>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.168</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="direct" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="AssertJ">
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.1</version>
<type>jar</type>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.1</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="ApacheCommons">
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
<type>jar</type>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
</locations>
<targetJRE path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/>
Expand Down
12 changes: 12 additions & 0 deletions net.sf.eclipsecs.target/net.sf.eclipsecs.target.tpd
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ maven AssertJ
version="3.25.1"
}
}
maven Classgraph
scope=compile
dependencyDepth=none
missingManifest=generate
includeSources
{
dependency {
groupId="io.github.classgraph"
artifactId="classgraph"
version="4.8.168"
}
}
maven DOM4J
scope=compile
dependencyDepth=none
Expand Down

0 comments on commit 37c95ac

Please sign in to comment.