Skip to content

Conversation

rahlk
Copy link
Collaborator

@rahlk rahlk commented Feb 18, 2025

Summary

Added source code position tracking (line/column) for parameters in method declarations to enable precise source code navigation and analysis in codeanalyzer-java.

Motivation and Context

Previously, parameters in method declarations lacked source position information, making it difficult to accurately locate them in source code when performing navigation, cross-referencing, and precise code analysis.

This update adds start/end line and column positions to ParameterInCallable, enabling:

  • Better source mapping for tools that analyze and visualize code structure.
  • Improved accuracy when linking parameters to their usage locations.
  • Seamless integration with downstream tools consuming analysis.json.

How Has This Been Tested?

  • Implemented new tests:
    • parametersInCallableMustHaveStartAndEndLineAndColumns(): Verifies that method parameters correctly store start/end line and column positions.

Breaking Changes

This is a breaking change, because users consuming analysis.json should be aware of the new fields in ParameterInCallable:

private int startLine;
private int endLine;
private int startCol;
private int endCol;

These fields will now appear in the serialized output.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

Additional Context

Changes Implemented

  1. Modified ParameterInCallable to store position information:
    @Data
    public class ParameterInCallable {
        private String type;
        private String name;
        private List<String> annotations;
        private List<String> modifiers;
        private int startLine;
        private int endLine;
        private int startCol;
        private int endCol;
    }
  2. Updated processParameterDeclaration to extract positions using JavaParser:
    private static ParameterInCallable processParameterDeclaration(Parameter paramDecl) {
         ParameterInCallable parameter = new ParameterInCallable();
         parameter.setType(resolveType(paramDecl.getType()));
         parameter.setName(paramDecl.getName().toString());
         parameter.setAnnotations(paramDecl.getAnnotations().stream().map(a -> a.toString().strip()).collect(Collectors.toList()));
         parameter.setModifiers(paramDecl.getModifiers().stream().map(a -> a.toString().strip()).collect(Collectors.toList()));
         parameter.setStartLine(paramDecl.getRange().isPresent() ? paramDecl.getRange().get().begin.line : -1);
         parameter.setStartColumn(paramDecl.getRange().isPresent() ? paramDecl.getRange().get().begin.column : -1);
         parameter.setEndLine(paramDecl.getRange().isPresent() ? paramDecl.getRange().get().end.line : -1);
         parameter.setEndColumn(paramDecl.getRange().isPresent() ? paramDecl.getRange().get().end.column : -1);
         return parameter;
     }
  3. Test Coverage Added
    • Verified parameter extraction includes accurate source positions.
    • Ensured JSON output correctly serializes line and column numbers.

Signed-off-by: Rahul Krishna <i.m.ralk@gmail.com>
Signed-off-by: Rahul Krishna <i.m.ralk@gmail.com>
Signed-off-by: Rahul Krishna <i.m.ralk@gmail.com>
@rahlk rahlk added enhancement New feature or request kind/feature Feature breaking Breaking Change labels Feb 18, 2025
@rahlk rahlk requested review from sinha108 and rangeetpan February 18, 2025 20:45
@rahlk rahlk self-assigned this Feb 18, 2025
@rahlk rahlk linked an issue Feb 18, 2025 that may be closed by this pull request
@rahlk rahlk merged commit 4240990 into main Feb 18, 2025
@rahlk rahlk deleted the 103-extend-parameterincallable-class-to-capture-line-and-column-offsets branch February 19, 2025 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Breaking Change enhancement New feature or request kind/feature Feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend ParameterInCallable class to capture Line and Column offsets.
2 participants