< prev index next >

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

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2001, 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.  Oracle designates this
--- 1,7 ---
  /*
!  * Copyright (c) 2001, 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

*** 23,12 ***
   * questions.
   */
  
  package jdk.internal.reflect;
  
! import java.lang.reflect.*;
  import jdk.internal.misc.Unsafe;
  
  /** Used only for the first few invocations of a Constructor;
      afterward, switches to bytecode-based implementation */
  
  class NativeConstructorAccessorImpl extends ConstructorAccessorImpl {
--- 23,14 ---
   * questions.
   */
  
  package jdk.internal.reflect;
  
! import java.lang.reflect.Constructor;
+ import java.lang.reflect.InvocationTargetException;
  import jdk.internal.misc.Unsafe;
+ import sun.reflect.misc.ReflectUtil;
  
  /** Used only for the first few invocations of a Constructor;
      afterward, switches to bytecode-based implementation */
  
  class NativeConstructorAccessorImpl extends ConstructorAccessorImpl {

*** 49,30 ***
      public Object newInstance(Object[] args)
          throws InstantiationException,
                 IllegalArgumentException,
                 InvocationTargetException
      {
!         // We can't inflate a constructor belonging to a vm-anonymous class
!         // because that kind of class can't be referred to by name, hence can't
!         // be found from the generated bytecode.
!         if (++numInvocations > ReflectionFactory.inflationThreshold()
!                 && !c.getDeclaringClass().isHidden()
!                 && generated == 0
!                 && U.compareAndSetInt(this, GENERATED_OFFSET, 0, 1)) {
              try {
!                 ConstructorAccessorImpl acc = (ConstructorAccessorImpl)
-                     new MethodAccessorGenerator().
-                         generateConstructor(c.getDeclaringClass(),
-                                             c.getParameterTypes(),
-                                             c.getExceptionTypes(),
-                                             c.getModifiers());
-                 parent.setDelegate(acc);
              } catch (Throwable t) {
!                 // Throwable happens in generateConstructor, restore generated to 0
                  generated = 0;
                  throw t;
              }
          }
  
          return newInstance0(c, args);
      }
  
--- 51,38 ---
      public Object newInstance(Object[] args)
          throws InstantiationException,
                 IllegalArgumentException,
                 InvocationTargetException
      {
!         boolean generate = false;
!         if (!c.getDeclaringClass().isHidden()) {
!             if (Thread.currentThread().isVirtual()) {
!                 generate = true;
!             } else {
!                 if (++numInvocations > ReflectionFactory.inflationThreshold()
!                         && generated == 0
+                         && U.compareAndSetInt(this, GENERATED_OFFSET, 0, 1)) {
+                     generate = true;
+                 }
+             }
+         }
+ 
+         if (generate) {
+             // class initializer may not have run
+             Unsafe.getUnsafe().ensureClassInitialized(c.getDeclaringClass());
+ 
+             ConstructorAccessorImpl acc;
              try {
!                 acc = NewAccessorImplFactory.newConstructorAccessorImpl(c);
              } catch (Throwable t) {
!                 // newConstructorAccessorImpl failed, restore generated to 0
                  generated = 0;
                  throw t;
              }
+ 
+             parent.setDelegate(acc);
+             return acc.newInstance(args);
          }
  
          return newInstance0(c, args);
      }
  
< prev index next >