1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -43,55 +43,45 @@ class AnnotationIntrospectionFailureTests {
43
43
44
44
@ Test
45
45
void filteredTypeThrowsTypeNotPresentException () throws Exception {
46
- FilteringClassLoader classLoader = new FilteringClassLoader (
47
- getClass ().getClassLoader ());
48
- Class <?> withExampleAnnotation = ClassUtils .forName (
49
- WithExampleAnnotation .class .getName (), classLoader );
50
- Annotation annotation = withExampleAnnotation .getAnnotations ()[0 ];
46
+ FilteringClassLoader classLoader = new FilteringClassLoader (getClass ().getClassLoader ());
47
+ Class <?> withAnnotation = ClassUtils .forName (WithExampleAnnotation .class .getName (), classLoader );
48
+ Annotation annotation = withAnnotation .getAnnotations ()[0 ];
51
49
Method method = annotation .annotationType ().getMethod ("value" );
52
50
method .setAccessible (true );
53
- assertThatExceptionOfType (TypeNotPresentException .class ). isThrownBy (() ->
54
- ReflectionUtils .invokeMethod (method , annotation ))
55
- .withCauseInstanceOf (ClassNotFoundException .class );
51
+ assertThatExceptionOfType (TypeNotPresentException .class )
52
+ . isThrownBy (() -> ReflectionUtils .invokeMethod (method , annotation ))
53
+ .withCauseInstanceOf (ClassNotFoundException .class );
56
54
}
57
55
58
56
@ Test
59
57
@ SuppressWarnings ("unchecked" )
60
58
void filteredTypeInMetaAnnotationWhenUsingAnnotatedElementUtilsHandlesException () throws Exception {
61
- FilteringClassLoader classLoader = new FilteringClassLoader (
62
- getClass ().getClassLoader ());
63
- Class <?> withExampleMetaAnnotation = ClassUtils .forName (
64
- WithExampleMetaAnnotation .class .getName (), classLoader );
65
- Class <Annotation > exampleAnnotationClass = (Class <Annotation >) ClassUtils .forName (
66
- ExampleAnnotation .class .getName (), classLoader );
67
- Class <Annotation > exampleMetaAnnotationClass = (Class <Annotation >) ClassUtils .forName (
68
- ExampleMetaAnnotation .class .getName (), classLoader );
69
- assertThat (AnnotatedElementUtils .getMergedAnnotationAttributes (
70
- withExampleMetaAnnotation , exampleAnnotationClass )).isNull ();
71
- assertThat (AnnotatedElementUtils .getMergedAnnotationAttributes (
72
- withExampleMetaAnnotation , exampleMetaAnnotationClass )).isNull ();
73
- assertThat (AnnotatedElementUtils .hasAnnotation (withExampleMetaAnnotation ,
74
- exampleAnnotationClass )).isFalse ();
75
- assertThat (AnnotatedElementUtils .hasAnnotation (withExampleMetaAnnotation ,
76
- exampleMetaAnnotationClass )).isFalse ();
59
+ FilteringClassLoader classLoader = new FilteringClassLoader (getClass ().getClassLoader ());
60
+ Class <?> withAnnotation = ClassUtils .forName (WithExampleMetaAnnotation .class .getName (), classLoader );
61
+ Class <Annotation > annotationClass = (Class <Annotation >)
62
+ ClassUtils .forName (ExampleAnnotation .class .getName (), classLoader );
63
+ Class <Annotation > metaAnnotationClass = (Class <Annotation >)
64
+ ClassUtils .forName (ExampleMetaAnnotation .class .getName (), classLoader );
65
+ assertThat (AnnotatedElementUtils .getMergedAnnotationAttributes (withAnnotation , annotationClass )).isNull ();
66
+ assertThat (AnnotatedElementUtils .getMergedAnnotationAttributes (withAnnotation , metaAnnotationClass )).isNull ();
67
+ assertThat (AnnotatedElementUtils .hasAnnotation (withAnnotation , annotationClass )).isFalse ();
68
+ assertThat (AnnotatedElementUtils .hasAnnotation (withAnnotation , metaAnnotationClass )).isFalse ();
77
69
}
78
70
79
71
@ Test
80
72
@ SuppressWarnings ("unchecked" )
81
73
void filteredTypeInMetaAnnotationWhenUsingMergedAnnotationsHandlesException () throws Exception {
82
- FilteringClassLoader classLoader = new FilteringClassLoader (
83
- getClass ().getClassLoader ());
84
- Class <?> withExampleMetaAnnotation = ClassUtils .forName (
85
- WithExampleMetaAnnotation .class .getName (), classLoader );
86
- Class <Annotation > exampleAnnotationClass = (Class <Annotation >) ClassUtils .forName (
87
- ExampleAnnotation .class .getName (), classLoader );
88
- Class <Annotation > exampleMetaAnnotationClass = (Class <Annotation >) ClassUtils .forName (
89
- ExampleMetaAnnotation .class .getName (), classLoader );
90
- MergedAnnotations annotations = MergedAnnotations .from (withExampleMetaAnnotation );
91
- assertThat (annotations .get (exampleAnnotationClass ).isPresent ()).isFalse ();
92
- assertThat (annotations .get (exampleMetaAnnotationClass ).isPresent ()).isFalse ();
93
- assertThat (annotations .isPresent (exampleMetaAnnotationClass )).isFalse ();
94
- assertThat (annotations .isPresent (exampleAnnotationClass )).isFalse ();
74
+ FilteringClassLoader classLoader = new FilteringClassLoader (getClass ().getClassLoader ());
75
+ Class <?> withAnnotation = ClassUtils .forName (WithExampleMetaAnnotation .class .getName (), classLoader );
76
+ Class <Annotation > annotationClass = (Class <Annotation >)
77
+ ClassUtils .forName (ExampleAnnotation .class .getName (), classLoader );
78
+ Class <Annotation > metaAnnotationClass = (Class <Annotation >)
79
+ ClassUtils .forName (ExampleMetaAnnotation .class .getName (), classLoader );
80
+ MergedAnnotations annotations = MergedAnnotations .from (withAnnotation );
81
+ assertThat (annotations .get (annotationClass ).isPresent ()).isFalse ();
82
+ assertThat (annotations .get (metaAnnotationClass ).isPresent ()).isFalse ();
83
+ assertThat (annotations .isPresent (metaAnnotationClass )).isFalse ();
84
+ assertThat (annotations .isPresent (annotationClass )).isFalse ();
95
85
}
96
86
97
87
@@ -103,17 +93,16 @@ static class FilteringClassLoader extends OverridingClassLoader {
103
93
104
94
@ Override
105
95
protected boolean isEligibleForOverriding (String className ) {
106
- return className .startsWith (
107
- AnnotationIntrospectionFailureTests . class . getName () );
96
+ return className .startsWith (AnnotationIntrospectionFailureTests . class . getName ()) ||
97
+ className . startsWith ( "jdk.internal" );
108
98
}
109
99
110
100
@ Override
111
- protected Class <?> loadClass (String name , boolean resolve ) throws ClassNotFoundException {
112
- if (name .startsWith (AnnotationIntrospectionFailureTests .class .getName ()) &&
113
- name .contains ("Filtered" )) {
101
+ protected Class <?> loadClassForOverriding (String name ) throws ClassNotFoundException {
102
+ if (name .contains ("Filtered" ) || name .startsWith ("jdk.internal" )) {
114
103
throw new ClassNotFoundException (name );
115
104
}
116
- return super .loadClass (name , resolve );
105
+ return super .loadClassForOverriding (name );
117
106
}
118
107
}
119
108
0 commit comments