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

[javasrc2cpg] Populate generic signatures roughly following the class file signature format #5274

Merged
merged 6 commits into from
Jan 31, 2025

Conversation

johannescoetzee
Copy link
Contributor

@johannescoetzee johannescoetzee commented Jan 30, 2025

The generic signatures added by this PR follow the format described in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.9.1 and largely correspond to the generic signatures in class files with some notable differences:

Class type signatures

  • In most cases, only the simple name for the class will be used (so LString; will be used instead of Ljava/lang/String)
  • Where a qualified name is used in source, that name is used verbatim in the signature, for example Ljava.util.List (note the . were not substituted for /.
  • For local classes, the name of the class as it appears in the CPG is used in the signature for instances of that class (we don't follow the JVM naming scheme for these), for example Ltestpackage.TestClass.testMethod.LocalClass;

Type parameter bounds

From the language specification:

TypeParameter:
  Identifier ClassBound {InterfaceBound}

ClassBound:
 : [ReferenceTypeSignature]

InterfaceBound:
  : ReferenceTypeSignature

If a type parameter only has interface bounds I1, I2, ..., then the signature should contain <T::LI1;:...> (note the empty class bound), but in general we won't know if a type is a class or interface without resolving the it, so the signature in the CPG will contain <T:LI1;:...> instead.

Unspecified types

Where no type name is specified, the special L__unspecified_type; type is used in generic signatures. This happens in a few places:

  • For lambda return types and lambda parameters which do not have explicit type annotations
  • For locals with a var type, for example var x = 42
  • For synthetic locals created for foreach loops, for example in for (String item : items()), we create a temporary String[] $iterLocal0 = items() local which will have an unspecified signature (item will still have the signature LString; as expected)
  • For synthetic locals created for the LHS of instanceof expressions with pattern matching, for example in foo() instanceof String s, we create an Object o = foo() local (since the type depends on the return type of foo).

Copy link
Contributor

@ml86 ml86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the description from the PR to into the GenericSignatureTests file so that we are aware of the deviations.

}

"have an empty generic signature for the lambda type decl" in {
cpg.typeDecl.name(".*lambda.*").genericSignature.l shouldBe List("<empty>")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isnt a single L__unspecified_type more appropriate?

@johannescoetzee johannescoetzee merged commit b02caa7 into master Jan 31, 2025
5 checks passed
@johannescoetzee johannescoetzee deleted the johannes/generic-signature branch January 31, 2025 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants