Search in sources :

Example 46 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class AccessControlTest_sibling method test.

@Test
public void test() {
    makeCases(lookups());
    if (verbosity > 0) {
        verbosity += 9;
        Method pro_in_self = targetMethod(THIS_CLASS, PROTECTED, methodType(void.class));
        testOneAccess(lookupCase("AccessControlTest/public"), pro_in_self, "find");
        testOneAccess(lookupCase("Remote_subclass/public"), pro_in_self, "find");
        testOneAccess(lookupCase("Remote_subclass"), pro_in_self, "find");
        verbosity -= 9;
    }
    Set<Class<?>> targetClassesDone = new HashSet<>();
    for (LookupCase targetCase : CASES) {
        Class<?> targetClass = targetCase.lookupClass();
        // already saw this one
        if (!targetClassesDone.add(targetClass))
            continue;
        String targetPlace = placeName(targetClass);
        // Object, String, not a target
        if (targetPlace == null)
            continue;
        for (int targetAccess : ACCESS_CASES) {
            MethodType methodType = methodType(void.class);
            Method method = targetMethod(targetClass, targetAccess, methodType);
            // Try to access target method from various contexts.
            for (LookupCase sourceCase : CASES) {
                testOneAccess(sourceCase, method, "find");
                testOneAccess(sourceCase, method, "unreflect");
            }
        }
    }
    System.out.println("tested " + testCount + " access scenarios; " + testCountFails + " accesses were denied");
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 47 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class BigArityTest method MH_hashArguments.

static MethodHandle MH_hashArguments(Class<? extends Object[]> arrayClass, int arity) {
    if (arrayClass == Object[].class)
        return MH_hashArguments(arity);
    ArrayList<Class<?>> ptypes = new ArrayList<>(Collections.<Class<?>>nCopies(arity, arrayClass.getComponentType()));
    MethodType mt = MethodType.methodType(Object.class, ptypes);
    return MH_hashArguments_VA.asType(mt);
}
Also used : MethodType(java.lang.invoke.MethodType) ArrayList(java.util.ArrayList)

Example 48 with MethodType

use of java.lang.invoke.MethodType in project dubbo-faker by moyada.

the class MethodInvokeHandler method fetchHandleInfo.

@Override
public MethodHandle fetchHandleInfo(String className, String methodName, String returnType, Class<?>[] paramClass) {
    Class<?> classType, returnClassType;
    try {
        classType = ReflectUtil.getClassType(className);
    } catch (ClassNotFoundException e) {
        throw new InitializeInvokerException("接口类型不存在: " + returnType);
    }
    try {
        returnClassType = ReflectUtil.getClassType(returnType);
    } catch (ClassNotFoundException e) {
        throw new InitializeInvokerException("结果类型不存在: " + returnType);
    }
    MethodHandle methodHandle;
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    try {
        // 创建方法信息
        MethodType methodType = MethodType.methodType(returnClassType, paramClass);
        // 查询方法返回方法具柄
        methodHandle = lookup.findVirtual(classType, methodName, methodType);
    } catch (NoSuchMethodException e) {
        throw new InitializeInvokerException("方法不存在: " + methodName);
    } catch (IllegalAccessException e) {
        throw new InitializeInvokerException("方法具柄获取失败");
    }
    return methodHandle;
}
Also used : MethodHandles(java.lang.invoke.MethodHandles) MethodType(java.lang.invoke.MethodType) InitializeInvokerException(cn.moyada.dubbo.faker.core.exception.InitializeInvokerException) MethodHandle(java.lang.invoke.MethodHandle)

Example 49 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class ValueConversions method zeroConstantFunction.

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);
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 50 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class ValueConversions method boxExact.

public static MethodHandle boxExact(Wrapper wrap) {
    WrapperCache cache = BOX_CONVERSIONS[0];
    MethodHandle mh = cache.get(wrap);
    if (mh != null) {
        return mh;
    }
    // look up the method
    String name = "box" + wrap.wrapperSimpleName();
    MethodType type = boxType(wrap);
    try {
        mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
    } catch (ReflectiveOperationException ex) {
        mh = null;
    }
    if (mh != null) {
        return cache.put(wrap, mh);
    }
    throw new IllegalArgumentException("cannot find box adapter for " + wrap);
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

MethodType (java.lang.invoke.MethodType)103 MethodHandle (java.lang.invoke.MethodHandle)37 MethodHandles (java.lang.invoke.MethodHandles)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)3 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)3 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)3 CallSite (java.lang.invoke.CallSite)3 LambdaReceiver_A (LambdaReceiver_anotherpkg.LambdaReceiver_A)2 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)2 ConnectorSession (com.facebook.presto.spi.ConnectorSession)2 PrestoException (com.facebook.presto.spi.PrestoException)2 Map (java.util.Map)2 InitializeInvokerException (cn.moyada.dubbo.faker.core.exception.InitializeInvokerException)1 Session (com.facebook.presto.Session)1 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)1 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)1 Parameter (com.facebook.presto.bytecode.Parameter)1 Scope (com.facebook.presto.bytecode.Scope)1