question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add validation of cyclic inheritance for declared types

See original GitHub issue

Currently, no validation of this code, where type X extends Y, Y extends Z and Z extends X.

interface X extends Y {}
interface Y extends Z {}
interface Z extends X {}

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
snarkipuscommented, Dec 19, 2022

@pluralia Sure, I’d be happy to - it will just take me a few days. I’m not sure that @msujew had a PR from me on his Christmas list. 😃

2reactions
snarkipuscommented, Dec 16, 2022

I implemented something similar to this when I was attempting to learn how to use Langium.

Helper Functions
export function classHierarchy(c: SJClass): Set<Reference<SJClass>> {
    let visited = new Set<Reference<SJClass>>();
    let current = c.superClass;
    while (current != undefined && !visited.has(current)) {
        visited.add(current);
        current = current.ref?.superClass;
    }
    return visited;
}

export function classHierarchyMethods(c: SJClass): Map<string, SJMethod> {
        let returnMap = new Map<string, SJMethod>();

        [...classHierarchy(c)]
            .reverse()
            .flatMap(r => r.ref?.members as SJMember[])
            .forEach(m => returnMap.set(m.name, m as SJMethod));

        return returnMap;
}

export function classHierarchyMembers(c: SJClass): Array<SJMember> {
    return [...classHierarchy(c)]
                .map(r => r.ref)
                .map(m => m?.members as SJMember[])
                .flat();
}
Validator
    checkClassHierarchy(c: SJClass, accept: ValidationAcceptor): void {
        const hierarchy = classHierarchy(c);
        hierarchy.forEach(ref => {
            if (ref.ref?.name === c.name) {
                accept(
                    'error',
                    "cycle in hierarchy of class '" + c.name + "'",
                    {
                        node: c,
                        property: 'name',
                        code: IssueCodes.ClassHierarchy
                    }
                );
            }
        })
    }
Test Code
describe('Small Java Validator: Valid Hierarchy', () => {
    const text=`
    class A extends C {}
    class C extends B {}
    class B {}
    `;

    let validationResult: ValidationResult<SJProgram>;

    beforeAll(async () => {
        validationResult = await validate(text);
    });

    it('Should detect no cycle hierarchies', () => {
        expectNoIssues(validationResult);
    });
});

describe('Small Java Validator: Hierarchy Cycles', () => {
    const text=`
    class A extends C {}
    class C extends B {}
    class B extends A {}
    `;

    let validationResult: ValidationResult<SJProgram>;

    beforeAll(async () => {
        validationResult = await validate(text);
    });

    it('Should detect cycle hierarchies', () => {
        const rule = validationResult.document.parseResult.value.classes;
        for (let i = 0; i < 3; i++) {
            expectError(validationResult, "cycle in hierarchy of class '" + rule[i].name + "'",
            {
                node: rule[i],
                property: {
                    name: 'name'
                }
            });
        }
    });
});

Not sure how helpful this is since it was married to my own nascent type computation implementation, but it worked for what I was attempting to do. Hopefully this saves you some time. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Circular Inheritance for validation - Stack Overflow
I want to build an object model that automatically wires in validation when I attempt to save an object. I am using DataAnnotations...
Read more >
Hibernate Validator 5.2.5.Final - JBoss.org
Constraint inheritance. When a class implements an interface or extends another class, all constraint annotations declared on the super-type apply in the same ......
Read more >
[JDK-8057651] Different error message should be generated if cyclic ...
JDK-8057651Different error message should be generated if cyclic inheritance logic involves Object class ; JDK-8047379Inference of formal type parameter (unused ...
Read more >
Inheritance Relationship - an overview | ScienceDirect Topics
When we create a specialized class, it inherits all the attributes, operations and relationships of the parent class. In Figure 4.14, the specialized...
Read more >
packaging a java plugin as in the doc fails with "illegal cyclic ...
I start with the basic example with hello.jar from the doc, and use openmole 12 snapshot. ... I tried with other machines and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found