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: Type of string concatenation is null #3075

Merged
merged 2 commits into from
Aug 14, 2019
Merged

Conversation

Egor18
Copy link
Contributor

@Egor18 Egor18 commented Aug 10, 2019

Hi!
I found out that Spoon returns null type for 2 or more concatenations of string literals.
For example:
String s = "one" + "two" + "three";
Spoon says that the type of "two" + "three" is null, but it's actually String.
I provided the test that demonstrates that.

Moreover, I'm sure that the problem is here:

} else if (jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof StringLiteralConcatenation) {
CtBinaryOperator<?> op = operator.getFactory().Core().createBinaryOperator();
op.setKind(BinaryOperatorKind.PLUS);
op.setLeftHandOperand(operator.getRightHandOperand());
op.setRightHandOperand((CtExpression<?>) child);
operator.setRightHandOperand(op);
int[] lineSeparatorPositions = jdtTreeBuilder.getContextBuilder().getCompilationUnitLineSeparatorPositions();
SourcePosition leftPosition = op.getLeftHandOperand().getPosition();
SourcePosition rightPosition = op.getRightHandOperand().getPosition();
op.setPosition(op.getFactory().createSourcePosition(leftPosition.getCompilationUnit(), leftPosition.getSourceStart(), rightPosition.getSourceEnd(), lineSeparatorPositions));
return;
}

We create a new CtBinaryOperator but do not set its type.

So I tried to fix that by adding setType call:
op.setType((CtTypeReference) operator.getFactory().Type().STRING);

This fix works and type appears, but AstParentConsistencyChecker does not like it, because setType sets parent of the reference to op, so I'm getting:

java.lang.IllegalStateException: Element: java.lang.String
Signature: spoon.support.reflect.reference.CtTypeReferenceImpl@943a4c32
Class: class spoon.support.reflect.reference.CtTypeReferenceImpl
position: (unknown file)
 is set as child of
Element: (("two" + "three") + "four")
Signature: spoon.support.reflect.code.CtBinaryOperatorImpl@1
Class: class spoon.support.reflect.code.CtBinaryOperatorImpl
position: (/home/egor/spoon/src/test/java/spoon/test/type/testclasses/Foo.java:7)
however it is visited as a child of
Element: ("two" + "three")
Signature: spoon.support.reflect.code.CtBinaryOperatorImpl@1
Class: class spoon.support.reflect.code.CtBinaryOperatorImpl
position: (/home/egor/spoon/src/test/java/spoon/test/type/testclasses/Foo.java:7)

Do you have any ideas how to set the parent properly here?
Thanks.

@monperrus
Copy link
Collaborator

Thanks for the fix!

Maybe the short op.setType((CtTypeReference) operator.getFactory().Type().STRING.clone()) (call to clone)

or

ref =
(CtTypeReference) operator.getFactory().Type().STRING.clone()
ref.setParent(op)
op.setType(ref)

@Egor18
Copy link
Contributor Author

Egor18 commented Aug 12, 2019

Yes, it works, but Travis is stuck. I'll try to trigger it again.

@Egor18 Egor18 closed this Aug 12, 2019
@Egor18 Egor18 reopened this Aug 12, 2019
@Egor18 Egor18 closed this Aug 12, 2019
@Egor18 Egor18 reopened this Aug 12, 2019
@Egor18
Copy link
Contributor Author

Egor18 commented Aug 14, 2019

I think it's ready :)

@monperrus monperrus merged commit 5e6fa98 into INRIA:master Aug 14, 2019
@monperrus
Copy link
Collaborator

Thanks a lot!

@monperrus monperrus mentioned this pull request Nov 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants