Java Code Examples for java.lang.invoke.MethodHandles.Lookup#unreflect()

The following examples show how to use java.lang.invoke.MethodHandles.Lookup#unreflect() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedefineMethodUsedByMultipleMethodHandles.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {

        Lookup lookup = MethodHandles.lookup();
        Method fooMethod = Foo.class.getDeclaredMethod("getName");

        // fooMH2 displaces fooMH1 from the MemberNamesTable
        MethodHandle fooMH1 = lookup.unreflect(fooMethod);
        MethodHandle fooMH2 = lookup.unreflect(fooMethod);

        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
        System.out.println("fooMH2.invoke = " + fooMH2.invokeExact());

        // Redefining Foo.getName() causes vmtarget to be updated
        // in fooMH2 but not fooMH1
        redefineFoo();

        // Full GC causes fooMH1.vmtarget to be deallocated
        System.gc();

        // Calling fooMH1.vmtarget crashes the VM
        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
    }
 
Example 2
Source File: RedefineMethodUsedByMultipleMethodHandles.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {

        Lookup lookup = MethodHandles.lookup();
        Method fooMethod = Foo.class.getDeclaredMethod("getName");

        // fooMH2 displaces fooMH1 from the MemberNamesTable
        MethodHandle fooMH1 = lookup.unreflect(fooMethod);
        MethodHandle fooMH2 = lookup.unreflect(fooMethod);

        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
        System.out.println("fooMH2.invoke = " + fooMH2.invokeExact());

        // Redefining Foo.getName() causes vmtarget to be updated
        // in fooMH2 but not fooMH1
        redefineFoo();

        // Full GC causes fooMH1.vmtarget to be deallocated
        System.gc();

        // Calling fooMH1.vmtarget crashes the VM
        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
    }
 
Example 3
Source File: RedefineMethodUsedByMultipleMethodHandles.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {

        Lookup lookup = MethodHandles.lookup();
        Method fooMethod = Foo.class.getDeclaredMethod("getName");

        // fooMH2 displaces fooMH1 from the MemberNamesTable
        MethodHandle fooMH1 = lookup.unreflect(fooMethod);
        MethodHandle fooMH2 = lookup.unreflect(fooMethod);

        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
        System.out.println("fooMH2.invoke = " + fooMH2.invokeExact());

        // Redefining Foo.getName() causes vmtarget to be updated
        // in fooMH2 but not fooMH1
        redefineFoo();

        // Full GC causes fooMH1.vmtarget to be deallocated
        System.gc();

        // Calling fooMH1.vmtarget crashes the VM
        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
    }
 
Example 4
Source File: RedefineMethodUsedByMultipleMethodHandles.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {

        Lookup lookup = MethodHandles.lookup();
        Method fooMethod = Foo.class.getDeclaredMethod("getName");

        // fooMH2 displaces fooMH1 from the MemberNamesTable
        MethodHandle fooMH1 = lookup.unreflect(fooMethod);
        MethodHandle fooMH2 = lookup.unreflect(fooMethod);

        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
        System.out.println("fooMH2.invoke = " + fooMH2.invokeExact());

        // Redefining Foo.getName() causes vmtarget to be updated
        // in fooMH2 but not fooMH1
        redefineFoo();

        // Full GC causes fooMH1.vmtarget to be deallocated
        System.gc();

        // Calling fooMH1.vmtarget crashes the VM
        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
    }
 
Example 5
Source File: RedefineMethodUsedByMultipleMethodHandles.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {

        Lookup lookup = MethodHandles.lookup();
        Method fooMethod = Foo.class.getDeclaredMethod("getName");

        // fooMH2 displaces fooMH1 from the MemberNamesTable
        MethodHandle fooMH1 = lookup.unreflect(fooMethod);
        MethodHandle fooMH2 = lookup.unreflect(fooMethod);

        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
        System.out.println("fooMH2.invoke = " + fooMH2.invokeExact());

        // Redefining Foo.getName() causes vmtarget to be updated
        // in fooMH2 but not fooMH1
        redefineFoo();

        // Full GC causes fooMH1.vmtarget to be deallocated
        System.gc();

        // Calling fooMH1.vmtarget crashes the VM
        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
    }
 
Example 6
Source File: TransactionExtractor.java    From exonum-java-binding with Apache License 2.0 6 votes vote down vote up
private static TransactionMethod toTransactionMethod(Method method, Lookup lookup) {
  Serializer<?> argumentsSerializer = StandardSerializers.bytes();
  Class<?> parameterType = method.getParameterTypes()[0];
  if (isProtobufArgument(parameterType)) {
    @SuppressWarnings("unchecked") /* Checked above */
    var messageType = (Class<? extends MessageLite>) parameterType;
    argumentsSerializer = StandardSerializers.protobuf(messageType);
  }
  MethodHandle methodHandle;
  try {
    methodHandle = lookup.unreflect(method);
  } catch (IllegalAccessException e) {
    throw new IllegalArgumentException(
        String.format("Couldn't access method %s", method.getName()), e);
  }
  return new TransactionMethod(methodHandle, argumentsSerializer);
}
 
