Skip to content

How to add an annotation with valid scoping construct on return type? #1845

@adamretter

Description

@adamretter

I am trying to create a plugin which adds JSpecify annotations like org.jspecify.annotations.NonNull and @link org.jspecify.annotations.Nullable.

These annotations are more precise than the JSR 305 annotations about where they may be placed.

The vanilla code generated by JAXB 4.0.5 in my ObjectFactory looks like:

public Catalog.TestSet createCatalogTestSet() {
    return new Catalog.TestSet();
}

Previously, with JSR 305 you could write:

@javax.annotation.Nonnull
public Catalog.TestSet createCatalogTestSet() {
  return new Catalog.TestSet();
}

I could generate that just fine in my JAXB plugin by calling: method.annotate(javax.annotation.Nonnull.class) where method is an instance of com.sun.codemodel.JMethod.

However, with JSpecify you need to write:

public Catalog.@org.jspecify.annotations.NonNull TestSet createCatalogTestSet() {
  return new Catalog.TestSet();
}

Calling method.annotate(org.jspecify.annotations.NonNull.class) from my plugin generates the incorrect code:

@org.jspecify.annotations.NonNull 
public Catalog.TestSet createCatalogTestSet() {
  return new Catalog.TestSet();
}

Instead of method.annotate(org.jspecify.annotations.NonNull.class), I tried calling the following in my plugin:

final JType returnType = method.type();
((JDefinedClass) returnType).annotate(org.jspecify.annotations.NonNull.class)

but when I do, I get no annotation at all in the generated code. I can't figure out how to call the appropriate codemodel method to get the annotation into the correct place. How can I do that please?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions