Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Test and Performance Tools Platform (TPTP) » Programmatically initiate code review
Programmatically initiate code review [message #84304] Tue, 26 September 2006 17:25 Go to next message
Eclipse User
Originally posted by: matthew.gregory.us.michelin.com

I am (quite unsuccessfully) trying to figure out how (if) I can
programmatically initiate a code review using TPTP. I'm finding very
little on Google or in this newsgroup, perhaps there is somewhere else I
could be looking? My method so far is the browsing javadoc, peeking at
TPTP source and picking things that seem like they might be right.
Further complicating things is this is my first experience doing any
plugin development or Eclipse customization of any sort.

Here's the mess I have so far:

<snippet>

IProject project =
ResourcesPlugin.getWorkspace().getRoot().getProject("helloWorld ");
IResource[] resources = project.members(IResource.FILE);
List files = Arrays.asList(resources);

System.out.println("found " + files.size() + " files");

AnalysisHistory hist =
AnalysisHistoryFactory.instance().createAnalysisHistory("configName ");

CodeReviewProvider provider = new CodeReviewProvider();
provider.addOwnedElement(AnalysisUtil.getAnalysisElement("codereview.java.j2sebestpractices "));
provider.addOwnedElement(AnalysisUtil.getAnalysisElement("codereview.java.category.statement "));
provider.addOwnedElement(AnalysisUtil.getAnalysisElement(" codereview.java.rules.statement.RuleStatementSurroundWithBra ces "));
mgr.addOwnedElement(provider);

mgr.analyze(hist, files);

List history = mgr.getHistoryResults("configName");
System.out.println("found " + history.size() + " history results");

Iterator iter = history.iterator();
while (iter != null && iter.hasNext()) {
System.out.println(iter.next().toString());
}

if (true)
System.out.println("i KNOW this line fails analysis");

if (true) { /* this one too */ }

System.out.println("finished.");


</snippet>

The code actually runs through without exception, I just get no elements
in my history List.

Can anyone tell me the proper way to go about programmatically initiating
a code review?

Many thanks in advance!
Re: Programmatically initiate code review [message #84319 is a reply to message #84304] Wed, 27 September 2006 01:58 Go to previous messageGo to next message
Eclipse User
Originally posted by: s_gutz.ca.ibm.com

Take a look at the AnalysisLaunchConfigurationDelegate and you will see
how we launch the analysis form the UI by calling the
AnalysisProviderManager.analyze() methos.. You can do exactly the same
thing.

I will put your code into a project tomorrow and see if there is
anything obvious going on. In first glance you need to use the
AnalysisProviderManager class to get things rolling. It initializes the
rule model and history and does what you are trying to do here from
first principles.

If you can't figure it out I'll try to find some code that does what you
need. It should only take a few lines to select the rules you want to
run and kick off analysis using the AnalysisProviderManager class.

Steve


Matt Gregory wrote:
> I am (quite unsuccessfully) trying to figure out how (if) I can
> programmatically initiate a code review using TPTP. I'm finding very
> little on Google or in this newsgroup, perhaps there is somewhere else I
> could be looking? My method so far is the browsing javadoc, peeking at
> TPTP source and picking things that seem like they might be right.
> Further complicating things is this is my first experience doing any
> plugin development or Eclipse customization of any sort.
>
> Here's the mess I have so far:
>
> <snippet>
>
> IProject project =
> ResourcesPlugin.getWorkspace().getRoot().getProject("helloWorld ");
> IResource[] resources = project.members(IResource.FILE);
> List files = Arrays.asList(resources);
>
> System.out.println("found " + files.size() + " files");
>
> AnalysisHistory hist =
> AnalysisHistoryFactory.instance().createAnalysisHistory("configName ");
>
> CodeReviewProvider provider = new CodeReviewProvider();
> provider.addOwnedElement(AnalysisUtil.getAnalysisElement("codereview.java.j2sebestpractices "));
>
> provider.addOwnedElement(AnalysisUtil.getAnalysisElement("codereview.java.category.statement "));
>
> provider.addOwnedElement(AnalysisUtil.getAnalysisElement(" codereview.java.rules.statement.RuleStatementSurroundWithBra ces "));
>
> mgr.addOwnedElement(provider);
>
> mgr.analyze(hist, files);
>
> List history = mgr.getHistoryResults("configName");
> System.out.println("found " + history.size() + " history results");
>
> Iterator iter = history.iterator();
> while (iter != null && iter.hasNext()) {
> System.out.println(iter.next().toString());
> }
>
> if (true)
> System.out.println("i KNOW this line fails analysis");
>
> if (true) { /* this one too */ }
>
> System.out.println("finished.");
>
>
> </snippet>
>
> The code actually runs through without exception, I just get no elements
> in my history List.
>
> Can anyone tell me the proper way to go about programmatically
> initiating a code review?
>
> Many thanks in advance!
>
Re: Programmatically initiate code review [message #84381 is a reply to message #84319] Wed, 27 September 2006 13:03 Go to previous messageGo to next message
Eclipse User
Originally posted by: matthew.gregory.us.michelin.com

Thank you very much for the tips. Before you go to any trouble with that
code I'll investigate the AnalysisLaunchConfigurationDelegate class and
see if I can gleen anything else from the AnalysisProviderManager class.
Although I have been playing around with the AnalysisProviderManager and
not getting very far. But if I know it's a step in the right direction I
can focus on it a little more.

I posted that code more as an example as what I'm doing so it would be
easier to steer me in the right direction. I'm confident that it may have
some of the right lines, but I'm also confident it's missing missing some
other key ones, has some extra ones that aren't needed, and perhaps even a
few that are counter-productive. I didn't mean it as an example that I
thought should actually function correctly. So I hope you haven't spent
any more time on it than a few minutes of glancing. Unless of course
those few minutes of glancing lead you to believe I've gotten extremely
lucky and the code should actually function.

Thanks again for the help!
Re: Programmatically initiate code review [message #84477 is a reply to message #84381] Wed, 27 September 2006 19:00 Go to previous messageGo to next message
Steve Gutz is currently offline Steve Gutz
Messages: 70
Registered: July 2009
Member
Yes your code was headed in the right direction - I was impressed actually
that you got so far. :-) The big problem is that the history instance you
create doesn't have any selections in it. As you will see from
AnalysisLaunchConfigurationDelegate, when a history is created we poke it
full of AnalysisHistoryElements. These instances are used to track selected
rules in the history so we can keep track of what categories and rules were
run. In 4.3 we will/may also track resources in history, which we don't
currently do in 4.2.1. Here is kind of a minimum example of what you need:

AnalysisHistory history =
AnalysisHistoryFactory.instance().createAnalysisHistory( conf.getName() );
buildSelectionList( conf, history ); // List this verbatim from
AnalysisLaunConfigurationDelegate to map selected rules into the history

// Get the manager and if it is enabled begin processing it
IAnalysisProviderManager manager = AnalysisUtil.getProviderManager();

// Let the results model know when analysis events happen
manager.addAnalysisListener( ResultsModel.instance() );

List resources == new ArrayList( // Get your resources however you want );

// Tell analysis to happen
manager.analyze( history, list );



"Matt Gregory" <matthew.gregory@us.michelin.com> wrote in message
news:7ca73b3bbfcb97f9686450243a95c7bd$1@www.eclipse.org...
> Thank you very much for the tips. Before you go to any trouble with that
> code I'll investigate the AnalysisLaunchConfigurationDelegate class and
> see if I can gleen anything else from the AnalysisProviderManager class.
> Although I have been playing around with the AnalysisProviderManager and
> not getting very far. But if I know it's a step in the right direction I
> can focus on it a little more.
> I posted that code more as an example as what I'm doing so it would be
> easier to steer me in the right direction. I'm confident that it may have
> some of the right lines, but I'm also confident it's missing missing some
> other key ones, has some extra ones that aren't needed, and perhaps even a
> few that are counter-productive. I didn't mean it as an example that I
> thought should actually function correctly. So I hope you haven't spent
> any more time on it than a few minutes of glancing. Unless of course
> those few minutes of glancing lead you to believe I've gotten extremely
> lucky and the code should actually function.
>
> Thanks again for the help!
>
Re: Programmatically initiate code review [message #84488 is a reply to message #84477] Wed, 27 September 2006 19:47 Go to previous messageGo to next message
Eclipse User
Originally posted by: matthew.gregory.us.michelin.com

It's inspiring to know I was headed in the right direction. What I've
come up in the mean time was almost exactly what you just suggested. I
used AnalysisLaunchConfigurationDelegate as the model. Everything is hard
coded because it's a prototype, but the launch configuration exists and is
valid, and the project exists and is valid (actually it's the same
project).

<snippet>

LaunchManager launchManager = new LaunchManager();
ILaunchConfiguration conf = launchManager.getLaunchConfiguration("<?xml
version=\"1.0\" encoding=\"UTF-8\"?><launchConfiguration local=\"true\"
path=\"New_configuration (2).launch\"/>");

AnalysisHistory history =
AnalysisHistoryFactory.instance().createAnalysisHistory(conf .getName());

buildSelectionList( conf, history ); //copied exactly from
AnalysisLaunchConfigurationDelegate

//Get the manager and if it is enabled begin processing it
IAnalysisProviderManager manager = AnalysisUtil.getProviderManager();

IAnalysisListener listener = new MyAnalysisListener();
manager.addAnalysisListener( listener );

IProject project =
ResourcesPlugin.getWorkspace().getRoot().getProject("helloWorld ");
List list = new ArrayList(1);
list.add(project);

manager.analyze(history, list);

List results = manager.getHistoryResults(history.getHistoryId());
System.out.println("got " + results.size() + " results");

</snippet>

Notice I used a different IAnalysisListener. ResultsModel seems to be
forbidden ("Access restriction: The type ResultsModel is not accessible
due to restriction on required library"). So I simply created another
which writes to System.out when either analysisComplete() method is
called. If the ResultsModel is a key to the equation then is there
another way around it?

Deep down in the execution there is a NullPointerException thrown during
CodeReviewProvider.analyze(). It tries to iterate a loop using
getOwnedElements().iterator(), but getOwnedElements() returns null. I'm
not sure if this is a symptom of my not having initialized something
correctly.

So now I'm to the point where I have code that I think should work, but I
can't figure out why it doesn't. I'm sure there are some concepts I'm
missing. Any advice?

Thanks again for the help!
Re: Programmatically initiate code review [message #84500 is a reply to message #84477] Wed, 27 September 2006 21:42 Go to previous messageGo to next message
Eclipse User
Originally posted by: matthew.gregory.us.michelin.com

Some more information:

This code is wrapped in a headless plugin (implements IPlatformRunnable).
The output I receive varies depending on whether I run it headless or from
within Eclipse. Here are the differences:

- I've put a println in the buildChildren() method just after the test for
the element being enabled. In headless mode, I get a nice list of all the
rules that I've seleced in my configuration. If I launch from within
Eclipse, I only get the first "Code Review for Java" entry.

- In headless mode I get 4 lines of output from my Listener. 3 from
analysisComplete(element) and 1 from analysisComplete(history).


Actually, during the writing (and a little testing) of this message, now
the plugin will not work at all from within Eclipse. I get a
NullPointerException in buildChildren() because parent.getOwnedElements()
is returning null, so elements.iterator() is throwing the exception. Not
sure what happened there? Seems I'm environmentally messing things up ...
wish I knew how I did that :-)
Re: Programmatically initiate code review [message #84511 is a reply to message #84488] Wed, 27 September 2006 21:45 Go to previous messageGo to next message
Eclipse User
Originally posted by: s_gutz.ca.ibm.com

There is "kind of" a way around the protect ResultModel class. In TPTP
4.3 we are unifying the notification mechanism. You can now add a
history listener to a history object which can tell you when analysis is
started, ended, or if history is changed (e.g. a result had been
quickfixed and subsequently removed from history). You could then most
likely eliminate the ResultsModel reference all together (I think).

Maybe you should work with a 4.3 build - with the understanding that
there is still a bit of flux (though API is preserved).

Admittedly you are doing something we never thought people would want to
do. We were hoping that people would just use the UI we provide since
it provides consistency. I'm intrigued. Can I ask what your
application for this is? E-mail me if it's something you don't want to
talk about in the open community.

Steve


Matt Gregory wrote:
> It's inspiring to know I was headed in the right direction. What I've
> come up in the mean time was almost exactly what you just suggested. I
> used AnalysisLaunchConfigurationDelegate as the model. Everything is
> hard coded because it's a prototype, but the launch configuration exists
> and is valid, and the project exists and is valid (actually it's the
> same project).
>
> <snippet>
>
> LaunchManager launchManager = new LaunchManager();
> ILaunchConfiguration conf = launchManager.getLaunchConfiguration("<?xml
> version=\"1.0\" encoding=\"UTF-8\"?><launchConfiguration local=\"true\"
> path=\"New_configuration (2).launch\"/>");
>
> AnalysisHistory history =
> AnalysisHistoryFactory.instance().createAnalysisHistory(conf .getName());
>
> buildSelectionList( conf, history ); //copied exactly from
> AnalysisLaunchConfigurationDelegate
> //Get the manager and if it is enabled begin processing it
> IAnalysisProviderManager manager = AnalysisUtil.getProviderManager();
>
> IAnalysisListener listener = new MyAnalysisListener();
> manager.addAnalysisListener( listener );
>
> IProject project =
> ResourcesPlugin.getWorkspace().getRoot().getProject("helloWorld ");
> List list = new ArrayList(1);
> list.add(project);
>
> manager.analyze(history, list);
>
> List results = manager.getHistoryResults(history.getHistoryId());
> System.out.println("got " + results.size() + " results");
>
> </snippet>
>
> Notice I used a different IAnalysisListener. ResultsModel seems to be
> forbidden ("Access restriction: The type ResultsModel is not accessible
> due to restriction on required library"). So I simply created another
> which writes to System.out when either analysisComplete() method is
> called. If the ResultsModel is a key to the equation then is there
> another way around it?
>
> Deep down in the execution there is a NullPointerException thrown during
> CodeReviewProvider.analyze(). It tries to iterate a loop using
> getOwnedElements().iterator(), but getOwnedElements() returns null. I'm
> not sure if this is a symptom of my not having initialized something
> correctly.
>
> So now I'm to the point where I have code that I think should work, but
> I can't figure out why it doesn't. I'm sure there are some concepts I'm
> missing. Any advice?
>
> Thanks again for the help!
>
Re: Programmatically initiate code review [message #84609 is a reply to message #84511] Thu, 28 September 2006 14:48 Go to previous messageGo to next message
Eclipse User
Originally posted by: matthew.gregory.us.michelin.com

We want to use TPTP as a base for a custom analysis package that we can
roll out to all of our developers which use RAD6 (and soon RAD7). What
made me choose TPTP over some of the other existing tools is it's
integration into Eclipse/RAD and it's customizability. One of our
requirements is to be able to do some basic architectural rule validation
to make sure applications are correctly implemented according to our
framework. None of the other tools (other than quite expensive ones)
would allow for these kinds of rules.

What TPTP doesn't do that I need to customize is some automation (Please
correct me if I'm wrong). We don't want developers to be able to ignore
the tool, so we want to automate the execution of code analysis. I think
a custom builder will be best for this, but not exactly sure just yet.
This automation is why I'm trying to figure out how to programmatically
initiate the analysis.

Another "cool" (not so cool, but that's life) thing I need to do is an
interim solution while we're in RAD6/7 limbo. Since we can't create
custom rules in RAD6 I need to create all my rules in Eclipse 3.2 in a
headless plugin, use RAD6 to call it, and then just capture the output
somehow. Won't be terribly pretty, but won't be long-term either. We're
still waiting to learn when the top-secret release date of RAD7 ... any of
you guy associated with that project as well? ;-) If so, I have some
questions ...

We also want to be able to execute the analysis in a batch mode for
projects which do nightly builds. In any of the three cases, though, I
think I need to know how to programmatically launch this analysis.
Perhaps not the last(?)

So that's the basics. I think TPTP is a great tool. I can't say I'm an
expert in code analysis, but based on my research it gains leverage from
Eclipse that gives it far more power than the other tools out there, with
the added benefit of tying it all into a neat little IDE package.
Re: Programmatically initiate code review [message #84622 is a reply to message #84500] Fri, 29 September 2006 17:32 Go to previous messageGo to next message
Steve Gutz is currently offline Steve Gutz
Messages: 70
Registered: July 2009
Member
One thing that may help you. In AnalysisProviderManager there are several
analyze() methods (see the source). For your application you probably want
to use synchronousAnalyze() since it elimninates all the asynchronous job
activity that may mess you up. So to review, the basic steps you did before
are probably OK except for the manager.analyze() call. Since you don't have
UI you can probably just skip the ResultModel reference completely - it's
only used to to keep the UI notified of changes.

Since people (you at least) want to interact with the core API directly
we'll try to simplify it a bit for TPTP 4.3 and beyond.

Steve

"Matt Gregory" <matthew.gregory@us.michelin.com> wrote in message
news:529f997d310861595e90d5be51fe28af$1@www.eclipse.org...
> Some more information:
>
> This code is wrapped in a headless plugin (implements IPlatformRunnable).
> The output I receive varies depending on whether I run it headless or from
> within Eclipse. Here are the differences:
>
> - I've put a println in the buildChildren() method just after the test for
> the element being enabled. In headless mode, I get a nice list of all the
> rules that I've seleced in my configuration. If I launch from within
> Eclipse, I only get the first "Code Review for Java" entry.
> - In headless mode I get 4 lines of output from my Listener. 3 from
> analysisComplete(element) and 1 from analysisComplete(history).
>
>
> Actually, during the writing (and a little testing) of this message, now
> the plugin will not work at all from within Eclipse. I get a
> NullPointerException in buildChildren() because parent.getOwnedElements()
> is returning null, so elements.iterator() is throwing the exception. Not
> sure what happened there? Seems I'm environmentally messing things up ...
> wish I knew how I did that :-)
Re: Programmatically initiate code review [message #84643 is a reply to message #84622] Fri, 29 September 2006 20:24 Go to previous messageGo to next message
Eclipse User
Originally posted by: matthew.gregory.us.michelin.com

I'll take a look at the synchronousAnalyze() method. I've got it working
pretty well though. It works within Eclipse32 and headlessly from command
line. All it does is print out to the console or command line the rule
violated, file, and line. You're right, I just avoided the ResultsModel
class all together. I just had to figure out how to parse through the
results in the history to get the information I wanted. I'm happy to
share the code (although I've employed the brute force method since it's a
prototype) with whomever is interested.
Re: Programmatically initiate code review [message #84661 is a reply to message #84643] Fri, 29 September 2006 21:01 Go to previous message
Eclipse User
Originally posted by: s_gutz.ca.ibm.com

Excellent news.

The history contains AnalysisHistoryElement instances so its not totally
straightforward. Each of these keeps track of which analysis elements
(a.k.a categories and rules) are enabled and maps it back to a real
AnalysisElement.

BTW in 4.3 the internal ResultsModel class has been replaced by a public
listener mechanism on AnalaysisHistoryFactory. For either UI or
command-line application you can now listen for history activity with ease.

Steve


Matt Gregory wrote:
> I'll take a look at the synchronousAnalyze() method. I've got it
> working pretty well though. It works within Eclipse32 and headlessly
> from command line. All it does is print out to the console or command
> line the rule violated, file, and line. You're right, I just avoided
> the ResultsModel class all together. I just had to figure out how to
> parse through the results in the history to get the information I
> wanted. I'm happy to share the code (although I've employed the brute
> force method since it's a prototype) with whomever is interested.
>
>
Previous Topic:Ant script on Linux different behavior file not found
Next Topic:where does TPTP sit in relation to pde-build?
Goto Forum:
  


Current Time: Wed Sep 17 07:38:28 GMT 2014

Powered by FUDForum. Page generated in 0.02820 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software