Skip to content

Address Issue 118: Support Record Declarations #121

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

Merged
merged 2 commits into from
Feb 18, 2025

Conversation

rahlk
Copy link
Collaborator

@rahlk rahlk commented Feb 18, 2025

Summary

Added support for parsing and analyzing record declarations in codeanalyzer-java. This includes integration with the symbol table and call graph analysis, ensuring record components and implicit methods are correctly handled.

Motivation and Context

Previously, codeanalyzer-java did not correctly process Java record declarations, leading to:

  • Incomplete symbol tables (records were missing).
  • Call graphs that failed to include methods defined inside records.
  • Inability to track record components properly.

This update ensures:

  • record declarations are parsed correctly.
  • Record components are included in the symbol table.
  • Methods inside records appear in the call graph.
  • Implicit methods (equals(), hashCode(), toString()) are accounted for.
  • A new RecordComponent entity is introduced to represent individual components of a record.

How Has This Been Tested?

  • Implemented two new test cases:
    • symbolTableShouldHaveRecords(): Verifies that records are correctly stored in the symbol table.
    • symbolTableShouldHaveDefaultRecordComponents(): Ensures default record components are recognized and stored.
  • Added a test application:
    • Path: src/test/resources/test-applications/record-class-test
    • Contains sample records to validate parsing and analysis.
  • Tests pass successfully, confirming that records are now processed correctly.

Breaking Changes

Yes, this is a breaking change:

  1. The analysis.json format has been updated:
    • A new field record_components is now included in Type entity.
  2. This is captured as a new entity RecordComponent that has been introduced:
    package com.ibm.cldk.entities;
    
    import lombok.Data;
    import java.util.ArrayList;
    import java.util.List;
    
    @Data
    public class RecordComponent {
        private String comment;
        private String name;
        private String type;
        private List<String> modifiers;
        private List<String> annotations = new ArrayList<>();
        private Object defaultValue = null; // Stores the string representation of the default value
        private boolean isVarArgs = false;
    }
  3. Users who parse analysis.json will need to update their handling logic to accommodate record_components.

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

This implementation ensures that codeanalyzer-java is compatible with modern Java records (introduced in Java 14, finalized in Java 16). By recognizing records as distinct entities and introducing RecordComponent lombok class, this update improves analysis accuracy for modern Java applications.

Users consuming analysis.json should be aware of the new record_components field and update their deserialization logic accordingly.

Signed-off-by: Rahul Krishna <i.m.ralk@gmail.com>
@rahlk rahlk added enhancement New feature or request kind/feature Feature labels Feb 18, 2025
@rahlk rahlk requested review from sinha108 and rangeetpan February 18, 2025 20:19
@rahlk rahlk self-assigned this Feb 18, 2025
@rahlk rahlk linked an issue Feb 18, 2025 that may be closed by this pull request
Signed-off-by: Rahul Krishna <i.m.ralk@gmail.com>
@rahlk rahlk added the breaking Breaking Change label Feb 18, 2025
@rahlk rahlk merged commit 4b88b85 into main Feb 18, 2025
@rahlk rahlk deleted the 118-support-analysis-of-java-14-record-classes 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.

Support analysis of Java 14+ record classes
1 participant