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

fix #2899 does not work for shadowed String constants #2977

Closed
mictaege opened this issue May 17, 2019 · 2 comments
Closed

fix #2899 does not work for shadowed String constants #2977

mictaege opened this issue May 17, 2019 · 2 comments

Comments

@mictaege
Copy link
Contributor

Upgrading from spoon 7.0.0 to 7.4.0. I faced problems that has been caused by the behavior described in the issues:

Both issues has been fixed with

but the fix does not work for String constants and I still get "not possible to transform to expression ..." errors.

Debugging the application I could see the following behavior:

With #2899 EvalHelper#getCorrespondingRuntimeObject has been extendend with this additional condition

if (field.getDefaultExpression() instanceof CtLiteral) {
  return ((CtLiteral) field.getDefaultExpression()).getValue();
}

But if the shadowed field is a String constant getDefaultExpression() returns null and therefore the Exception below will still be raised.

This is because the default expression has never been set in JavaReflectionTreeBuilder#visitField(Field):

if (modifiers.contains(ModifierKind.STATIC)
    && modifiers.contains(ModifierKind.PUBLIC)
    && field.getType().isPrimitive()
    ) {
      CtLiteral<Object> defaultExpression = factory.createLiteral(field.get(null));
      ctField.setDefaultExpression(defaultExpression);
}

For String constants field.getType().isPrimitive() is not true and so the whole if-condition turns to false and the default expression will not be set into the CtField.

Changing the if-condition to something like that

if (modifiers.contains(ModifierKind.STATIC)
    && modifiers.contains(ModifierKind.PUBLIC)
   && (field.getType().isPrimitive() || field.getType().getName().equals("java.lang.String"))
    ) {
      CtLiteral<Object> defaultExpression = factory.createLiteral(field.get(null));
      ctField.setDefaultExpression(defaultExpression);
}

in the debugger would make the whole condition true and then the default expression will be set correctly.

@monperrus
Copy link
Collaborator

Thanks a lot for the super detailed bug report.

It seems that the test case and the patch is super easy to write based on your extensive analysis. Would you give it a try in a pull-request? That would be awesome.

@mictaege
Copy link
Contributor Author

Thanks a lot for for your prompt reply!

As you suggested I tried to make a pull-request to fix this issue.

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

No branches or pull requests

2 participants