本文整理汇总了Java中java.lang.invoke.MethodType.methodType方法的典型用法代码示例。如果您正苦于以下问题:Java MethodType.methodType方法的具体用法?Java MethodType.methodType怎么用?Java MethodType.methodType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodType
的用法示例。
在下文中一共展示了MethodType.methodType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateMethodTest7
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
/**
* Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a
* MethodHandle of kind invoke interface. The target method is a method into an interface
* that is shadowed by another definition into a sub interfaces.
*/
private void generateMethodTest7(ClassVisitor cv) {
MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test7", "()V",
null, null);
MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
MethodType.class, MethodHandle.class);
Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
"bsmCreateCallCallingtargetMethodTest8", mt.toMethodDescriptorString(), false);
mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class));
mv.visitInsn(Opcodes.DUP);
mv.visitMethodInsn(
Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false);
mv.visitInvokeDynamicInsn("targetMethodTest8", "(Linvokecustom/J;)V", bootstrap,
new Handle(Opcodes.H_INVOKEINTERFACE, Type.getInternalName(J.class),
"targetMethodTest8", "()V", true));
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(-1, -1);
}
示例2: randomMethodTypeGenerator
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
/**
* Routine used to obtain a randomly generated method type.
*
* @param arity Arity of returned method type.
* @return MethodType generated randomly.
*/
public static MethodType randomMethodTypeGenerator(int arity) {
final Class<?>[] CLASSES = {
Object.class,
int.class,
boolean.class,
byte.class,
short.class,
char.class,
long.class,
float.class,
double.class
};
if (arity > MAX_ARITY) {
throw new IllegalArgumentException(
String.format("Arity should not exceed %d!", MAX_ARITY));
}
List<Class<?>> list = randomClasses(CLASSES, arity);
list = getParams(list, false, arity);
int i = RNG.nextInt(CLASSES.length + 1);
Class<?> rtype = i == CLASSES.length ? void.class : CLASSES[i];
return MethodType.methodType(rtype, list);
}
示例3: getPropertyTest2
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
@Test(dataProvider = "flags")
public void getPropertyTest2(final boolean publicLookup) throws Throwable {
final MethodType mt = MethodType.methodType(Object.class, Object.class);
final CallSite cs = createCallSite(publicLookup, GET_PROPERTY, "class", mt);
Assert.assertEquals(cs.getTarget().invoke(new Object()), Object.class);
Assert.assertEquals(cs.getTarget().invoke(new Date()), Date.class);
}
示例4: zeroConstantFunction
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
public static MethodHandle zeroConstantFunction(Wrapper wrap) {
WrapperCache cache = CONSTANT_FUNCTIONS[0];
MethodHandle mh = cache.get(wrap);
if (mh != null) {
return mh;
}
// slow path
MethodType type = MethodType.methodType(wrap.primitiveType());
switch (wrap) {
case VOID:
mh = EMPTY;
break;
case OBJECT:
case INT: case LONG: case FLOAT: case DOUBLE:
try {
mh = IMPL_LOOKUP.findStatic(THIS_CLASS, "zero"+wrap.wrapperSimpleName(), type);
} catch (ReflectiveOperationException ex) {
mh = null;
}
break;
}
if (mh != null) {
return cache.put(wrap, mh);
}
// use zeroInt and cast the result
if (wrap.isSubwordOrInt() && wrap != Wrapper.INT) {
mh = MethodHandles.explicitCastArguments(zeroConstantFunction(Wrapper.INT), type);
return cache.put(wrap, mh);
}
throw new IllegalArgumentException("cannot find zero constant for " + wrap);
}
示例5: testFindConstructor
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
void testFindConstructor(boolean positive, Lookup lookup,
Class<?> defc, Class<?>... params) throws Throwable {
countTest(positive);
MethodType type = MethodType.methodType(void.class, params);
MethodHandle target = null;
Exception noAccess = null;
try {
if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
target = lookup.findConstructor(defc, type);
} catch (ReflectiveOperationException ex) {
noAccess = ex;
assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
}
if (verbosity >= 3)
System.out.println("findConstructor "+defc.getName()+".<init>/"+type+" => "+target
+(target == null ? "" : target.type())
+(noAccess == null ? "" : " !! "+noAccess));
if (positive && noAccess != null) throw noAccess;
assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
if (!positive) return; // negative test failed as expected
assertEquals(type.changeReturnType(defc), target.type());
Object[] args = randomArgs(params);
printCalled(target, defc.getSimpleName(), args);
Object obj = target.invokeWithArguments(args);
if (!(defc == Example.class && params.length < 2))
assertCalled(defc.getSimpleName()+".<init>", args);
assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
示例6: testMake_MethodType
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
/**
* Test of make method, of class MethodType.
*/
@Test
public void testMake_MethodType() {
System.out.println("make (from rtype, MethodType)");
MethodType expResult = mt_iO2;
MethodType result = MethodType.methodType(int.class, mt_IO2);
assertSame(expResult, result);
}
示例7: testBind
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
void testBind(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
countTest(positive);
String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo
MethodType type = MethodType.methodType(ret, params);
Object receiver = randomArg(defc);
MethodHandle target = null;
Exception noAccess = null;
try {
if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
target = maybeMoveIn(lookup, defc).bind(receiver, methodName, type);
} catch (ReflectiveOperationException ex) {
noAccess = ex;
assertExceptionClass(
(name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
? NoSuchMethodException.class
: IllegalAccessException.class,
noAccess);
if (verbosity >= 5) ex.printStackTrace(System.out);
}
if (verbosity >= 3)
System.out.println("bind "+receiver+"."+name+"/"+type+" => "+target
+(noAccess == null ? "" : " !! "+noAccess));
if (positive && noAccess != null) throw noAccess;
assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
if (!positive) return; // negative test failed as expected
assertEquals(type, target.type());
Object[] args = randomArgs(params);
printCalled(target, name, args);
target.invokeWithArguments(args);
Object[] argsWithReceiver = cat(array(Object[].class, receiver), args);
assertCalled(name, argsWithReceiver);
if (verbosity >= 1)
System.out.print(':');
}
示例8: varargsArray
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
public static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
Class<?> elemType = arrayType.getComponentType();
MethodType vaType = MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType));
MethodHandle mh = varargsArray(nargs);
if (arrayType != Object[].class)
mh = MethodHandles.filterReturnValue(mh, CHANGE_ARRAY_TYPE.bindTo(arrayType));
return mh.asType(vaType);
}
示例9: testInvokePolymorphicRange
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
public void testInvokePolymorphicRange() {
MethodType mt = MethodType.methodType(String.class, byte.class, char.class, short.class,
float.class, double.class, long.class, Integer.class, int.class, String.class);
MethodHandles.Lookup lk = MethodHandles.lookup();
try {
MethodHandle mh = lk.findVirtual(getClass(), "buildString", mt);
System.out.println(
mh.invoke(this, (byte) 2, 'a', (short) 0xFFFF, 1.1f, 2.24d, 12345678L, null,
1, "string"));
} catch (Throwable t) {
t.printStackTrace();
}
}
示例10: getArrayCreatorMethodType
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
private static MethodType getArrayCreatorMethodType(final MethodType type) {
final Class<?>[] callParamTypes = type.parameterArray();
for(int i = 0; i < callParamTypes.length; ++i) {
callParamTypes[i] = getNashornParamType(callParamTypes[i], true);
}
return MethodType.methodType(Object[].class, callParamTypes);
}
示例11: main
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable {
MethodHandle mh = MethodHandles.lookup().findVirtual(CharSequence.class, "toString", MethodType.methodType(String.class));
MethodType mt = MethodType.methodType(Object.class, CharSequence.class);
mh = mh.asType(mt);
Object res = mh.invokeExact((CharSequence)"123");
System.out.println("TEST PASSED");
}
示例12: getPropertyNegativeTest
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
@Test(dataProvider = "flags")
public void getPropertyNegativeTest(final boolean publicLookup) throws Throwable {
final MethodType mt = MethodType.methodType(Object.class, Object.class, String.class);
final CallSite cs = createCallSite(publicLookup, GET_PROPERTY, mt);
Assert.assertNull(cs.getTarget().invoke(new Object(), "DOES_NOT_EXIST"));
}
示例13: invokeMethod
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
protected void invokeMethod(Class<?> vcls, int expected) throws Throwable {
MethodType mt = MethodType.methodType(int.class);
MethodHandle mh = MethodHandles.lookup().findVirtual(vcls, "getVersion", mt);
Assert.assertEquals(expected, (int) mh.invoke(vcls.newInstance()));
}
示例14: methodType
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
public static MethodType methodType(Method method) {
return MethodType.methodType(method.getReturnType(), method.getParameterTypes());
}
示例15: type
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
@Override
public MethodType type(final Class<?> returnType, final Class<?>... paramTypes) {
final MethodType mt = MethodType.methodType(returnType, paramTypes);
log.log(TRACE_LEVEL, "methodType ", returnType, " ", Arrays.toString(paramTypes), " ", mt);
return mt;
}