/
CheckstyleCommand.java
309 lines (260 loc) · 8.56 KB
/
CheckstyleCommand.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* Copyright (C) 2005 - 2012 Eric Van Dewoestine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.eclim.plugin.jdt.command.src;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.eclim.Services;
import org.eclim.annotation.Command;
import org.eclim.command.CommandLine;
import org.eclim.command.Error;
import org.eclim.command.Options;
import org.eclim.logging.Logger;
import org.eclim.plugin.core.command.AbstractCommand;
import org.eclim.plugin.core.preference.Preferences;
import org.eclim.plugin.core.util.ProjectUtils;
import org.eclim.plugin.jdt.util.JavaUtils;
import org.eclim.util.IOUtils;
import org.eclim.util.StringUtils;
import org.eclim.util.file.FileUtils;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaModelException;
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.AuditListener;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
/**
* Command which invokes checkstyle on the specified file.
*
* @author Eric Van Dewoestine
*/
@Command(
name = "java_checkstyle",
options =
"REQUIRED p project ARG," +
"REQUIRED f file ARG"
)
public class CheckstyleCommand
extends AbstractCommand
{
private static final Logger logger =
Logger.getLogger(CheckstyleCommand.class);
@Override
public Object execute(CommandLine commandLine)
throws Exception
{
String name = commandLine.getValue(Options.PROJECT_OPTION);
String file = commandLine.getValue(Options.FILE_OPTION);
IProject project = ProjectUtils.getProject(name, true);
Preferences prefs = getPreferences();
String configFile =
prefs.getValue(project, "org.eclim.java.checkstyle.config");
String propsFile =
prefs.getValue(project, "org.eclim.java.checkstyle.properties");
Properties properties = System.getProperties();
if (propsFile != null && !propsFile.equals(StringUtils.EMPTY)){
FileInputStream fis = null;
try{
File pfile = new File(propsFile);
if(!pfile.exists()){
pfile = new File(ProjectUtils.getFilePath(project, propsFile));
if(!pfile.exists()){
throw new RuntimeException(
Services.getMessage("file.not.found", configFile));
}
}
fis = new FileInputStream(pfile);
properties = new Properties();
properties.load(fis);
}finally{
IOUtils.closeQuietly(fis);
}
}
if (!new File(configFile).exists()){
String projectConfigFile = ProjectUtils.getFilePath(project, configFile);
if (!new File(projectConfigFile).exists()){
throw new RuntimeException(
Services.getMessage("file.not.found", configFile));
}
configFile = projectConfigFile;
}
Configuration config = ConfigurationLoader.loadConfiguration(
configFile, new PropertiesExpander(properties));
CheckstyleListener listener = new CheckstyleListener();
List<File> files = new ArrayList<File>();
files.add(new File(ProjectUtils.getFilePath(project, file)));
Checker checker = new Checker();
checker.setModuleClassLoader(new ProjectClassLoader(
JavaUtils.getJavaProject(project),
Checker.class.getClassLoader()));
checker.setClassloader(new ProjectClassLoader(
JavaUtils.getJavaProject(project)));
checker.configure(config);
checker.addListener(listener);
checker.process(files);
return listener.getErrors();
}
private static class CheckstyleListener
implements AuditListener
{
private static final String CHECKSTYLE_PREFIX = "[checkstyle] ";
private ArrayList<Error> errors;
public CheckstyleListener ()
{
errors = new ArrayList<Error>();
}
@Override
public void auditStarted(AuditEvent event)
{
// ignore
}
@Override
public void auditFinished(AuditEvent event)
{
// ignore
}
@Override
public void fileStarted(AuditEvent event)
{
// ignore
}
@Override
public void fileFinished(AuditEvent event)
{
// ignore
}
@Override
public void addError(AuditEvent event)
{
errors.add(
new Error(
CHECKSTYLE_PREFIX + event.getMessage(),
event.getFileName().replace('\\', '/'),
event.getLine(),
event.getColumn(),
event.getSeverityLevel() != SeverityLevel.ERROR));
}
@Override
public void addException(AuditEvent event, Throwable throwable)
{
throw new RuntimeException(throwable);
}
/**
* Gets the errors for this instance.
*
* @return The errors.
*/
public ArrayList<Error> getErrors()
{
return this.errors;
}
}
private static class ProjectClassLoader
extends URLClassLoader
{
public ProjectClassLoader (IJavaProject project)
throws Exception
{
super(ProjectClassLoader.classpath(project));
}
public ProjectClassLoader (IJavaProject project, ClassLoader parent)
throws Exception
{
super(ProjectClassLoader.classpath(project), parent);
}
private static URL[] classpath(IJavaProject project)
throws Exception
{
Set<IJavaProject> visited = new HashSet<IJavaProject>();
List<URL> urls = new ArrayList<URL>();
collect(project, urls, visited, true);
//logger.info(StringUtils.join(urls, "\n"));
return urls.toArray(new URL[urls.size()]);
}
private static void collect(
IJavaProject javaProject,
List<URL> urls,
Set<IJavaProject> visited,
boolean isFirstProject)
throws Exception
{
if(visited.contains(javaProject)){
return;
}
visited.add(javaProject);
try{
IPath out = javaProject.getOutputLocation();
String path = ProjectUtils.getFilePath(
javaProject.getProject(), out.toOSString());
path = FileUtils.addTrailingSlash(path.replace('\\', '/'));
urls.add(new URL("file://" + path));
}catch(JavaModelException ignore){
// ignore... just signals that no output dir was configured.
}
IProject project = javaProject.getProject();
String name = project.getName();
IClasspathEntry[] entries = null;
try {
entries = javaProject.getResolvedClasspath(true);
}catch(JavaModelException jme){
// this may or may not be a problem.
logger.warn(
"Unable to retreive resolved classpath for project: " + name, jme);
return;
}
for(IClasspathEntry entry : entries) {
switch (entry.getEntryKind()) {
case IClasspathEntry.CPE_LIBRARY :
case IClasspathEntry.CPE_CONTAINER :
case IClasspathEntry.CPE_VARIABLE :
String path = entry.getPath().toOSString();
if(path.startsWith("/" + name + "/")){
path = ProjectUtils.getFilePath(project, path);
}
urls.add(new URL("file://" + path));
break;
case IClasspathEntry.CPE_PROJECT :
if (isFirstProject || entry.isExported()){
collect(getJavaProject(entry), urls, visited, false);
}
break;
}
}
}
private static IJavaProject getJavaProject(IClasspathEntry entry)
throws Exception
{
IProject project = ProjectUtils.getProject(entry.getPath().segment(0));
if (project != null){
return JavaUtils.getJavaProject(project);
}
return null;
}
}
}