how to get the fully qualified name of a method call when it exists in the source code using javaparser - javaparser

My test code to excercise various functions of JavaParser:
public class test001 {
public static void main(String[] args) {
test001 t = new test001();
t.run();
}
#sample.mkGetSet
int g1;
#sample.start
test001(){
g1 = 14;
}
#sample.funky
void run() {
#sample.flagVar
int a,b,c;
a=1;
b=2;
c=a+b;
c=c+g1;
System.out.println("result:"+c);
}
}
My code correctly notes that I call println in the method run.
Is there a fairly easy way to get the 'System.out.' part in addition to the println part?
In the case where the qualification is not there, I dont need it but if it is i would like it.
The type of answer I'm looking for is along the line of 'In the MethodCallExpr look at XXXXX' (with the obligatory admonition to RTFD)
I've been over the documentation and its still not clear how to do this - or if i even can.

Related

nullPointerException error greenfoot

I'm working on a project for an intro to programming class, and I've run into a slight problem. We're making a side scroller, and I'm working on the score counter right now. My issue is that when I try to create a reference to the counter class in anything other than the act method(called once every frame) I get a null pointer exception error. You can download the zip file with my code in it here if you want to take a look.
EDIT:
Here's the offending code:
public class HeroMissile extends Missiles
{
/**
* Act - do whatever the HeroMissile wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move(8);
remove();
}
public void remove() {
if(isTouching(Drone.class)) {
removeTouching(Drone.class);
getWorld().addObject(new Explosion(), getX(), getY());
getWorld().removeObject(this);
addScore();
return;
}
}
public void addScore() {
City cityWorld = (City) getWorld();
**Counter scoreCounter = cityWorld.getCounter();**
scoreCounter.add(1);
}
}
You are calling getWorld() [in addScore()] after you removed yourself from the world. In this case, getWorld() will return null, so you will get a null pointer exception. Try changing the order in remove() to add the score before you remove yourself from the world.

How do you use code fragment mode in the Intellij debugger?

In the debugger, you can press alt-f8 and evaluate an expression.
There's also a code fragment mode. The documentation in IntelliJ only says:
Code Fragment Mode for evaluating short code portions introducing them
in the Statements to evaluate text field. Supported constructs are
declarations, assignments, loops and if/else.
I can't find any examples on the web of how to use it and can't figure out myself.
Can you give examples of how to use the supported constructs?
Given
public class CodeFragment {
public static void main(String[] args) {
List<Foo> list = new ArrayList<Foo>();
list.add(new Foo("555"));
list.add(new Foo("777"));
list.add(new Foo("999"));
list.add(new Foo("bill"));
System.out.println();
}
public static class Foo {
String s;
public Foo(String s) {
this.s = s;
}
}
}
If we set a break point on the println, we can put the following into the code fragment
Foo resultFoo = null;
Iterator<Foo> it = list.iterator();
while (it.hasNext()) {
Foo foo = it.next();
if (foo.s.equals("777")) {
resultFoo = foo;
}
}
resultFoo = resultFoo;
This demonstrates declaration, assignment, a loop, and an if.
Note that the foreach loop is not supported, in older versions of intellij!
Also note the assignment at the end. As far as I can tell, the result that gets displayed is the result of the last statement. Without that last statement, this code would display 'false' - ie the result of the last call to it.next.

ArrayList method is creating an error upon calling

Hello everyone I'm tying to create a method that receives a list of string values and returns the list in reverse. The for loop is supposed to traverse the values in reverse order, starting with the last element. I'm getting an error message when I try to call the method in the main method, I don't know what argument to pass. Here is my code.
enter code here :import java.util.*;
public class ThisList
{
public static void main (String[] args)
{
list(ArrayList<String> words);
}
public static ArrayList<String> list(ArrayList<String> words)
{
ArrayList<String> phrase =new ArrayList<String>();
words.add("before");
words.add("gone");
words.add("has");
words.add("man");
words.add("no");
words.add("where");
words.add("go");
words.add("bodly");
words.add("To");
for(int i= words.size()-1; i>= 0; i--)
{
phrase.add(words.get(i));
}
return phrase;
}
}
Your problem lies in how you are calling your list() function in main, specifically in how you are improperly sending in your argument. You should not declare/initialize an object in the calling of a function. It is syntactically incorrect, plus how would you access it afterwards? Even if it was possible to declare an object in the calling of the function, the words array is also uninitialized when you are trying to use it. Also, your function list() is returning an ArrayList. You are not setting an ArrayList in your main() function to be set to the returning ArrayList(phrase) from list().
I would advise looking over Object-Oriented programming basics and a few beginner Java tutorials so you can get a better understanding to be able to solve this kind of problem on your own.
http://docs.oracle.com/javase/tutorial/java/concepts/
Below is the code you posted, with the proper way to call your function. If this is what you need, do not forget to upvote and select this answer as the correct one! (Upvote by pressing the up arrow next to the post, and accept answer by clicking the checkmark next to the post so that it turns green).
import java.util.*;
public class ThisList
{
public static void main (String[] args)
{
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> phrase_returned = list(words);
}
public static ArrayList<String> list(ArrayList<String> words)
{
ArrayList<String> phrase =new ArrayList<String>();
words.add("before");
words.add("gone");
words.add("has");
words.add("man");
words.add("no");
words.add("where");
words.add("go");
words.add("bodly");
words.add("To");
for(int i= words.size()-1; i>= 0; i--)
{
phrase.add(words.get(i));
}
return phrase;
}
}

A central location for catching throwables of JUnit tests?

I would like to catch any throwable during a Selenium test e.g. in order to make a screenshot. The only solution I could come up with for now is to separately surround the test steps with a try and catch block in every test method as following:
#Test
public void testYouTubeVideo() throws Throwable {
try {
// My test steps go here
} catch (Throwable t) {
captureScreenshots();
throw t;
}
}
I'm sure there is a better solution for this. I would like a higher, more centralized location for this try-catch-makeScreenshot routine, so that my test would be able to include just the test steps again. Any ideas would be greatly appreciated.
You need to declare a TestRule, probably a TestWatcher or if you want to define the rules more explicitly, ExternalResource. This would look something like:
public class WatchmanTest {
#Rule
public TestRule watchman= new TestWatcher() {
#Override
protected void failed(Description d) {
// take screenshot here
}
};
#Test
public void fails() {
fail();
}
#Test
public void succeeds() {
}
}
The TestWatcher anonymous class can of course be factored out, and just referenced from the test classes.
I solved a similar problem using Spring's AOP. In summary:
Declare the selenium object as a bean
Add an aspect using
#AfterThrowing
The aspect can take the screenshot and save it to a
file with a semirandom generated name.
The aspect also rethrows the exception, with the exception message including the filename so you can look at it afterwards.
I found it more helpful to save the HTML of the page due to flakiness of grabbing screenshots.

question in using CDirectScreenAccess of Symbian dev

Recently, I am studying Symbian development. When I want to use CDirectScreenAccess to draw on the device directly, there occurs the question.
My code is below :
//MySnakeAppView.h
class CMySnakeAppView : public CCoeControl
{
...
private:
void ConstructL(const TRect& aRect);
CDirectScreenAccess* iDSA;
void Restart(RDirectScreenAccess::TTerminationReasons aReason);
void AbortNow(RDirectScreenAccess::TTerminationReasons aReason);
void DrawGraphics();
...
}
//MySnakeAppView.cpp
void CMySnakeAppView::ConstructL(const TRect& aRect)
{
// Create a window for this application view
CreateWindowL();
// Set the windows size
SetRect(aRect);
// Activate the window, which makes it ready to be drawn
ActivateL();
CEikonEnv* env = CEikonEnv::Static();
iDSA = CDirectScreenAccess::NewL(env->WsSession(), *env->ScreenDevice(), Window(), *this);
iDSA->StartL();
DrawGraphics();
}
void CMySnakeAppView::DrawGraphics()
{
CFbsBitGc *gc = iDSA->Gc();
TRgb colorRed = AKN_LAF_COLOR(35);
gc->SetPenColor(colorRed);
gc->DrawRect(TRect(0,0,100,100));
iDSA->ScreenDevice()->Update();
}
void CMySnakeAppView::Restart(RDirectScreenAccess::TTerminationReasons aReason)
{
iDSA->StartL();
DrawGraphics();
}
void CMySnakeAppView::AbortNow(RDirectScreenAccess::TTerminationReasons aReason)
{
iDSA->Cancel();
}
when I build this project, it is wrong with the code
iDSA = CDirectScreenAccess::NewL(env->WsSession(), *env->ScreenDevice(), Window(), *this);
this is a mistake writing that :
'MDirectScreenAccess &'
- illegal implicit conversion from
'CMySnakeAppView' to
but when I do it like this :
iDSA = CDirectScreenAccess::NewL(env->WsSession(), *env->ScreenDevice(), Window(), (MDirectScreenAccess &)*this);
there is no mistake in building,but still have mistake in the project, I don't know why,I need your help
Your CMySnakeAppView should derive from MDirectScreenAccess - it looks like you're already implementing the right methods, you're just missing the declaration:
class CMySnakeAppView : public CCoeControl, public MDirectScreenAccess
You can't pass *this, because it is not an istance of a MDirectScreenAccess class nor an istance of a class that inherits from it. You can cast it manually, but that is an error. You should construct a class that inherits from MDirectScreenAccess and from there build the iDSA. Look at here:
http://www.developer.nokia.com/Community/Wiki/Anti-tearing_with_CDirectScreenBitmap