1

How can I determine type of Groovy variable using Java?
For example we have the next Groovy code:

a = new Integer(4);
b = a + 1;
a = "b = " + b;

How can we programatically analyze it and determine that type of a variable at b = a + 1; line is java.lang.Integer?

As a is dynamic variable we can't get its type from Groovy AST, because AST doesn't contain such info.

  1. update: added more details
  2. update: clarified question
  • 1
    a.getClass() would do it. More info here http://stackoverflow.com/questions/2060427/groovy-grails-how-to-determine-a-data-type –  Dec 20 '12 at 17:44

2 Answers2

1

it is possible using StaticTypeCheckingVisitor. In order to see more details, please, follow this link

0

In java you can use instanceof to determine a type of an object

Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
  • Thanks for response, `instanceof` will help to determine a type of an object inside code, but I need to determine a type outside of code - i.e. some kind of code analyzer with type inferencing. – akula barakula Jan 07 '13 at 13:29