All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gwt.user.rebind.ClassSourceFileComposerFactory Maven / Gradle / Ivy

Go to download

Everything needed to run a comprehensive dev environment. Just type X_ and pick a service from autocomplete; new dev modules will be added as they are built. The only dev service not included in the uber jar is xapi-dev-maven, as it includes all runtime dependencies of maven, adding ~4 seconds to build time, and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).

The newest version!
/*
 * Copyright 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.user.rebind;

import com.google.gwt.core.ext.GeneratorContext;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Factory clas to create SourceFileComposer instances.
 *
 */
public class ClassSourceFileComposerFactory {
  /**
   * Represents a java source file category. Right now support interface and
   * class, later should support abstract class, static class, etc.
   */
  public static enum JavaSourceCategory {

      CLASS("class"),
      INTERFACE("interface");


    private String key;

    private JavaSourceCategory(final String key) {
      this.key = key;
    }
  }

  private final List annotations = new ArrayList();

  private JavaSourceCategory classCategory = JavaSourceCategory.CLASS;

  private String classComment;

  private final String className;

  private final Set imports = new LinkedHashSet();

  private final Set interfaceNames = new LinkedHashSet();

  private final String packageName;

  private String superClassName;

  private String privacy;

  public ClassSourceFileComposerFactory(final String packageName, final String className) {
    this.packageName = packageName;
    this.className = className;
    this.privacy = "public";
  }

  public void addAnnotationDeclaration(final String declaration) {
    annotations.add(declaration);
  }

  public void addImplementedInterface(final String intfName) {
    interfaceNames.add(intfName);
  }

  public void addImport(final String typeName) {
    imports.add(typeName);
  }

  /**
   * Creates an implementation of {@link SourceWriter} that can be used to write
   * the innards of a class. Note that the subsequent changes to this factory do
   * not affect the returned instance.
   *
   * @throws RuntimeException If the settings on this factory are inconsistent
   *           or invalid
   */
  public SourceWriter createSourceWriter(final GeneratorContext ctx,
      final PrintWriter printWriter) {
    return new ClassSourceFileComposer(ctx, printWriter, getCreatedPackage(),
        getAnnotationDeclarations(), getCreatedClassShortName(),
        getSuperclassName(), getInterfaceNames(), getImports(), classCategory,
        classComment, getPrivacy());
  }

  /**
   * Creates an implementation of {@link SourceWriter} that can be used to write
   * the innards of a class. Note that the subsequent changes to this factory do
   * not affect the returned instance.
   *
   * @param printWriter underlying writer
   * @return the source writer
   * @throws RuntimeException If the settings on this factory are inconsistent
   *           or invalid
   */
  public SourceWriter createSourceWriter(final PrintWriter printWriter) {
    return new ClassSourceFileComposer(null, printWriter, getCreatedPackage(),
        getAnnotationDeclarations(), getCreatedClassShortName(),
        getSuperclassName(), getInterfaceNames(), getImports(), classCategory,
        classComment, getPrivacy());
  }

  public String[] getAnnotationDeclarations() {
    return annotations.toArray(new String[annotations.size()]);
  }

  public String getCreatedClassName() {
    return getCreatedPackage() + "." + getCreatedClassShortName();
  }

  public String getCreatedClassShortName() {
    return className;
  }

  public String getCreatedPackage() {
    return packageName;
  }

  public String[] getInterfaceNames() {
    return interfaceNames.toArray(new String[interfaceNames.size()]);
  }

  public String getSuperclassName() {
    return superClassName;
  }

  public String getPrivacy() {
    return privacy;
  }

  /**
   * This class is an interface.
   */
  public void makeInterface() {
    classCategory = JavaSourceCategory.INTERFACE;
  }

  /**
   * Sets the java doc comment for this.
   *
   * @param comment java doc comment.
   */
  public void setJavaDocCommentForClass(final String comment) {
    classComment = comment;
  }

  public void setSuperclass(final String superclassName) {
    superClassName = superclassName;
  }

  public void setPrivacy(final String privacy) {
    this.privacy = privacy;
  }

  private String[] getImports() {
    return imports.toArray(new String[imports.size()]);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy