Java Code Examples for com.google.javascript.rhino.jstype.JSType#toObjectType()

The following examples show how to use com.google.javascript.rhino.jstype.JSType#toObjectType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 6 votes vote down vote up
@Override public Iterable<JSType> getTypeAlternatives(JSType type) {
  if (type.isUnionType()) {
    return type.toMaybeUnionType().getAlternates();
  } else {
    ObjectType objType = type.toObjectType();
    if (objType != null &&
        objType.getConstructor() != null &&
        objType.getConstructor().isInterface()) {
      List<JSType> list = Lists.newArrayList();
      for (FunctionType impl
               : registry.getDirectImplementors(objType)) {
        list.add(impl.getInstanceType());
      }
      return list;
    } else {
      return null;
    }
  }
}
 
Example 2
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
Map<String, InstanceProperty> getInstanceProperties(JSType type) {
  Map<String, InstanceProperty> properties = new HashMap<>();

  if (type.isConstructor() || type.isInterface()) {
    type = ((FunctionType) type).getInstanceType();
  }

  ObjectType object = type.toObjectType();
  FunctionType ctor = object.getConstructor();
  if (ctor != null) {
    ObjectType prototype = ctor.getPrototype();
    verify(prototype != null);
    properties = getOwnProperties(prototype);
  }
  properties.putAll(getOwnProperties(object));
  return properties;
}
 
Example 3
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override public Iterable<JSType> getTypeAlternatives(JSType type) {
  if (type.isUnionType()) {
    return type.toMaybeUnionType().getAlternates();
  } else {
    ObjectType objType = type.toObjectType();
    if (objType != null &&
        objType.getConstructor() != null &&
        objType.getConstructor().isInterface()) {
      List<JSType> list = Lists.newArrayList();
      for (FunctionType impl
               : registry.getDirectImplementors(objType)) {
        list.add(impl.getInstanceType());
      }
      return list;
    } else {
      return null;
    }
  }
}
 
Example 4
Source File: FieldCleanupPass.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node p) {
  // We are a root GetProp
  if (n.isGetProp() && !p.isGetProp()) {
    String propName = getFieldName(n);
    JSType type = n.getFirstChild().getJSType();
    if (type == null || type.toObjectType() == null) {
      // Note cases like <primitive>.field
      return;
    }
    removeProperty(type.toObjectType(), propName);
  }
  if (n.getJSDocInfo() != null) {
    n.getJSDocInfo().setAssociatedNode(null);
  }
}
 
Example 5
Source File: Closure_118_DisambiguateProperties_t.java    From coming with MIT License 6 votes vote down vote up
@Override public Iterable<JSType> getTypeAlternatives(JSType type) {
  if (type.isUnionType()) {
    return type.toMaybeUnionType().getAlternates();
  } else {
    ObjectType objType = type.toObjectType();
    if (objType != null &&
        objType.getConstructor() != null &&
        objType.getConstructor().isInterface()) {
      List<JSType> list = Lists.newArrayList();
      for (FunctionType impl
               : registry.getDirectImplementors(objType)) {
        list.add(impl.getInstanceType());
      }
      return list;
    } else {
      return null;
    }
  }
}
 
Example 6
Source File: Closure_103_DisambiguateProperties_t.java    From coming with MIT License 6 votes vote down vote up
@Override public Iterable<JSType> getTypeAlternatives(JSType type) {
  if (type.isUnionType()) {
    return ((UnionType) type).getAlternates();
  } else {
    ObjectType objType = type.toObjectType();
    if (objType != null &&
        objType.getConstructor() != null &&
        objType.getConstructor().isInterface()) {
      List<JSType> list = Lists.newArrayList();
      for (FunctionType impl
               : registry.getDirectImplementors(objType)) {
        list.add(impl.getInstanceType());
      }
      return list;
    } else {
      return null;
    }
  }
}
 
Example 7
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 6 votes vote down vote up
@Override public Iterable<JSType> getTypeAlternatives(JSType type) {
  if (type.isUnionType()) {
    return ((UnionType) type).getAlternates();
  } else {
    ObjectType objType = type.toObjectType();
    if (objType != null &&
        objType.getConstructor() != null &&
        objType.getConstructor().isInterface()) {
      List<JSType> list = Lists.newArrayList();
      for (FunctionType impl
               : registry.getDirectImplementors(objType)) {
        list.add(impl.getInstanceType());
      }
      return list;
    } else {
      return null;
    }
  }
}
 
Example 8
Source File: Closure_118_DisambiguateProperties_t.java    From coming with MIT License 5 votes vote down vote up
private Set<JSType> getTypesToSkipForTypeNonUnion(JSType type) {
  Set<JSType> types = Sets.newHashSet();
  JSType skipType = type;
  while (skipType != null) {
    types.add(skipType);

    ObjectType objSkipType = skipType.toObjectType();
    if (objSkipType != null) {
      skipType = objSkipType.getImplicitPrototype();
    } else {
      break;
    }
  }
  return types;
}
 
Example 9
Source File: Closure_103_DisambiguateProperties_t.java    From coming with MIT License 5 votes vote down vote up
private Set<JSType> getTypesToSkipForTypeNonUnion(JSType type) {
  Set<JSType> types = Sets.newHashSet();
  JSType skipType = type;
  while (skipType != null) {
    types.add(skipType);

    ObjectType objSkipType = skipType.toObjectType();
    if (objSkipType != null) {
      skipType = objSkipType.getImplicitPrototype();
    } else {
      break;
    }
  }
  return types;
}
 
Example 10
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 5 votes vote down vote up
private Set<JSType> getTypesToSkipForTypeNonUnion(JSType type) {
  Set<JSType> types = Sets.newHashSet();
  JSType skipType = type;
  while (skipType != null) {
    types.add(skipType);

    ObjectType objSkipType = skipType.toObjectType();
    if (objSkipType != null) {
      skipType = objSkipType.getImplicitPrototype();
    } else {
      break;
    }
  }
  return types;
}
 
Example 11
Source File: NameReferenceGraphConstruction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void recordPrototypePropUse(
    NodeTraversal t, Node n, Node parent) {
  Preconditions.checkArgument(n.isGetProp());
  Node instance = n.getFirstChild();
  JSType instanceType = getType(instance);
  JSType boxedType = instanceType.autoboxesTo();
  instanceType = boxedType != null ? boxedType : instanceType;

  // Retrieves the property.
  ObjectType objType = instanceType.toObjectType();
  Preconditions.checkState(objType != null);

  if (!isExtern) {
    // Don't count reference in extern as a use.
    Reference ref = new Reference(n, parent);

    FunctionType constructor = objType.getConstructor();
    if (constructor != null) {
      String propName = n.getLastChild().getString();
      if (!constructor.getPrototype().hasOwnProperty(propName)) {
        recordSuperClassPrototypePropUse(constructor, propName, ref);
      }

      // TODO(user): TightenType can help a whole lot here.
      recordSubclassPrototypePropUse(constructor, propName, ref);
    } else {
      recordUnknownUse(t, n, parent);
    }
  }
}
 
Example 12
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Set<JSType> getTypesToSkipForTypeNonUnion(JSType type) {
  Set<JSType> types = Sets.newHashSet();
  JSType skipType = type;
  while (skipType != null) {
    types.add(skipType);

    ObjectType objSkipType = skipType.toObjectType();
    if (objSkipType != null) {
      skipType = objSkipType.getImplicitPrototype();
    } else {
      break;
    }
  }
  return types;
}
 
Example 13
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 5 votes vote down vote up
private Set<JSType> getTypesToSkipForTypeNonUnion(JSType type) {
  Set<JSType> types = Sets.newHashSet();
  JSType skipType = type;
  while (skipType != null) {
    types.add(skipType);

    ObjectType objSkipType = skipType.toObjectType();
    if (objSkipType != null) {
      skipType = objSkipType.getImplicitPrototype();
    } else {
      break;
    }
  }
  return types;
}
 
Example 14
Source File: Closure_117_TypeValidator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {
  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }

  // The best type name is the actual type name.
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  }

  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  String qualifiedName = n.getQualifiedName();
  if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}
 
Example 15
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {
  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }

  String qualifiedName = n.getQualifiedName();
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  } else if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}
 
Example 16
Source File: Closure_6_TypeValidator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {
  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }

  String qualifiedName = n.getQualifiedName();
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  } else if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}
 
Example 17
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {
  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }

  String qualifiedName = n.getQualifiedName();
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  } else if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}
 
Example 18
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {

  // The best type name is the actual type name.

  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  }
  String qualifiedName = n.getQualifiedName();
  if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}