Search in sources :

Example 1 with ConstantPool

use of sun.reflect.ConstantPool in project cucumber-jvm by cucumber.

the class ConstantPoolTypeIntrospector method getGenericTypes.

@Override
public Type[] getGenericTypes(Class<? extends StepdefBody> clazz, Class<? extends StepdefBody> interfac3) throws Exception {
    ConstantPool constantPool = (ConstantPool) Class_getConstantPool.invoke(clazz);
    String typeString = getLambdaTypeString(constantPool);
    int typeParameterCount = interfac3.getTypeParameters().length;
    jdk.internal.org.objectweb.asm.Type[] argumentTypes = jdk.internal.org.objectweb.asm.Type.getArgumentTypes(typeString);
    // Only look at the N last arguments to the lambda static method, since the first ones might be variables
    // who only pass in the states of closed variables
    List<jdk.internal.org.objectweb.asm.Type> interestingArgumentTypes = Arrays.asList(argumentTypes).subList(argumentTypes.length - typeParameterCount, argumentTypes.length);
    Type[] typeArguments = new Type[typeParameterCount];
    for (int i = 0; i < typeParameterCount; i++) {
        typeArguments[i] = Class.forName(interestingArgumentTypes.get(i).getClassName());
    }
    return typeArguments;
}
Also used : Type(java.lang.reflect.Type) ConstantPool(sun.reflect.ConstantPool)

Example 2 with ConstantPool

use of sun.reflect.ConstantPool in project openj9 by eclipse.

the class StructuralComparator method resolveInvokeDynamic.

