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

Difficulty excluding some generated code #690

Open
ansman opened this issue Oct 3, 2024 · 6 comments
Open

Difficulty excluding some generated code #690

ansman opened this issue Oct 3, 2024 · 6 comments
Assignees
Labels
Bug Bug issue type S: waiting for clarification Status: additional information required to proceed

Comments

@ansman
Copy link

ansman commented Oct 3, 2024

Describe the bug
Dagger generates code that looks like this:

@ScopeMetadata
@QualifierMetadata
@DaggerGenerated
@Generated(
    value = "dagger.internal.codegen.ComponentProcessor",
    comments = "https://dagger.dev"
)
@SuppressWarnings({
    "unchecked",
    "rawtypes",
    "KotlinInternal",
    "KotlinInternalInJava",
    "cast",
    "deprecation"
})
public final class ElapsedRealTimeClockImpl_Factory implements Factory<ElapsedRealTimeClockImpl> {
  @Override
  public ElapsedRealTimeClockImpl get() {
    return newInstance();
  }

  public static ElapsedRealTimeClockImpl_Factory create() {
    return InstanceHolder.INSTANCE;
  }

  public static ElapsedRealTimeClockImpl newInstance() {
    return new ElapsedRealTimeClockImpl();
  }

  private static final class InstanceHolder {
    private static final ElapsedRealTimeClockImpl_Factory INSTANCE = new ElapsedRealTimeClockImpl_Factory();
  }
}

We exclude this by excluding classes annotated by @Generated and @DaggerGenerated. The issue is the inner InstanceHolder class which isn't annotated.

Expected behavior
I'm not sure I'd honestly expect it to exclude this class since it's not annotated, but most code generation frameworks will only annotate the top class and most people will want to exclude the inner class too so there should be some mechanism for excluding these.

Reports
image

Environment

  • Kover Gradle Plugin version: 0.9.0-RC
  • Gradle version: 8.10.2
  • Kotlin project type: Android
  • Coverage Toolset: Kover
  • Other context important for this bug: macOS 14.7
@ansman ansman added Bug Bug issue type S: untriaged Status: issue reported but unprocessed labels Oct 3, 2024
@NinoDLC
Copy link
Contributor

NinoDLC commented Oct 10, 2024

Unfortunately they don't want to spread the exclusion of annotated classes to their inner classes.

#331

That being said, I have exactly the same issue, and even more "conventional" way of excluding this nasty inner class InstanceHolder won't work. I tried everything I could think of, this ain't moving from the report.

kover {
    reports {
        filters {
            excludes {
                ...
                packages(
                    "InstanceHolder",
                    "*InstanceHolder",
                    "\$*InstanceHolder",
                    "*\$*InstanceHolder",
                    "*\$InstanceHolder",
                    "*ProvideFactory\$InstanceHolder",
                )
                ...
            }
        }
    }
}

Coverage:
image

JADX bytecode inspection:
image

@ansman
Copy link
Author

ansman commented Oct 10, 2024

You can exclude them by name:

excludedClasses.addAll(
    "*\$InstanceHolder",
    "ComposableSingletons*",
    "Hilt_*",
    "*\$DefaultImpls"
)

@ansman
Copy link
Author

ansman commented Oct 10, 2024

Also, like I mentioned in my description I agree that excluding annotated classes probably shouldn't exclude inner classes if they're not annotated but I do think kover need to add a mechanism for excluding based on parent annotation to allow excluding these types of files

@NinoDLC
Copy link
Contributor

NinoDLC commented Oct 10, 2024

Oh yes I was confused between classes and packages 🙈

@shanshin
Copy link
Collaborator

Hi,
the problem with the additional filter will be that it is needed in very rare cases, it will confuse users who do not need it, and when you add it, report generation will slow down significantly - because it will be necessary for any class to scan its containing class for annotations.

Could you give an example of nested classes other than InstanceHolder that are not solved by specifying the excludes.classes("*\$NestedName") filter?

@shanshin shanshin added S: waiting for clarification Status: additional information required to proceed and removed S: untriaged Status: issue reported but unprocessed labels Oct 14, 2024
@beigirad
Copy link

I ignored all hilt classes by these lines (until better solution):

packages(
    "dagger.hilt.internal.aggregatedroot.codegen",
    "hilt_aggregated_deps",
)
annotatedBy(
    "javax.annotation.processing.Generated",
    "dagger.internal.DaggerGenerated",
    "dagger.hilt.android.internal.lifecycle.HiltViewModelMap\$KeySet",
)
classes(
    "*\$InstanceHolder",
    "*Hilt_*",
    "*BindsModule*",
    "*\$DefaultImpls"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug issue type S: waiting for clarification Status: additional information required to proceed
Projects
None yet
Development

No branches or pull requests

4 participants