com.google.javascript.rhino.jstype.ObjectType Java Examples

The following examples show how to use com.google.javascript.rhino.jstype.ObjectType. 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: TypedScopeCreatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testStubsInExterns4() {
  testSame(
      "Extern.prototype.foo;" +
      "/** @constructor */ function Extern() {}",
      "", null);

  JSType e = globalScope.getVar("Extern").getType();
  assertEquals("function (new:Extern): ?", e.toString());

  ObjectType externProto = ((FunctionType) e).getPrototype();
  assertTrue(globalScope.getRootNode().toStringTree(),
      externProto.hasOwnProperty("foo"));
  assertTrue(externProto.isPropertyTypeInferred("foo"));
  assertEquals("?", externProto.getPropertyType("foo").toString());
  assertTrue(externProto.isPropertyInExterns("foo"));
}
 
Example #3
Source File: Closure_2_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Given a constructor or an interface type, find out whether the unknown
 * type is a supertype of the current type.
 */
private static boolean hasUnknownOrEmptySupertype(FunctionType ctor) {
  Preconditions.checkArgument(ctor.isConstructor() || ctor.isInterface());
  Preconditions.checkArgument(!ctor.isUnknownType());

  // The type system should notice inheritance cycles on its own
  // and break the cycle.
  while (true) {
    ObjectType maybeSuperInstanceType =
        ctor.getPrototype().getImplicitPrototype();
    if (maybeSuperInstanceType == null) {
      return false;
    }
    if (maybeSuperInstanceType.isUnknownType() ||
        maybeSuperInstanceType.isEmptyType()) {
      return true;
    }
    ctor = maybeSuperInstanceType.getConstructor();
    if (ctor == null) {
      return false;
    }
    Preconditions.checkState(ctor.isConstructor() || ctor.isInterface());
  }
}
 
Example #4
Source File: Closure_71_CheckAccessControls_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Returns the deprecation reason for the property if it is marked
 * as being deprecated. Returns empty string if the property is deprecated
 * but no reason was given. Returns null if the property is not deprecated.
 */
private static String getPropertyDeprecationInfo(ObjectType type,
                                                 String prop) {
  JSDocInfo info = type.getOwnPropertyJSDocInfo(prop);
  if (info != null && info.isDeprecated()) {
    if (info.getDeprecationReason() != null) {
      return info.getDeprecationReason();
    }

    return "";
  }
  ObjectType implicitProto = type.getImplicitPrototype();
  if (implicitProto != null) {
    return getPropertyDeprecationInfo(implicitProto, prop);
  }
  return null;
}
 
Example #5
Source File: Closure_41_FunctionTypeBuilder_t.java    From coming with MIT License 6 votes vote down vote up
@Override
public boolean apply(JSType type) {
  ObjectType objectType = ObjectType.cast(type);
  if (objectType == null) {
    reportWarning(EXTENDS_NON_OBJECT, fnName, type.toString());
    return false;
  } else if (objectType.isEmptyType()) {
    reportWarning(RESOLVED_TAG_EMPTY, "@extends", fnName);
    return false;
  } else if (objectType.isUnknownType()) {
    if (hasMoreTagsToResolve(objectType)) {
      return true;
    } else {
      reportWarning(RESOLVED_TAG_EMPTY, "@extends", fnName);
      return false;
    }
  } else {
    return true;
  }
}
 
Example #6
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the super class of the given type that has a constructor.
 */
private JSType getFinalParentClass(JSType type) {
  if (type != null) {
    ObjectType iproto = ObjectType.cast(type).getImplicitPrototype();
    while (iproto != null && iproto.getConstructor() == null) {
      iproto = iproto.getImplicitPrototype();
    }
    if (iproto != null) {
      Node source = iproto.getConstructor().getSource();
      JSDocInfo jsDoc = source != null ? NodeUtil.getBestJSDocInfo(source) : null;
      if (jsDoc != null && jsDoc.isConstant()) {
        return iproto;
      }
    }
  }
  return null;
}
 
Example #7
Source File: Nopol2017_0051_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example #8
Source File: Closure_25_TypeInference_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Declares a property on its owner, if necessary.
 * @return True if a property was declared.
 */
private boolean ensurePropertyDeclaredHelper(
    Node getprop, ObjectType objectType) {
  String propName = getprop.getLastChild().getString();
  String qName = getprop.getQualifiedName();
  if (qName != null) {
    Var var = syntacticScope.getVar(qName);
    if (var != null && !var.isTypeInferred()) {
      // Handle normal declarations that could not be addressed earlier.
      if (propName.equals("prototype") ||
      // Handle prototype declarations that could not be addressed earlier.
          (!objectType.hasOwnProperty(propName) &&
           (!objectType.isInstanceType() ||
               (var.isExtern() && !objectType.isNativeObjectType())))) {
        return objectType.defineDeclaredProperty(
            propName, var.getType(), getprop);
      }
    }
  }
  return false;
}
 
Example #9
Source File: Closure_95_TypedScopeCreator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType instanceof FunctionType) {
    return (FunctionType) propType;
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType instanceof FunctionType) {
        return (FunctionType) propType;
      }
    }
  }

  return null;
}
 
Example #10
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 #11
Source File: NameReferenceGraphConstruction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates the name in the graph if it does not already exist. Also puts all
 * the properties and prototype properties of this name in the graph.
 */
private Name recordClassConstructorOrInterface(
    String name, FunctionType type, @Nullable Node n, @Nullable Node parent,
    @Nullable Node gParent, @Nullable Node rhs) {
  Preconditions.checkArgument(type.isConstructor() || type.isInterface());
  Name symbol = graph.defineNameIfNotExists(name, isExtern);
  if (rhs != null) {
    // TODO(user): record the definition.
    symbol.setType(getType(rhs));
    if (n.isAssign()) {
      symbol.addAssignmentDeclaration(n);
    } else {
      symbol.addFunctionDeclaration(n);
    }
  }
  ObjectType prototype = type.getPrototype();
  for (String prop : prototype.getOwnPropertyNames()) {
    graph.defineNameIfNotExists(
        name + ".prototype." + prop, isExtern);
  }
  return symbol;
}
 
Example #12
Source File: TightenTypes.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private Collection<Action> getImplicitActionsFromArgument(
    Node arg, ObjectType thisType, JSType paramType) {
  if (paramType.isUnionType()) {
    List<Action> actions = Lists.newArrayList();
    for (JSType paramAlt : paramType.toMaybeUnionType().getAlternates()) {
      actions.addAll(
          getImplicitActionsFromArgument(arg, thisType, paramAlt));
    }
    return actions;
  } else if (paramType.isFunctionType()) {
    return Lists.<Action>newArrayList(createExternFunctionCall(
        arg, thisType, paramType.toMaybeFunctionType()));
  } else {
    return Lists.<Action>newArrayList(createExternFunctionCall(
        arg, thisType, null));
  }
}
 
Example #13
Source File: TightenTypes.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a concrete type from the given JSType that includes the concrete
 * types for subtypes and implementing types for any interfaces.
 */
private ConcreteType createTypeWithSubTypes(JSType jsType) {
  ConcreteType ret = ConcreteType.NONE;
  if (jsType.isUnionType()) {
    for (JSType alt : jsType.toMaybeUnionType().getAlternates()) {
      ret = ret.unionWith(createTypeWithSubTypes(alt));
    }
  } else {
    ObjectType instType = ObjectType.cast(jsType);
    if (instType != null &&
        instType.getConstructor() != null &&
        instType.getConstructor().isInterface()) {
      Collection<FunctionType> implementors =
          getTypeRegistry().getDirectImplementors(instType);

      for (FunctionType implementor : implementors) {
        ret = ret.unionWith(createTypeWithSubTypes(
            implementor.getInstanceType()));
      }
    } else {
      ret = ret.unionWith(createUnionWithSubTypes(createType(jsType)));
    }
  }
  return ret;
}
 
Example #14
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invalidates the given type, so that no properties on it will be renamed.
 */
private void addInvalidatingType(JSType type, JSError error) {
  type = type.restrictByNotNullOrUndefined();
  if (type.isUnionType()) {
    for (JSType alt : type.toMaybeUnionType().getAlternates()) {
      addInvalidatingType(alt, error);
    }
  } else if (type.isEnumElementType()) {
    addInvalidatingType(
        type.toMaybeEnumElementType().getPrimitiveType(), error);
  } else {
    typeSystem.addInvalidatingType(type);
    recordInvalidationError(type, error);
    ObjectType objType = ObjectType.cast(type);
    if (objType != null && objType.getImplicitPrototype() != null) {
      typeSystem.addInvalidatingType(objType.getImplicitPrototype());
      recordInvalidationError(objType.getImplicitPrototype(), error);
    }
  }
}
 
Example #15
Source File: Closure_54_TypedScopeCreator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType != null && propType.isFunctionType()) {
    return propType.toMaybeFunctionType();
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType != null && propType.isFunctionType()) {
        return propType.toMaybeFunctionType();
      }
    }
  }

  return null;
}
 
