/** 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); }
@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); }
@SuppressWarnings("unchecked") @Override public JS visit(WriterVisitor<JS> visitor, InstanceOfTree tree, GenerationContext<JS> context) { // build stjs.isInstanceOf(expr.constructor, type); // TODO do I need a check or parenthesis around !? TypeMirror type = context.getTrees().getTypeMirror(new TreePath(context.getCurrentPath(), tree.getType())); JS getConstructor = context .js() .property(visitor.scan(tree.getExpression(), context), JavascriptKeywords.CONSTRUCTOR); JS targetInst = context.js().property(context.js().name("stjs"), "isInstanceOf"); JS typeName = context.js().name(context.getNames().getTypeName(context, type)); return context.js().functionCall(targetInst, Arrays.asList(getConstructor, typeName)); }