Skip to content

Java: PrimitiveType.getADefaultValue() is misleading and might not work correctly #6615

Open
@Marcono1234

Description

@Marcono1234

There are several issues with PrimitiveType.getADefaultValue():

  • The documentation says:

    Gets a default value for this primitive type, as assigned by the compiler for variables that are declared but not initialized explicitly.

    This is misleading; unless the source actually contains literals with the default value, the predicate will have no result (except for some BooleanLiterals which are found in the JDK code as part of annotations). For example for this Java code:

    class DefaultLiteral {
        byte b;
        short s;
        int i;
        long l;
        float f;
        double d;
        boolean bool;
        char c;
        
        Object o;
    }

    the following query only finds BooleanLiterals:

    import java
    
    from PrimitiveType t, Literal defaultValue
    where
    defaultValue = t.getADefaultValue()
    select t, defaultValue, defaultValue.getLocation()
  • The way it matches literals by using getLiteral() misses some cases where getValue() would actually represent the default value (relates to Java: Replace incorrect usage of Literal.getLiteral() #6612).

Would it make sense to deprecate the predicate (for removal) and instead adjust the only query using it (DeadStoreOfLocal.ql)?
It appears that query actually wants to find out if an assigned literal has a default value; so maybe it should be rewritten to explicitly test that. (Note that adding a predicate Literal.hasDefaultValue() might not make much sense because technically for StringLiterals type String the default value is null; and NullLiteral has no type which could occur as field type.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    JavaacknowledgedGitHub staff acknowledges this issuequestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions