Skip to content

Improve lambda method discovery Fixes: #477 - #478

Merged
Jinbo Wang (testforstephen) merged 6 commits into
microsoft:mainfrom
gayanper:improve_lambda_mthd_discovery
May 24, 2023
Merged

Jinbo Wang (testforstephen) merged 6 commits into
microsoft:mainfrom
gayanper:improve_lambda_mthd_discovery

Conversation

@gayanper

Copy link
Copy Markdown
Contributor

The issue seems to be when resolving lambda method for reactive operators, the binding resolver is not the default binding resolvers which actually resolves the lambda method.

It seems. the AST construction effects which binding resolver is used at runtime in the node. Therefore switched to the using cache AST when available which seems to resolved the issue. Will do some more testing.

@testforstephen

Copy link
Copy Markdown
Contributor

parser.setSource(source.toCharArray());
/**
* See the java doc for { @link ASTParser#setResolveBindings(boolean) }.
* Binding information is obtained from the Java model. This means that the
* compilation unit must be located relative to the Java model.
* This happens automatically when the source code comes from either
* setSource(ICompilationUnit) or setSource(IClassFile).
* When source is supplied by setSource(char[]), the location must be
* established explicitly
* by setting an environment using setProject(IJavaProject) or
* setEnvironment(String [], String [], String [], boolean)
* and a unit name setUnitName(String).
*/
parser.setEnvironment(new String[0], new String[0], null, true);
parser.setUnitName(Paths.get(filePath).getFileName().toString());

When parsing the AST for the lambda, we just passed a char array without the context Java model or project, that's why the binding information is not resolved correctly.

Your workaround seems OK. Another way to fix this is to find the Java project that the current file belongs to and set the parser to use that project for binding resolver.

@gayanper

Copy link
Copy Markdown
Contributor Author

Jinbo Wang (@testforstephen) WDYT about the latest change ?

@gayanper
Gayan Perera (gayanper) force-pushed the improve_lambda_mthd_discovery branch from f008742 to 0d93da4 Compare May 11, 2023 07:39
@gayanper

Copy link
Copy Markdown
Contributor Author

Jinbo Wang (@testforstephen) anything pending for approval and merge ?

@testforstephen

Copy link
Copy Markdown
Contributor

this PR is on the list for May sprint.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parser.setSource(source.toCharArray());
/**
* See the java doc for { @link ASTParser#setResolveBindings(boolean) }.
* Binding information is obtained from the Java model. This means that the
* compilation unit must be located relative to the Java model.
* This happens automatically when the source code comes from either
* setSource(ICompilationUnit) or setSource(IClassFile).
* When source is supplied by setSource(char[]), the location must be
* established explicitly
* by setting an environment using setProject(IJavaProject) or
* setEnvironment(String [], String [], String [], boolean)
* and a unit name setUnitName(String).
*/
parser.setEnvironment(new String[0], new String[0], null, true);
parser.setUnitName(Paths.get(filePath).getFileName().toString());

When parsing the AST for the lambda, we just passed a char array without the context Java model or project, that's why the binding information is not resolved correctly.

A simpler fix is to use the parent Java project as the environment. This works for the case in issue #477.

            IFile resource = (IFile) JDTUtils.findResource(JDTUtils.toURI(uri), ResourcesPlugin.getWorkspace().getRoot()::findFilesForLocationURI);
            if (resource != null && JdtUtils.isJavaProject(resource.getProject())) {
                parser.setProject(JavaCore.create(resource.getProject()));
            } else {
                parser.setEnvironment(new String[0], new String[0], null, true);
            }

- Use classfile and compilationunit resolution as the first method
before fallback to old strategy.
- Switch to using jdt.debug lambda helper which resolves lambda method
signature with outer local variables.
@gayanper
Gayan Perera (gayanper) force-pushed the improve_lambda_mthd_discovery branch from 0d93da4 to a06f28b Compare May 22, 2023 16:41
@gayanper

Copy link
Copy Markdown
Contributor Author

parser.setSource(source.toCharArray());
/**
* See the java doc for { @link ASTParser#setResolveBindings(boolean) }.
* Binding information is obtained from the Java model. This means that the
* compilation unit must be located relative to the Java model.
* This happens automatically when the source code comes from either
* setSource(ICompilationUnit) or setSource(IClassFile).
* When source is supplied by setSource(char[]), the location must be
* established explicitly
* by setting an environment using setProject(IJavaProject) or
* setEnvironment(String [], String [], String [], boolean)
* and a unit name setUnitName(String).
*/
parser.setEnvironment(new String[0], new String[0], null, true);
parser.setUnitName(Paths.get(filePath).getFileName().toString());

When parsing the AST for the lambda, we just passed a char array without the context Java model or project, that's why the binding information is not resolved correctly.

A simpler fix is to use the parent Java project as the environment. This works for the case in issue #477.

            IFile resource = (IFile) JDTUtils.findResource(JDTUtils.toURI(uri), ResourcesPlugin.getWorkspace().getRoot()::findFilesForLocationURI);
            if (resource != null && JdtUtils.isJavaProject(resource.getProject())) {
                parser.setProject(JavaCore.create(resource.getProject()));
            } else {
                parser.setEnvironment(new String[0], new String[0], null, true);
            }

This is a good suggestion, I change the code now.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for contribution.

@testforstephen
Jinbo Wang (testforstephen) merged commit a011049 into microsoft:main May 24, 2023
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