Package com.google.gwt.core.ext.typeinfo

Examples of com.google.gwt.core.ext.typeinfo.JClassType


    return null;
  }

  static JMethod findConstructor(TypeOracle oracle, JType type) {
    JClassType asClass = type.isClassOrInterface();
    if (asClass == null) {
      return null;
    }

    JMethod m = asClass.findMethod(JSFlyweightWrapperGenerator.CREATE_PEER,
        new JType[] {oracle.findType(JavaScriptObject.class.getName())});

    if (m == null || !m.isStatic() || !type.equals(m.getReturnType())) {
      return null;
    }
View Full Code Here


    return m;
  }

  static JField findExtractor(TypeOracle oracle, JType type) {
    JClassType asClass = type.isClassOrInterface();

    while (asClass != null) {
      JField f = asClass.findField(JSWrapperGenerator.EXTRACTOR);
      if (f != null
          && isAssignable(oracle, f.getType().isClassOrInterface(),
              Extractor.class)) {
        return f;
      }
      asClass = asClass.getSuperclass();
    }

    return null;
  }
View Full Code Here

* Encapsulates accessors for boxed primitive values.
*/
class BoxedTypeFragmentGenerator extends FragmentGenerator {
  @Override
  boolean accepts(TypeOracle typeOracle, JType type) {
    JClassType asClass = type.isClass();

    if (asClass == null) {
      return false;
    }

View Full Code Here

  @Override
  String defaultValue(TypeOracle typeOracle, JType type)
      throws UnableToCompleteException {

    JClassType returnType = type.isClassOrInterface();

    if (isAssignable(typeOracle, returnType, Boolean.class)) {
      return "false";

    } else if (isAssignable(typeOracle, returnType, Character.class)) {
View Full Code Here

    TreeLogger logger = context.parentLogger.branch(TreeLogger.DEBUG,
        "Building value getter statement", null);

    SourceWriter sw = context.sw;
    TypeOracle typeOracle = context.typeOracle;
    JClassType returnType = context.returnType.isClassOrInterface();

    // Don't use "x || null" expression because the value may be boolean false
    sw.print("(");
    sw.print(context.parameterName);
    sw.print(" == null || ");
    sw.print(context.parameterName);
    sw.print(" == undefined) ? null : ");
    sw.print("@com.google.gwt.jsio.client.impl.JSONWrapperUtil::createWrapper");

    // Just plow through the Boxed types
    if (isAssignable(typeOracle, returnType, Boolean.class)) {
      sw.print("(Z)(");

    } else if (isAssignable(typeOracle, returnType, Byte.class)) {
      sw.print("(B)(");

    } else if (isAssignable(typeOracle, returnType, Character.class)) {
      sw.print("(C)(");

    } else if (isAssignable(typeOracle, returnType, Double.class)) {
      sw.print("(D)(");

    } else if (isAssignable(typeOracle, returnType, Float.class)) {
      sw.print("(F)(");

    } else if (isAssignable(typeOracle, returnType, Integer.class)) {
      sw.print("(I)(");

    } else if (isAssignable(typeOracle, returnType, Long.class)) {
      sw.print("(J)(");

    } else if (isAssignable(typeOracle, returnType, Short.class)) {
      sw.print("(S)(");

    } else {
      logger.log(TreeLogger.ERROR, "Unknown boxed type "
          + returnType.getQualifiedSourceName(), null);
      throw new UnableToCompleteException();
    }

    sw.print(context.parameterName);
    sw.print(")");
View Full Code Here

  @Override
  void toJS(FragmentGeneratorContext context) throws UnableToCompleteException {
    context.parentLogger.branch(TreeLogger.DEBUG,
        "Building boxed value setter statement", null);
    SourceWriter sw = context.sw;
    JClassType returnType = context.returnType.isClassOrInterface();

    sw.print(context.parameterName);
    if (isAssignable(context.typeOracle, returnType, Boolean.class)) {
      sw.print(".@java.lang.Boolean::booleanValue()()");
    } else if (isAssignable(context.typeOracle, returnType, Character.class)) {
View Full Code Here

    if (extendsJSWrapper == null) {
      return null;
    }

    // Are we looking at JSWrapper<T>; if so, return it's parameterization
    JClassType rawJSWrapper = oracle.findType(JSWrapper.class.getName()).getErasedType();
    JParameterizedType asParam = extendsJSWrapper.isParameterized();
    if (asParam != null && asParam.getErasedType().equals(rawJSWrapper)) {
      return asParam.getTypeArgs()[0].getErasedType();
    }

    // Try the supertype
    JClassType toReturn = findJSWrapperParameterization(oracle,
        extendsJSWrapper.getSuperclass());
    if (toReturn != null) {
      return toReturn;
    }
View Full Code Here

    // The TypeOracle knows about all types in the type system
    final TypeOracle typeOracle = context.getTypeOracle();

    // Get a reference to the type that the generator should implement
    final JClassType sourceType = typeOracle.findType(typeName);

    // Ensure that the requested type exists
    if (sourceType == null) {
      logger.log(TreeLogger.ERROR, "Could not find requested typeName", null);
      throw new UnableToCompleteException();
    }

    // Pick a name for the generated class to not conflict. Enclosing class
    // 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);
View Full Code Here

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

    // Determine the correct expression to use to initialize the object
    JClassType asClass = context.returnType.isClassOrInterface();
    Constructor constructorAnnotation = hasTag(logger, asClass,
        Constructor.class);
    Global globalAnnotation = hasTag(logger, asClass, Global.class);
    String constructor;
    if (globalAnnotation != null) {
      constructor = globalAnnotation.value();
    } else if (constructorAnnotation != null) {
      constructor = "new " + constructorAnnotation.value() + "()";
    } else {
      boolean hasImports = false;
      for (Task t : context.tasks) {
        hasImports |= t.imported != null;

        if (hasImports) {
          break;
        }
      }

      if (!hasImports) {
        // Probably a JSON or pojo-style object
        constructor = "{}";
      } else {
        constructor = "null";
      }
    }

    JClassType parameterization = findJSWrapperParameterization(
        context.typeOracle, asClass);
    if (parameterization == null) {
      parameterization = asClass;
    }

    // Initialize native state of the wrapper
    sw.println("private native JavaScriptObject __nativeInit() /*-{");
    sw.indent();
    sw.print("return ");
    sw.print(constructor);
    sw.println(";");
    sw.outdent();
    sw.println("}-*/;");

    // Allow the backing JSONObject to be accessed
    sw.println("public JavaScriptObject getJavaScriptObject() {");
    sw.indent();
    sw.print("return ");
    sw.print(OBJ);
    sw.println(";");
    sw.outdent();
    sw.println("}");

    // Defer actual parsing to JSONWrapperUtil to take advantage of using
    // a common function implementation between generated classes.
    sw.println("public void setJSONData(String data)");
    sw.println("throws JSONWrapperException {");
    sw.indent();
    sw.println("setJavaScriptObject(JSONWrapperUtil.evaluate(data));");
    sw.outdent();
    sw.println("}");

    // Satisfies JSWrapper and allows generated implementations to
    // efficiently initialize new objects.
    // Method declaration
    sw.print("public " + parameterization.getParameterizedQualifiedSourceName()
        + " setJavaScriptObject(");
    sw.println("JavaScriptObject obj) {");
    sw.indent();

    sw.println("if (obj != null) {");
    sw.indent();
    for (Task t : context.tasks) {
      if (t.imported != null) {
        String fieldName = t.getFieldName(logger);
        sw.print("assert JSONWrapperUtil.hasField(obj, \"");
        sw.print(fieldName);
        sw.print("\") : \"Backing JSO missing imported function ");
        sw.print(fieldName);
        sw.println("\";");
      }
    }
    sw.outdent();
    sw.println("}");
    sw.println("return setJavaScriptObjectNative(obj);");
    sw.outdent();
    sw.println("}");

    // Method declaration
    sw.print("public native " + context.simpleTypeName
        + " setJavaScriptObjectNative(JavaScriptObject obj) /*-{");
    sw.indent();

    if (context.maintainIdentity) {
      // Delete the backing object's reference to the current wrapper
      sw.print("if (");
      sw.print(context.objRef);
      sw.println(") {");
      sw.indent();
      sw.print("delete ");
      sw.print(context.objRef);
      sw.print(".");
      sw.print(BACKREF);
      sw.println(";");
      sw.outdent();
      sw.println("}");
    }

    // If the incoming JSO is null or undefined, reset the JSWrapper
    sw.println("if (!obj) {");
    sw.indent();
    sw.print(context.objRef);
    sw.println(" = null;");
    sw.println("return this;");
    sw.outdent();
    sw.println("}");

    if (context.maintainIdentity) {
      // Verify that the incoming object doesn't already have a wrapper object.
      // If there is a backreference, throw an exception.
      sw.print("if (obj.");
      sw.print(BACKREF);
      sw.println(") {");
      sw.indent();
      sw.println("@com.google.gwt.jsio.client.impl.JSONWrapperUtil::throwMultipleWrapperException()();");
      sw.outdent();
      sw.println("}");
    }

    // Capture the object in the wrapper
    sw.print(context.objRef);
    sw.println(" = obj;");

    if (context.maintainIdentity) {
      // Assign the backreference from the wrapped object to the wrapper
      sw.print(context.objRef);
      sw.print(".");
      sw.print(BACKREF);
      sw.println(" = this;");
    }

    if (!context.readOnly) {
      // Initialize any other fields if the JSWrapper is read-write
      sw.print("this.@");
      sw.print(context.qualifiedTypeName);
      sw.print("::__initializeEmptyFields(Lcom/google/gwt/core/client/JavaScriptObject;)(");
      sw.print(context.objRef);
      sw.println(");");
    }

    sw.println("return this;");
    sw.outdent();
    sw.println("}-*/;");

    // If the generated class will be used with a JSList, we need an Extractor
    // implementation. We'll create an implementation per generated
    // class to ensure that if the class is used with a JSList, only one
    // instance of the Extractor will ever exist.
    sw.println("public final Extractor<"
        + parameterization.getParameterizedQualifiedSourceName()
        + "> getExtractor() {");
    sw.indent();
    sw.print("return ");
    sw.print(EXTRACTOR);
    sw.println(";");
View Full Code Here

   */
  protected void writeEmptyFieldInitializerMethod(final TreeLogger logger,
      final Map<String, Task> propertyAccessors,
      final FragmentGeneratorContext context) throws UnableToCompleteException {
    SourceWriter sw = context.sw;
    JClassType returnType = context.returnType.isClassOrInterface();

    sw.println("private native void __initializeEmptyFields(JavaScriptObject jso) /*-{");
    sw.indent();

    FragmentGeneratorContext subContext = new FragmentGeneratorContext(context);
View Full Code Here

TOP

Related Classes of com.google.gwt.core.ext.typeinfo.JClassType

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.