Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug in setPosition #1636

Closed
monperrus opened this issue Oct 21, 2017 · 8 comments · Fixed by #1678
Closed

bug in setPosition #1636

monperrus opened this issue Oct 21, 2017 · 8 comments · Fixed by #1678
Labels

Comments

@monperrus
Copy link
Collaborator

from @Egor18

Also I probably have found some issue with getPosition() method.
Consider the following code:

if (cond) return 10;
else return 20;

When I'm trying to get positions this way:

@Override
public void visitCtIf(CtIf ifElement)
{
  super.visitCtIf(ifElement);
  
  CtStatement thenStatement = ifElement.getThenStatement();
  CtStatement elseStatement = ifElement.getElseStatement();
 
  System.out.println(thenStatement.getPosition());
  System.out.println(elseStatement.getPosition());

  ...
}

It outputs:

(unknown file)
(unknown file)

But if I change source code to this:

if (cond)
{
  return 10;
}
else
{
  return 20;
}

It works fine:

(/Users/Egor/eclipse-workspace/Test/src/Test.java:10)
(/Users/Egor/eclipse-workspace/Test/src/Test.java:14)

To bypass this issue I have to use this trick:

SourcePosition thenPos = thenBody.getStatement(0).getPosition();
SourcePosition elsePos = elseBody.getStatement(0).getPosition();

It would be great to have it fixed :)
Thank you.

@tdurieux
Copy link
Collaborator

it is normal because CtStatement thenStatement = ifElement.getThenStatement(); and CtStatement elseStatement = ifElement.getElseStatement(); return implicit block, implicit element does not have position

@Egor18
Copy link
Contributor

Egor18 commented Oct 22, 2017

So, Is it possible to get first non-implicit element, to get it's position?

@monperrus
Copy link
Collaborator Author

implicit element does not have position

This may be the bug. The implicit blocks should have the position of their contained statement.

@monperrus
Copy link
Collaborator Author

So, Is it possible to get first non-implicit element, to get it's position?

Here is a workaround:

CtElement parentWithPosition = element.getParent(new Filter<CtElement>() {
	@Override
	public boolean matches(CtElement element) {
		return element.getPosition().getFile()!=null
}
})

@monperrus monperrus added the bug label Oct 23, 2017
@pvojtechovsky
Copy link
Collaborator

I would say that the implicit element does not have position of parent, but position of all children?

@monperrus
Copy link
Collaborator Author

monperrus commented Oct 23, 2017 via email

@tdurieux
Copy link
Collaborator

what does it mean for implicit constructors, where all the children are implicit?

what does it mean for implicit thisaccess where there is no children and the position of parent means nothing

@monperrus
Copy link
Collaborator Author

monperrus commented Oct 23, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants