-1

EDIT: How do I use the eclipse parser to workout the type of an parameter at compile time?


I'm trying to check if a function was called by value. So i activate the following function on the user's code:

public boolean visit(MethodInvocation node){}

My question is how to actually check whether the call was by value or by reference. I tried member functions of node like the following:

typeArguments()
arguments()
getExpression()

and couldn't go further. For instance i tried:

List r_type= node.typeArguments();
if(r_type.toString() == "int")
{
  ....
}

but r_type seems to be always empty.

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
Alaa M.
  • 3,949
  • 7
  • 41
  • 81

1 Answers1

1

i'd like to know how to detect the arguments type (int / bool / double / ...)

In the case of primitives, you know the type, there is no need to detect which type it is as it is not dynamic. i.e. there is no way it could be different to what you know at compile time.

For objects however, there is a number of way to detect this as a common one is to use instanceof e.g.

 Object o = ...
 if (o instanceof Integer) {

 } else if (o instanceof Boolean) {

 } else if (o insatanceof Double) {

 } else {
     // some error
 }

A simple way to show that Java has passed a reference by value is to change the reference.

public boolean visit(MethodInvocation node){ node = null; }

MethodInvocation node = new MethodInvocation();
visit(node); // does it change to null
System.out.println(node); // no it doesn't. node is unchanged.

but r_type seems to be always empty.

This could be because your test will always fail. i.e. it is always false. Or the list is always empty because you didn't put anything in it.

I suggest you step through your code using your debugger to get a better understanding of what your program is doing.

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
  • OK then i'd say the question is not relevant anymore. But in that matter, i'd like to know how to detect the arguments type (int / bool / double / ...) – Alaa M. Apr 21 '14 at 19:40
  • @AlaaM. Doesn't the API have a method that returns the method parameter types? – Sotirios Delimanolis Apr 21 '14 at 19:43
  • @Peter Lawrey It's not my program yet. i'm upgrading someone else's... But how would you check the parameters' type though? – Alaa M. Apr 21 '14 at 19:43
  • @SotiriosDelimanolis i thought it's `typeArguments()`, but it's always empty as i explained in the question... – Alaa M. Apr 21 '14 at 19:45
  • @AlaaM. What is the type of `node`? I want to look up the API. – Sotirios Delimanolis Apr 21 '14 at 19:45
  • @AlaaM. I have updated my answer. If the typeArguments are a List, they it can only be a class which implements List such as ArrayList. If you want to see what the List contains, you need to examine each element and check it's type. – Peter Lawrey Apr 21 '14 at 19:46
  • @SotiriosDelimanolis `node` in function `visit()` is declared as `MethodInvocation`. Is that what you mean? – Alaa M. Apr 21 '14 at 19:48
  • @AlaaM. Yeah, my bad, didn't see that. So what method are you invoking? – Sotirios Delimanolis Apr 21 '14 at 19:49
  • @SotiriosDelimanolis i'm running the plugin on a `.java` file with a `public class` which has a `public static void main(String[] args)` and a lot of member functions, such as `private static int test1(int x)`. – Alaa M. Apr 21 '14 at 19:53
  • @AlaaM. The `MethodInvocation` should be describing the invocation of a single method. Which method is that? See the [API](http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fdom%2FMethodInvocation.html). – Sotirios Delimanolis Apr 21 '14 at 19:55