Skip to content

Annotation attributes not available at runtime #535

Closed

Description

@dsyer

Spring has a bunch of use cases for annotations and quite a rich set of convenience classes for introspecting them. They don't seem to fail, much, at runtime in a native image, but they don't actually work either.

Example code:

public class Annotations {

	@Autowired(required = false)
	public void setConverters(List<Bar> converters) {
	}

	public static void main(String[] args) throws Exception {
		new Annotations().run();
	}

	private void run() throws Exception {
		Method method = getClass().getMethod("setConverters", List.class);
		Autowired ann = method.getAnnotation(Autowired.class);
		AnnotationAttributes attributes = AnnotatedElementUtils
				.getMergedAnnotationAttributes(method, Autowired.class);
		// Null in native image, {required=false} in normal JDK
		System.err.println(attributes);

		// Boom! Blows up in native image, {required=false} in normal JDK
		// System.err.println(AnnotationUtils.getAnnotationAttributes(ann, true));
	}
}

I have tried to isolate the failure to specific reflective access to the annotation, but haven't been successful. Once (only) I saw some logs from the call to AnnotatedElementUtils:

Jul 09, 2018 8:51:29 AM org.springframework.core.annotation.AnnotationUtils handleIntrospectionFailure
INFO: Failed to introspect annotations on public void Annotations.setConverters(java.util.List): java.lang.IllegalStateException: Could not obtain annotation attribute value for public abstract boolean org.springframework.beans.factory.annotation.Autowired.required()

The fact that I only saw this once suggests there is perhaps another issue lurking to do with log configuration (Spring uses commons-logging which defaults to java.util.logging if nothing else is available).

Activity

  1. cstancu commented on Jul 9, 2018

    @cstancu
    Member

    Thank you for reporting! Do you have a repo that we can use to easily replicate this?

  2. dsyer commented on Jul 9, 2018

    @dsyer
    Author

    The code above will compile and run with just spring-beans (and spring-core which is transitive) on the class path.

  3. dsyer commented on Apr 1, 2019

    @dsyer
    Author

    FWIW I think this is a duplicate of #999.

  4. added
    spring
    on Mar 13, 2020
  5. sbrannen commented on Mar 15, 2020

    @sbrannen

    I just verified that the following (modified version of the sample application) passes in the spring-core native image tests build against GraalVM 20.0.

    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.reflect.Method;
    import java.util.List;
    
    import org.junit.jupiter.api.Test;
    
    import org.springframework.core.annotation.AnnotatedElementUtils;
    import org.springframework.core.annotation.AnnotationAttributes;
    import org.springframework.core.annotation.AnnotationUtils;
    
    import static org.assertj.core.api.Assertions.assertThat;
    import static org.assertj.core.api.Assertions.entry;
    
    public class AnnotationsTests {
    
    	@Test
    	void test() throws Exception {
    		Method method = getClass().getMethod("setConverters", List.class);
    		AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(method, Autowired.class);
    		assertThat(attributes).containsExactly(entry("required", false));
    
    		Autowired ann = method.getAnnotation(Autowired.class);
    		assertThat(AnnotationUtils.getAnnotationAttributes(ann, true)).containsExactly(entry("required", false));
    	}
    
    	@Autowired(required = false)
    	public void setConverters(List<Bar> converters) {
    	}
    
    
    	@Retention(RetentionPolicy.RUNTIME)
    	@interface Autowired {
    
    		boolean required() default true;
    	}
    
    	static class Bar {
    	}
    
    }

    Thus, feel free to close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

native-imagespring

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions