Re: [pmd-devel] Need a lookover - Re: Question on the old PMD BlueJ extension
A source code analyzer
Brought to you by:
adangel,
juansotuyo
From: Andreas D. <ad...@us...> - 2015-05-31 10:24:41
|
Hi Ted, If you want, I can create a new repository on https://github.com/pmd/pmd-bluej and you can push your version there and we can upload new official bluej pmd extension :) btw. I was wondering, on which source code you are working on. I finally found the code (I moved it away in the past, as nobody was maintaining it) here: https://github.com/pmd/pmd-misc/tree/master/pmd-bluej Regards, Andreas On 25.05.2015 10:52, Ted Mittelstaedt wrote: > Hi Andreas, > > Well here's where I am now. I got frustrated enough that I rewrote > the BlueJ extension to simply exec a shell script with the absolute > filename of the file being edited passed to the shell script. The idea > being that the user/installer can more easily modify the shell script > (or windows .cmd file) to add in whatever rulesets they want. So the > shell script cd's to the pmd directory and runs run.sh with the option > passed from the extention, along with whatever rulesets that the > installer has added into the script. The extension picks up the > stdout of pmd and displays it on the user's BlueJ screen, the same way > that the original 14 year old extension did. > > It's a hack and might be non-portable (I have yet to test on Windows > but I'm doing that later today) but the users using Bluej likely won't > care. And since the project is most of the grade for the class, I'd > rather turn in a working project where I can just demonstrate it and > be done with it, then turn in a broken one where I would have to spend > time justifying each code line in the project to get the grade. > > I'm kind of torn on the whole thing, myself. On the pro side, doing > it this way makes the extension far, far, smaller, and it decouples > the extension from further API changes or command line option changes > that might go on in PMD. It also solves the issue of how to configure > what rulesets to use since the person installing it just modifies the > command line options in the script. And debugging BlueJ extensions is > extremely trying since when you run BlueJ under java with the remote > debugger port specified the program just hangs after the splash > screen, so stepping through code of the extension and examining > variables and such is out of the question (at least for me, I'm a poor > student without the folding green for a super duper commercial > debugger that might be able to do it) And, lastly, the menu > extension calls PMD from the Swing dispatch thread which according to > the BlueJ author Ian Utting, might be trouble. > > On the con side, the installer has to get PMD working on their system, > and in their PATH. And I spent maybe 2 hours on writing the hack which > works and at least 20 hours beating my head against the wall trying to > understand PMD's apis and I'm kind of loath to give that up! I just > keep thinking that there is something really simple that I am > overlooking and it's just bugging me. > > I have tested on multiple PMD versions including 5.3.2. > > I'll take another look at it. Thanks for the info on pmd-core and > pmd-java, that helps to close one of the rabbit holes. I didn't write > a Maven pom file for this, I'm still ant's build.xml so > it would take some more work to integrate it into the current pmd > distro if you even want it. > > > Ted > > On 5/25/2015 12:36 AM, Andreas Dangel wrote: >> Hi, >> >> I can't exactly tell what happens here... did you try to execute PMD >> from commandline with the same ruleset and analyzing the same file? >> Maybe you just triggered a bug independent of the integration? >> Another thing worth trying is the concurrency settings: By default, PMD >> tries to parallelize the work. You can prevent this by setting the >> threads property on the configuration object >> (configuration.setThreads(1)). >> Also, which version of PMD are you using? The latest released version is >> now 5.3.2. Maybe you are hitting a bug that has been fixed already? >> >> Regarding the api docs - they are still there. But since PMD 5.2.0, PMD >> has been modularized. So, you'll find the apidocs for the integration >> part in the pmd-core module: >> http://pmd.sourceforge.net/pmd-5.3.2/pmd-core/apidocs/ >> Also, if you upgrade to pmd> 5.2.0, you'll need at least the modules >> pmd-core and pmd-java. >> I'll try to add an aggregated javadoc report for the future. >> >> Regards, >> Andreas >> >> >> On 25.05.2015 05:55, Ted Mittelstaedt wrote: >>> Hi All, >>> >>> I should have been more clear on this - the call to >>> PMD.processFiles() >>> doesn't have a return, it's not supposed to return anything. What it >>> does appear to do, though, is crash - as far as I can tell. >>> >>> Immediately after the PMD.processFiles() the next line has the >>> extension printing "Class Checked". However, it doesn't do that. >>> It does nothing. However, if I do something that would cause the >>> call to PMD.processFiles() to fail, like change the name of the >>> rulesets in setRuleSets() to a ruleset name that does not exist - then >>> PMD.processFiles throws up an exception which is caught and >>> printed. If >>> I comment out PMD.processFiles then the extension proceeds to the >>> "Class Checked" printout. >>> >>> I cannot tell what is going on, whether PMD is running or not. >>> If it >>> runs properly it seems as though should return back to the next >>> statement in the extension which will then print out the rule >>> violations >>> - or if no violations exist then print out that none exist. >>> If it crashes or is fed an improper parameter than shouldn't it return >>> an exception? Since if I deliberately misspell the ruleset, it DOES >>> return an exception. And, I have checked the code multiple times >>> and it >>> seems to me that I am passing the correct parameters. >>> >>> The example that Andreas posted seems valid for PMD 5.0.5 through >>> 5.1.13. I have looked at processfiles() in >>> pmd.sourceforge.net/pmd-5.1.0/apidocs but there appears to be no api >>> docs for any later version of PMD. In particular it also appears PMD >>> was split into multiple >>> .jar files in later versions and there seems no document anywhere that >>> explains what was taken out of the pmd.jar file to create the current >>> pmd-core jar file. So I don't even know if maybe I'm doing everything >>> right and the simple problem is that I need to merge more jar files >>> from >>> PMD into the extension jar file. >>> >>> The current version of the maven plugin seems designed for PMD >>> 5.3 and >>> while it appears to still use processFiles() the setup for the API call >>> appears different than the example posted. >>> >>> Please help! >>> >>> Ted >>> >>> >>> On 5/21/2015 1:03 AM, Ted Mittelstaedt wrote: >>>> Hi All, >>>> >>>> Using the URL that Andreas posted and the original PMD BlueJ extension >>>> code I created a new PMD Extension for BlueJ. >>>> >>>> Unfortunately, I am having a problem with it. The extension works but >>>> the call to PMD does not return - the last thing the extension prints >>>> out is Rules used. >>>> >>>> Here is the code if anyone can see anything let me know: >>>> >>>>> >>>>> public class MenuBuilder extends MenuGenerator { >>>>> >>>>> private BClass curClass; >>>>> private Frame frame; >>>>> private RuleSet ruleSet = new RuleSet(); >>>>> private String javaFileName; >>>>> >>>>> public MenuBuilder(Frame frame) { >>>>> this.frame = frame; >>>>> } >>>>> >>>>> public JMenuItem getClassMenuItem(BClass aClass) { >>>>> try { >>>>> javaFileName = aClass.getJavaFile().getPath(); >>>>> } catch (ProjectNotOpenException pnoe) { >>>>> } catch (PackageNotFoundException pnfe) { >>>>> } >>>>> JMenu jm = new JMenu("PMD"); >>>>> jm.add(new JMenuItem(new SimpleAction("Check code"))); >>>>> return jm; >>>>> } >>>>> >>>>> public void notifyPostClassMenu(BClass bc, JMenuItem jmi) { >>>>> curClass = bc; >>>>> } >>>>> >>>>> // The nested class that instantiates the different (simple) >>>>> menus. >>>>> class SimpleAction extends AbstractAction { >>>>> >>>>> public SimpleAction(String menuName) { >>>>> putValue(AbstractAction.NAME, menuName); >>>>> } >>>>> >>>>> public void actionPerformed(ActionEvent anEvent) { >>>>> >>>>> try { >>>>> int textLen = curClass.getEditor().getTextLength(); >>>>> TextLocation lastLine = >>>>> curClass.getEditor().getTextLocationFromOffset(textLen); >>>>> String code = curClass.getEditor().getText(new >>>>> TextLocation(0,0), lastLine); >>>>> >>>>> PMDConfiguration configuration = new >>>>> PMDConfiguration(); >>>>> >>>>> configuration.setRuleSets("rulesets/java/basic.xml, >>>>> rulesets/java/unusedcode.xml"); >>>>> configuration.setSourceEncoding("UTF-8"); >>>>> >>>>> RuleSetFactory ruleSetFactory = new >>>>> RuleSetFactory(); >>>>> >>>>> ruleSetFactory.setMinimumPriority(RulePriority.MEDIUM); >>>>> >>>>> // the files to process >>>>> List<DataSource> dataSources = new >>>>> ArrayList<DataSource>(); >>>>> dataSources.add(new FileDataSource(new >>>>> File(javaFileName))); >>>>> >>>>> // collect the results in a listener >>>>> final Report masterReport = new Report(); >>>>> ReportListener listener = new ReportListener() { >>>>> @Override >>>>> public void ruleViolationAdded(RuleViolation >>>>> ruleViolation) { >>>>> >>>>> masterReport.addRuleViolation(ruleViolation); >>>>> } >>>>> @Override >>>>> public void metricAdded(Metric metric) { >>>>> // TODO Auto-generated method stub >>>>> } >>>>> }; >>>>> >>>>> RuleContext ruleContext = new RuleContext(); >>>>> ruleContext.getReport().addListener(listener); >>>>> >>>>> JOptionPane.showMessageDialog(frame, "checking >>>>> class " + curClass.getJavaClass().getName()); >>>>> JOptionPane.showMessageDialog(frame, "class >>>>> filename " + javaFileName); >>>>> JOptionPane.showMessageDialog(frame, "Rules used >>>>> " + configuration.getRuleSets()); >>>>> >>>>> PMD.processFiles( configuration, ruleSetFactory, >>>>> dataSources, ruleContext, Collections.<Renderer>emptyList() ); >>>>> >>>>> JOptionPane.showMessageDialog(frame, "Class >>>>> Checked"); >>>>> >>>>> // all violations should be in the master report >>>>> StringBuffer msg = new StringBuffer(""); >>>>> int countOfViolations = masterReport.size(); >>>>> if (countOfViolations == 0) { >>>>> msg.append("No problems found!"); >>>>> } else { >>>>> if (countOfViolations == 1) { >>>>> msg.append(" " + countOfViolations + " >>>>> problem found"); >>>>> } else { >>>>> msg.append(" " + countOfViolations + " >>>>> problems found"); >>>>> } >>>>> >>>>> msg.append(System.getProperty("line.separator")); >>>>> >>>>> Iterator<RuleViolation> violations = >>>>> masterReport.iterator(); >>>>> while (violations.hasNext()) { >>>>> RuleViolation ruleViolation = >>>>> violations.next(); >>>>> msg.append(" " + >>>>> ruleViolation.getFilename() + ":" + ruleViolation.getBeginLine() + >>>>> ": " + ruleViolation.getDescription()); >>>>> } >>>>> } >>>>> JOptionPane.showMessageDialog(frame, msg); >>>>> } catch (Exception exc) { >>>>> exc.printStackTrace(); >>>>> JOptionPane.showMessageDialog(frame, "Couldn't >>>>> run PMD: " + exc.getMessage()); >>>>> } >>>>> JOptionPane.showMessageDialog(frame, "PMD run >>>>> completed"); >>>>> } >>>>> } >>>>> } >>>> >>>> Thanks for looking at it, >>>> Ted >>>> >>>> On 5/14/2015 12:16 PM, Ted Mittelstaedt wrote: >>>>> Hi Andreas, >>>>> >>>>> Thanks! It looks like the "API" as they put it completely >>>>> changed in >>>>> PMD 5.0 and later, calling PMD now involves more setup. I tried >>>>> using >>>>> some of the example code and I'm getting<identifier expected> >>>>> errors >>>>> even when including all classes in PMD with a wildcard. For example >>>>> configuration.setRulesets() and so on. I'll dig into it further, >>>>> later, >>>>> probably I'll have to look at the complete Maven plugin. >>>>> >>>>> Thanks, >>>>> Ted >>>>> >>>>> On 5/13/2015 10:27 AM, Andreas Dangel wrote: >>>>>> Hi Ted, >>>>>> >>>>>> there is indeed a little bit documentation about how to run PMD >>>>>> programmatically: >>>>>> https://sourceforge.net/p/pmd/discussion/188193/thread/a256cb23/ >>>>>> >>>>>> Hope this helps! >>>>>> >>>>>> Regards, >>>>>> Andreas >>>>>> >>>>>> On 13.05.2015 17:54, Ted Mittelstaedt wrote: >>>>>>> Hi All, >>>>>>> >>>>>>> I'm a beginning Java student and I want to update the old >>>>>>> PMDExtension >>>>>>> for BlueJ to run with the latest version of BlueJ and the latest >>>>>>> version >>>>>>> of PMD. >>>>>>> >>>>>>> I made the following changes in MenuBuilder.java and got the >>>>>>> extension >>>>>>> to build under PMD version 4.3 from the original version it was >>>>>>> written >>>>>>> with, which was version 3.3: >>>>>>> >>>>>>> commented out import net.sourceforge.pmd.TargetJDK1_4 and changed >>>>>>> new PMD(new TargetJDK1_4()).processFiles(reader, ruleSet, ctx, >>>>>>> null); >>>>>>> to new PMD().processFiles(reader, ruleSet, ctx, null); >>>>>>> >>>>>>> changed msg.append to remove the call to getNode() >>>>>>> >>>>>>> replaced rsf.createRuleSet with rsf.createSingleRuleSet >>>>>>> >>>>>>> This got it to build and it does insert the PMD button in the >>>>>>> BlueJ menu >>>>>>> but the button doesn't work. >>>>>>> >>>>>>> And in the latest version of PMD 5.3 the rule classes seem to have >>>>>>> changed significantly anyway. >>>>>>> >>>>>>> The plugin uses just the basic and unusedcode rulesets which for >>>>>>> now is >>>>>>> fine. >>>>>>> >>>>>>> Is there a document or post that explains how to hook into PMD >>>>>>> with a >>>>>>> plugin? >>>>>>> >>>>>>> Any advice OTHER than "run pmd at the command line" and "look at >>>>>>> the >>>>>>> eclipse plugin" would be greatly appreciated (I can already >>>>>>> figure out >>>>>>> those 2 myself ;-)) >>>>>>> >>>>>>> Thanks! >>>>>>> Ted >>>>>>> >>>>>>> ------------------------------------------------------------------------------ >>>>>>> >>>>>>> One dashboard for servers and applications across >>>>>>> Physical-Virtual-Cloud >>>>>>> Widest out-of-the-box monitoring support with 50+ applications >>>>>>> Performance metrics, stats and reports that give you Actionable >>>>>>> Insights >>>>>>> Deep dive visibility with transaction tracing using APM Insight. >>>>>>> http://ad.doubleclick.net/ddm/clk/290420510;117567292;y >>>>>>> _______________________________________________ >>>>>>> Pmd-devel mailing list >>>>>>> Pmd...@li... >>>>>>> https://lists.sourceforge.net/lists/listinfo/pmd-devel >>>>>> >>>>> >>>>> ------------------------------------------------------------------------------ >>>>> >>>>> One dashboard for servers and applications across >>>>> Physical-Virtual-Cloud >>>>> Widest out-of-the-box monitoring support with 50+ applications >>>>> Performance metrics, stats and reports that give you Actionable >>>>> Insights >>>>> Deep dive visibility with transaction tracing using APM Insight. >>>>> http://ad.doubleclick.net/ddm/clk/290420510;117567292;y >>>>> _______________________________________________ >>>>> Pmd-devel mailing list >>>>> Pmd...@li... >>>>> https://lists.sourceforge.net/lists/listinfo/pmd-devel >>>> >>>> ------------------------------------------------------------------------------ >>>> >>>> One dashboard for servers and applications across >>>> Physical-Virtual-Cloud >>>> Widest out-of-the-box monitoring support with 50+ applications >>>> Performance metrics, stats and reports that give you Actionable >>>> Insights >>>> Deep dive visibility with transaction tracing using APM Insight. >>>> http://ad.doubleclick.net/ddm/clk/290420510;117567292;y >>>> _______________________________________________ >>>> Pmd-devel mailing list >>>> Pmd...@li... >>>> https://lists.sourceforge.net/lists/listinfo/pmd-devel >>> >>> ------------------------------------------------------------------------------ >>> >>> One dashboard for servers and applications across >>> Physical-Virtual-Cloud >>> Widest out-of-the-box monitoring support with 50+ applications >>> Performance metrics, stats and reports that give you Actionable >>> Insights >>> Deep dive visibility with transaction tracing using APM Insight. >>> http://ad.doubleclick.net/ddm/clk/290420510;117567292;y >>> _______________________________________________ >>> Pmd-devel mailing list >>> Pmd...@li... >>> https://lists.sourceforge.net/lists/listinfo/pmd-devel >> >> > -- Andreas Dangel https://github.com/adangel skype: andreas_dangel |