Example #16
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Defines a declared property if it has not been defined yet.
 *
 * This handles the case where a property is declared on an object where
 * the object type is inferred, and so the object type will not
 * be known in {@code TypedScopeCreator}.
 */
private void ensurePropertyDeclared(Node getprop) {
  ObjectType ownerType = ObjectType.cast(
      getJSType(getprop.getFirstChild()).restrictByNotNullOrUndefined());
  if (ownerType != null) {
    ensurePropertyDeclaredHelper(getprop, ownerType);
  }
}
 
Example #17
Source File: Nopol2017_0029_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if any type in the chain has an implicitCast annotation for
 * the given property.
 */
private boolean propertyIsImplicitCast(ObjectType type, String prop) {
  for (; type != null; type = type.getImplicitPrototype()) {
    JSDocInfo docInfo = type.getOwnPropertyJSDocInfo(prop);
    if (docInfo != null && docInfo.isImplicitCast()) {
      return true;
    }
  }
  return false;
}
 
Example #18
Source File: Closure_54_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub delcarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example #19
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public void recordInterfaces(JSType type, JSType relatedType,
                             DisambiguateProperties<JSType>.Property p) {
  ObjectType objType = ObjectType.cast(type);
  if (objType != null) {
    FunctionType constructor;
    if (objType.isFunctionType()) {
      constructor = objType.toMaybeFunctionType();
    } else if (objType.isFunctionPrototypeType()) {
      constructor = objType.getOwnerFunction();
    } else {
      constructor = objType.getConstructor();
    }
    while (constructor != null) {
      for (ObjectType itype : constructor.getImplementedInterfaces()) {
        JSType top = getTypeWithProperty(p.name, itype);
        if (top != null) {
          p.addType(itype, top, relatedType);
        } else {
          recordInterfaces(itype, relatedType, p);
        }

        // If this interface invalidated this property, return now.
        if (p.skipRenaming) {
          return;
        }
      }
      if (constructor.isInterface() || constructor.isConstructor()) {
        constructor = constructor.getSuperClassConstructor();
      } else {
        constructor = null;
      }
    }
  }
}
 
Example #20
Source File: Nopol2017_0027_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Apply special properties that only apply to delegates.
 */
private void applyDelegateRelationship(
    DelegateRelationship delegateRelationship) {
  ObjectType delegatorObject = ObjectType.cast(
      typeRegistry.getType(delegateRelationship.delegator));
  ObjectType delegateBaseObject = ObjectType.cast(
      typeRegistry.getType(delegateRelationship.delegateBase));
  ObjectType delegateSuperObject = ObjectType.cast(
      typeRegistry.getType(codingConvention.getDelegateSuperclassName()));
  if (delegatorObject != null &&
      delegateBaseObject != null &&
      delegateSuperObject != null) {
    FunctionType delegatorCtor = delegatorObject.getConstructor();
    FunctionType delegateBaseCtor = delegateBaseObject.getConstructor();
    FunctionType delegateSuperCtor = delegateSuperObject.getConstructor();

    if (delegatorCtor != null && delegateBaseCtor != null &&
        delegateSuperCtor != null) {
      FunctionParamBuilder functionParamBuilder =
          new FunctionParamBuilder(typeRegistry);
      functionParamBuilder.addRequiredParams(
          getNativeType(U2U_CONSTRUCTOR_TYPE));
      FunctionType findDelegate = typeRegistry.createFunctionType(
          typeRegistry.createDefaultObjectUnion(delegateBaseObject),
          functionParamBuilder.build());

      FunctionType delegateProxy = typeRegistry.createConstructorType(
          delegateBaseObject.getReferenceName() + DELEGATE_PROXY_SUFFIX,
          null, null, null);
      delegateProxy.setPrototypeBasedOn(delegateBaseObject);

      codingConvention.applyDelegateRelationship(
          delegateSuperObject, delegateBaseObject, delegatorObject,
          delegateProxy, findDelegate);
      delegateProxyPrototypes.add(delegateProxy.getPrototype());
    }
  }
}
 
Example #21
Source File: TypedScopeCreator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Resolve any stub declarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example #22
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Check whether there's any property conflict for for a particular super
 * interface
 * @param t The node traversal object that supplies context
 * @param n The node being visited
 * @param functionName The function name being checked
 * @param properties The property names in the super interfaces that have
 * been visited
 * @param currentProperties The property names in the super interface
 * that have been visited
 * @param interfaceType The super interface that is being visited
 */
