Package com.google.gwt.user.rebind

Examples of com.google.gwt.user.rebind.ClassSourceFileComposerFactory


                final String modifiedTypeName = typeName.replace('.', '_');
                TypeSerializerCreator tsc = new TypeSerializerCreator(logger, typesSentFromBrowser, typesSentToBrowser, context, "comet." + modifiedTypeName, modifiedTypeName);
        String realize = tsc.realize(logger);
       
        // Create the CometSerializer impl
        ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className);
       
        composerFactory.addImport(Serializer.class.getName());
        composerFactory.addImport(SerialMode.class.getName());
       
        composerFactory.setSuperclass(typeName);
        // TODO is the SERIALIZER required for DE RPC?
        SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter);
        sourceWriter.print("private Serializer SERIALIZER = new " + realize + "();");
        sourceWriter.print("protected Serializer getSerializer() {return SERIALIZER;}");
        sourceWriter.print("public SerialMode getMode() {return SerialMode." + annotation.mode().name() + ";}");
                sourceWriter.print("public SerialMode getPushMode() {return SerialMode." + annotation.pushmode().name() + ";}");
        sourceWriter.commit(logger);
View Full Code Here


      String superClass, String... interfaceNames) {
    PrintWriter printWriter = context.tryCreate(logger, packageName, className);
    if (printWriter == null) {
      return null;
    }
    ClassSourceFileComposerFactory composerFactory
        = new ClassSourceFileComposerFactory(packageName, className);
    composerFactory.addImport("com.google.gwt.core.client.GWT");
    composerFactory.addImport("com.google.gwt.core.client.JavaScriptObject");
   
    if (superClass != null) {
      composerFactory.setSuperclass(superClass);
    }
    for (String interfaceName : interfaceNames) {
      composerFactory.addImplementedInterface(interfaceName);
    }

    composerFactory.addImport("org.timepedia.exporter.client.Exporter");
    return composerFactory.createSourceWriter(context, printWriter);
  }
View Full Code Here

      GeneratorContext context,
      JClassType eventBinderType,
      JClassType targetType) {
    String simpleName = getSimpleGeneratedClassName(eventBinderType);
    String packageName = eventBinderType.getPackage().getName();
    ClassSourceFileComposerFactory composer =
        new ClassSourceFileComposerFactory(packageName, simpleName);

    composer.setSuperclass(AbstractEventBinder.class.getCanonicalName()
        + "<" + targetType.getName() + ">");
    composer.addImplementedInterface(eventBinderType.getName());

    composer.addImport(EventBinder.class.getCanonicalName());
    composer.addImport(EventBus.class.getCanonicalName());
    composer.addImport(GenericEvent.class.getCanonicalName());
    composer.addImport(GenericEventHandler.class.getCanonicalName());
    composer.addImport(HandlerRegistration.class.getCanonicalName());
    composer.addImport(LinkedList.class.getCanonicalName());
    composer.addImport(List.class.getCanonicalName());

    PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName);
    return (printWriter != null) ? composer.createSourceWriter(context, printWriter) : null;
  }
View Full Code Here

    // Pick a name for the generated class to not conflict.
    String generatedSimpleSourceName = sourceType.getSimpleSourceName()
        + "GadgetImpl";

    // Begin writing the generated source.
    ClassSourceFileComposerFactory f = new ClassSourceFileComposerFactory(
        sourceType.getPackage().getName(), generatedSimpleSourceName);
    f.addImport(GWT.class.getName());
    f.setSuperclass(typeName);

    // All source gets written through this Writer
    PrintWriter out = context.tryCreate(logger,
        sourceType.getPackage().getName(), generatedSimpleSourceName);

    // If an implementation already exists, we don't need to do any work
    if (out != null) {

      JClassType userPrefsType = GadgetUtils.getUserPrefsType(logger,
          typeOracle, sourceType);

      // We really use a SourceWriter since it's convenient
      SourceWriter sw = f.createSourceWriter(context, out);
      sw.println("public " + generatedSimpleSourceName + "() {");
      sw.indent();
      sw.println("nativeInit();");
      sw.println("init((" + userPrefsType.getQualifiedSourceName()
          + ")GWT.create(" + userPrefsType.getQualifiedSourceName()
          + ".class));");
      sw.outdent();
      sw.println("}");

      sw.println("private native void nativeInit() /*-{");
      sw.indent();
      for (JClassType interfaceType : sourceType.getImplementedInterfaces()) {
        generateFeatureInitializer(logger, typeOracle, sw, sourceType,
            interfaceType);
      }
      sw.outdent();
      sw.println("}-*/;");

      // Write out the manifest
      OutputStream manifestOut = context.tryCreateResource(logger, typeName
          + ".gadget.xml");
      if (manifestOut == null) {
        logger.log(TreeLogger.ERROR, "Gadget manifest was already created",
            null);
        throw new UnableToCompleteException();
      }

      generateGadgetManifest(logger, typeOracle, sourceType, new PrintWriter(
          new OutputStreamWriter(manifestOut)));
      context.commitResource(logger, manifestOut);

      sw.commit(logger);
    }

    return f.getCreatedClassName();
  }
View Full Code Here

    // Pick a name for the generated class to not conflict.
    String generatedSimpleSourceName = sourceType.getSimpleSourceName()
        + "UserPreferencesImpl";

    // Begin writing the generated source.
    ClassSourceFileComposerFactory f = new ClassSourceFileComposerFactory(
        sourceType.getPackage().getName(), generatedSimpleSourceName);
    f.addImport(GWT.class.getName());
    f.addImport(PreferencesUtil.class.getName());
    f.addImplementedInterface(typeName);

    // All source gets written through this Writer
    PrintWriter out = context.tryCreate(logger,
        sourceType.getPackage().getName(), generatedSimpleSourceName);

    // If an implementation already exists, we don't need to do any work
    if (out != null) {
      JClassType preferenceType = typeOracle.findType(Preference.class.getName().replace('$', '.'));
      assert preferenceType != null;

      // We really use a SourceWriter since it's convenient
      SourceWriter sw = f.createSourceWriter(context, out);

      for (JMethod m : sourceType.getOverridableMethods()) {
        JClassType extendsPreferenceType = m.getReturnType().isClassOrInterface();
        if (extendsPreferenceType == null
            || !preferenceType.isAssignableFrom(extendsPreferenceType)) {
          logger.log(TreeLogger.ERROR, "Cannot assign "
              + extendsPreferenceType.getQualifiedSourceName() + " to "
              + preferenceType.getQualifiedSourceName(), null);
          throw new UnableToCompleteException();
        }

        // private final FooProperty __propName = new FooProperty() {...}
        sw.println("private final "
            + extendsPreferenceType.getParameterizedQualifiedSourceName()
            + " __" + m.getName() + " = ");
        sw.indent();
        writeInstantiation(logger, sw, extendsPreferenceType, m);
        sw.println(";");
        sw.outdent();

        // public FooProperty property() { return __property; }
        sw.print("public ");
        sw.print(m.getReadableDeclaration(true, true, true, true, true));
        sw.println("{");
        sw.indent();
        sw.println("return __" + m.getName() + ";");
        sw.outdent();
        sw.println("}");
      }

      sw.commit(logger);
    }

    return f.getCreatedClassName();
  }
View Full Code Here

      String superClass, String... interfaceNames) {
    PrintWriter printWriter = context.tryCreate(logger, packageName, className);
    if (printWriter == null) {
      return null;
    }
    ClassSourceFileComposerFactory composerFactory
        = new ClassSourceFileComposerFactory(packageName, className);
    composerFactory.addImport("com.google.gwt.core.client.GWT");
    composerFactory.addImport("com.google.gwt.core.client.JavaScriptObject");
   
    if (superClass != null) {
      composerFactory.setSuperclass(superClass);
    }
    for (String interfaceName : interfaceNames) {
      composerFactory.addImplementedInterface(interfaceName);
    }

    composerFactory.addImport("org.timepedia.exporter.client.Exporter");
    composerFactory.addImport("org.timepedia.exporter.client.ExporterUtil");
    return composerFactory.createSourceWriter(context, printWriter);
  }
View Full Code Here

    // names must be preserved.
    final String generatedSimpleSourceName = generateSimpleSourceName(sourceType.getName())
        + locale;

    // Begin writing the generated source.
    final ClassSourceFileComposerFactory f = new ClassSourceFileComposerFactory(
        sourceType.getPackage().getName(), generatedSimpleSourceName);

    // The generated class needs to be able to determine the module base URL
    f.addImport(GWT.class.getName());
    f.addImport(ResourcePrototype.class.getName());

    // Determine the interface to implement
    if (sourceType.isInterface() != null) {
      f.addImplementedInterface(sourceType.getQualifiedSourceName());

    } else {
      // The incoming type wasn't a plain interface, we don't support
      // abstract base classes
      logger.log(TreeLogger.ERROR, sourceType.getQualifiedSourceName()
          + " is not an interface.", null);
      throw new UnableToCompleteException();
    }

    // All source gets written through this Writer
    final PrintWriter out = context.tryCreate(logger,
        sourceType.getPackage().getName(), generatedSimpleSourceName);

    // Aggregates the field names of the resources for use with
    // ResourceBundle.getResources()
    List<String> fieldNames = new ArrayList<String>();

    // If an implementation already exists, we don't need to do any work
    if (out != null) {

      // We really use a SourceWriter since it's convenient
      final SourceWriter sw = f.createSourceWriter(context, out);

      JMethod[] methods = sourceType.getMethods();

      Map<Class<? extends ResourceGenerator>, List<JMethod>> resourceGenerators = new HashMap<Class<? extends ResourceGenerator>, List<JMethod>>();

      ResourceContext resourceContext = createResourceContext(logger, context,
          sourceType, sw);

      // First assemble all of the ResourceGenerators that we may need for the
      // type
      for (JMethod m : methods) {
        JClassType returnType = m.getReturnType().isClassOrInterface();
        if (returnType == null) {
          logger.log(TreeLogger.ERROR, "Cannot implement " + m.getName()
              + ": not a class or interface.", null);
          throw new UnableToCompleteException();
        }

        Class<? extends ResourceGenerator> clazz = findResourceGenerator(
            logger, typeOracle, m);
        List<JMethod> generatorMethods;
        if (resourceGenerators.containsKey(clazz)) {
          generatorMethods = resourceGenerators.get(clazz);
        } else {
          generatorMethods = new ArrayList<JMethod>();
          resourceGenerators.put(clazz, generatorMethods);
        }

        generatorMethods.add(m);
      }

      // Run the ResourceGenerator code
      for (Map.Entry<Class<? extends ResourceGenerator>, List<JMethod>> entry : resourceGenerators.entrySet()) {
        Class<? extends ResourceGenerator> generatorClass = entry.getKey();
        List<JMethod> generatorMethods = entry.getValue();

        // Create the ResourceGenerator
        ResourceGenerator rg;
        try {
          rg = generatorClass.newInstance();
          rg.init(logger.branch(TreeLogger.DEBUG,
              "Initializing ResourceGenerator", null), resourceContext);
        } catch (InstantiationException e) {
          logger.log(TreeLogger.ERROR,
              "Unable to initialize ResourceGenerator", e);
          throw new UnableToCompleteException();
        } catch (IllegalAccessException e) {
          logger.log(TreeLogger.ERROR,
              "Unable to instantiate ResourceGenerator. "
                  + "Does it have a public default constructor?", e);
          throw new UnableToCompleteException();
        }

        // Prepare the ResourceGenerator by telling it all methods that it is
        // expected to produce.
        for (JMethod m : generatorMethods) {
          rg.prepare(logger.branch(TreeLogger.DEBUG, "Preparing method "
              + m.getName(), null), m);
        }

        // Write all field values
        rg.writeFields(logger.branch(TreeLogger.DEBUG, "Writing fields", null));

        // Create the instance variables in the IRB subclass by calling
        // writeAssignment() on the ResourceGenerator
        for (JMethod m : generatorMethods) {
          // Strip off all but the access modifiers
          sw.print(m.getReadableDeclaration(false, true, true, true, true));
          sw.println(" {");
          sw.indent();

          String fieldName = (m.getName() + "_instance").toUpperCase();
          fieldNames.add(fieldName);
          sw.println("return " + fieldName + ";");

          sw.outdent();
          sw.println("}");

          sw.print("private final "
              + m.getReturnType().getQualifiedSourceName() + " " + fieldName
              + " = ");

          rg.writeAssignment(logger.branch(TreeLogger.DEBUG,
              "Writing assignment for " + m.getName(), null), m);

          sw.println(";");
        }

        // Finalize the ResourceGenerator
        rg.finish(logger.branch(TreeLogger.DEBUG,
            "Finishing ResourceGenerator", null));
      }

      // Complete the IRB contract
      sw.println("public ResourcePrototype[] getResources() {");
      sw.indent();
      sw.println("return new ResourcePrototype[] {");
      sw.indent();
      for (String fieldName : fieldNames) {
        sw.println(fieldName + ",");
      }
      sw.outdent();
      sw.println("};");
      sw.outdent();
      sw.println("}");

      // Allow map-style lookup
      sw.println("public native ResourcePrototype "
          + "getResource(String name) /*-{");
      sw.indent();
      sw.println("switch (name) {");
      sw.indent();
      for (JMethod m : methods) {
        sw.println("case '" + m.getName() + "': return this.@"
            + f.getCreatedClassName() + "::"
            + (m.getName() + "_instance").toUpperCase() + ";");
      }
      sw.outdent();
      sw.println("}");
      sw.println("return null;");
      sw.outdent();
      sw.println("}-*/;");

      // Write the generated code to disk
      sw.commit(logger);
    }

    // Return the name of the concrete class
    return f.getCreatedClassName();
  }
