michael-o commented on code in PR #144: URL: https://github.com/apache/maven-pmd-plugin/pull/144#discussion_r1590128034
########## src/main/java/org/apache/maven/plugins/pmd/exec/CpdExecutor.java: ########## @@ -155,46 +149,50 @@ private CpdResult run() throws MavenReportException { CPDConfiguration cpdConfiguration = new CPDConfiguration(); cpdConfiguration.setMinimumTileSize(request.getMinimumTokens()); + cpdConfiguration.setIgnoreAnnotations(request.isIgnoreAnnotations()); + cpdConfiguration.setIgnoreLiterals(request.isIgnoreLiterals()); + cpdConfiguration.setIgnoreIdentifiers(request.isIgnoreIdentifiers()); - Language cpdLanguage; - if ("java".equals(request.getLanguage()) || null == request.getLanguage()) { - cpdLanguage = new JavaLanguage(request.getLanguageProperties()); - } else if ("javascript".equals(request.getLanguage())) { - cpdLanguage = new EcmascriptLanguage(); - } else if ("jsp".equals(request.getLanguage())) { - cpdLanguage = new JSPLanguage(); - } else { - cpdLanguage = LanguageFactory.createLanguage(request.getLanguage(), request.getLanguageProperties()); + String languageId = request.getLanguage(); + if ("javascript".equals(languageId)) { + languageId = "ecmascript"; + } else if (languageId == null) { + languageId = "java"; // default } + Language cpdLanguage = cpdConfiguration.getLanguageRegistry().getLanguageById(languageId); - cpdConfiguration.setLanguage(cpdLanguage); - cpdConfiguration.setSourceEncoding(request.getSourceEncoding()); + cpdConfiguration.setOnlyRecognizeLanguage(cpdLanguage); + cpdConfiguration.setSourceEncoding(Charset.forName(request.getSourceEncoding())); - CPD cpd = new CPD(cpdConfiguration); - try { - cpd.add(request.getFiles()); - } catch (IOException e) { - throw new MavenReportException(e.getMessage(), e); - } + request.getFiles().forEach(f -> cpdConfiguration.addInputPath(f.toPath())); LOG.debug("Executing CPD..."); - cpd.go(); - LOG.debug("CPD finished."); // always create XML format. we need to output it even if the file list is empty or we have no duplications // so the "check" goals can check for violations - writeXmlReport(cpd); + try (CpdAnalysis cpd = CpdAnalysis.create(cpdConfiguration)) { + cpd.performAnalysis(report -> { + try { + writeXmlReport(report); - // html format is handled by maven site report, xml format has already been rendered - String format = request.getFormat(); - if (!"html".equals(format) && !"xml".equals(format)) { - writeFormattedReport(cpd); + // html format is handled by maven site report, xml format has already been rendered + String format = request.getFormat(); + if (!"html".equals(format) && !"xml".equals(format)) { + writeFormattedReport(report); + } + } catch (MavenReportException e) { + LOG.error("Error while writing CPD report", e); + } + }); + } catch (IOException e) { + LOG.error("Error while executing CPD", e); Review Comment: This is a bad change -- it swallows the exception, build continues. ########## src/main/java/org/apache/maven/plugins/pmd/exec/CpdExecutor.java: ########## @@ -155,46 +149,50 @@ private CpdResult run() throws MavenReportException { CPDConfiguration cpdConfiguration = new CPDConfiguration(); cpdConfiguration.setMinimumTileSize(request.getMinimumTokens()); + cpdConfiguration.setIgnoreAnnotations(request.isIgnoreAnnotations()); + cpdConfiguration.setIgnoreLiterals(request.isIgnoreLiterals()); + cpdConfiguration.setIgnoreIdentifiers(request.isIgnoreIdentifiers()); - Language cpdLanguage; - if ("java".equals(request.getLanguage()) || null == request.getLanguage()) { - cpdLanguage = new JavaLanguage(request.getLanguageProperties()); - } else if ("javascript".equals(request.getLanguage())) { - cpdLanguage = new EcmascriptLanguage(); - } else if ("jsp".equals(request.getLanguage())) { - cpdLanguage = new JSPLanguage(); - } else { - cpdLanguage = LanguageFactory.createLanguage(request.getLanguage(), request.getLanguageProperties()); + String languageId = request.getLanguage(); + if ("javascript".equals(languageId)) { + languageId = "ecmascript"; + } else if (languageId == null) { + languageId = "java"; // default } + Language cpdLanguage = cpdConfiguration.getLanguageRegistry().getLanguageById(languageId); - cpdConfiguration.setLanguage(cpdLanguage); - cpdConfiguration.setSourceEncoding(request.getSourceEncoding()); + cpdConfiguration.setOnlyRecognizeLanguage(cpdLanguage); + cpdConfiguration.setSourceEncoding(Charset.forName(request.getSourceEncoding())); - CPD cpd = new CPD(cpdConfiguration); - try { - cpd.add(request.getFiles()); - } catch (IOException e) { - throw new MavenReportException(e.getMessage(), e); - } + request.getFiles().forEach(f -> cpdConfiguration.addInputPath(f.toPath())); LOG.debug("Executing CPD..."); - cpd.go(); - LOG.debug("CPD finished."); // always create XML format. we need to output it even if the file list is empty or we have no duplications // so the "check" goals can check for violations - writeXmlReport(cpd); + try (CpdAnalysis cpd = CpdAnalysis.create(cpdConfiguration)) { + cpd.performAnalysis(report -> { + try { + writeXmlReport(report); - // html format is handled by maven site report, xml format has already been rendered - String format = request.getFormat(); - if (!"html".equals(format) && !"xml".equals(format)) { - writeFormattedReport(cpd); + // html format is handled by maven site report, xml format has already been rendered + String format = request.getFormat(); + if (!"html".equals(format) && !"xml".equals(format)) { + writeFormattedReport(report); + } + } catch (MavenReportException e) { + LOG.error("Error while writing CPD report", e); Review Comment: This is a bad change -- it swallows the exception, build continues. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org