@SuppressWarnings("unused")
private static final MethodHandle resolveInvokeDynamic(long j9class, String name, String methodDescriptor, long bsmData) throws Throwable {
    MethodHandle result = null;
    MethodType type = null;
    try {
        VMLangAccess access = VM.getVMLangAccess();
        Object internalRamClass = access.createInternalRamClass(j9class);
        Class<?> classObject = null;
        if (JITHELPERS.is32Bit()) {
            classObject = JITHELPERS.getClassFromJ9Class32((int) j9class);
        } else {
            classObject = JITHELPERS.getClassFromJ9Class64(j9class);
        }
        Objects.requireNonNull(classObject);
        type = MethodType.vmResolveFromMethodDescriptorString(methodDescriptor, access.getClassloader(classObject), null);
        int bsmIndex = UNSAFE.getShort(bsmData);
        int bsmArgCount = UNSAFE.getShort(bsmData + BSM_ARGUMENT_COUNT_OFFSET);
        long bsmArgs = bsmData + BSM_ARGUMENTS_OFFSET;
        MethodHandle bsm = getCPMethodHandleAt(internalRamClass, bsmIndex);
        if (null == bsm) {
            // $NON-NLS-1$
            throw new NullPointerException(Msg.getString("K05cd", classObject.toString(), bsmIndex));
        }
        Object[] staticArgs = new Object[BSM_OPTIONAL_ARGUMENTS_START_INDEX + bsmArgCount];
        /* Mandatory arguments */
        staticArgs[BSM_LOOKUP_ARGUMENT_INDEX] = new MethodHandles.Lookup(classObject, false);
        staticArgs[BSM_NAME_ARGUMENT_INDEX] = name;
        staticArgs[BSM_TYPE_ARGUMENT_INDEX] = type;
        /* Static optional arguments */
        /* internalRamClass is not a j.l.Class object but the ConstantPool natives know how to
			 * get the internal constantPool from the j9class
			 */
        ConstantPool cp = access.getConstantPool(internalRamClass);
        /* Check if we need to treat the last parameter specially when handling primitives.
			 * The type of the varargs array will determine how primitive ints from the constantpool
			 * get boxed: {Boolean, Byte, Short, Character or Integer}.
			 */
        boolean treatLastArgAsVarargs = bsm.isVarargsCollector();
        Class<?> varargsComponentType = bsm.type.lastParameterType().getComponentType();
        int bsmTypeArgCount = bsm.type.parameterCount();
        for (int i = 0; i < bsmArgCount; i++) {
            int staticArgIndex = BSM_OPTIONAL_ARGUMENTS_START_INDEX + i;
            short index = UNSAFE.getShort(bsmArgs + (i * BSM_ARGUMENT_SIZE));
            int cpType = getCPTypeAt(internalRamClass, index);
            Object cpEntry = null;
            switch(cpType) {
                case 1:
                    cpEntry = cp.getClassAt(index);
                    if (cpEntry == null) {
                        throw throwNoClassDefFoundError(classObject, index);
                    }
                    break;
                case 2:
                    cpEntry = cp.getStringAt(index);
                    break;
                case 3:
                    {
                        int cpValue = cp.getIntAt(index);
                        Class<?> argClass;
                        if (treatLastArgAsVarargs && (staticArgIndex >= (bsmTypeArgCount - 1))) {
                            argClass = varargsComponentType;
                        } else {
                            argClass = bsm.type.parameterType(staticArgIndex);
                        }
                        if (argClass == Short.TYPE) {
                            cpEntry = (short) cpValue;
                        } else if (argClass == Boolean.TYPE) {
                            cpEntry = cpValue == 0 ? Boolean.FALSE : Boolean.TRUE;
                        } else if (argClass == Byte.TYPE) {
                            cpEntry = (byte) cpValue;
                        } else if (argClass == Character.TYPE) {
                            cpEntry = (char) cpValue;
                        } else {
                            cpEntry = cpValue;
                        }
                        break;
                    }
                case 4:
                    cpEntry = cp.getFloatAt(index);
                    break;
                case 5:
                    cpEntry = cp.getLongAt(index);
                    break;
                case 6:
                    cpEntry = cp.getDoubleAt(index);
                    break;
                case 13:
                    cpEntry = getCPMethodTypeAt(internalRamClass, index);
                    break;
                case 14:
                    cpEntry = getCPMethodHandleAt(internalRamClass, index);
                    break;
                default:
            }
            // Implicit NPE
            cpEntry.getClass();
            staticArgs[staticArgIndex] = cpEntry;
        }
        /* Take advantage of the per-MH asType cache */
        CallSite cs = null;
        switch(staticArgs.length) {
            case 3:
                cs = (CallSite) bsm.invoke(staticArgs[0], staticArgs[1], staticArgs[2]);
                break;
            case 4:
                cs = (CallSite) bsm.invoke(staticArgs[0], staticArgs[1], staticArgs[2], staticArgs[3]);
                break;
            case 5:
                cs = (CallSite) bsm.invoke(staticArgs[0], staticArgs[1], staticArgs[2], staticArgs[3], staticArgs[4]);
                break;
            case 6:
                cs = (CallSite) bsm.invoke(staticArgs[0], staticArgs[1], staticArgs[2], staticArgs[3], staticArgs[4], staticArgs[5]);
                break;
            case 7:
                cs = (CallSite) bsm.invoke(staticArgs[0], staticArgs[1], staticArgs[2], staticArgs[3], staticArgs[4], staticArgs[5], staticArgs[6]);
                break;
            default:
                cs = (java.lang.invoke.CallSite) bsm.invokeWithArguments(staticArgs);
                break;
        }
        if (cs != null) {
            MethodType callsiteType = cs.type();
            if (callsiteType != type) {
                throw WrongMethodTypeException.newWrongMethodTypeException(type, callsiteType);
            }
            result = cs.dynamicInvoker();
        }
    } catch (Throwable e) {
        /*[IF Sidecar19-SE]*/
        if (e instanceof Error) {
            throw e;
        }
        if (type == null) {
            throw new BootstrapMethodError(e);
        }
        /* create an exceptionHandle with appropriate drop adapter and install that */
        try {
            MethodHandle thrower = MethodHandles.throwException(type.returnType(), BootstrapMethodError.class);
            MethodHandle constructor = MethodHandles.Lookup.IMPL_LOOKUP.findConstructor(BootstrapMethodError.class, MethodType.methodType(void.class, Throwable.class));
            result = MethodHandles.foldArguments(thrower, constructor.bindTo(e));
            result = MethodHandles.dropArguments(result, 0, type.parameterList());
        } catch (IllegalAccessException iae) {
            throw new Error(iae);
        } catch (NoSuchMethodException nsme) {
            throw new Error(nsme);
        }
    }
    return result;
}
Also used : VMLangAccess(com.ibm.oti.vm.VMLangAccess) Lookup(java.lang.invoke.MethodHandles.Lookup) ConstantPool(sun.reflect.ConstantPool)

Aggregations

ConstantPool (sun.reflect.ConstantPool)2 VMLangAccess (com.ibm.oti.vm.VMLangAccess)1 Lookup (java.lang.invoke.MethodHandles.Lookup)1 Type (java.lang.reflect.Type)1