pascal-lab/Tai-e

About field initializer in class

ShiningChuang opened this issue · 3 comments

I know that given a Stmt of IR, I can call getLineNumber() to get the position of this sentence; but it seems that only JMethod will have IR. So for a JClass type, how can I know where(LineNumber) each member variable is defined? For example:

Class Student {
     ID id = new ID();
}

How can I get the new ID() Stmt is at line 2?

Such field initializer will be compiled into constructor of the class, so you can find new ID() Stmt in constructor of Student.

We provide API JMethod.isConstructor() to help you determine whether a method is a constructor or not.

I got you. Furthermore, if I just declare a JField without initializing it, can I still get its line number? For example:

Class Student {
     ID id = new ID();
    Food favoriteFood;
}

How can I get the Food favoriteFood stmt is at line 3?

On the other hand, for a class definition file without a main function, how can I analyze it separately?

How can I get the Food favoriteFood stmt is at line 3?

Unfortunately, we cannot get field's line number, as this information is absent in the bytecode.

On the other hand, for a class definition file without a main function, how can I analyze it separately?

Not sure what kind of analysis you want to run. You could give Tai-e the classes you want to analyze via --input-classes option (see Command-Line Options for details), and then run the analysis.

BTW, this problem is irrelevant to field initializer. If my answer doesn't address your problem, could you please open another issue for it?