< prev index next >

src/java.base/share/classes/jdk/internal/reflect/ReflectionFactory.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.reflect;
  27 
  28 import java.lang.reflect.Field;
  29 import java.lang.reflect.Executable;
  30 import java.lang.reflect.Method;
  31 import java.lang.reflect.Constructor;
  32 import java.lang.reflect.Modifier;
  33 import java.security.AccessController;
  34 import java.security.Permission;
  35 import java.security.PrivilegedAction;

  36 import sun.reflect.misc.ReflectUtil;

  37 
  38 /** <P> The master factory for all reflective objects, both those in
  39     java.lang.reflect (Fields, Methods, Constructors) as well as their
  40     delegates (FieldAccessors, MethodAccessors, ConstructorAccessors).
  41     </P>
  42 
  43     <P> The methods in this class are extremely unsafe and can cause
  44     subversion of both the language and the verifier. For this reason,
  45     they are all instance methods, and access to the constructor of
  46     this factory is guarded by a security check, in similar style to
  47     {@link jdk.internal.misc.Unsafe}. </P>
  48 */
  49 
  50 public class ReflectionFactory {
  51 
  52     private static boolean initted = false;
  53     private static final Permission reflectionFactoryAccessPerm
  54         = new RuntimePermission("reflectionFactoryAccess");
  55     private static final ReflectionFactory soleInstance = new ReflectionFactory();
  56     // Provides access to package-private mechanisms in java.lang.reflect


 365         setConstructorAccessor(c, acc);
 366         return c;
 367     }
 368 
 369     //--------------------------------------------------------------------------
 370     //
 371     // Internals only below this point
 372     //
 373 
 374     static int inflationThreshold() {
 375         return inflationThreshold;
 376     }
 377 
 378     /** We have to defer full initialization of this class until after
 379         the static initializer is run since java.lang.reflect.Method's
 380         static initializer (more properly, that for
 381         java.lang.reflect.AccessibleObject) causes this class's to be
 382         run, before the system properties are set up. */
 383     private static void checkInitted() {
 384         if (initted) return;
 385         AccessController.doPrivileged(
 386             new PrivilegedAction<>() {
 387                 public Void run() {
 388                     // Tests to ensure the system properties table is fully
 389                     // initialized. This is needed because reflection code is
 390                     // called very early in the initialization process (before
 391                     // command-line arguments have been parsed and therefore
 392                     // these user-settable properties installed.) We assume that
 393                     // if System.out is non-null then the System class has been
 394                     // fully initialized and that the bulk of the startup code
 395                     // has been run.
 396 
 397                     if (System.out == null) {
 398                         // java.lang.System not yet fully initialized
 399                         return null;
 400                     }
 401 
 402                     String val = System.getProperty("sun.reflect.noInflation");

 403                     if (val != null && val.equals("true")) {
 404                         noInflation = true;
 405                     }
 406 
 407                     val = System.getProperty("sun.reflect.inflationThreshold");
 408                     if (val != null) {
 409                         try {
 410                             inflationThreshold = Integer.parseInt(val);
 411                         } catch (NumberFormatException e) {
 412                             throw new RuntimeException("Unable to parse property sun.reflect.inflationThreshold", e);
 413                         }
 414                     }
 415 
 416                     initted = true;
 417                     return null;
 418                 }
 419             });
 420     }
 421 
 422     private static LangReflectAccess langReflectAccess() {
 423         if (langReflectAccess == null) {
 424             // Call a static method to get class java.lang.reflect.Modifier
 425             // initialized. Its static initializer will cause
 426             // setLangReflectAccess() to be called from the context of the
 427             // java.lang.reflect package.
 428             Modifier.isPublic(Modifier.PUBLIC);
 429         }
 430         return langReflectAccess;
 431     }
 432 }


  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.reflect;
  27 
  28 import java.lang.reflect.Field;
  29 import java.lang.reflect.Executable;
  30 import java.lang.reflect.Method;
  31 import java.lang.reflect.Constructor;
  32 import java.lang.reflect.Modifier;

  33 import java.security.Permission;
  34 import java.security.PrivilegedAction;
  35 import java.util.Properties;
  36 import sun.reflect.misc.ReflectUtil;
  37 import sun.security.action.GetPropertyAction;
  38 
  39 /** <P> The master factory for all reflective objects, both those in
  40     java.lang.reflect (Fields, Methods, Constructors) as well as their
  41     delegates (FieldAccessors, MethodAccessors, ConstructorAccessors).
  42     </P>
  43 
  44     <P> The methods in this class are extremely unsafe and can cause
  45     subversion of both the language and the verifier. For this reason,
  46     they are all instance methods, and access to the constructor of
  47     this factory is guarded by a security check, in similar style to
  48     {@link jdk.internal.misc.Unsafe}. </P>
  49 */
  50 
  51 public class ReflectionFactory {
  52 
  53     private static boolean initted = false;
  54     private static final Permission reflectionFactoryAccessPerm
  55         = new RuntimePermission("reflectionFactoryAccess");
  56     private static final ReflectionFactory soleInstance = new ReflectionFactory();
  57     // Provides access to package-private mechanisms in java.lang.reflect


 366         setConstructorAccessor(c, acc);
 367         return c;
 368     }
 369 
 370     //--------------------------------------------------------------------------
 371     //
 372     // Internals only below this point
 373     //
 374 
 375     static int inflationThreshold() {
 376         return inflationThreshold;
 377     }
 378 
 379     /** We have to defer full initialization of this class until after
 380         the static initializer is run since java.lang.reflect.Method's
 381         static initializer (more properly, that for
 382         java.lang.reflect.AccessibleObject) causes this class's to be
 383         run, before the system properties are set up. */
 384     private static void checkInitted() {
 385         if (initted) return;
 386 


 387         // Tests to ensure the system properties table is fully
 388         // initialized. This is needed because reflection code is
 389         // called very early in the initialization process (before
 390         // command-line arguments have been parsed and therefore
 391         // these user-settable properties installed.) We assume that
 392         // if System.out is non-null then the System class has been
 393         // fully initialized and that the bulk of the startup code
 394         // has been run.
 395 
 396         if (System.out == null) {
 397             // java.lang.System not yet fully initialized
 398             return;
 399         }
 400 
 401         Properties props = GetPropertyAction.getProperties();
 402         String val = props.getProperty("sun.reflect.noInflation");
 403         if (val != null && val.equals("true")) {
 404             noInflation = true;
 405         }
 406 
 407         val = props.getProperty("sun.reflect.inflationThreshold");
 408         if (val != null) {
 409             try {
 410                 inflationThreshold = Integer.parseInt(val);
 411             } catch (NumberFormatException e) {
 412                 throw new RuntimeException("Unable to parse property sun.reflect.inflationThreshold", e);
 413             }
 414         }
 415 
 416         initted = true;



 417     }
 418 
 419     private static LangReflectAccess langReflectAccess() {
 420         if (langReflectAccess == null) {
 421             // Call a static method to get class java.lang.reflect.Modifier
 422             // initialized. Its static initializer will cause
 423             // setLangReflectAccess() to be called from the context of the
 424             // java.lang.reflect package.
 425             Modifier.isPublic(Modifier.PUBLIC);
 426         }
 427         return langReflectAccess;
 428     }
 429 }
< prev index next >