Пример #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);
  }
Пример #2
0
  private boolean isCallToSuperConstructor(MethodInvocationTree tree) {
    if (!TreeUtils.isSuperCall(tree)) {
      return false;
    }

    Element methodElement = TreeUtils.elementFromUse(tree);
    if (JavaNodes.isStatic(methodElement)) {
      // this is a call of type super.staticMethod(args) -> it should be handled as a simple call to
      // staticMethod
      return false;
    }

    return true;
  }