コード例 #1
0
 private FieldBridge getFieldBridgeFromBridgeProvider(
     BridgeProvider bridgeProvider,
     ExtendedBridgeProvider.ExtendedBridgeProviderContext context,
     ContainerType containerType) {
   FieldBridge bridge = bridgeProvider.provideFieldBridge(context);
   if (bridge == null) {
     return null;
   }
   populateReturnType(context.getReturnType(), bridge.getClass(), bridge);
   switch (containerType) {
     case SINGLE:
       return bridge;
     case ITERABLE:
       // Should we cache these per bridge instance?
       // would make sense at least for the known built-in bridges
       // but is that worth it?
       return new BuiltinIterableBridge(bridge);
     case ARRAY:
       return new BuiltinArrayBridge(bridge);
     case MAP:
       return new BuiltinMapBridge(bridge);
     default:
       throw new AssertionFailure("Unknown ContainerType " + containerType);
   }
 }
コード例 #2
0
  public FieldBridge guessType(
      Field field,
      NumericField numericField,
      XMember member,
      ReflectionManager reflectionManager,
      ServiceManager serviceManager) {
    FieldBridge bridge = findExplicitFieldBridge(field, member, reflectionManager);
    if (bridge != null) {
      return bridge;
    }

    ExtendedBridgeProvider.ExtendedBridgeProviderContext context =
        new XMemberBridgeProviderContext(member, numericField, reflectionManager, serviceManager);
    ContainerType containerType = getContainerType(member, reflectionManager);

    // We do annotation based providers as Tike at least needs priority over
    // default providers because it might override the type for String
    // TODO: introduce the notion of bridge contributor annotations to cope with this in the future
    for (BridgeProvider provider : annotationBasedProviders) {
      bridge = getFieldBridgeFromBridgeProvider(provider, context, containerType);
      if (bridge != null) {
        return bridge;
      }
    }

    // walk through all regular bridges and if multiple match
    // raise an exception containing the conflicting bridges
    StringBuilder multipleMatchError = null;
    BridgeProvider initialMatchingBridgeProvider = null;
    for (BridgeProvider provider : regularProviders) {
      FieldBridge createdBridge =
          getFieldBridgeFromBridgeProvider(provider, context, containerType);
      if (createdBridge != null) {
        // oops we found a duplicate
        if (bridge != null) {
          // first duplicate, add the initial bridge
          if (multipleMatchError == null) {
            multipleMatchError =
                new StringBuilder("\n")
                    .append("FieldBridge: ")
                    .append(bridge)
                    .append(" - BridgeProvider: ")
                    .append(initialMatchingBridgeProvider.getClass());
          }
          multipleMatchError
              .append("\n")
              .append("FieldBridge: ")
              .append(createdBridge)
              .append(" - BridgeProvider: ")
              .append(provider.getClass());
        } else {
          bridge = createdBridge;
          initialMatchingBridgeProvider = provider;
        }
      }
    }
    if (multipleMatchError != null) {
      throw LOG.multipleMatchingFieldBridges(
          member, member.getType(), multipleMatchError.toString());
    }
    if (bridge != null) {
      return bridge;
    }

    throw LOG.unableToGuessFieldBridge(member.getType().getName(), member.getName());
  }