1111import java .io .Reader ;
1212import java .util .ArrayList ;
1313import java .util .Arrays ;
14- import java .util .Collections ;
1514import java .util .HashSet ;
1615import java .util .List ;
1716import java .util .Locale ;
1817import java .util .Map ;
1918import java .util .Set ;
2019import java .util .Stack ;
2120
21+ import org .apache .commons .io .IOUtils ;
2222import org .apache .commons .lang3 .StringUtils ;
2323import org .eclipse .core .resources .IFile ;
2424import org .eclipse .core .resources .IMarker ;
3030import org .slf4j .Logger ;
3131import org .slf4j .LoggerFactory ;
3232
33- import net .sourceforge .pmd .PMD ;
3433import net .sourceforge .pmd .PMDConfiguration ;
35- import net .sourceforge .pmd .PMDException ;
34+ import net .sourceforge .pmd .PmdAnalysis ;
3635import net .sourceforge .pmd .Report ;
3736import net .sourceforge .pmd .Report .ConfigurationError ;
3837import net .sourceforge .pmd .Report .ProcessingError ;
3938import net .sourceforge .pmd .Rule ;
4039import net .sourceforge .pmd .RuleSet ;
41- import net .sourceforge .pmd .RuleSets ;
4240import net .sourceforge .pmd .RuleViolation ;
4341import net .sourceforge .pmd .eclipse .plugin .PMDPlugin ;
4442import net .sourceforge .pmd .eclipse .runtime .PMDRuntimeConstants ;
4947import net .sourceforge .pmd .lang .LanguageVersion ;
5048import net .sourceforge .pmd .lang .LanguageVersionDiscoverer ;
5149import net .sourceforge .pmd .lang .java .JavaLanguageModule ;
52- import net .sourceforge .pmd .renderers .Renderer ;
53- import net .sourceforge .pmd .util .datasource .DataSource ;
54- import net .sourceforge .pmd .util .datasource .ReaderDataSource ;
5550
5651/**
5752 * Factor some useful features for visitors
@@ -167,27 +162,12 @@ public void worked(final int work) {
167162 }
168163 }
169164
170- // /**
171- // * @return Returns the pmdEngine.
172- // */
173- // public PMDEngine getPmdEngine() {
174- // return pmdEngine;
175- // }
176- //
177- // /**
178- // * @param pmdEngine
179- // * The pmdEngine to set.
180- // */
181- // public void setPmdEngine(final PMDEngine pmdEngine) {
182- // this.pmdEngine = pmdEngine;
183- // }
184-
185165 /**
186166 * @return Returns all the ruleSets.
187167 * @deprecated Use {@link #getRuleSetList()}
188168 */
189169 @ Deprecated
190- public RuleSets getRuleSets () {
170+ public net . sourceforge . pmd . RuleSets getRuleSets () {
191171 return InternalRuleSetUtil .toRuleSets (this .ruleSets );
192172 }
193173
@@ -217,7 +197,7 @@ public void setRuleSet(RuleSet ruleSet) {
217197 * @deprecated Use {@link #setRuleSetList(List)}
218198 */
219199 @ Deprecated
220- public void setRuleSets (final RuleSets ruleSets ) {
200+ public void setRuleSets (final net . sourceforge . pmd . RuleSets ruleSets ) {
221201 setRuleSetList (Arrays .asList (ruleSets .getAllRuleSets ()));
222202 }
223203
@@ -316,24 +296,24 @@ protected final void reviewResource(IResource resource) {
316296
317297 long start = System .currentTimeMillis ();
318298
299+ // need to disable multi threading, as the ruleset is
300+ // not recreated and shared between threads...
301+ // but as we anyway have only one file to process, it won't hurt
302+ // here.
303+ configuration ().setThreads (0 );
304+
319305 Report collectingReport = null ;
320306
321307 try (Reader input = new InputStreamReader (file .getContents (), file .getCharset ());
322- DataSource dataSource = new ReaderDataSource (input , file .getRawLocation ().toFile ().getPath ());) {
323-
324- // getPmdEngine().processFile(input, getRuleSet(), context);
325- // getPmdEngine().processFile(sourceCodeFile, getRuleSet(),
326- // context);
327-
328-
329- // need to disable multi threading, as the ruleset is
330- // not recreated and shared between threads...
331- // but as we anyway have only one file to process, it won't hurt
332- // here.
333- configuration ().setThreads (0 );
308+ PmdAnalysis pmdAnalysis = PmdAnalysis .create (configuration ());) {
309+
310+ String sourceContents = IOUtils .toString (input );
311+ pmdAnalysis .files ().addSourceFile (sourceContents , sourceCodeFile .getAbsolutePath ());
312+
313+ pmdAnalysis .addRuleSets (getRuleSetList ());
314+
334315 LOG .debug ("PMD running on file {}" , file .getName ());
335- collectingReport = PMD .processFiles (configuration (), ruleSets , Arrays .asList (dataSource ),
336- Collections .<Renderer >emptyList ());
316+ collectingReport = pmdAnalysis .performAnalysisAndCollectReport ();
337317 LOG .debug ("PMD run finished." );
338318 }
339319
@@ -356,7 +336,7 @@ protected final void reviewResource(IResource resource) {
356336 .append ("\n " );
357337 }
358338 PMDPlugin .getDefault ().logWarn (message .toString ());
359- throw new PMDException (message .toString ());
339+ throw new RuntimeException (message .toString ());
360340 }
361341
362342 updateMarkers (file , collectingReport .getViolations ());
@@ -370,17 +350,16 @@ protected final void reviewResource(IResource resource) {
370350 } catch (CoreException e ) {
371351 // TODO: complete message
372352 LOG .error ("Core exception visiting " + file .getName (), e );
373- } catch (PMDException e ) {
374- // TODO: complete message
375- LOG .error ("PMD exception visiting " + file .getName (), e );
376353 } catch (IOException e ) {
377354 // TODO: complete message
378355 LOG .error ("IO exception visiting " + file .getName (), e );
379356 } catch (PropertiesException e ) {
380357 // TODO: complete message
381- LOG .error ("Properties exception visiting " + file .getName (), e );
358+ LOG .error ("Properties exception visiting {}" , file .getName (), e );
382359 } catch (IllegalArgumentException e ) {
383360 LOG .error ("Illegal argument: {}" , e .toString (), e );
361+ } catch (RuntimeException e ) {
362+ LOG .error ("Runtime exception visiting {}" , file .getName (), e );
384363 }
385364
386365 }
0 commit comments