How to add annotation to a wildcard type

i want to write an value extractor to support validation scala list item by jakarta validation
the value extractor is defined as

public interface ValueExtractor<T> {

	/**
	 * Extracts the values to validate from the original object.
	 *
	 * @param originalValue the original value from which to extract the values, never
	 * {@code null}
	 * @param receiver the corresponding {@code ValueReceiver}
	 */
	void extractValues(T originalValue, ValueReceiver receiver);

	/**
	 * Provides a set of methods receiving value extracted by the {@link ValueExtractor}.
	 * <p>
	 * The value has to be passed to the method corresponding best to the type of the
	 * original value.
	 *
	 * @since 2.0
	 */
	interface ValueReceiver {

		/**
		 * Receives the value extracted from an object.
		 *
		 * @param nodeName the name of the node representing the container element. If not
		 * {@code null}, the name will be used when adding a container element node to the
		 * {@link Path}
		 * @param object the value to validate
		 */
		void value(String nodeName, Object object);

		/**
		 * Receives the value extracted from an iterable object that is not indexed (e.g.
		 * a {@link Iterable}, {@link Set} or a {@link Map}).
		 *
		 * @param nodeName the name of the node representing the container element. If not
		 * {@code null}, the name will be used when adding a container element node to the
		 * {@link Path}
		 * @param object the value to validate
		 */
		void iterableValue(String nodeName, Object object);

		/**
		 * Receives the value extracted from an indexed object (e.g. a {@link List}).
		 *
		 * @param nodeName the name of the node representing the container element. If not
		 * {@code null}, the name will be used when adding a container element node to the
		 * {@link Path}
		 * @param i the index of the value in the original object
		 * @param object the value to validate
		 */
		void indexedValue(String nodeName, int i, Object object);

		/**
		 * Receives the value extracted from a keyed object (e.g. a {@link Map}).
		 *
		 * @param nodeName the name of the node representing the container element. If not
		 * {@code null}, the name will be used when adding a container element node to the
		 * {@link Path}
		 * @param key the key of the value in the original object
		 * @param object the value to validate
		 */
		void keyedValue(String nodeName, Object key, Object object);
	}
}

/**
 * Marks the type parameter of a generic container type to which a {@link ValueExtractor} is
 * tied or specifies the type of the wrapped element(s) of non-generic container types.
 * <p>
 * Must be given exactly once for a value extractor type.
 *
 * @author Gunnar Morling
 * @author Guillaume Smet
 *
 * @see ValueExtractor
 * @since 2.0
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@Documented
public @interface ExtractedValue {

	/**
	 * The type of the value extracted by the {@link ValueExtractor}. If not set, the type
	 * will be automatically inferred from the type argument of the parameterized type.
	 * <p>
	 * Used to define value extractors for non-generic wrapper types e.g.
	 * {@link OptionalInt}.
	 * <p>
	 * May not be used when {@code ExtractedValue} is defined on the type parameter of
	 * a generic container type. A {@code ValueExtractorDefinitionException} will be thrown
	 * in this case.
	 *
	 * @return the type of the value extracted by the value extractor
	 */
	Class<?> type() default void.class;
}

and the java list value extractor is


how can i do the same thing with scala, my scala is 2.13, i have try to upgrade to scala 3.3.1 but it was broken by thousand of errors and some part can’t upgrade to scala 3

You could define the extractor in Java, then the annotations would be the same as you do in the example you posted. Scala collection convertors can help also with iterator syntax.

Otherwise you can write a type like this

class ListExtractor extends ValueExtractor[List[_ @ExtractedValue]] {
  ...
}

where [_ @ExtractedValue] is equivalent to <@ExtractedValue ?> in Java. So Scala uses _ to mean a wildcard type argument

Do most of these errors make sense? have you considered opening an issue if something seems unable to be fixed by you

thanks very much, it works on 2.13.11 but not on 3.3.1, the ide tip an error, but compile

the code compiled, but it raise an error when startup


the annotation is mising

the parameter type arg become object without annotation

the assert failed. object not a WildcardType

the code check the type argument is a wildcardType and have the annotation ExtractedValue


none of this match the check

If the code compiled, then it’s probably an issue with IntelliJ’s Scala 3 plugin.