diff options
author | mchung <none@none> | 2013-04-16 21:39:52 -0700 |
---|---|---|
committer | mchung <none@none> | 2013-04-16 21:39:52 -0700 |
commit | 14d01df621a2c9ac5cb083c567a3cf55910f9e79 (patch) | |
tree | 614b2c50ad21f2836ffccd6f865d6dd7560e27e5 /src/share/classes/sun/reflect | |
parent | 0fb5f5a0dca3a6e38c11b51949ebdfc72b7507a9 (diff) |
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
Reviewed-by: jrose, alanb, twisti
Diffstat (limited to 'src/share/classes/sun/reflect')
-rw-r--r-- | src/share/classes/sun/reflect/CallerSensitive.java | 41 | ||||
-rw-r--r-- | src/share/classes/sun/reflect/Reflection.java | 38 |
2 files changed, 69 insertions, 10 deletions
diff --git a/src/share/classes/sun/reflect/CallerSensitive.java b/src/share/classes/sun/reflect/CallerSensitive.java new file mode 100644 index 000000000..19e47cdd5 --- /dev/null +++ b/src/share/classes/sun/reflect/CallerSensitive.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2013, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ + +package sun.reflect; + +import java.lang.annotation.*; +import static java.lang.annotation.ElementType.*; + +/** + * A method annotated @CallerSensitive is sensitive to its calling class, + * via {@link sun.reflect.Reflection#getCallerClass Reflection.getCallerClass}, + * or via some equivalent. + * + * @author John R. Rose + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({CONSTRUCTOR, METHOD}) +public @interface CallerSensitive { +} diff --git a/src/share/classes/sun/reflect/Reflection.java b/src/share/classes/sun/reflect/Reflection.java index fb5598269..274705fc2 100644 --- a/src/share/classes/sun/reflect/Reflection.java +++ b/src/share/classes/sun/reflect/Reflection.java @@ -51,16 +51,11 @@ public class Reflection { methodFilterMap = new HashMap<>(); } - /** Returns the class of the method <code>realFramesToSkip</code> - frames up the stack (zero-based), ignoring frames associated - with java.lang.reflect.Method.invoke() and its implementation. - The first frame is that associated with this method, so - <code>getCallerClass(0)</code> returns the Class object for - sun.reflect.Reflection. Frames associated with - java.lang.reflect.Method.invoke() and its implementation are - completely ignored and do not count toward the number of "real" - frames skipped. */ - public static native Class<?> getCallerClass(int realFramesToSkip); + /** Returns the class of the caller of the method calling this method, + ignoring frames associated with java.lang.reflect.Method.invoke() + and its implementation. */ + @CallerSensitive + public static native Class<?> getCallerClass(); /** Retrieves the access flags written to the class file. For inner classes these flags may differ from those returned by @@ -321,4 +316,27 @@ public class Reflection { } return newMembers; } + + /** + * Tests if the given method is caller-sensitive and the declaring class + * is defined by either the bootstrap class loader or extension class loader. + */ + public static boolean isCallerSensitive(Method m) { + final ClassLoader loader = m.getDeclaringClass().getClassLoader(); + if (sun.misc.VM.isSystemDomainLoader(loader) || isExtClassLoader(loader)) { + return m.isAnnotationPresent(CallerSensitive.class); + } + return false; + } + + private static boolean isExtClassLoader(ClassLoader loader) { + ClassLoader cl = ClassLoader.getSystemClassLoader(); + while (cl != null) { + if (cl.getParent() == null && cl == loader) { + return true; + } + cl = cl.getParent(); + } + return false; + } } |