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

The following examples show how to use java.lang.invoke.MethodHandles.Lookup#unreflectGetter() . 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: MarshalBeanToMap.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
private void introspectFields(ArrayList<MarshalField> fieldList,
                              PodImport moduleImport,
                              Class<?> sourceClass)
{
  if (sourceClass == null || Object.class.equals(sourceClass)) {
    return;
  }
  
  introspectFields(fieldList, 
                   moduleImport,
                   sourceClass.getSuperclass());
  
  Lookup lookup = MethodHandles.lookup();
  
  for (Field sourceField : sourceClass.getDeclaredFields()) {
    if (Modifier.isStatic(sourceField.getModifiers())) {
      continue;
    }
    
    try {
      sourceField.setAccessible(true);
      
      ModuleMarshal marshal;
      marshal = moduleImport.marshalArg(sourceField.getType(),
                                        Object.class);
      
      MethodHandle sourceHandle = lookup.unreflectGetter(sourceField);
      sourceHandle = sourceHandle.asType(MethodType.methodType(Object.class, Object.class));
    
      MarshalField fieldMarshal = new MarshalFieldObject(sourceField.getName(),
                                                         marshal, 
                                                         sourceHandle);
      
      fieldList.add(fieldMarshal);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
 
Example 2
Source File: MarshalBean.java    From baratine with GNU General Public License v2.0 4 votes vote down vote up
private boolean introspectFields(ArrayList<MarshalField> fieldList,
                                 RampImport moduleImport,
                                 Class<?> sourceClass,
                                 Class<?> sourceTopClass,
                                 Class<?> targetClass)
{
  if (targetClass == null || Object.class.equals(targetClass)) {
    return true;
  }
  
  boolean isValue;
  
  if (sourceClass != null) {
    isValue = introspectFields(fieldList, 
                               moduleImport,
                               sourceClass.getSuperclass(),
                               sourceTopClass,
                               targetClass.getSuperclass());
  }
  else {
    isValue = introspectFields(fieldList, 
                               moduleImport,
                               null,
                               sourceTopClass,
                               targetClass.getSuperclass());
  }
  
  Lookup lookup = MethodHandles.lookup();
  
  for (Field targetField : targetClass.getDeclaredFields()) {
    if (Modifier.isStatic(targetField.getModifiers())) {
      continue;
    }
    
    Field sourceField = findSourceField(targetField, 
                                        sourceClass, 
                                        sourceTopClass);
    
    if (sourceField == null) {
      continue;
    }
    
    try {
      sourceField.setAccessible(true);
      targetField.setAccessible(true);
      
      ModuleMarshal marshal;
      marshal = moduleImport.marshal(sourceField.getType(),
                                     targetField.getType());
      
      if (! marshal.isValue()) {
        isValue = false;
      }

      MethodHandle sourceHandle = lookup.unreflectGetter(sourceField);
      sourceHandle = sourceHandle.asType(MethodType.methodType(Object.class, Object.class));
      
      MethodHandle targetHandle = lookup.unreflectSetter(targetField);
      targetHandle = targetHandle.asType(MethodType.methodType(void.class, Object.class, Object.class));
      
      MarshalField fieldMarshal
        = new MarshalFieldObject(marshal, sourceHandle, targetHandle, targetField);
      
      fieldList.add(fieldMarshal);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  return isValue;
}
 
Example 3
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));
}