Exemplo n.º 1
0
  /** super(args) -> SuperType.call(this, args) */
  private JS callToSuperConstructor(
      WriterVisitor<JS> visitor, MethodInvocationTree tree, GenerationContext<JS> context) {
    Element methodElement = TreeUtils.elementFromUse(tree);
    TypeElement typeElement = (TypeElement) methodElement.getEnclosingElement();

    String methodName = MethodInvocationWriter.buildMethodName(tree);

    // avoid useless call to super() when the super class is Object
    if (GeneratorConstants.SUPER.equals(methodName)
        && JavaNodes.sameRawType(typeElement.asType(), Object.class)) {
      return null;
    }

    // avoid call to super for synthetic types
    if (GeneratorConstants.SUPER.equals(methodName)
        && context.getCurrentWrapper().getEnclosingType().isSyntheticType()) {
      return null;
    }

    // transform it into superType.[prototype.method].call(this, args..);
    String typeName = context.getNames().getTypeName(context, typeElement, DependencyType.STATIC);
    JS superType =
        context
            .js()
            .name(
                GeneratorConstants.SUPER.equals(methodName)
                    ? typeName
                    : typeName + ".prototype." + methodName);

    List<JS> arguments = MethodInvocationWriter.buildArguments(visitor, tree, context);
    arguments.add(0, context.js().keyword(Keyword.THIS));
    return context.js().functionCall(context.js().property(superType, "call"), arguments);
  }
Exemplo n.º 2
0
  @Override
  public JS visit(
      WriterVisitor<JS> visitor, MethodInvocationTree tree, GenerationContext<JS> context) {
    if (isCallToSuperConstructor(tree)) {
      return callToSuperConstructor(visitor, tree, context);
    }

    JS target =
        MethodInvocationWriter.buildTarget(
            visitor, context.<MethodInvocationTree>getCurrentWrapper());
    String name = MethodInvocationWriter.buildMethodName(tree);
    List<JS> arguments = MethodInvocationWriter.buildArguments(visitor, tree, context);
    return context.js().functionCall(context.js().property(target, name), arguments);
  }