Example 7
Source File: RedefineMethodUsedByMultipleMethodHandles.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {

        Lookup lookup = MethodHandles.lookup();
        Method fooMethod = Foo.class.getDeclaredMethod("getName");

        // fooMH2 displaces fooMH1 from the MemberNamesTable
        MethodHandle fooMH1 = lookup.unreflect(fooMethod);
        MethodHandle fooMH2 = lookup.unreflect(fooMethod);

        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
        System.out.println("fooMH2.invoke = " + fooMH2.invokeExact());

        // Redefining Foo.getName() causes vmtarget to be updated
        // in fooMH2 but not fooMH1
        redefineFoo();

        // Full GC causes fooMH1.vmtarget to be deallocated
        System.gc();

        // Calling fooMH1.vmtarget crashes the VM
        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
    }
 
Example 8
Source File: MethodBuilder.java    From proxy2 with Apache License 2.0 5 votes vote down vote up
/**
 * Create a method handle that will apply all transformations specified by the current method builder
 * and then call the {@code method} method. 
 * This method uses a cache if the method is a virtual method (either on class or interface)
 * 
 * @param lookup the lookup object used to find the @code method}
 * @param method the method called at the end of the transformation.
 * @return a new method handle constructed by applying all transformations on the target method.
 * @throws NoSuchMethodException throws is a method is not visible.
 * @throws NoSuchFieldException throws if a field is not visible.
 * @throws IllegalAccessException throws if a type or a member of a type is not visible.
 */
public MethodHandle unreflect(Lookup lookup, Method method) throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException {
  MethodHandle target = lookup.unreflect(method);
  MethodType targetType = target.type();
  if (!targetType.equals(sig)) {
    throw new WrongMethodTypeException("target type " + targetType + " is not equals to current type " + sig);
  }
  
  int modifiers = method.getModifiers();
  if (!Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers)) { // can be virtual
    target = new InliningCacheCallSite(target).dynamicInvoker();
  }
  return call(target);
}
 
Example 9
Source File: Properties.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle getComputedValueMethodHandle(Lookup lookup, Method method)
        throws IllegalAccessException {
    // check: (ExecutionContext) -> Object
    MethodHandle handle = lookup.unreflect(method);
    MethodType type = handle.type();
    if (type.parameterCount() != 1 || !ExecutionContext.class.equals(type.parameterType(0))) {
        throw new IllegalArgumentException(handle.toString());
    }
    if (!Object.class.equals(type.returnType())) {
        throw new IllegalArgumentException(handle.toString());
    }
    return handle;
}
 
Example 10
Source File: RuntimeEntityResolver.java    From bazel with Apache License 2.0 4 votes vote down vote up
private static MethodHandle getMethodHandle(
    RuntimeMethodHandle methodHandleRequest,
    Lookup lookup,
    List<JarTransformationRecord> jarTransformationRecords,
    ClassLoader initialInputClassLoader,
    Table<Integer, ClassMemberKey<?>, java.lang.reflect.Member> reflectionBasedMembers,
    Table<Integer, ClassMemberKey<?>, Set<ClassMemberKey<?>>> missingDescriptorLookupRepo,
    String workingJavaPackage)
    throws Throwable {
  int round = methodHandleRequest.round();
  Class<?> classLiteral =
      loadClassLiteral(
          DesugarRule.createDynamicClassLiteral(methodHandleRequest.className(), round),
          jarTransformationRecords,
          initialInputClassLoader,
          reflectionBasedMembers,
          missingDescriptorLookupRepo,
          workingJavaPackage);

  ClassName owner = ClassName.create(classLiteral);
  String memberName = methodHandleRequest.memberName();
  String memberDescriptor = methodHandleRequest.memberDescriptor();

  ClassMemberKey<?> classMemberKey =
      methodHandleRequest.usage() == MemberUseContext.METHOD_INVOCATION
          ? MethodKey.create(owner, memberName, memberDescriptor)
          : FieldKey.create(owner, memberName, memberDescriptor);

  if (classMemberKey.descriptor().isEmpty()) {
    classMemberKey = restoreMissingDescriptor(classMemberKey, round, missingDescriptorLookupRepo);
  }

  switch (methodHandleRequest.usage()) {
    case METHOD_INVOCATION:
      return classMemberKey.isConstructor()
          ? lookup.unreflectConstructor(
              (Constructor<?>) reflectionBasedMembers.get(round, classMemberKey))
          : lookup.unreflect((Method) reflectionBasedMembers.get(round, classMemberKey));
    case FIELD_GETTER:
      return lookup.unreflectGetter((Field) reflectionBasedMembers.get(round, classMemberKey));
    case FIELD_SETTER:
      return lookup.unreflectSetter((Field) reflectionBasedMembers.get(round, classMemberKey));
  }

  throw new AssertionError(
      String.format(
          "Beyond exhaustive enum values: Unexpected enum value (%s) for (Enum:%s)",
          methodHandleRequest.usage(), MemberUseContext.class));
}
 