private void checkInterfaceConflictProperties(NodeTraversal t, Node n,
    String functionName, HashMap<String, ObjectType> properties,
    HashMap<String, ObjectType> currentProperties,
    ObjectType interfaceType) {
  ObjectType implicitProto = interfaceType.getImplicitPrototype();
  Set<String> currentPropertyNames;
  if (implicitProto == null) {
    // This can be the case if interfaceType is proxy to a non-existent
    // object (which is a bad type annotation, but shouldn't crash).
    currentPropertyNames = ImmutableSet.of();
  } else {
    currentPropertyNames = implicitProto.getOwnPropertyNames();
  }
  for (String name : currentPropertyNames) {
    ObjectType oType = properties.get(name);
    if (oType != null) {
      if (!interfaceType.getPropertyType(name).isEquivalentTo(
          oType.getPropertyType(name))) {
        compiler.report(
            t.makeError(n, INCOMPATIBLE_EXTENDED_PROPERTY_TYPE,
                functionName, name, oType.toString(),
                interfaceType.toString()));
      }
    }
    currentProperties.put(name, interfaceType);
  }
  for (ObjectType iType : interfaceType.getCtorExtendedInterfaces()) {
    checkInterfaceConflictProperties(t, n, functionName, properties,
        currentProperties, iType);
  }
}
 
Example #23
Source File: Closure_95_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Apply special properties that only apply to delegates.
 */
private void applyDelegateRelationship(
    DelegateRelationship delegateRelationship) {
  ObjectType delegatorObject = ObjectType.cast(
      typeRegistry.getType(delegateRelationship.delegator));
  ObjectType delegateBaseObject = ObjectType.cast(
      typeRegistry.getType(delegateRelationship.delegateBase));
  ObjectType delegateSuperObject = ObjectType.cast(
      typeRegistry.getType(codingConvention.getDelegateSuperclassName()));
  if (delegatorObject != null &&
      delegateBaseObject != null &&
      delegateSuperObject != null) {
    FunctionType delegatorCtor = delegatorObject.getConstructor();
    FunctionType delegateBaseCtor = delegateBaseObject.getConstructor();
    FunctionType delegateSuperCtor = delegateSuperObject.getConstructor();

    if (delegatorCtor != null && delegateBaseCtor != null &&
        delegateSuperCtor != null) {
      FunctionParamBuilder functionParamBuilder =
          new FunctionParamBuilder(typeRegistry);
      functionParamBuilder.addRequiredParams(
          getNativeType(U2U_CONSTRUCTOR_TYPE));
      FunctionType findDelegate = typeRegistry.createFunctionType(
          typeRegistry.createDefaultObjectUnion(delegateBaseObject),
          functionParamBuilder.build());

      FunctionType delegateProxy = typeRegistry.createConstructorType(
          delegateBaseObject.getReferenceName() + DELEGATE_PROXY_SUFFIX,
          null, null, null);
      delegateProxy.setPrototypeBasedOn(delegateBaseObject);

      codingConvention.applyDelegateRelationship(
          delegateSuperObject, delegateBaseObject, delegatorObject,
          delegateProxy, findDelegate);
      delegateProxyPrototypes.add(delegateProxy.getPrototype());
    }
  }
}
 
Example #24
Source File: TypedScopeCreatorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testInferredProperty2b() {
  testSame("var foo = { /** @type {number} */ Bar: 3 };");
  ObjectType foo = (ObjectType) findNameType("foo", globalScope);
  assertTrue(foo.toString(), foo.hasProperty("Bar"));
  assertEquals("number", foo.getPropertyType("Bar").toString());
  assertFalse(foo.isPropertyTypeInferred("Bar"));
}
 
Example #25
Source File: Closure_35_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
private FlowScope traverseGetElem(Node n, FlowScope scope) {
  scope = traverseChildren(n, scope);
  ObjectType objType = ObjectType.cast(
      getJSType(n.getFirstChild()).restrictByNotNullOrUndefined());
  if (objType != null) {
    JSType type = objType.getParameterType();
    if (type != null) {
      n.setJSType(type);
    }
  }
  return dereferencePointer(n.getFirstChild(), scope);
}
 
Example #26
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the interfaces implemented by this inspected type. If the inspected type is itself an
 * interface, this will return only the extended interfaces, <em>not</em> the interface itself.
 */
public ImmutableSet<NamedType> getImplementedTypes() {
  JSType type = inspectedType.getType();
  if (type.toMaybeFunctionType() == null || !type.toMaybeFunctionType().hasInstanceType()) {
    return ImmutableSet.of();
  }

  ImmutableSet<ObjectType> implementedTypes =
      registry.getImplementedInterfaces(inspectedType.getType());
  if (implementedTypes.isEmpty()) {
    return ImmutableSet.of();
  }

  TypeExpressionParser parser =
      expressionParserFactory.create(linkFactory.withTypeContext(inspectedType));
  TemplateTypeReplacer replacer =
      TemplateTypeReplacer.forPartialReplacement(
          jsRegistry,
          inspectedType.getType().toMaybeFunctionType().getInstanceType().getTemplateTypeMap());

  return implementedTypes
      .stream()
      .map(t -> t.visit(replacer))
      .map(t -> parseNamedType(parser, t))
      .sorted(compareNamedTypes())
      .collect(toImmutableSet());
}
 
