New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JDK-8267936: PreserveAllAnnotations option doesn't expose the annotation to Java code #4245
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e1aa800
Expose runtime invisible annotations via Class.getAnnotation when -XX…
699861e
Only expose non RetentionPolicy.RUNTIME annotations when -XX:+Preserv…
0f689ea
Test long time existing behavior of -XX:+PreserveAllAnnotations
0fd2fce
Merging with test for long time existing behavior of -XX:+PreserveAll…
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
test/jdk/java/lang/annotation/AnnotationType/AnnotationTypeChangedToRuntimeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/* | ||
* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
/* | ||
* @test | ||
* @summary Expose annotations made RUNTIME visible via Class.getAnnotation when -XX:+PreserveAllAnnotations is on | ||
* @run main/othervm -XX:+PreserveAllAnnotations AnnotationTypeChangedToRuntimeTest | ||
*/ | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
import static java.lang.annotation.RetentionPolicy.CLASS; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
/** | ||
* This test simulates a situation where an existing annotation with | ||
* {@link RetentionPolicy#CLASS CLASS} retention was changed into | ||
* {@link RetentionPolicy#RUNTIME RUNTIME} and we want to read | ||
* its value during runtime with the help of | ||
* -XX:+PreserveAllAnnotations option | ||
*/ | ||
public class AnnotationTypeChangedToRuntimeTest { | ||
|
||
@Retention(CLASS) | ||
@AnnB | ||
public @interface AnnA_v1 { | ||
} | ||
|
||
// An alternative version of AnnA_v1 with CLASS retention instead. | ||
// Used to simulate separate compilation (see AltClassLoader below). | ||
@Retention(RUNTIME) | ||
@AnnB | ||
public @interface AnnA_v2 { | ||
} | ||
|
||
@Retention(RUNTIME) | ||
@AnnA_v1 | ||
public @interface AnnB { | ||
} | ||
|
||
@AnnA_v1 | ||
public static class TestTask implements Runnable { | ||
@Override | ||
public void run() { | ||
AnnA_v1 ann1 = TestTask.class.getDeclaredAnnotation(AnnA_v1.class); | ||
if (ann1 == null) { | ||
throw new IllegalStateException( | ||
"@" + AnnA_v1.class.getSimpleName() + | ||
" should be visible at runtime"); | ||
} | ||
AnnA_v1 ann2 = AnnB.class.getDeclaredAnnotation(AnnA_v1.class); | ||
if (ann2 != null) { | ||
throw new IllegalStateException( | ||
"@" + ann2.annotationType().getSimpleName() + | ||
" found on: " + AnnB.class.getName() + | ||
" should not be visible at runtime"); | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
ClassLoader altLoader = new AltClassLoader( | ||
AnnotationTypeChangedToRuntimeTest.class.getClassLoader()); | ||
|
||
Runnable altTask = (Runnable) Class.forName( | ||
TestTask.class.getName(), | ||
true, | ||
altLoader).newInstance(); | ||
|
||
altTask.run(); | ||
} | ||
|
||
/** | ||
* A ClassLoader implementation that loads alternative implementations of | ||
* classes. If class name ends with "_v1" it locates instead a class with | ||
* name ending with "_v2" and loads that class instead. | ||
*/ | ||
static class AltClassLoader extends ClassLoader { | ||
AltClassLoader(ClassLoader parent) { | ||
super(parent); | ||
} | ||
|
||
@Override | ||
protected Class<?> loadClass(String name, boolean resolve) | ||
throws ClassNotFoundException { | ||
if (name.indexOf('.') < 0) { // root package is our class | ||
synchronized (getClassLoadingLock(name)) { | ||
// First, check if the class has already been loaded | ||
Class<?> c = findLoadedClass(name); | ||
if (c == null) { | ||
c = findClass(name); | ||
} | ||
if (resolve) { | ||
resolveClass(c); | ||
} | ||
return c; | ||
} | ||
} | ||
else { // not our class | ||
return super.loadClass(name, resolve); | ||
} | ||
} | ||
|
||
@Override | ||
protected Class<?> findClass(String name) | ||
throws ClassNotFoundException { | ||
// special class name -> replace it with alternative name | ||
if (name.endsWith("_v1")) { | ||
String altName = name.substring(0, name.length() - 3) + "_v2"; | ||
String altPath = altName.replace('.', '/').concat(".class"); | ||
try (InputStream is = getResourceAsStream(altPath)) { | ||
if (is != null) { | ||
byte[] bytes = is.readAllBytes(); | ||
// patch class bytes to contain original name | ||
for (int i = 0; i < bytes.length - 2; i++) { | ||
if (bytes[i] == '_' && | ||
bytes[i + 1] == 'v' && | ||
bytes[i + 2] == '2') { | ||
bytes[i + 2] = '1'; | ||
} | ||
} | ||
return defineClass(name, bytes, 0, bytes.length); | ||
} | ||
else { | ||
throw new ClassNotFoundException(name); | ||
} | ||
} | ||
catch (IOException e) { | ||
throw new ClassNotFoundException(name, e); | ||
} | ||
} | ||
else { // not special class name -> just load the class | ||
String path = name.replace('.', '/').concat(".class"); | ||
try (InputStream is = getResourceAsStream(path)) { | ||
if (is != null) { | ||
byte[] bytes = is.readAllBytes(); | ||
return defineClass(name, bytes, 0, bytes.length); | ||
} | ||
else { | ||
throw new ClassNotFoundException(name); | ||
} | ||
} | ||
catch (IOException e) { | ||
throw new ClassNotFoundException(name, e); | ||
} | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
test/jdk/java/lang/reflect/TestAllAnnotationsPreserved.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Target; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Retention; | ||
|
||
/* | ||
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
/* | ||
* @test | ||
* @bug 8267936 | ||
* @summary Expose runtime invisible annotations via Class.getAnnotation when -XX:+PreserveAllAnnotations is on | ||
* @run main/othervm -XX:+PreserveAllAnnotations TestAllAnnotationsPreserved | ||
*/ | ||
|
||
public class TestAllAnnotationsPreserved { | ||
public static void main(String... args) { | ||
new TestAllAnnotationsPreserved().test(); | ||
} | ||
|
||
void test() { | ||
NotAtRuntimeByDefault a = Test.class.getAnnotation(NotAtRuntimeByDefault.class); | ||
if (a == null) { | ||
throw new NullPointerException("Annotation NotAtRuntimeByDefault isn't available on class Test!"); | ||
} | ||
if (a.value() != 9) { | ||
throw new IllegalStateException("Expecting value 9 from the @NotAtRuntimeByDefault annotation"); | ||
} | ||
} | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.CLASS) | ||
static @interface NotAtRuntimeByDefault { | ||
int value(); | ||
} | ||
|
||
@NotAtRuntimeByDefault(9) | ||
static class Test { | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
AltClassLoader
usesAnnA_v2
instead ofAnnA_v1
when loading the classes and that makes (in cooperation with-XX:+PreserveAllAnnotations
flag) theRuntimeInvisibleAnnotation
visible.