Skip to content

ClassPath.getTopLevelClasses() does not return a top-level class with $ character #3349

@suztomo

Description

@suztomo

@netdpb and I found that ClassPath.getTopLevelClasses() does not return top-level class with $ character in its name. Example class: com.google.cloud.bigquery.$AutoValue_Labels in google-cloud-bigquery-1.56.0.jar.

Test case

  @Test
  public void testGuavaTopLevelClass() throws Exception {
    // google-cloud-bigquery-1.56.0.jar contains com.google.cloud.bigquery.$AutoValue_Labels
    URL jarFileUrl =
        new URL(
            "file:///home/suztomo/.m2/repository/com/google/cloud/google-cloud-bigquery/1.56.0/google-cloud-bigquery-1.56.0.jar");
    URL[] jarFileUrls = new URL[] {jarFileUrl};

    URLClassLoader classLoaderFromJar = new URLClassLoader(jarFileUrls, null);

    com.google.common.reflect.ClassPath classPath =
        com.google.common.reflect.ClassPath.from(classLoaderFromJar);

    ImmutableList<String> allClassNames =
        classPath.getAllClasses().stream()
            .map(classInfo -> classInfo.getName())
            .collect(toImmutableList());

    // This code does not return class com.google.cloud.bigquery.$AutoValue_Labels
    // Guava only checks the existence of '$'
    // https://github.com/google/guava/blob/master/guava/src/com/google/common/reflect/ClassPath.java#L85
    ImmutableList<String> topLevelClassNames =
        classPath.getTopLevelClasses().stream()
            .map(classInfo -> classInfo.getName())
            .collect(toImmutableList());

    String class$AutoValue_Labels = "com.google.cloud.bigquery.$AutoValue_Labels";
    System.out.println("In all classes? " + allClassNames.contains(class$AutoValue_Labels)); // true
    System.out.println(
        "In top-level classes? " + topLevelClassNames.contains(class$AutoValue_Labels)); // false. It should be true.
  }

JLS 3.8 states that dollar sign ('$') is a valid Java letter.

AutoValue has @Memoized annotation, which creates class $AutoValue_XXXX and AutoValue_XXXX extends $AutoValue_XXXX in the same package.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions