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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#autoboxesTo() . 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_t.java    From coming with MIT License 6 votes vote down vote up
private ConcreteType maybeAddAutoboxes(
    ConcreteType cType, JSType jsType, String prop) {
  jsType = jsType.restrictByNotNullOrUndefined();
  if (jsType.isUnionType()) {
    for (JSType alt : jsType.toMaybeUnionType().getAlternates()) {
      cType = maybeAddAutoboxes(cType, alt, prop);
    }
    return cType;
  } else if (jsType.isEnumElementType()) {
    return maybeAddAutoboxes(
        cType, jsType.toMaybeEnumElementType().getPrimitiveType(), prop);
  }

  if (jsType.autoboxesTo() != null) {
    JSType autoboxed = jsType.autoboxesTo();
    return cType.unionWith(tt.getConcreteInstance((ObjectType) autoboxed));
  } else if (jsType.unboxesTo() != null) {
    return cType.unionWith(tt.getConcreteInstance((ObjectType) jsType));
  }

  return cType;
}
 
Example 2
Source File: SymbolTable.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void visitTypeNode(SymbolScope scope, Node n) {
  if (n.isString()) {
    Symbol symbol = scope.getSlot(n.getString());
    if (symbol == null) {
      // If we can't find this type, it might be a reference to a
      // primitive type (like {string}). Autobox it to check.
      JSType type = typeRegistry.getType(n.getString());
      JSType autobox = type == null ? null : type.autoboxesTo();
      symbol = autobox == null
          ? null : getSymbolForTypeHelper(autobox, true);
    }
    if (symbol != null) {
      symbol.defineReferenceAt(n);
    }
  }

  for (Node child = n.getFirstChild();
       child != null; child = child.getNext()) {
    visitTypeNode(scope, child);
  }
}
 
Example 3
Source File: Closure_103_DisambiguateProperties_t.java    From coming with MIT License 6 votes vote down vote up
private ConcreteType maybeAddAutoboxes(
    ConcreteType cType, JSType jsType, String prop) {
  jsType = jsType.restrictByNotNullOrUndefined();
  if (jsType instanceof UnionType) {
    for (JSType alt : ((UnionType) jsType).getAlternates()) {
      return maybeAddAutoboxes(cType, alt, prop);
    }
  }

  if (jsType.autoboxesTo() != null) {
    JSType autoboxed = jsType.autoboxesTo();
    return cType.unionWith(tt.getConcreteInstance((ObjectType) autoboxed));
  } else if (jsType.unboxesTo() != null) {
    return cType.unionWith(tt.getConcreteInstance((ObjectType) jsType));
  }

  return cType;
}
 
Example 4
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 6 votes vote down vote up
private ConcreteType maybeAddAutoboxes(
    ConcreteType cType, JSType jsType, String prop) {
  jsType = jsType.restrictByNotNullOrUndefined();
  if (jsType instanceof UnionType) {
    for (JSType alt : ((UnionType) jsType).getAlternates()) {
      return maybeAddAutoboxes(cType, alt, prop);
    }
  }

  if (jsType.autoboxesTo() != null) {
    JSType autoboxed = jsType.autoboxesTo();
    return cType.unionWith(tt.getConcreteInstance((ObjectType) autoboxed));
  } else if (jsType.unboxesTo() != null) {
    return cType.unionWith(tt.getConcreteInstance((ObjectType) jsType));
  }

  return cType;
}
 
Example 5
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private ConcreteType maybeAddAutoboxes(
    ConcreteType cType, JSType jsType, String prop) {
  jsType = jsType.restrictByNotNullOrUndefined();
  if (jsType.isUnionType()) {
    for (JSType alt : jsType.toMaybeUnionType().getAlternates()) {
      cType = maybeAddAutoboxes(cType, alt, prop);
    }
    return cType;
  } else if (jsType.isEnumElementType()) {
    return maybeAddAutoboxes(
        cType, jsType.toMaybeEnumElementType().getPrimitiveType(), prop);
  }

  if (jsType.autoboxesTo() != null) {
    JSType autoboxed = jsType.autoboxesTo();
    return cType.unionWith(tt.getConcreteInstance((ObjectType) autoboxed));
  } else if (jsType.unboxesTo() != null) {
    return cType.unionWith(tt.getConcreteInstance((ObjectType) jsType));
  }

  return cType;
}
 
Example 6
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 5 votes vote down vote up
@Override public ObjectType getTypeWithProperty(String field, JSType type) {
  if (!(type instanceof ObjectType)) {
    if (type.autoboxesTo() != null) {
      type = type.autoboxesTo();
    } else {
      return null;
    }
  }

  // Ignore the prototype itself at all times.
  if ("prototype".equals(field)) {
    return null;
  }

  // We look up the prototype chain to find the highest place (if any) that
  // this appears.  This will make references to overriden properties look
  // like references to the initial property, so they are renamed alike.
  ObjectType foundType = null;
  ObjectType objType = ObjectType.cast(type);
  while (objType != null && objType.getImplicitPrototype() != objType) {
    if (objType.hasOwnProperty(field)) {
      foundType = objType;
    }
    objType = objType.getImplicitPrototype();
  }
  // If the property does not exist on the referenced type but the original
  // type is an object type, see if any subtype has the property.
    // getGreatestSubtypeWithProperty does not guarantee that the property
    // is defined on the returned type, it just indicates that it might be,
    // so we have to double check.
  return foundType;
}
 
Example 7
Source File: Closure_103_DisambiguateProperties_t.java    From coming with MIT License 5 votes vote down vote up
@Override public ObjectType getTypeWithProperty(String field, JSType type) {
  if (!(type instanceof ObjectType)) {
    if (type.autoboxesTo() != null) {
      type = type.autoboxesTo();
    } else {
      return null;
    }
  }

  // Ignore the prototype itself at all times.
  if ("prototype".equals(field)) {
    return null;
  }

  // We look up the prototype chain to find the highest place (if any) that
  // this appears.  This will make references to overriden properties look
  // like references to the initial property, so they are renamed alike.
  ObjectType foundType = null;
  ObjectType objType = ObjectType.cast(type);
  while (objType != null && objType.getImplicitPrototype() != objType) {
    if (objType.hasOwnProperty(field)) {
      foundType = objType;
    }
    objType = objType.getImplicitPrototype();
  }
  // If the property does not exist on the referenced type but the original
  // type is an object type, see if any subtype has the property.
  if (foundType == null) {
    ObjectType maybeType = ObjectType.cast(
        registry.getGreatestSubtypeWithProperty(type, field));
    // getGreatestSubtypeWithProperty does not guarantee that the property
    // is defined on the returned type, it just indicates that it might be,
    // so we have to double check.
    if (maybeType != null && maybeType.hasOwnProperty(field)) {
      foundType = maybeType;
    }
  }
  return foundType;
}
 
Example 8
Source File: Closure_117_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the type of a switch condition matches the type of its
 * case condition.
 */
void expectSwitchMatchesCase(NodeTraversal t, Node n, JSType switchType,
    JSType caseType) {
  // ECMA-262, page 68, step 3 of evaluation of CaseBlock,
  // but allowing extra autoboxing.
  // TODO(user): remove extra conditions when type annotations
  // in the code base have adapted to the change in the compiler.
  if (!switchType.canTestForShallowEqualityWith(caseType) &&
      (caseType.autoboxesTo() == null ||
          !caseType.autoboxesTo().isSubtype(switchType))) {
    mismatch(t, n.getFirstChild(),
        "case expression doesn't match switch",
        caseType, switchType);
  }
}
 
Example 9
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the type of a switch condition matches the type of its
 * case condition.
 */
void expectSwitchMatchesCase(NodeTraversal t, Node n, JSType switchType,
    JSType caseType) {
  // ECMA-262, page 68, step 3 of evaluation of CaseBlock,
  // but allowing extra autoboxing.
  // TODO(user): remove extra conditions when type annotations
  // in the code base have adapted to the change in the compiler.
  if (!switchType.canTestForShallowEqualityWith(caseType) &&
      (caseType.autoboxesTo() == null ||
          !caseType.autoboxesTo().isSubtype(switchType))) {
    mismatch(t, n.getFirstChild(),
        "case expression doesn't match switch",
        caseType, switchType);
  }
}
 
Example 10
Source File: Closure_6_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the type of a switch condition matches the type of its
 * case condition.
 */
void expectSwitchMatchesCase(NodeTraversal t, Node n, JSType switchType,
    JSType caseType) {
  // ECMA-262, page 68, step 3 of evaluation of CaseBlock,
  // but allowing extra autoboxing.
  // TODO(user): remove extra conditions when type annotations
  // in the code base have adapted to the change in the compiler.
  if (!switchType.canTestForShallowEqualityWith(caseType) &&
      (caseType.autoboxesTo() == null ||
          !caseType.autoboxesTo().isSubtype(switchType))) {
    mismatch(t, n.getFirstChild(),
        "case expression doesn't match switch",
        caseType, switchType);
  }
}
 
Example 11
Source File: NameReferenceGraphConstruction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return true if n MUST be a prototype name reference.
 */
private boolean isPrototypeNameReference(Node n) {
  if (!n.isGetProp()) {
    return false;
  }
  JSType type = getType(n.getFirstChild());
  if (type.isUnknownType() || type.isUnionType()) {
    return false;
  }
  return (type.isInstanceType() || type.autoboxesTo() != null);
}
 
Example 12
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 13
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Expect that the type of a switch condition matches the type of its
 * case condition.
 */
void expectSwitchMatchesCase(NodeTraversal t, Node n, JSType switchType,
    JSType caseType) {
  // ECMA-262, page 68, step 3 of evaluation of CaseBlock,
  // but allowing extra autoboxing.
  // TODO(user): remove extra conditions when type annotations
  // in the code base have adapted to the change in the compiler.
  if (!switchType.canTestForShallowEqualityWith(caseType) &&
      (caseType.autoboxesTo() == null ||
          !caseType.autoboxesTo().isSubtype(switchType))) {
    mismatch(t, n.getFirstChild(),
        "case expression doesn't match switch",
        caseType, switchType);
  }
}
 
Example 14
Source File: Closure_103_DisambiguateProperties_t.java    From coming with MIT License 4 votes vote down vote up
@Override public boolean isTypeToSkip(JSType type) {
  return type.isEnumType() || (type.autoboxesTo() != null);
}
 
Example 15
Source File: Closure_118_DisambiguateProperties_t.java    From coming with MIT License 4 votes vote down vote up
@Override public boolean isTypeToSkip(JSType type) {
  return type.isEnumType() || (type.autoboxesTo() != null);
}
 
Example 16
Source File: Closure_118_DisambiguateProperties_t.java    From coming with MIT License 4 votes vote down vote up
@Override public ObjectType getTypeWithProperty(String field, JSType type) {
  if (type == null) {
    return null;
  }

  if (type.isEnumElementType()) {
    return getTypeWithProperty(
        field, type.toMaybeEnumElementType().getPrimitiveType());
  }

  if (!(type instanceof ObjectType)) {
    if (type.autoboxesTo() != null) {
      type = type.autoboxesTo();
    } else {
      return null;
    }
  }

  // Ignore the prototype itself at all times.
  if ("prototype".equals(field)) {
    return null;
  }

  // We look up the prototype chain to find the highest place (if any) that
  // this appears.  This will make references to overridden properties look
  // like references to the initial property, so they are renamed alike.
  ObjectType foundType = null;
  ObjectType objType = ObjectType.cast(type);
  if (objType != null && objType.getConstructor() != null
      && objType.getConstructor().isInterface()) {
    ObjectType topInterface = FunctionType.getTopDefiningInterface(
        objType, field);
    if (topInterface != null && topInterface.getConstructor() != null) {
      foundType = topInterface.getConstructor().getPrototype();
    }
  } else {
    while (objType != null && objType.getImplicitPrototype() != objType) {
      if (objType.hasOwnProperty(field)) {
        foundType = objType;
      }
      objType = objType.getImplicitPrototype();
    }
  }

  // If the property does not exist on the referenced type but the original
  // type is an object type, see if any subtype has the property.
  if (foundType == null) {
    ObjectType maybeType = ObjectType.cast(
        registry.getGreatestSubtypeWithProperty(type, field));
    // getGreatestSubtypeWithProperty does not guarantee that the property
    // is defined on the returned type, it just indicates that it might be,
    // so we have to double check.
    if (maybeType != null && maybeType.hasOwnProperty(field)) {
      foundType = maybeType;
    }
  }
  return foundType;
}
 
Example 17
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 4 votes vote down vote up
@Override public boolean isTypeToSkip(JSType type) {
  return type.isEnumType() || (type.autoboxesTo() != null);
}
 
Example 18
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 4 votes vote down vote up
@Override public ObjectType getTypeWithProperty(String field, JSType type) {
  if (type == null) {
    return null;
  }

  if (type.isEnumElementType()) {
    return getTypeWithProperty(
        field, type.toMaybeEnumElementType().getPrimitiveType());
  }

  if (!(type instanceof ObjectType)) {
    if (type.autoboxesTo() != null) {
      type = type.autoboxesTo();
    } else {
      return null;
    }
  }

  // Ignore the prototype itself at all times.
  if ("prototype".equals(field)) {
    return null;
  }

  // We look up the prototype chain to find the highest place (if any) that
  // this appears.  This will make references to overridden properties look
  // like references to the initial property, so they are renamed alike.
  ObjectType foundType = null;
  ObjectType objType = ObjectType.cast(type);
  if (objType != null && objType.getConstructor() != null
      && objType.getConstructor().isInterface()) {
    ObjectType topInterface = FunctionType.getTopDefiningInterface(
        objType, field);
    if (topInterface != null && topInterface.getConstructor() != null) {
      foundType = topInterface.getConstructor().getPrototype();
    }
  } else {
    while (objType != null && objType.getImplicitPrototype() != objType) {
      if (objType.hasOwnProperty(field)) {
        foundType = objType;
      }
      objType = objType.getImplicitPrototype();
    }
  }

  // If the property does not exist on the referenced type but the original
  // type is an object type, see if any subtype has the property.
  if (foundType == null) {
    ObjectType maybeType = ObjectType.cast(
        registry.getGreatestSubtypeWithProperty(type, field));
    // getGreatestSubtypeWithProperty does not guarantee that the property
    // is defined on the returned type, it just indicates that it might be,
    // so we have to double check.
    if (maybeType != null && maybeType.hasOwnProperty(field)) {
      foundType = maybeType;
    }
  }
  return foundType;
}
 
Example 19
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override public ObjectType getTypeWithProperty(String field, JSType type) {
  if (type == null) {
    return null;
  }

  if (type.isEnumElementType()) {
    return getTypeWithProperty(
        field, type.toMaybeEnumElementType().getPrimitiveType());
  }

  if (!(type instanceof ObjectType)) {
    if (type.autoboxesTo() != null) {
      type = type.autoboxesTo();
    } else {
      return null;
    }
  }

  // Ignore the prototype itself at all times.
  if ("prototype".equals(field)) {
    return null;
  }

  // We look up the prototype chain to find the highest place (if any) that
  // this appears.  This will make references to overridden properties look
  // like references to the initial property, so they are renamed alike.
  ObjectType foundType = null;
  ObjectType objType = ObjectType.cast(type);
  if (objType != null && objType.getConstructor() != null
      && objType.getConstructor().isInterface()) {
    ObjectType topInterface = FunctionType.getTopDefiningInterface(
        objType, field);
    if (topInterface != null && topInterface.getConstructor() != null) {
      foundType = topInterface.getConstructor().getPrototype();
    }
  } else {
    while (objType != null && objType.getImplicitPrototype() != objType) {
      if (objType.hasOwnProperty(field)) {
        foundType = objType;
      }
      objType = objType.getImplicitPrototype();
    }
  }

  // If the property does not exist on the referenced type but the original
  // type is an object type, see if any subtype has the property.
  if (foundType == null) {
    ObjectType maybeType = ObjectType.cast(
        registry.getGreatestSubtypeWithProperty(type, field));
    // getGreatestSubtypeWithProperty does not guarantee that the property
    // is defined on the returned type, it just indicates that it might be,
    // so we have to double check.
    if (maybeType != null && maybeType.hasOwnProperty(field)) {
      foundType = maybeType;
    }
  }
  return foundType;
}
 
Example 20
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override public boolean isTypeToSkip(JSType type) {
  return type.isEnumType() || (type.autoboxesTo() != null);
}