Example #27
Source File: Closure_48_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Find the ObjectType associated with the given slot.
 * @param slotName The name of the slot to find the type in.
 * @return An object type, or null if this slot does not contain an object.
 */
private ObjectType getObjectSlot(String slotName) {
  Var ownerVar = scope.getVar(slotName);
  if (ownerVar != null) {
    JSType ownerVarType = ownerVar.getType();
    return ObjectType.cast(ownerVarType == null ?
        null : ownerVarType.restrictByNotNullOrUndefined());
  }
  return null;
}
 
Example #28
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 5 votes vote down vote up
@Override public boolean isInvalidatingType(JSType type) {
  if (type == null || invalidatingTypes.contains(type) ||
      type.isUnknownType() /* unresolved types */) {
    return true;
  }

  ObjectType objType = ObjectType.cast(type);
  return objType != null && !objType.hasReferenceName();
}
 
Example #29
Source File: TypedScopeCreator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Apply special properties that only apply to delegates.
 */
private void applyDelegateRelationship(
    DelegateRelationship delegateRelationship) {
  ObjectType delegatorObject = ObjectType.cast(
      typeRegistry.getType(delegateRelationship.delegator));
  ObjectType delegateBaseObject = ObjectType.cast(
      typeRegistry.getType(delegateRelationship.delegateBase));
  ObjectType delegateSuperObject = ObjectType.cast(
      typeRegistry.getType(codingConvention.getDelegateSuperclassName()));
  if (delegatorObject != null &&
      delegateBaseObject != null &&
      delegateSuperObject != null) {
    FunctionType delegatorCtor = delegatorObject.getConstructor();
    FunctionType delegateBaseCtor = delegateBaseObject.getConstructor();
    FunctionType delegateSuperCtor = delegateSuperObject.getConstructor();

    if (delegatorCtor != null && delegateBaseCtor != null &&
        delegateSuperCtor != null) {
      FunctionParamBuilder functionParamBuilder =
          new FunctionParamBuilder(typeRegistry);
      functionParamBuilder.addRequiredParams(
          getNativeType(U2U_CONSTRUCTOR_TYPE));
      FunctionType findDelegate = typeRegistry.createFunctionType(
          typeRegistry.createDefaultObjectUnion(delegateBaseObject),
          functionParamBuilder.build());

      FunctionType delegateProxy = typeRegistry.createConstructorType(
          delegateBaseObject.getReferenceName() + DELEGATE_PROXY_SUFFIX,
          null, null, null, null);
      delegateProxy.setPrototypeBasedOn(delegateBaseObject);

      codingConvention.applyDelegateRelationship(
          delegateSuperObject, delegateBaseObject, delegatorObject,
          delegateProxy, findDelegate);
      delegateProxyPrototypes.add(delegateProxy.getPrototype());
    }
  }
}
 
Example #30
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Nullable
private DefinedByType getDefinedByComment(
    final LinkFactory linkFactory,
    final NominalType context,
    JSType currentType,
    InstanceProperty property) {
  JSType propertyDefinedOn = property.getDefinedByType();

  if (propertyDefinedOn.isConstructor() || propertyDefinedOn.isInterface()) {
    propertyDefinedOn = propertyDefinedOn.toMaybeFunctionType().getInstanceType();
  }
  if (currentType.equals(propertyDefinedOn)) {
    return null;
  }

  JSType definedByType = stripTemplateTypeInformation(propertyDefinedOn);

  List<NominalType> types = registry.getTypes(definedByType);
  if (types.isEmpty() && definedByType.isInstanceType()) {
    types = registry.getTypes(definedByType.toObjectType().getConstructor());
  }

  TypeExpressionParser parser =
      expressionParserFactory.create(linkFactory.withTypeContext(context));

  if (!types.isEmpty()) {
    definedByType = types.get(0).getType();
    if (definedByType.isConstructor() || definedByType.isInterface()) {
      definedByType =
          stripTemplateTypeInformation(definedByType.toMaybeFunctionType().getInstanceType());
    }
  }

  NamedType type = buildPropertyLink(parser, definedByType, property.getName());
  return DefinedByType.create(
      type, ObjectType.cast(definedByType).getConstructor().isInterface());
}