Skip to content

Commit 0f1d70e

Browse files
committed
Stop exposing project classpath to Checkstyle (#673).
This has not been required since Checkstyle since 6.2 (checkstyle/checkstyle#473)
1 parent 73eef81 commit 0f1d70e

File tree

15 files changed

+25
-340
lines changed

15 files changed

+25
-340
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
# CheckStyle-IDEA Changelog
33

4+
* **5.109.2** Fixed: Removed project code from classpath, as it hasn't been required since Checkstyle 6.2 (#673). Thanks to @LlamaLad7.
45
* **5.109.1** New: Added grouping of results by the source check (#670).
56
* **5.109.0** New: Added Checkstyle 10.26.1.
67
* **5.108.1** Fixed: Resolved edge case if Piccolo is in the project classpath (#668).

README.md

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ Released under a BSD-style licence - please see the LICENCE file for details.
2121
Once installed, a new inspection will be available in the group 'CheckStyle'. The 'Inspections' item in the preferences
2222
panel will allow you to turn this on and to configure it.
2323

24-
Project exceptions are treated a little oddly. Because CheckStyle demands these to be on the current classpath, errors
25-
will appear if these have not as yet been compiled. Further, because we cache the real-time checkers for performance
26-
reasons, real-time scans may continue to show the errors after a compilation. A static scan will force a reload of the
27-
Checker and should resolve this.
28-
29-
3024
## Configuration
3125

3226
Configuration is available under the *Settings* dialogue, under *Tools* -> *Checkstyle*. This controls configuration for both the inspection and static
@@ -92,7 +86,6 @@ If an error occurs during the check an exception will be thrown, which IDEA will
9286
exceptions dialogue. If you're unsure as to why things are awry this would be your best bet - chances are it's a missing
9387
property or classpath pre-requisite.
9488

95-
9689
## Notable Extensions
9790

9891
### [sevntu.checkstyle](http://sevntu-checkstyle.github.io/sevntu.checkstyle/)
@@ -105,7 +98,6 @@ property or classpath pre-requisite.
10598
*Checkstyle Addons* offers additional Checkstyle checks not found in other Checkstyle extensions, and it's easy to
10699
[set up in Checkstyle-IDEA](http://checkstyle-addons.thomasjensen.com/run.html#run-intellij).
107100

108-
109101
## Development
110102

111103
Note that the plugin has been entirely developed on OS X - while it should be fine on Linux, I've no idea what result
@@ -134,12 +126,6 @@ If you're on OS X, use IDEA with the bundled JVM. Otherwise, please ensure IDEA
134126
[Jetbrains offer a support document on this
135127
subject](https://intellij-support.jetbrains.com/entries/23455956-Selecting-the-JDK-version-the-IDE-will-run-under).
136128

137-
### I see 'Got an exception - java.lang.RuntimeException: Unable to get class information for <Exception Class>. (0:0)'
138-
139-
CheckStyle is unable to retrieve information on exceptions in your project until you have built it. Build your project
140-
in IDEA and then rescan.
141-
142-
143129
## Limitations
144130

145131
* If you import Gradle project withs **Create separate module per source set** active in IDEA 2016 or above then the
@@ -155,21 +141,18 @@ in IDEA and then rescan.
155141
* CheckStyle errors and warnings from the inspection are both shown at a single level, as IDEA will only allow one
156142
warning level for an inspection.
157143

158-
159144
## Feedback
160145

161146
Any comments or bug reports are most welcome - please visit
162147
the project website on [GitHub](https://github.com/jshiell/checkstyle-idea/).
163148

164-
165149
## I need debug information!
166150

167151
The debug logging of the plugin is arcane and not particularly well done, for which I can only thank myself. However, if
168152
such context is needed then it can be seen by using IDEA's **Help** -> **Debug Log Settings...** and adding:
169153

170154
#org.infernus.idea.checkstyle
171155

172-
173156
## Acknowledgements
174157

175158
This plug-in owes its existence to both the style-overlords at work mandating compliance with a CheckStyle
@@ -219,6 +202,7 @@ Thanks to those who have contributed work and effort directly to this project:
219202
* Ali Ustek (@austek)
220203
* Richard (@rhierlmeier)
221204
* James Baker (@james-baker-aera)
205+
* @LlamaLad7
222206

223207
And also thanks are due to the authors and contributors of:
224208

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ plugins {
1919
id("org.infernus.idea.checkstyle.build")
2020
}
2121

22-
version = "5.109.1"
22+
version = "5.109.2"
2323

2424
intellijPlatform {
2525
pluginConfiguration {

src/csaccess/java/org/infernus/idea/checkstyle/service/CheckstyleActionsImpl.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,17 @@ public CheckstyleActionsImpl(@NotNull final Project project,
4646
@Override
4747
public CheckStyleChecker createChecker(@Nullable final Module module,
4848
@NotNull final ConfigurationLocation location,
49-
final Map<String, String> properties,
50-
@NotNull final ClassLoader loaderOfCheckedCode) {
51-
return createChecker(module, location, properties, null, loaderOfCheckedCode);
49+
final Map<String, String> properties) {
50+
return createChecker(module, location, properties, null);
5251
}
5352

5453
@Override
5554
public CheckStyleChecker createChecker(@Nullable final Module module,
5655
@NotNull final ConfigurationLocation location,
5756
final Map<String, String> properties,
58-
@Nullable final TabWidthAndBaseDirProvider configurations,
59-
@NotNull final ClassLoader loaderOfCheckedCode) {
57+
@Nullable final TabWidthAndBaseDirProvider configurations) {
6058
return executeCommand(new OpCreateChecker(
61-
module, location, properties, configurations, loaderOfCheckedCode, checkstyleProjectService));
59+
module, location, properties, configurations, checkstyleProjectService));
6260
}
6361

6462
@Override

src/csaccess/java/org/infernus/idea/checkstyle/service/cmd/OpCreateChecker.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
import org.infernus.idea.checkstyle.CheckstyleProjectService;
99
import org.infernus.idea.checkstyle.checker.CheckStyleChecker;
1010
import org.infernus.idea.checkstyle.csapi.TabWidthAndBaseDirProvider;
11-
import org.infernus.idea.checkstyle.exception.CheckstyleServiceException;
1211
import org.infernus.idea.checkstyle.exception.CheckstyleToolException;
1312
import org.infernus.idea.checkstyle.model.ConfigurationLocation;
1413
import org.infernus.idea.checkstyle.service.Configurations;
1514
import org.infernus.idea.checkstyle.service.entities.CheckerWithConfig;
1615
import org.jetbrains.annotations.NotNull;
1716
import org.jetbrains.annotations.Nullable;
1817

19-
import java.lang.reflect.InvocationTargetException;
20-
import java.lang.reflect.Method;
2118
import java.util.Map;
2219

2320
/**
@@ -30,20 +27,17 @@ public class OpCreateChecker
3027
private final ConfigurationLocation location;
3128
private final Map<String, String> variables;
3229
private final TabWidthAndBaseDirProvider configurations;
33-
private final ClassLoader loaderOfCheckedCode;
3430
private final CheckstyleProjectService checkstyleProjectService;
3531

3632
public OpCreateChecker(@Nullable final Module module,
3733
@NotNull final ConfigurationLocation location,
3834
final Map<String, String> variables,
3935
@Nullable final TabWidthAndBaseDirProvider configurations,
40-
@NotNull final ClassLoader loaderOfCheckedCode,
4136
@NotNull final CheckstyleProjectService checkstyleProjectService) {
4237
this.module = module;
4338
this.location = location;
4439
this.variables = variables;
4540
this.configurations = configurations;
46-
this.loaderOfCheckedCode = loaderOfCheckedCode;
4741
this.checkstyleProjectService = checkstyleProjectService;
4842
}
4943

@@ -55,7 +49,6 @@ public CheckStyleChecker execute(@NotNull final Project project) throws Checksty
5549

5650
final Checker checker = new Checker();
5751
checker.setModuleClassLoader(getClass().getClassLoader()); // for Checkstyle to load modules (checks)
58-
setClassLoader(checker, loaderOfCheckedCode); // for checks to load the classes and resources to be analyzed
5952

6053
try {
6154
checker.configure(csConfig);
@@ -72,28 +65,6 @@ public CheckStyleChecker execute(@NotNull final Project project) throws Checksty
7265
checkstyleProjectService.getCheckstyleInstance(), location.getNamedScope());
7366
}
7467

75-
private void setClassLoader(final Checker checker, final ClassLoader classLoader) {
76-
try {
77-
Method classLoaderMethod = null;
78-
try {
79-
classLoaderMethod = Checker.class.getMethod("setClassloader", ClassLoader.class);
80-
} catch (NoSuchMethodException | SecurityException e) {
81-
try {
82-
classLoaderMethod = Checker.class.getMethod("setClassLoader", ClassLoader.class); // 8.0 and above
83-
} catch (NoSuchMethodException | SecurityException ignored) {
84-
// it is a no-op from 8.27, and removed entirely in 8.31
85-
}
86-
}
87-
88-
if (classLoaderMethod != null) {
89-
classLoaderMethod.invoke(checker, classLoader);
90-
}
91-
92-
} catch (IllegalAccessException | InvocationTargetException e) {
93-
throw new CheckstyleServiceException("Failed to set classloader", e);
94-
}
95-
}
96-
9768
private Configuration loadConfig(@NotNull final Project project) throws CheckstyleException {
9869
return new OpLoadConfiguration(location, variables, module, checkstyleProjectService).execute(project).getConfiguration();
9970
}

src/csaccessTest/java/org/infernus/idea/checkstyle/service/ServiceLayerBasicTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ private void createChecker(@NotNull final String configXmlFile)
7878
Optional.of(new File(Objects.requireNonNull(getClass().getResource(configXmlFile)).toURI()).getParent()));
7979

8080
final CheckstyleActions csInstance = checkstyleProjectService.getCheckstyleInstance();
81-
csInstance.createChecker(module, configLoc, Collections.emptyMap(), configurations, getClass()
82-
.getClassLoader());
81+
csInstance.createChecker(module, configLoc, Collections.emptyMap(), configurations);
8382
}
8483

8584
}

src/csaccessTest/java/org/infernus/idea/checkstyle/service/cmd/OpCreateCheckerTest.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,31 @@ public static void setUp() throws URISyntaxException {
5252

5353
@Test
5454
public void testCreateCheckerWithConfigsMock() throws IOException, URISyntaxException {
55-
5655
final ConfigurationLocation configLoc = new StringConfigurationLocation(
5756
FileUtil.readFile("cmd/" + CONFIG_FILE), TestHelper.mockProject());
5857

59-
final CheckStyleChecker checker = new CheckstyleActionsImpl(PROJECT, checkstyleProjectServiceMock).createChecker(moduleMock, configLoc,
60-
emptyMap(), configurationsMock, getClass().getClassLoader());
58+
final CheckStyleChecker checker = new CheckstyleActionsImpl(PROJECT, checkstyleProjectServiceMock)
59+
.createChecker(moduleMock, configLoc, emptyMap(), configurationsMock);
6160
assertNotNull(checker);
6261
}
6362

6463

6564
@Test(expected = CheckStylePluginException.class)
6665
public void testCreateCheckerWithNoConfigLoc() {
67-
6866
//noinspection ConstantConditions
69-
new CheckstyleActionsImpl(PROJECT, checkstyleProjectServiceMock).createChecker(moduleMock, null, emptyMap(),
70-
configurationsMock, getClass().getClassLoader());
67+
new CheckstyleActionsImpl(PROJECT, checkstyleProjectServiceMock)
68+
.createChecker(moduleMock, null, emptyMap(), configurationsMock);
7169
fail("expected exception was not thrown");
7270
}
7371

7472

7573
@Test
7674
public void testCreateCheckerWithNoModule() throws IOException, URISyntaxException {
77-
7875
final ConfigurationLocation configLoc = new StringConfigurationLocation(
7976
FileUtil.readFile("cmd/" + CONFIG_FILE), TestHelper.mockProject());
8077

81-
CheckStyleChecker checker = new CheckstyleActionsImpl(PROJECT, checkstyleProjectServiceMock).createChecker(null, configLoc,
82-
emptyMap(), configurationsMock, getClass().getClassLoader());
78+
CheckStyleChecker checker = new CheckstyleActionsImpl(PROJECT, checkstyleProjectServiceMock)
79+
.createChecker(null, configLoc, emptyMap(), configurationsMock);
8380
assertNotNull(checker);
8481
}
8582
}

src/main/java/org/infernus/idea/checkstyle/checker/CheckerFactory.java

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.infernus.idea.checkstyle.exception.CheckStylePluginException;
1010
import org.infernus.idea.checkstyle.exception.CheckstyleToolException;
1111
import org.infernus.idea.checkstyle.model.ConfigurationLocation;
12-
import org.infernus.idea.checkstyle.util.ClassLoaderDumper;
1312
import org.infernus.idea.checkstyle.util.Notifications;
1413
import org.jetbrains.annotations.NotNull;
1514
import org.jetbrains.annotations.Nullable;
@@ -124,10 +123,6 @@ private void addIfAbsent(final String key, final String value, final Map<String,
124123
}
125124
}
126125

127-
private ModuleClassPathBuilder moduleClassPathBuilder() {
128-
return project.getService(ModuleClassPathBuilder.class);
129-
}
130-
131126
private CachedChecker createChecker(@NotNull final ConfigurationLocation location,
132127
@Nullable final Module module) {
133128
final ListPropertyResolver propertyResolver;
@@ -140,15 +135,7 @@ private CachedChecker createChecker(@NotNull final ConfigurationLocation locatio
140135
return blockAndShowMessage(location, module, e, "checkstyle.file-io-failed", location.getLocation());
141136
}
142137

143-
final ClassLoader loaderOfCheckedCode = moduleClassPathBuilder().build(module);
144-
if (LOG.isDebugEnabled()) {
145-
LOG.debug("Call to create new checker with properties:\n"
146-
+ dumpProperties(propertyResolver)
147-
+ "With plugin classloader:"
148-
+ ClassLoaderDumper.dumpClassLoader(loaderOfCheckedCode));
149-
}
150-
151-
final Object workerResult = executeWorker(location, module, propertyResolver, loaderOfCheckedCode);
138+
final Object workerResult = executeWorker(location, module, propertyResolver);
152139

153140
if (workerResult instanceof CheckstyleToolException) {
154141
return blockAndShowMessageFromException(location, module, (CheckstyleToolException) workerResult);
@@ -175,10 +162,9 @@ private Map<String, String> removeEmptyProperties(final Map<String, String> prop
175162

176163
private Object executeWorker(@NotNull final ConfigurationLocation location,
177164
@Nullable final Module module,
178-
final ListPropertyResolver resolver,
179-
@NotNull final ClassLoader loaderOfCheckedCode) {
165+
final ListPropertyResolver resolver) {
180166
final CheckerFactoryWorker worker = new CheckerFactoryWorker(location,
181-
resolver.getPropertyNamesToValues(), module, checkstyleProjectService, loaderOfCheckedCode);
167+
resolver.getPropertyNamesToValues(), module, checkstyleProjectService);
182168
worker.start();
183169

184170
while (worker.isAlive()) {
@@ -247,18 +233,4 @@ private CachedChecker blockAndShowMessageFromException(final ConfigurationLocati
247233
"checkstyle.parse-failed", checkstyleException.getMessage());
248234
}
249235

250-
private String dumpProperties(final ListPropertyResolver resolver) {
251-
StringBuilder dump = new StringBuilder();
252-
if (resolver != null) {
253-
final Map<String, String> propertiesToValues = resolver.getPropertyNamesToValues();
254-
for (final Map.Entry<String, String> propertyEntry : propertiesToValues.entrySet()) {
255-
dump.append("- Property: ")
256-
.append(propertyEntry.getKey())
257-
.append("=")
258-
.append(propertyEntry.getValue())
259-
.append('\n');
260-
}
261-
}
262-
return dump.toString();
263-
}
264236
}

src/main/java/org/infernus/idea/checkstyle/checker/CheckerFactoryWorker.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,27 @@ class CheckerFactoryWorker extends Thread {
1414
private final Map<String, String> properties;
1515
private final Module module;
1616
private final CheckstyleProjectService checkstyleProjectService;
17-
private final ClassLoader loaderOfCheckedCode;
1817

1918
private Object threadReturn = null;
2019

2120
CheckerFactoryWorker(@NotNull final ConfigurationLocation location,
2221
@Nullable final Map<String, String> properties,
2322
@Nullable final Module module,
24-
@NotNull final CheckstyleProjectService checkstyleProjectService,
25-
@NotNull final ClassLoader loaderOfCheckedCode) {
23+
@NotNull final CheckstyleProjectService checkstyleProjectService) {
2624
this.location = location;
2725
this.properties = properties;
2826
this.module = module;
2927
this.checkstyleProjectService = checkstyleProjectService;
30-
this.loaderOfCheckedCode = loaderOfCheckedCode;
3128
}
3229

3330
@Override
3431
public void run() {
3532
super.run();
3633

37-
setContextClassLoader(loaderOfCheckedCode);
38-
3934
try {
4035
final CheckStyleChecker checker = checkstyleProjectService
4136
.getCheckstyleInstance()
42-
.createChecker(module, location, properties, loaderOfCheckedCode);
37+
.createChecker(module, location, properties);
4338
threadReturn = new CachedChecker(checker);
4439
} catch (RuntimeException e) {
4540
threadReturn = e;

0 commit comments

Comments
 (0)