Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

static imports don't work with annotations #861

Open
prabhu-madanagopal opened this issue Jan 20, 2022 · 3 comments
Open

static imports don't work with annotations #861

prabhu-madanagopal opened this issue Jan 20, 2022 · 3 comments

Comments

@prabhu-madanagopal
Copy link

In MethodSpec.Builder, I have...
builder.addAnnotation(Value.Lazy.class);

In JavaFile.Builder, I have..

 JavaFile.builder(..)
                    .addStaticImport(Value.class, "Immutable", "Default", "Lazy");

static import is added, however the annotation contains the fully qualified static name....

    @Value.Lazy
    public Optional<String> get...() {
@JakeWharton
Copy link
Collaborator

Static imports are for static functions and JavaPoet does nothing with them when handling type references. Nested types in Java use regular imports, but JavaPoet will always prefer to import the top-level type and use a semi-qualified reference instead. There is no way to force it that I can remember.

@prabhu-madanagopal
Copy link
Author

generated file contains the static import as below but the type reference in annotations don't reflect the static import, as you said.

import static org.immutables.value.Value.Lazy;
import static org.immutables.value.Value.Immutable;

@JakeWharton
Copy link
Collaborator

Right but static imports are not for types, they're for methods. So that is working as expected.

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

No branches or pull requests

2 participants