Ejemplo n.º 1
0
 static void generateCallable(
     final Tree.QualifiedMemberOrTypeExpression that, String name, final GenerateJsVisitor gen) {
   final Declaration d = that.getDeclaration();
   if (that.getPrimary() instanceof Tree.BaseTypeExpression) {
     // it's a static method ref
     if (name == null) {
       name = gen.memberAccess(that, "");
     }
     if (TypeUtils.isConstructor(d)) {
       Constructor cd = TypeUtils.getConstructor(d);
       final boolean hasTargs =
           BmeGenerator.hasTypeParameters((Tree.BaseTypeExpression) that.getPrimary());
       if (hasTargs) {
         if (that.getDirectlyInvoked()) {
           gen.out(
               gen.qualifiedPath(that, cd),
               gen.getNames().constructorSeparator(cd),
               gen.getNames().name(cd));
         } else {
           BmeGenerator.printGenericMethodReference(
               gen,
               (Tree.BaseTypeExpression) that.getPrimary(),
               "0",
               gen.qualifiedPath(that, cd)
                   + gen.getNames().constructorSeparator(cd)
                   + gen.getNames().name(cd));
         }
       } else {
         gen.qualify(that, cd);
         gen.out(gen.getNames().name(cd));
         if (cd.isValueConstructor()) {
           gen.out("()");
         }
       }
     } else if (d.isStatic()) {
       BmeGenerator.generateStaticReference(that, d, gen);
     } else {
       gen.out("function(x){return ");
       if (BmeGenerator.hasTypeParameters(that)) {
         BmeGenerator.printGenericMethodReference(gen, that, "x", "x." + name);
       } else {
         gen.out(gen.getClAlias(), "jsc$3(x,x.", name, ")");
       }
       gen.out(";}");
     }
     return;
   }
   if (d.isToplevel() && d instanceof Function) {
     // Just output the name
     gen.out(gen.getNames().name(d));
     return;
   }
   String primaryVar = gen.createRetainedTempVar();
   gen.out("(", primaryVar, "=");
   that.getPrimary().visit(gen);
   if (!(that.getStaticMethodReferencePrimary()
       && !TypeUtils.isConstructor(that.getDeclaration()))) {
     gen.out(",");
     final String member =
         (name == null) ? gen.memberAccess(that, primaryVar) : (primaryVar + "." + name);
     if (that.getDeclaration() instanceof Function
         && !((Function) that.getDeclaration()).getTypeParameters().isEmpty()) {
       // Function ref with type parameters
       BmeGenerator.printGenericMethodReference(gen, that, primaryVar, member);
     } else {
       if (that.getUnit().isOptionalType(that.getPrimary().getTypeModel())) {
         gen.out(
             gen.getClAlias(),
             "jsc$3(",
             primaryVar,
             ",",
             gen.getClAlias(),
             "nn$(",
             primaryVar,
             ")?",
             member,
             ":null)");
       } else {
         gen.out(gen.getClAlias(), "jsc$3(", primaryVar, ",", member, ")");
       }
     }
   }
   gen.out(")");
 }