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

Bug: Incorrect field count in Java source file when multiple classes have the same field name #1396

Open
noahd1 opened this issue Jan 3, 2025 · 1 comment

Comments

@noahd1
Copy link
Member

noahd1 commented Jan 3, 2025

What happened?

With a Java source file defined as follows (note that both classes define foo and bar as fields):

class BooleanLogic {
    int foo;
    int bar;
}

class BooleanLogic1 {
    boolean foo;
    boolean bar;
    boolean baz;
    boolean qux;
}

Running qlty metrics --all reports only 4 fields even though 6 fields are present.

 name           | classes | funcs | fields | cyclo | complex | LCOM | lines | LOC
----------------+---------+-------+--------+-------+---------+------+-------+-----
 SomeClass.java |       2 |     0 |      4 |     1 |       0 |    0 |    11 |  10
 TOTAL          |       2 |     0 |      4 |     1 |       0 |    0 |    11 |  10

Changing the source file to one with 6 uniquely named fields across the classes:

class BooleanLogic {
    int zee;
    int zoo;
}

class BooleanLogic1 {
    boolean foo;
    boolean bar;
    boolean baz;
    boolean qux;
}

correctly reports 6 fields.

 name           | classes | funcs | fields | cyclo | complex | LCOM | lines | LOC
----------------+---------+-------+--------+-------+---------+------+-------+-----
 SomeClass.java |       2 |     0 |      6 |     1 |       0 |    0 |    11 |  10
 TOTAL          |       2 |     0 |      6 |     1 |       0 |    0 |    11 |  10

I'm unsure if this bug is restricted to Java and the "fields" count metric. In Java, a source file can only have 1 public class defined in it with the same name as the file. However, it is possible to define more than 1 class in the file if the other classes are not public. Unsure if this is common. If not, and if restricted to Java, this is a relatively minor bug.

What did you expect to happen?

Count all fields regardless of name

Can you reproduce the problem?

Yes, consistently

CLI Version

qlty 0.466.0 macos-arm64 (354ff80 debug 2025-01-03)

Relevant log output

See issue description
@noahd1
Copy link
Member Author

noahd1 commented Jan 3, 2025

Note that this behavior / bug is actually encoded in a test: https://github.com/qltysh/qlty/blob/main/qlty-cli/tests/lang/java/basic.in/BooleanLogic.java which only counts 4 fields instead of 6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant