本文整理汇总了Java中java.lang.invoke.SwitchPoint类的典型用法代码示例。如果您正苦于以下问题:Java SwitchPoint类的具体用法?Java SwitchPoint怎么用?Java SwitchPoint使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SwitchPoint类属于java.lang.invoke包,在下文中一共展示了SwitchPoint类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findFastGetIndexMethod
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
/**
* Return a fast linked array getter, or null if we have to dispatch to super class
* @param desc descriptor
* @param request link request
* @return invocation or null if needs to be sent to slow relink
*/
@Override
public GuardedInvocation findFastGetIndexMethod(final Class<? extends ArrayData> clazz, final CallSiteDescriptor desc, final LinkRequest request) {
final MethodType callType = desc.getMethodType();
final Class<?> indexType = callType.parameterType(1);
final Class<?> returnType = callType.returnType();
if (ContinuousArrayData.class.isAssignableFrom(clazz) && indexType == int.class) {
final Object[] args = request.getArguments();
final int index = (int)args[args.length - 1];
if (has(index)) {
final MethodHandle getArray = ScriptObject.GET_ARRAY.methodHandle();
final int programPoint = NashornCallSiteDescriptor.isOptimistic(desc) ? NashornCallSiteDescriptor.getProgramPoint(desc) : INVALID_PROGRAM_POINT;
MethodHandle getElement = getElementGetter(returnType, programPoint);
if (getElement != null) {
getElement = MH.filterArguments(getElement, 0, MH.asType(getArray, getArray.type().changeReturnType(clazz)));
final MethodHandle guard = MH.insertArguments(FAST_ACCESS_GUARD, 0, clazz);
return new GuardedInvocation(getElement, guard, (SwitchPoint)null, ClassCastException.class);
}
}
}
return null;
}
示例2: findFastSetIndexMethod
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
/**
* Return a fast linked array setter, or null if we have to dispatch to super class
* @param desc descriptor
* @param request link request
* @return invocation or null if needs to be sent to slow relink
*/
@Override
public GuardedInvocation findFastSetIndexMethod(final Class<? extends ArrayData> clazz, final CallSiteDescriptor desc, final LinkRequest request) { // array, index, value
final MethodType callType = desc.getMethodType();
final Class<?> indexType = callType.parameterType(1);
final Class<?> elementType = callType.parameterType(2);
if (ContinuousArrayData.class.isAssignableFrom(clazz) && indexType == int.class) {
final Object[] args = request.getArguments();
final int index = (int)args[args.length - 2];
if (hasRoomFor(index)) {
MethodHandle setElement = getElementSetter(elementType); //Z(continuousarraydata, int, int), return true if successful
if (setElement != null) {
//else we are dealing with a wider type than supported by this callsite
MethodHandle getArray = ScriptObject.GET_ARRAY.methodHandle();
getArray = MH.asType(getArray, getArray.type().changeReturnType(getClass()));
setElement = MH.filterArguments(setElement, 0, getArray);
final MethodHandle guard = MH.insertArguments(FAST_ACCESS_GUARD, 0, clazz);
return new GuardedInvocation(setElement, guard, (SwitchPoint)null, ClassCastException.class); //CCE if not a scriptObject anymore
}
}
}
return null;
}
示例3: findGetIndexMethod
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
/**
* Find the appropriate GETINDEX method for an invoke dynamic call.
*
* @param desc the call site descriptor
* @param request the link request
*
* @return GuardedInvocation to be invoked at call site.
*/
protected GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) {
final MethodType callType = desc.getMethodType();
final Class<?> returnType = callType.returnType();
final Class<?> returnClass = returnType.isPrimitive() ? returnType : Object.class;
final Class<?> keyClass = callType.parameterType(1);
final boolean explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request);
final String name;
if (returnClass.isPrimitive()) {
//turn e.g. get with a double into getDouble
final String returnTypeName = returnClass.getName();
name = "get" + Character.toUpperCase(returnTypeName.charAt(0)) + returnTypeName.substring(1, returnTypeName.length());
} else {
name = "get";
}
final MethodHandle mh = findGetIndexMethodHandle(returnClass, name, keyClass, desc);
return new GuardedInvocation(mh, getScriptObjectGuard(callType, explicitInstanceOfCheck), (SwitchPoint)null, explicitInstanceOfCheck ? null : ClassCastException.class);
}
示例4: getValidOptimisticInvocation
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
/**
* Returns a pair of an invocation created with a passed-in supplier and a non-invalidated switch point for
* optimistic assumptions (or null for the switch point if the function can not be deoptimized). While the method
* makes a best effort to return a non-invalidated switch point (compensating for possible deoptimizing
* recompilation happening on another thread) it is still possible that by the time this method returns the
* switchpoint has been invalidated by a {@code RewriteException} triggered on another thread for this function.
* This is not a problem, though, as these switch points are always used to produce call sites that fall back to
* relinking when they are invalidated, and in this case the execution will end up here again. What this method
* basically does is minimize such busy-loop relinking while the function is being recompiled on a different thread.
* @param invocationSupplier the supplier that constructs the actual invocation method handle; should use the
* {@code CompiledFunction} method itself in some capacity.
* @return a tuple object containing the method handle as created by the supplier and an optimistic assumptions
* switch point that is guaranteed to not have been invalidated before the call to this method (or null if the
* function can't be further deoptimized).
*/
private synchronized HandleAndAssumptions getValidOptimisticInvocation(final Supplier<MethodHandle> invocationSupplier) {
for(;;) {
final MethodHandle handle = invocationSupplier.get();
final SwitchPoint assumptions = canBeDeoptimized() ? optimismInfo.optimisticAssumptions : null;
if(assumptions != null && assumptions.hasBeenInvalidated()) {
// We can be in a situation where one thread is in the middle of a deoptimizing compilation when we hit
// this and thus, it has invalidated the old switch point, but hasn't created the new one yet. Note that
// the behavior of invalidating the old switch point before recompilation, and only creating the new one
// after recompilation is by design. If we didn't wait here for the recompilation to complete, we would
// be busy looping through the fallback path of the invalidated switch point, relinking the call site
// again with the same invalidated switch point, invoking the fallback, etc. stealing CPU cycles from
// the recompilation task we're dependent on. This can still happen if the switch point gets invalidated
// after we grabbed it here, in which case we'll indeed do one busy relink immediately.
try {
wait();
} catch (final InterruptedException e) {
// Intentionally ignored. There's nothing meaningful we can do if we're interrupted
}
} else {
return new HandleAndAssumptions(handle, assumptions);
}
}
}
示例5: relinkComposableInvoker
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
private static void relinkComposableInvoker(final CallSite cs, final CompiledFunction inv, final boolean constructor) {
final HandleAndAssumptions handleAndAssumptions = inv.getValidOptimisticInvocation(new Supplier<MethodHandle>() {
@Override
public MethodHandle get() {
return inv.getInvokerOrConstructor(constructor);
}
});
final MethodHandle handle = handleAndAssumptions.handle;
final SwitchPoint assumptions = handleAndAssumptions.assumptions;
final MethodHandle target;
if(assumptions == null) {
target = handle;
} else {
final MethodHandle relink = MethodHandles.insertArguments(RELINK_COMPOSABLE_INVOKER, 0, cs, inv, constructor);
target = assumptions.guardWithTest(handle, MethodHandles.foldArguments(cs.dynamicInvoker(), relink));
}
cs.setTarget(target.asType(cs.type()));
}
示例6: requestRecompile
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
boolean requestRecompile(final RewriteException e) {
final Type retType = e.getReturnType();
final Type previousFailedType = invalidatedProgramPoints.put(e.getProgramPoint(), retType);
if (previousFailedType != null && !previousFailedType.narrowerThan(retType)) {
final StackTraceElement[] stack = e.getStackTrace();
final String functionId = stack.length == 0 ?
data.getName() :
stack[0].getClassName() + "." + stack[0].getMethodName();
log.info("RewriteException for an already invalidated program point ", e.getProgramPoint(), " in ", functionId, ". This is okay for a recursive function invocation, but a bug otherwise.");
return false;
}
SwitchPoint.invalidateAll(new SwitchPoint[] { optimisticAssumptions });
return true;
}
示例7: addSwitchPoint
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
/**
* Add a switchpoint to this guarded invocation
* @param newSwitchPoint new switchpoint, or null for nop
* @return new guarded invocation with the extra switchpoint
*/
public GuardedInvocation addSwitchPoint(final SwitchPoint newSwitchPoint) {
if (newSwitchPoint == null) {
return this;
}
final SwitchPoint[] newSwitchPoints;
if (switchPoints != null) {
newSwitchPoints = new SwitchPoint[switchPoints.length + 1];
System.arraycopy(switchPoints, 0, newSwitchPoints, 0, switchPoints.length);
newSwitchPoints[switchPoints.length] = newSwitchPoint;
} else {
newSwitchPoints = new SwitchPoint[] { newSwitchPoint };
}
return new GuardedInvocation(invocation, guard, newSwitchPoints, exception);
}
示例8: getProtoSwitchPoints
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
/**
* Get a switch point for a property with the given {@code name} that will be invalidated when
* the property definition is changed in this object's prototype chain. Returns {@code null} if
* the property is defined in this object itself.
*
* @param name the property name
* @param owner the property owner, null if property is not defined
* @return a SwitchPoint or null
*/
public final SwitchPoint[] getProtoSwitchPoints(final String name, final ScriptObject owner) {
if (owner == this || getProto() == null) {
return null;
}
final List<SwitchPoint> switchPoints = new ArrayList<>();
for (ScriptObject obj = this; obj != owner && obj.getProto() != null; obj = obj.getProto()) {
final ScriptObject parent = obj.getProto();
parent.getMap().addListener(name, obj.getMap());
final SwitchPoint sp = parent.getMap().getSharedProtoSwitchPoint();
if (sp != null && !sp.hasBeenInvalidated()) {
switchPoints.add(sp);
}
}
switchPoints.add(getMap().getSwitchPoint(name));
return switchPoints.toArray(new SwitchPoint[0]);
}
示例9: addSwitchPoint
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
/**
* Create a new guarded invocation with an added switch point.
* @param newSwitchPoint new switch point. Can be null in which case this
* method return the current guarded invocation with no changes.
* @return a guarded invocation with the added switch point.
*/
public GuardedInvocation addSwitchPoint(final SwitchPoint newSwitchPoint) {
if (newSwitchPoint == null) {
return this;
}
final SwitchPoint[] newSwitchPoints;
if (switchPoints != null) {
newSwitchPoints = new SwitchPoint[switchPoints.length + 1];
System.arraycopy(switchPoints, 0, newSwitchPoints, 0, switchPoints.length);
newSwitchPoints[switchPoints.length] = newSwitchPoint;
} else {
newSwitchPoints = new SwitchPoint[] { newSwitchPoint };
}
return new GuardedInvocation(invocation, guard, newSwitchPoints, exception);
}
示例10: getLexicalScopeSwitchPoint
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
private synchronized SwitchPoint getLexicalScopeSwitchPoint() {
SwitchPoint switchPoint = lexicalScopeSwitchPoint;
if (switchPoint == null || switchPoint.hasBeenInvalidated()) {
switchPoint = lexicalScopeSwitchPoint = new SwitchPoint();
}
return switchPoint;
}
示例11: tagBuiltinProperties
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
/**
* Given a builtin object, traverse its properties recursively and associate them with a name that
* will be a key to their invalidation switchpoint.
* @param name name for key
* @param func builtin script object
*/
private void tagBuiltinProperties(final String name, final ScriptObject func) {
SwitchPoint sp = context.getBuiltinSwitchPoint(name);
if (sp == null) {
sp = context.newBuiltinSwitchPoint(name);
}
//get all builtin properties in this builtin object and register switchpoints keyed on the propery name,
//one overwrite destroys all for now, e.g. Function.prototype.apply = 17; also destroys Function.prototype.call etc
for (final jdk.nashorn.internal.runtime.Property prop : extractBuiltinProperties(name, func)) {
prop.setBuiltinSwitchPoint(sp);
}
}
示例12: getOrCreateSwitchPoint
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
private Access getOrCreateSwitchPoint(final String name) {
Access acc = map.get(name);
if (acc != null) {
return acc;
}
final SwitchPoint sp = new SwitchPoint();
map.put(name, acc = new Access(name, sp));
return acc;
}
示例13: invalidateSwitchPoint
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
@SuppressWarnings("unused")
private static Object invalidateSwitchPoint(final AccessorProperty property, final Object obj) {
if (!property.builtinSwitchPoint.hasBeenInvalidated()) {
SwitchPoint.invalidateAll(new SwitchPoint[] { property.builtinSwitchPoint });
}
return obj;
}
示例14: newBuiltinSwitchPoint
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
/**
* Create a new builtin switchpoint and return it
* @param name key name
* @return new builtin switchpoint
*/
public SwitchPoint newBuiltinSwitchPoint(final String name) {
assert builtinSwitchPoints.get(name) == null;
final SwitchPoint sp = new BuiltinSwitchPoint();
builtinSwitchPoints.put(name, sp);
return sp;
}
示例15: findBuiltinSwitchPoint
import java.lang.invoke.SwitchPoint; //导入依赖的package包/类
private SwitchPoint findBuiltinSwitchPoint(final String key) {
for (ScriptObject myProto = getProto(); myProto != null; myProto = myProto.getProto()) {
final Property prop = myProto.getMap().findProperty(key);
if (prop != null) {
final SwitchPoint sp = prop.getBuiltinSwitchPoint();
if (sp != null && !sp.hasBeenInvalidated()) {
return sp;
}
}
}
return null;
}