Example 11
Source File: Properties.java    From es6draft with MIT License 4 votes vote down vote up
private static ObjectLayout createExternalObjectLayout(Class<?> holder, boolean staticMethods) {
    try {
        ObjectLayout layout = new ObjectLayout();
        Lookup lookup = MethodHandles.publicLookup();
        for (Method method : holder.getDeclaredMethods()) {
            if (Modifier.isStatic(method.getModifiers()) != staticMethods)
                continue;
            Function function = method.getAnnotation(Function.class);
            Accessor accessor = method.getAnnotation(Accessor.class);
            Value value = method.getAnnotation(Value.class);
            AliasFunction[] aliases = method.getAnnotationsByType(AliasFunction.class);
            if (aliases.length > 0 && function == null) {
                throw new IllegalArgumentException();
            }
            if (function != null) {
                if (accessor != null || value != null) {
                    throw new IllegalArgumentException();
                }
                if (layout.functions == null) {
                    layout.functions = new LinkedHashMap<>();
                }
                MethodHandle mh = lookup.unreflect(method);
                layout.functions.put(function, mh);
                for (AliasFunction alias : aliases) {
                    layout.functions.put(createFunctionFromAlias(function, alias), mh);
                }
            }
            if (accessor != null) {
                if (value != null) {
                    throw new IllegalArgumentException();
                }
                if (layout.accessors == null) {
                    layout.accessors = new LinkedHashMap<>();
                }
                layout.accessors.put(accessor, lookup.unreflect(method));
            }
            if (value != null) {
                if (layout.values == null) {
                    layout.values = new LinkedHashMap<>();
                }
                layout.values.put(value, lookup.unreflect(method));
            }
        }
        return layout;
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example 12
Source File: Properties.java    From es6draft with MIT License 4 votes vote down vote up
private static MethodHandle getStaticMethodHandle(Lookup lookup, Method method) throws IllegalAccessException {
    MethodHandle handle = lookup.unreflect(method);
    MethodType type = handle.type();
    Class<?>[] params = type.parameterArray();
    int p = 0, pcount = type.parameterCount();
    boolean callerContext = false;

    // First three parameters are (ExecutionContext, ExecutionContext?, Object=ThisValue)
    if (!(p < pcount && ExecutionContext.class.equals(params[p++]))) {
        throw new IllegalArgumentException(type.toString());
    }
    if (p < pcount && ExecutionContext.class.equals(params[p])) {
        callerContext = true;
        p++;
    }
    if (!(p < pcount && Object.class.equals(params[p++]))) {
        throw new IllegalArgumentException(type.toString());
    }
    // Always required to return Object (for now at least)
    if (!Object.class.equals(type.returnType())) {
        throw new IllegalArgumentException(type.toString());
    }
    // Collect remaining arguments into Object[]
    if (!(p + 1 == pcount && Object[].class.equals(params[p]))) {
        final int fixedArguments = callerContext ? 3 : 2;
        final boolean varargs = handle.isVarargsCollector();
        // Otherwise all trailing arguments need to be of type Object or Object[]
        for (; p < pcount; ++p) {
            if (Object.class.equals(params[p])) {
                continue;
            }
            if (p + 1 == pcount && Object[].class.equals(params[p]) && varargs) {
                continue;
            }
            throw new IllegalArgumentException(type.toString());
        }
        // Trailing Object[] arguments are no longer spread in var-args methods (jdk8u40, jdk9).
        if (varargs) {
            handle = handle.asFixedArity();
        }
        // Convert to (ExecutionContext, Object, ...) -> Object handle
        handle = toCanonical(handle, fixedArguments, varargs, method);
    }
    if (!callerContext) {
        handle = MethodHandles.dropArguments(handle, 1, ExecutionContext.class);
    }

    // assert handle.type().parameterCount() == 4;
    // assert handle.type().parameterType(0) == ExecutionContext.class;
    // assert handle.type().parameterType(1) == ExecutionContext.class;
    // assert handle.type().parameterType(2) == Object.class;
    // assert handle.type().parameterType(3) == Object[].class;
    // assert handle.type().returnType() == Object.class;

    return handle;
}
 
Example 13
Source File: Misc.java    From uima-uimaj with Apache License 2.0 3 votes vote down vote up
/**
 * Given a class, a lookup context, and a protected method and its arg classes,
 * return the method handle for that method.
 * 
 * Using that method handle is slow, but converting it to a lambda makes for
 * JIT-able fast access.
 * 
 * @param clazz -
 * @param methodHandleAccessContext -
 * @param protectedMethod -
 * @param args -
 * @return -
 */
static public MethodHandle getProtectedMethodHandle(Class<?> clazz, Lookup methodHandleAccessContext, String protectedMethod, Class<?> ... args) {
  try {
    Method m = clazz.getDeclaredMethod(protectedMethod, args);
    m.setAccessible(true);    
    return methodHandleAccessContext.unreflect(m);
  } catch (NoSuchMethodException | SecurityException | IllegalAccessException e) {
    throw new RuntimeException(e);
  }   
}