Examples of GeneratorContext


Examples of rocket.generator.rebind.GeneratorContext

  }

  protected Map<Type, Type> findAllWriters(final Set<Type> types) {
    Checker.notNull("parameter:types", types);

    final GeneratorContext context = this.getGeneratorContext();
    context.branch();
    context.info("Finding existing ObjectWriters...");

    final ObjectWriterFinder finder = new ObjectWriterFinder() {
      @Override
      protected Type getImplementingInterface() {
        return SerializationFactoryGenerator.this.getObjectWriter();
      }

      @Override
      protected boolean shouldBeSerialized(final Type type) {
        return SerializationFactoryGenerator.this.isOrHasSerializableSubType(type);
      }
    };

    final Map<Type, Type> typesToWriters = finder.build(types);

    this.logMapping(typesToWriters);

    context.unbranch();

    return typesToWriters;
  }
View Full Code Here

Examples of rocket.generator.rebind.GeneratorContext

  }

  protected void logMapping(final Map<Type, Type> types) {
    Checker.notNull("parameter:types", types);

    final GeneratorContext context = this.getGeneratorContext();

    if (context.isDebugEnabled()) {
      final TreeMap<Type, Type> sorted = new TreeMap<Type, Type>(TypeComparator.INSTANCE);
      sorted.putAll(types);

      final Iterator<Map.Entry<Type, Type>> iterator = sorted.entrySet().iterator();
      while (iterator.hasNext()) {
        final Map.Entry<Type, Type> entry = iterator.next();

        final Type type = entry.getKey();
        final Type type1 = entry.getValue();

        String message = type.getName();
        if (null != type1) {
          message = message + " = " + type1.getName();
        }
        context.debug(message);
      }
    }
  }
View Full Code Here

Examples of rocket.generator.rebind.GeneratorContext

  protected String getResourceName() {
    String fileName = null;
    while (true) {
      // the return type of the getter is also the field type.
      final Type type = this.getFieldType();
      final GeneratorContext context = type.getGeneratorContext();
      if (context.getBoolean().equals(type)) {
        fileName = Constants.READ_FIELD_BOOLEAN_FIELD_TEMPLATE;
        break;
      }
      if (context.getByte().equals(type)) {
        fileName = Constants.READ_FIELD_BYTE_FIELD_TEMPLATE;
        break;
      }
      if (context.getShort().equals(type)) {
        fileName = Constants.READ_FIELD_SHORT_FIELD_TEMPLATE;
        break;
      }
      if (context.getInt().equals(type)) {
        fileName = Constants.READ_FIELD_INT_FIELD_TEMPLATE;
        break;
      }
      if (context.getLong().equals(type)) {
        fileName = Constants.READ_FIELD_LONG_FIELD_TEMPLATE;
        break;
      }
      if (context.getFloat().equals(type)) {
        fileName = Constants.READ_FIELD_FLOAT_FIELD_TEMPLATE;
        break;
      }
      if (context.getDouble().equals(type)) {
        fileName = Constants.READ_FIELD_DOUBLE_FIELD_TEMPLATE;
        break;
      }
      if (context.getChar().equals(type)) {
        fileName = Constants.READ_FIELD_CHAR_FIELD_TEMPLATE;
        break;
      }
      fileName = Constants.READ_FIELD_OBJECT_FIELD_TEMPLATE;
      break;
View Full Code Here

Examples of rocket.generator.rebind.GeneratorContext

  protected Set<Type> getTypesFromAnnotation(final String title, final Type type, final String annotation) {
    Checker.notNull("parameter:type", type);
    Checker.notEmpty("parameter:annotation", annotation);

    final GeneratorContext context = this.getGeneratorContext();
    context.branch();
    context.debug(title);

    final TreeSet<Type> types = new TreeSet<Type>(TypeComparator.INSTANCE);

    final Iterator<String> typeNames = type.getMetadataValues(annotation).iterator();
    while (typeNames.hasNext()) {
      final String typeName = typeNames.next();
      final Type listedType = context.findType(typeName);
      if (null == listedType) {
        this.throwUnableToFindAnnotatedType(typeName, type);
      }

      types.add(listedType);
      context.debug(listedType.getName());
    }

    context.unbranch();
    return types;
  }
View Full Code Here

Examples of rocket.generator.rebind.GeneratorContext

   */
  protected Set<Type> findSerializableTypes(final Type type, final Map<Type, Type> typesToReadersOrWriters) {
    Checker.notNull("parameter:type", type);
    Checker.notNull("typesToReadersOrWriters", typesToReadersOrWriters);

    final GeneratorContext context = this.getGeneratorContext();
    context.branch();
    context.info("Finding all serializable types reachable from " + type);

    final Type map = this.getMap();
    final Type list = this.getList();
    final Type set = this.getSet();

    final ReachableTypesVisitor visitor = new ReachableTypesVisitor() {

      @Override
      protected boolean skipTypeThatImplementsInterface(final Type type, final Type interfacee) {
        return false == SerializationFactoryGenerator.this.isOrHasSerializableSubType(type);
      }

      @Override
      protected boolean skipArray(final Type array) {
        Checker.notNull("parameter:array", array);

        return false;
      }

      @Override
      protected boolean skipType(final Type type) {
        boolean skip = false;

        while (true) {
          // we dont want to visit the fields of $type we already have
          // a reader/writer.
          if (typesToReadersOrWriters.containsKey(type)) {
            skip = true;
            break;
          }
          if (SerializationFactoryGenerator.this.isOrHasSerializableSubType(type)) {
            skip = false;
            break;
          }
          skip = false;
          break;
        }

        return skip;
      }

      @Override
      protected void visitType(final Type type) {
        if (false == SerializationFactoryGenerator.this.isOrHasSerializableSubType(type)) {
          SerializationFactoryGenerator.this.throwEncounteredUnserializableType(type);
        }
        context.branch();
        context.debug(type.getName());
        super.visitType(type);
        context.unbranch();
      }

      @Override
      protected boolean skipSuperType(final Type superType) {
        return !SerializationFactoryGenerator.this.isOrHasSerializableSubType(superType);
      }

      @Override
      protected void visitSuperType(final Type superType) {
        context.branch();
        context.debug(superType.getName());
        super.visitSuperType(superType);
        context.unbranch();
      }

      @Override
      protected boolean skipSubType(final Type subType) {
        return !SerializationFactoryGenerator.this.isOrHasSerializableSubType(subType);
      }

      @Override
      protected void visitSubType(final Type subType) {
        context.branch();
        context.debug(subType.getName());
        super.visitSubType(subType);
        context.unbranch();
      }

      @Override
      protected void visitFields(final Type type) {
        if (false == typesToReadersOrWriters.containsKey(type)) {
          super.visitFields(type);
        }
      }

      /**
       * Skip transient or static fields.
       */
      @Override
      protected boolean skipField(final Field field) {
        Checker.notNull("parameter:field", field);

        return field.isStatic() || field.isTransient();
      }

      /**
       * This method includes special tests to ensure that list/set/map
       * element types are visited.
       *
       * @param field
       */
      @Override
      protected void visitField(final Field field) {
        Checker.notNull("parameter:field", field);

        context.branch();
        context.debug("Field: " + field.getName());

        while (true) {
          final Type fieldType = field.getType();

          if (field.isFinal()) {
            SerializationFactoryGenerator.this.throwFinalFieldEncountered(field);
          }

          if (list.equals(fieldType)) {
            this.processInterface(fieldType);

            final Type elementType = SerializationFactoryGenerator.this.getTypeFromAnnotation(field);
            context.debug(elementType + " (List)");
            this.visitType(elementType);
            break;
          }
          if (set.equals(fieldType)) {
            this.processInterface(fieldType);

            final Type elementType = SerializationFactoryGenerator.this.getTypeFromAnnotation(field);
            context.debug(elementType + " (Set)");
            this.visitType(elementType);
            break;
          }
          if (map.equals(fieldType)) {
            this.processInterface(fieldType);

            final Type keyType = SerializationFactoryGenerator.this.getTypeFromAnnotation(field, 0);
            final Type valueType = SerializationFactoryGenerator.this.getTypeFromAnnotation(field, 1);
            context.debug(keyType + " (Map key)");
            context.debug(valueType + " (Map value)");

            this.visitType(keyType);
            this.visitType(valueType);
            break;
          }

          this.visitType(fieldType);
          break;
        }

        context.unbranch();
      }
    };
    visitor.start(type);

    final Set<Type> found = visitor.getConcreteTypes();

    found.remove(context.getString());
    found.remove(context.getObject());
    context.debug("Removing special cases types \"java.lang.Object\" and \"java.lang.String\".");

    context.unbranch();
    return found;
  }
View Full Code Here

Examples of rocket.generator.rebind.GeneratorContext

  protected void createObjectReaders(final Set<Type> serializables, final Map<Type, Type> typesToReaders) {
    Checker.notNull("parameter:serializables", serializables);
    Checker.notNull("parameter:typesToReaders", typesToReaders);

    final Set<Type> orderedSerializables = this.sortSerializablesIntoHeirarchyOrder(serializables);
    final GeneratorContext context = this.getGeneratorContext();
    context.branch();
    context.info("Creating necessary ObjectReaders.");

    final Iterator<Type> types = orderedSerializables.iterator();
    int skippedGeneratingCount = 0;
    final Set<Type> newReaders = new TreeSet<Type>(TypeComparator.INSTANCE);

    while (types.hasNext()) {
      final Type type = (Type) types.next();
      final Object reader = typesToReaders.get(type);
      if (null != reader) {
        skippedGeneratingCount++;
        continue;
      }

      context.branch();
      final NewConcreteType newReader = this.createObjectReader(type, typesToReaders);
      typesToReaders.put(type, newReader);

      context.branch();
      this.overrideObjectReaderNewInstanceMethod(type, newReader);

      if (false == type.isArray()) {
        this.overrideObjectReaderReadFieldsMethod(newReader, type);
        this.overrideObjectReaderReadMethod(newReader, type);
      }
      this.addSingletonField(newReader);
      context.unbranch();
      context.unbranch();

      newReaders.add(newReader);
    }

    this.writeTypes(newReaders);
    context.unbranch();
  }
View Full Code Here

Examples of rocket.generator.rebind.GeneratorContext

   * @param type
   * @param newtypesToReaders
   * @return
   */
  protected NewConcreteType createObjectReader(final Type type, final Map<Type, Type> typesToReaders) {
    final GeneratorContext context = this.getGeneratorContext();
    context.debug(type.getName());

    final String newTypeName = this.getGeneratedTypeName(type, SerializationConstants.OBJECT_READER_GENERATED_TYPE_SUFFIX,
        "rocket.serialization.client.reader");
    final NewConcreteType newConcreteType = context.newConcreteType(newTypeName);
    newConcreteType.setAbstract(false);
    newConcreteType.setFinal(false);

    context.debug(newTypeName);

    // pick the right super type.
    Type objectReaderSuperType = null;

    while (true) {
      // if its an array simply extend ObjectArrayReader
      if (type.isArray()) {
        objectReaderSuperType = this.getArrayReader();
        break;
      }

      // if super type is object extend ObjectReaderImpl
      Type superType = type.getSuperType();
      if (superType.equals(context.getObject())) {
        objectReaderSuperType = this.getObjectReaderImpl();
        break;
      }

      // find the super types object reader and extend that...
      objectReaderSuperType = (Type) typesToReaders.get(superType);

      if (null == objectReaderSuperType) {
        throw new IllegalStateException("Unable to fetch the reader for the super type \"" + superType
            + "\" whilst processing \"" + type.getName() + "\".");
      }
      break;
    }

    newConcreteType.setSuperType(objectReaderSuperType);
    context.debug("extends " + objectReaderSuperType);

    // add an annotation that marks the type being handled by this
    // ObjectReader
    newConcreteType.addMetaData(SerializationConstants.SERIALIZABLE_TYPE, type.getName());

View Full Code Here

Examples of rocket.generator.rebind.GeneratorContext

  protected void overrideObjectReaderNewInstanceMethodForArrayType(final Type arrayType, final NewConcreteType reader) {
    Checker.notNull("parameter:type", arrayType);
    Checker.notNull("parameter:reader", reader);

    final GeneratorContext context = this.getGeneratorContext();

    final List<Type> parameters = new ArrayList<Type>();
    parameters.add(context.getString());
    parameters.add(this.getObjectInputStream());

    final Method method = reader.getMostDerivedMethod(SerializationConstants.CLIENT_OBJECT_READER_IMPL_NEW_INSTANCE_METHOD,
        parameters);
    final NewMethod newMethod = method.copy(reader);
    newMethod.setAbstract(false);

    final NewArrayInstanceTemplatedFile populateNewArray = new NewArrayInstanceTemplatedFile();
    populateNewArray.setType(arrayType.getComponentType());
    newMethod.setBody(populateNewArray);

    context.debug("Overridden " + newMethod);
  }
View Full Code Here

Examples of rocket.generator.rebind.GeneratorContext

  protected void overrideObjectReaderNewInstanceMethodForNonArrayType(final Type type, final NewConcreteType reader) {
    Checker.notNull("parameter:type", type);
    Checker.notNull("parameter:reader", reader);

    final GeneratorContext context = this.getGeneratorContext();
    final List<Type> parameters = new ArrayList<Type>();
    parameters.add(context.getString());
    parameters.add(this.getObjectInputStream());

    final Method method = reader.getMostDerivedMethod(SerializationConstants.CLIENT_OBJECT_READER_IMPL_NEW_INSTANCE_METHOD,
        parameters);
    final NewMethod newMethod = method.copy(reader);
    newMethod.setAbstract(false);

    final Constructor constructor = type.getConstructor(Collections.EMPTY_LIST);
    final NewInstanceTemplatedFile newInstanceStatement = new NewInstanceTemplatedFile();
    newInstanceStatement.setConstructor(constructor);

    final CodeBlock returnStatement = new CodeBlock() {
      public boolean isEmpty() {
        return false;
      }

      public void write(final SourceWriter writer) {
        writer.print("return ");
        newInstanceStatement.write(writer);
      }
    };

    newMethod.setBody(returnStatement);

    context.debug("Overridden " + newMethod);
  }
View Full Code Here

Examples of rocket.generator.rebind.GeneratorContext

  protected void overrideObjectReaderReadMethod(final NewConcreteType reader, final Type type) {
    Checker.notNull("parameter:reader", reader);
    Checker.notNull("parameter:type", type);

    final GeneratorContext context = this.getGeneratorContext();

    // locate the writeFields method that will be overridden.
    final List<Type> parameterTypes = new ArrayList<Type>();
    parameterTypes.add(context.getObject());
    final Type objectInputStreamType = this.getObjectInputStream();
    parameterTypes.add(objectInputStreamType);

    final Method method = reader.getMostDerivedMethod(SerializationConstants.CLIENT_OBJECT_READER_IMPL_READ_METHOD, parameterTypes);
    final NewMethod newMethod = method.copy(reader);
    newMethod.setNative(false);

    // rename parameters to the same names used in templates...
    final List<MethodParameter> newMethodParameters = (List<MethodParameter>) newMethod.getParameters();
    final NewMethodParameter object = (NewMethodParameter) newMethodParameters.get(0);
    object.setName(SerializationConstants.CLIENT_OBJECT_READER_IMPL_READ_INSTANCE_PARAMETER);
    object.setFinal(true);

    final NewMethodParameter objectInputStreamParameter = (NewMethodParameter) newMethodParameters.get(1);
    objectInputStreamParameter.setName(SerializationConstants.CLIENT_OBJECT_READER_IMPL_READ_OBJECT_INPUT_STREAM_PARAMETER);
    objectInputStreamParameter.setFinal(true);

    final ReadTemplatedFile body = new ReadTemplatedFile();
    body.setType(type);
    newMethod.setBody(body);

    context.debug("Overridden " + newMethod);
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.