Class AttributeValue

java.lang.Object
org.openrewrite.java.trait.AttributeValue
All Implemented Interfaces:
org.openrewrite.trait.Trait<Expression>

@Incubating(since="8.87.0") public class AttributeValue extends Object implements org.openrewrite.trait.Trait<Expression>
The value of an annotation attribute (a JLS §9.7.1 "element value"), in any of the syntactic shapes annotations permit — not only the plain literals covered by Literal: Classification is syntax-first and never throws on missing type attribution; only the distinction between enum constants and other constant references requires attribution (Flag.Enum on the referenced JavaType.Variable). Unattributed references degrade to AttributeValue.Kind.CONSTANT_REFERENCE with a null getReferencedField(). The same degradation applies on Groovy and Kotlin sources, whose type mappings do not set Flag.Enum: there enum constants always classify as AttributeValue.Kind.CONSTANT_REFERENCE. Groovy and Kotlin list literals are not J.NewArray and classify as AttributeValue.Kind.EXPRESSION without element normalization.

The trait is cursor-bearing: Trait.getTree() / Trait.getCursor() give recipes the exact expression to edit, and for arrays getElements() yields one cursor-bearing value per element. Parentheses are transparent for classification and value access; Trait.getTree() still returns the original expression.

Optional.empty() from Annotated.getAttributeValue(String) means only that the attribute is not explicitly present in the source. A present value whose typed accessors return null means "present but not resolvable that way" — the two conditions the Optional<Literal> accessors used to conflate.

See Also:
  • Constructor Details

    • AttributeValue

      public AttributeValue()
  • Method Details

    • getKind

      public AttributeValue.Kind getKind()
    • isLiteral

      public boolean isLiteral()
    • isClassLiteral

      public boolean isClassLiteral()
    • isClassLiteral

      public boolean isClassLiteral(String fullyQualifiedName)
      Parameters:
      fullyQualifiedName - The fully qualified name of a reference type.
      Returns:
      true if this value is a class literal referencing the given type. Requires type attribution; inner class $ and . spellings are considered equal. Primitive and array class literals never match.
    • isEnumConstant

      public boolean isEnumConstant(String fullyQualifiedEnumName, String constantName)
      Parameters:
      fullyQualifiedEnumName - The fully qualified name of the enum type.
      constantName - The name of the enum constant.
      Returns:
      true if this value is the given enum constant, e.g. isEnumConstant("java.lang.annotation.RetentionPolicy", "RUNTIME"). Covers the qualified, fully qualified, and statically imported spellings uniformly.
    • isConstantReference

      public boolean isConstantReference()
    • isNestedAnnotation

      public boolean isNestedAnnotation()
    • isArray

      public boolean isArray()
      Returns:
      true if this value is written as an explicit array initializer (J.NewArray). The brace-less single-element form @Foo(exclude = A.class) of an array-typed attribute is NOT syntactically an array; use getElements() for uniformly normalized element access.
    • getName

      public String getName()
      Returns:
      the name of the annotation attribute this value is assigned to, or "value" for a positional (implicit) value. Array elements report the name of their containing attribute.
    • getElements

      public List<AttributeValue> getElements()
      The values of this attribute normalized to a list: an array initializer yields one AttributeValue per element, each with its own cursor, so that both reads and surgical edits can address individual elements; every scalar shape yields singletonList(this). This mirrors the normalization the JLS applies to array-typed attributes, where the brace-less single-element form @Foo(exclude = A.class) means {A.class} — both forms iterate identically here. An empty initializer {} yields an empty list.
      Returns:
      the individual values of this attribute.
    • asLiteral

      public Optional<Literal> asLiteral()
      Returns:
      this value as the Literal trait when it is a J.Literal or an array composed entirely of literals — exactly the shapes Literal.Matcher matches. Unlike the Optional<Literal> accessors on Annotated, this sees through parentheses. Note this answers "is it Literal-compatible", which differs from isLiteral() for all-literal arrays.
    • asAnnotated

      public Optional<Annotated> asAnnotated()
      Returns:
      this value as the Annotated trait when it is a nested annotation, enabling recursive attribute access.
    • getClassValue

      public @Nullable JavaType getClassValue()
      Returns:
      for a class literal, the referenced type: a JavaType.FullyQualified for A.class, a JavaType.Primitive for int.class, a JavaType.Array for String[].class. null when this value is not a class literal or type attribution is missing.
    • getReferencedField

      public @Nullable JavaType.Variable getReferencedField()
      Returns:
      the static field referenced by this value when it is an enum constant or a constant reference: the variable's owner is the declaring type and its name the constant's name — identical across the qualified, fully qualified, and statically imported spellings. Distinguish enums via isEnumConstant(String, String). null when this value is not a reference or attribution is missing; class literals return null here (their synthetic Variable{name="class"} is deliberately hidden — use getClassValue()).
    • getConstantValue

      public @Nullable Object getConstantValue()
      The compile-time constant represented by this value, if one can be determined.
      Returns:
      the constant value, or null when this value does not represent a determinable constant.
    • getValue

      public <T> @Nullable T getValue(Class<T> type)
      getConstantValue() coerced with Jackson, using the same mapper and scalar-to-collection normalization as Literal.getValue(Class).
      Returns:
      the coerced constant value, or null when unresolvable or not coercible.
    • getValue

      public <T> @Nullable T getValue(com.fasterxml.jackson.core.type.TypeReference<T> type)
      See Also: