Package rocket.generator.rebind

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


  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

   * @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

  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

  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

  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

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

    final GeneratorContext context = this.getGeneratorContext();

    final NewMethod newMethod = reader.newMethod();
    newMethod.setAbstract(false);
    newMethod.setFinal(true);
    newMethod.setName(SerializationConstants.CLIENT_OBJECT_READER_IMPL_READ_FIELDS_METHOD);
    newMethod.setReturnType(context.getVoid());
    newMethod.setStatic(false);
    newMethod.setVisibility(Visibility.PUBLIC);

    // rename parameters to the same names used in templates...
    final NewMethodParameter object = newMethod.newParameter();
    object.setName(SerializationConstants.CLIENT_OBJECT_READER_IMPL_READ_INSTANCE_PARAMETER);
    object.setFinal(true);
    object.setType(type);

    final NewMethodParameter objectInputStream = newMethod.newParameter();
    objectInputStream.setName(SerializationConstants.CLIENT_OBJECT_READER_IMPL_READ_FIELDS_OBJECT_INPUT_STREAM_PARAMETER);
    objectInputStream.setFinal(true);
    objectInputStream.setType(this.getObjectInputStream());

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

    // add all fields to the template
    final Iterator<Field> fields = this.filterSerializableFields(type.getFields()).iterator();
    int fieldCount = 0;
    context.branch();

    while (fields.hasNext()) {
      final Field field = fields.next();
      final Method setter = this.createFieldSetter(reader, field);
      body.addFieldSetter(setter);

      fieldCount++;
    }

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

   */
  protected Method createFieldSetter(final NewConcreteType reader, final Field field) {
    Checker.notNull("parameter:reader", reader);
    Checker.notNull("parameter:field", field);

    final GeneratorContext context = this.getGeneratorContext();

    final SetFieldTemplatedFile body = new SetFieldTemplatedFile();
    body.setField(field);

    final NewMethod method = reader.newMethod();
    method.setAbstract(false);
    method.setBody(body);
    method.setFinal(true);
    method.setName(GeneratorHelper.buildSetterName(field.getName()));
    method.setNative(true);
    method.setReturnType(context.getVoid());
    method.setStatic(false);
    method.setVisibility(Visibility.PRIVATE);

    final NewMethodParameter instance = method.newParameter();
    instance.setFinal(true);
    instance.setName(SerializationConstants.CLIENT_OBJECT_READER_IMPL_FIELD_SETTER_INSTANCE_PARAMETER);
    instance.setType(field.getEnclosingType());

    final NewMethodParameter value = method.newParameter();
    value.setFinal(true);
    value.setName(SerializationConstants.CLIENT_OBJECT_READER_IMPL_FIELD_SETTER_VALUE_PARAMETER);

    final Type fieldType = field.getType();
    value.setType(fieldType);

    if (fieldType.equals(context.getLong())) {
      method.addMetaData("com.google.gwt.core.client.UnsafeNativeLong", "");
    }

    context.debug(field.getName());

    return method;
  }
View Full Code Here

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

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

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

    while (types.hasNext()) {
      final Type type = (Type) types.next();
      final Object writer = typesToWriters.get(type);

      if (null != writer) {
        skippedGeneratingCount++;
        continue;
      }

      context.branch();
      final NewConcreteType newWriter = this.createObjectWriter(type, typesToWriters);
      typesToWriters.put(type, newWriter);

      context.branch();
      this.overrideObjectWriterWrite0Method(newWriter, type);
      this.addSingletonField(newWriter);

      context.unbranch();
      context.unbranch();

      newWriters.add(newWriter);
    }

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

   * @return
   */
  protected CodeBlock getRethrowExpectedExceptions() {
    // first build up a list of the exceptions that should be handled.
    final Method method = this.getMethod();
    final GeneratorContext context = method.getGeneratorContext();
    final List<Type> alreadyCaughts = new ArrayList<Type>();
    final Type exception = context.getType(Constants.EXCEPTION);
    final Type runtimeException = context.getType(Constants.RUNTIME_EXCEPTION);

    final List<Type> catchAndRethrow = new ArrayList<Type>();
    final Iterator<Type> thrown = this.getMethod().getThrownTypes().iterator();

    while (thrown.hasNext()) {
View Full Code Here

TOP

Related Classes of rocket.generator.rebind.GeneratorContext

Copyright © 2018 www.massapicom. 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.