View Full Code Here

    // names must be preserved.
    final String generatedSimpleSourceName = "__"
        + sourceType.getName().replaceAll("\\.", "__") + "Impl";

    // Begin writing the generated source.
    final ClassSourceFileComposerFactory f = new ClassSourceFileComposerFactory(
        sourceType.getPackage().getName(), generatedSimpleSourceName);

    // Pull in source imports
    f.addImport(GWT.class.getName());
    f.addImport(JavaScriptObject.class.getName());
    // This is a cheat, but doesn't require excessive maintenance
    f.addImport("com.google.gwt.jsio.client.*");
    f.addImport("com.google.gwt.jsio.client.impl.*");

    // Either extend an abstract base class or implement the interface
    if (sourceType.isClass() != null) {
      f.setSuperclass(sourceType.getQualifiedSourceName());

    } else if (sourceType.isInterface() != null) {
      f.addImplementedInterface(sourceType.getQualifiedSourceName());

    } else {
      // Something is very wrong if this statement is reached
      logger.log(TreeLogger.ERROR,
          "Requested JClassType is neither a class nor an interface.", null);
      throw new UnableToCompleteException();
    }

    // All source gets written through this Writer
    final PrintWriter out = context.tryCreate(logger,
        sourceType.getPackage().getName(), generatedSimpleSourceName);

    // If an implementation already exists, we don't need to do any work
    if (out != null) {
      // We really use a SourceWriter since it's convenient
      final SourceWriter sw = f.createSourceWriter(context, out);

      final Map<String, Task> propertyAccessors = TaskFactory.extractMethods(
          logger, typeOracle, sourceType, getPolicy());

      // Create the base context to be used during generation
      FragmentGeneratorContext fragmentContext = new FragmentGeneratorContext();
      fragmentContext.parentLogger = logger;
      fragmentContext.fragmentGeneratorOracle = FRAGMENT_ORACLE;
      fragmentContext.typeOracle = typeOracle;
      fragmentContext.sw = sw;
      fragmentContext.objRef = "this.@" + f.getCreatedClassName() + "::" + OBJ;
      fragmentContext.simpleTypeName = generatedSimpleSourceName;
      fragmentContext.qualifiedTypeName = f.getCreatedClassName();
      fragmentContext.returnType = sourceType;
      fragmentContext.creatorFixups = new HashSet<JClassType>();
      fragmentContext.readOnly = hasTag(logger, sourceType, ReadOnly.class) != null;
      fragmentContext.maintainIdentity = !(fragmentContext.readOnly || hasTag(
          logger, sourceType, NoIdentity.class) != null);
      fragmentContext.tasks = propertyAccessors.values();

      // Perform sanity checks on the extracted information
      validateType(propertyAccessors, fragmentContext);

      // Write all code that's not implementing methods
      writeBoilerplate(logger, fragmentContext);

      // Write the JSO initializer if required
      if (!fragmentContext.readOnly) {
        writeEmptyFieldInitializerMethod(logger, propertyAccessors,
            fragmentContext);
      }

      writeMethods(fragmentContext, propertyAccessors);
      writeFixups(logger, typeOracle, sw, fragmentContext.creatorFixups);

      // Write the generated code to disk
      sw.commit(logger);
    }

    // Return the name of the concrete class
    return f.getCreatedClassName();
  }
View Full Code Here

                final String modifiedTypeName = typeName.replace('.', '_');
                TypeSerializerCreator tsc = new TypeSerializerCreator(logger, typesSentFromBrowser, typesSentToBrowser, context, "comet." + modifiedTypeName, modifiedTypeName);
                String realize = tsc.realize(logger);

                // Create the CometSerializer impl
                ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className);

                composerFactory.addImport(Serializer.class.getName());
                composerFactory.addImport(SerializationException.class.getName());
                composerFactory.addImport(Serializable.class.getName());

                composerFactory.setSuperclass(typeName);
                SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter);
                sourceWriter.print("private Serializer SERIALIZER = new " + realize + "();");
                sourceWriter.print("protected Serializer getRPCSerializer() {return SERIALIZER;}");
                sourceWriter.commit(logger);

            } catch (NotFoundException e) {
View Full Code Here

        }
        logger.log(Type.INFO, "Detecting available criteria ...");
        Date date = new Date();

        // init composer, set class properties, create source writer
        ClassSourceFileComposerFactory composer = null;
        composer = new ClassSourceFileComposerFactory(packageName, className);
        composer.addImport("com.google.gwt.core.client.GWT");
        composer.setSuperclass("com.vaadin.client.ui.dd.VAcceptCriterionFactory");
        SourceWriter sourceWriter = composer.createSourceWriter(context,
                printWriter);

        // generator constructor source code
        generateInstantiatorMethod(sourceWriter, context, logger);
        // close generated class
View Full Code Here

TOP

Related Classes of com.google.gwt.user.rebind.ClassSourceFileComposerFactory

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.