Path: blob/master/src/java.base/share/classes/jdk/internal/reflect/NativeMethodAccessorImpl.java
41159 views
/*1* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.reflect;2627import sun.reflect.misc.ReflectUtil;2829import java.lang.reflect.*;30import jdk.internal.misc.Unsafe;3132/** Used only for the first few invocations of a Method; afterward,33switches to bytecode-based implementation */3435class NativeMethodAccessorImpl extends MethodAccessorImpl {36private static final Unsafe U = Unsafe.getUnsafe();37private static final long GENERATED_OFFSET38= U.objectFieldOffset(NativeMethodAccessorImpl.class, "generated");3940private final Method method;41private DelegatingMethodAccessorImpl parent;42private int numInvocations;43private volatile int generated;4445NativeMethodAccessorImpl(Method method) {46this.method = method;47}4849public Object invoke(Object obj, Object[] args)50throws IllegalArgumentException, InvocationTargetException51{52// We can't inflate methods belonging to vm-anonymous classes because53// that kind of class can't be referred to by name, hence can't be54// found from the generated bytecode.55if (++numInvocations > ReflectionFactory.inflationThreshold()56&& !method.getDeclaringClass().isHidden()57&& generated == 058&& U.compareAndSetInt(this, GENERATED_OFFSET, 0, 1)) {59try {60MethodAccessorImpl acc = (MethodAccessorImpl)61new MethodAccessorGenerator().62generateMethod(method.getDeclaringClass(),63method.getName(),64method.getParameterTypes(),65method.getReturnType(),66method.getExceptionTypes(),67method.getModifiers());68parent.setDelegate(acc);69} catch (Throwable t) {70// Throwable happens in generateMethod, restore generated to 071generated = 0;72throw t;73}74}7576return invoke0(method, obj, args);77}7879void setParent(DelegatingMethodAccessorImpl parent) {80this.parent = parent;81}8283private static native Object invoke0(Method m, Object obj, Object[] args);84}858687