protected void encodeMarketDataAliases(
     final FudgeSerializer serializer,
     final MutableFudgeMsg msg,
     final Map<ValueSpecification, Collection<ValueSpecification>> marketDataEntries) {
   final MutableFudgeMsg msgRequirements = msg.addSubMessage(MARKET_DATA_REQUIREMENTS_FIELD, null);
   final MutableFudgeMsg msgAliases = msg.addSubMessage(MARKET_DATA_ALIASES_FIELD, null);
   for (final Map.Entry<ValueSpecification, Collection<ValueSpecification>> requirement :
       marketDataEntries.entrySet()) {
     final ValueSpecification marketData = requirement.getKey();
     serializer.addToMessage(msgRequirements, null, null, marketData);
     if (requirement.getValue().size() == 1) {
       final ValueSpecification alias = requirement.getValue().iterator().next();
       if (alias.equals(marketData)) {
         msgAliases.add(null, null, IndicatorType.INSTANCE);
       } else {
         serializer.addToMessage(msgAliases, null, null, alias);
       }
     } else {
       final MutableFudgeMsg aliases = msgAliases.addSubMessage(null, null);
       aliases.add(null, 0, "list");
       for (ValueSpecification alias : requirement.getValue()) {
         if (alias.equals(marketData)) {
           msgAliases.add(null, null, IndicatorType.INSTANCE);
         } else {
           serializer.addToMessage(aliases, null, null, alias);
         }
       }
     }
   }
 }
 protected void encodeTerminalOutputSpecifications(
     final FudgeSerializer serializer,
     final MutableFudgeMsg msg,
     final Map<ValueSpecification, Set<ValueRequirement>> outputs) {
   final MutableFudgeMsg submsg = msg.addSubMessage(TERMINAL_OUTPUT_SPECIFICATIONS_FIELD, null);
   for (final Map.Entry<ValueSpecification, Set<ValueRequirement>> output : outputs.entrySet()) {
     serializer.addToMessage(submsg, null, MAP_KEY, output.getKey());
     final MutableFudgeMsg submsg2 = submsg.addSubMessage(null, MAP_VALUE);
     for (final ValueRequirement requirement : output.getValue()) {
       serializer.addToMessage(submsg2, null, null, requirement);
     }
   }
 }
 protected void encodeComputationTargets(
     final FudgeSerializer serializer,
     final MutableFudgeMsg msg,
     final Set<ComputationTargetSpecification> targets) {
   final MutableFudgeMsg submsg = msg.addSubMessage(COMPUTATION_TARGETS_FIELD, null);
   for (final ComputationTargetSpecification target : targets) {
     serializer.addToMessage(submsg, null, null, target);
   }
 }
示例#4
0
  @Override
  public MutableFudgeMsg buildMessage(FudgeSerializer serializer, final T auto) {
    MutableFudgeMsg outerMsg = serializer.newMessage();
    outerMsg.add(
        null,
        FudgeSerializer.TYPES_HEADER_ORDINAL,
        FudgeWireType.STRING,
        AutoFudgable.class.getName());
    final K inner = auto.object();
    assertValid(inner.getClass());
    try {
      MutableFudgeMsg msg = outerMsg.addSubMessage("inner", null);
      // save the internal class name
      msg.add(
          null,
          FudgeSerializer.TYPES_HEADER_ORDINAL,
          FudgeWireType.STRING,
          inner.getClass().getName());

      // save the ctor parameters
      List<Object> parameters =
          AccessController.doPrivileged(
              new PrivilegedAction<List<Object>>() {
                @Override
                public List<Object> run() {
                  try {
                    final Constructor<?>[] ctors = inner.getClass().getDeclaredConstructors();
                    // We require that inner classes got only one ctor (anonymous inner classes will
                    // do)
                    // in order to avoid disambiguity
                    if (ctors.length == 1) {
                      final Constructor<?> ctor = ctors[0];
                      // types of parameters of ctor
                      final Class<?>[] parameterTypes = ctor.getParameterTypes();
                      // all declared parameters of the inner class
                      final Field[] fs = inner.getClass().getDeclaredFields();
                      // extracting copiler synthetized fields of inner class
                      // first are the not sinthetized fields (regular ones) we need to skip
                      // then there are compiler synthetized fields with corresponds to ctor
                      // parameters
                      // the last field is the reference to enclosing object so we need to skipp it
                      // as well
                      final Field[] paramFields =
                          Arrays.copyOfRange(fs, fs.length - parameterTypes.length, fs.length - 1);
                      final List<Object> parameters = newArrayList();
                      for (Field paramField : paramFields) {
                        paramField.setAccessible(true);
                        parameters.add(paramField.get(inner));
                      }
                      return parameters;
                    }
                  } catch (IllegalAccessException e) {
                    // Ignore
                  }
                  return null;
                }
              });

      for (Object parameter : parameters) {
        // save the ctor parameter
        serializer.addToMessageWithClassHeaders(msg, null, 1, parameter);
      }
      return outerMsg;

    } catch (RuntimeException ex) {
      throw new FudgeRuntimeException("Unable to serialize: " + inner.getClass().getName(), ex);
    }
  }