Java Code Examples for com.google.javascript.rhino.jstype.ObjectType#getConstructor()

The following examples show how to use com.google.javascript.rhino.jstype.ObjectType#getConstructor() . 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: TypeInspector.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
private Map<String, InstanceProperty> getOwnProperties(ObjectType object) {
  ObjectType definingType = object;
  if (definingType.isFunctionPrototypeType()) {
    definingType = definingType.getOwnerFunction();
  } else if (definingType.isInstanceType()) {
    definingType = definingType.getConstructor();
  }

  Map<String, InstanceProperty> properties = new HashMap<>();
  for (String name : object.getOwnPropertyNames()) {
    if (!"constructor".equals(name)) {
      Property property = object.getOwnSlot(name);
      properties.put(
          property.getName(),
          InstanceProperty.builder()
              .setOwnerType(getFirst(registry.getTypes(definingType), null))
              .setDefinedByType(definingType)
              .setName(property.getName())
              .setType(getType(object, property))
              .setNode(property.getNode())
              .setJsDoc(JsDoc.from(property.getJSDocInfo()))
              .build());
    }
  }
  return properties;
}
 
Example 2
Source File: Nopol2017_0051_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 3
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 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_11_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 5
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 6
Source File: Closure_66_TypeCheck_t.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 7
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
private void addRelatedInterfaces(ObjectType instance, Set<ObjectType> interfaces) {
  FunctionType constructor = instance.getConstructor();
  if (constructor != null && constructor.isInterface() && !interfaces.contains(instance)) {
    interfaces.add(instance);

    for (ObjectType interfaceType : instance.getCtorExtendedInterfaces()) {
      addRelatedInterfaces(interfaceType, interfaces);
    }
  }
}
 
Example 8
Source File: Closure_25_TypeInference_t.java    From coming with MIT License 5 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      if (varType.equals(getNativeType(UNKNOWN_TYPE)) &&
          var != syntacticScope.getSlot(qualifiedName)) {
        // If the type of this qualified name has been checked in this scope,
        // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
        return getNativeType(CHECKED_UNKNOWN_TYPE);
      } else {
        return varType;
      }
    }
  }

  JSType propertyType = null;
  if (objType != null) {
    propertyType = objType.findPropertyType(propName);
  }

  if ((propertyType == null || propertyType.isUnknownType()) &&
      qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  return propertyType;
}
 
Example 9
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the first type is the direct superclass of the second type.
 *
 * @param t The node traversal.
 * @param n The node where warnings should point to.
 * @param superObject The expected super instance type.
 * @param subObject The sub instance type.
 */
void expectSuperType(NodeTraversal t, Node n, ObjectType superObject,
    ObjectType subObject) {
  FunctionType subCtor = subObject.getConstructor();
  ObjectType implicitProto = subObject.getImplicitPrototype();
  ObjectType declaredSuper =
      implicitProto == null ? null : implicitProto.getImplicitPrototype();
  if (declaredSuper != null && declaredSuper.isTemplatizedType()) {
    declaredSuper =
        declaredSuper.toMaybeTemplatizedType().getReferencedType();
  }
  if (declaredSuper != null &&
      !(superObject instanceof UnknownType) &&
      !declaredSuper.isEquivalentTo(superObject)) {
    if (declaredSuper.isEquivalentTo(getNativeType(OBJECT_TYPE))) {
      registerMismatch(superObject, declaredSuper, report(
          t.makeError(n, MISSING_EXTENDS_TAG_WARNING, subObject.toString())));
    } else {
      mismatch(t.getSourceName(), n,
          "mismatch in declaration of superclass type",
          superObject, declaredSuper);
    }

    // Correct the super type.
    if (!subCtor.hasCachedValues()) {
      subCtor.setPrototypeBasedOn(superObject);
    }
  }
}
 
Example 10
Source File: Closure_70_TypedScopeCreator_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 11
Source File: Closure_43_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 12
Source File: Closure_96_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a {@link Token#FUNCTION} node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitFunction(NodeTraversal t, Node n) {
  JSDocInfo info = n.getJSDocInfo();

  FunctionType functionType = (FunctionType) n.getJSType();
  String functionPrivateName = n.getFirstChild().getString();
  if (functionType.isInterface() || functionType.isConstructor()) {
    FunctionType baseConstructor = functionType.
        getPrototype().getImplicitPrototype().getConstructor();
    if (baseConstructor != null &&
        baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE) &&
        (baseConstructor.isConstructor() && functionType.isInterface() ||
         baseConstructor.isInterface() && functionType.isConstructor())) {
      compiler.report(
          t.makeError(n, CONFLICTING_EXTENDED_TYPE, functionPrivateName));
    }

    for (JSType baseInterface : functionType.getImplementedInterfaces()) {
      boolean badImplementedType = false;
      ObjectType baseInterfaceObj = ObjectType.cast(baseInterface);
      if (baseInterfaceObj != null) {
        FunctionType interfaceConstructor =
            baseInterfaceObj.getConstructor();
        if (interfaceConstructor != null &&
            !interfaceConstructor.isInterface()) {
          badImplementedType = true;
        }
      } else {
        badImplementedType = true;
      }
      if (badImplementedType) {
        report(t, n, BAD_IMPLEMENTED_TYPE, functionPrivateName);
      }
    }
    if (functionType.isConstructor()) {
      validator.expectAllInterfacePropertiesImplemented(functionType);
    }
  }
}
 
Example 13
Source File: Closure_54_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 14
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 15
Source File: Closure_117_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the first type is the direct superclass of the second type.
 *
 * @param t The node traversal.
 * @param n The node where warnings should point to.
 * @param superObject The expected super instance type.
 * @param subObject The sub instance type.
 */
void expectSuperType(NodeTraversal t, Node n, ObjectType superObject,
    ObjectType subObject) {
  FunctionType subCtor = subObject.getConstructor();
  ObjectType implicitProto = subObject.getImplicitPrototype();
  ObjectType declaredSuper =
      implicitProto == null ? null : implicitProto.getImplicitPrototype();
  if (declaredSuper != null && declaredSuper.isTemplatizedType()) {
    declaredSuper =
        declaredSuper.toMaybeTemplatizedType().getReferencedType();
  }
  if (declaredSuper != null &&
      !(superObject instanceof UnknownType) &&
      !declaredSuper.isEquivalentTo(superObject)) {
    if (declaredSuper.isEquivalentTo(getNativeType(OBJECT_TYPE))) {
      registerMismatch(superObject, declaredSuper, report(
          t.makeError(n, MISSING_EXTENDS_TAG_WARNING, subObject.toString())));
    } else {
      mismatch(t.getSourceName(), n,
          "mismatch in declaration of superclass type",
          superObject, declaredSuper);
    }

    // Correct the super type.
    if (!subCtor.hasCachedValues()) {
      subCtor.setPrototypeBasedOn(superObject);
    }
  }
}
 
Example 16
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
/**
 * If a constructor statement is not emitted TS will assume a constructor with no arguments and
 * no body (default ctor) for base classes, or the constructor of the superclass.
 *
 * <p>Omitting the constructor is correct only if the closure class and *all* its superclasses
 * have zero argument constructors. If the supertype is NoResolvedType we cannot know whether
 * that's the case so we must return true.
 */
private boolean mustEmitConstructor(FunctionType type) {
  while (type != null) {
    if (type.getParameters().iterator().hasNext()) return true;
    ObjectType oType = getSuperType(type);
    if (oType == null) return false;
    if (oType.isNoResolvedType()) return true;
    type = oType.getConstructor();
  }
  return false;
}
 
Example 17
Source File: Nopol2017_0029_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits a {@link Token#FUNCTION} node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitFunction(NodeTraversal t, Node n) {
  FunctionType functionType = JSType.toMaybeFunctionType(n.getJSType());
  String functionPrivateName = n.getFirstChild().getString();
  if (functionType.isConstructor()) {
    FunctionType baseConstructor = functionType.getSuperClassConstructor();
    if (baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE) &&
        baseConstructor != null &&
        baseConstructor.isInterface()) {
      compiler.report(
          t.makeError(n, CONFLICTING_EXTENDED_TYPE,
                      "constructor", functionPrivateName));
    } else {
      if (baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE)) {
        ObjectType proto = functionType.getPrototype();
        if (functionType.makesStructs() && !proto.isStruct()) {
          compiler.report(t.makeError(n, CONFLICTING_EXTENDED_TYPE,
                                      "struct", functionPrivateName));
        } else if (functionType.makesDicts() && !proto.isDict()) {
          compiler.report(t.makeError(n, CONFLICTING_EXTENDED_TYPE,
                                      "dict", functionPrivateName));
        }
      }
      // All interfaces are properly implemented by a class
      for (JSType baseInterface : functionType.getImplementedInterfaces()) {
        boolean badImplementedType = false;
        ObjectType baseInterfaceObj = ObjectType.cast(baseInterface);
        if (baseInterfaceObj != null) {
          FunctionType interfaceConstructor =
            baseInterfaceObj.getConstructor();
          if (interfaceConstructor != null &&
              !interfaceConstructor.isInterface()) {
            badImplementedType = true;
          }
        } else {
          badImplementedType = true;
        }
        if (badImplementedType) {
          report(t, n, BAD_IMPLEMENTED_TYPE, functionPrivateName);
        }
      }
      // check properties
      validator.expectAllInterfaceProperties(t, n, functionType);
    }
  } else if (functionType.isInterface()) {
    // Interface must extend only interfaces
    for (ObjectType extInterface : functionType.getExtendedInterfaces()) {
      if (extInterface.getConstructor() != null
          && !extInterface.getConstructor().isInterface()) {
        compiler.report(
            t.makeError(n, CONFLICTING_EXTENDED_TYPE,
                        "interface", functionPrivateName));
      }
    }

    // Check whether the extended interfaces have any conflicts
    if (functionType.getExtendedInterfacesCount() > 1) {
      // Only check when extending more than one interfaces
      HashMap<String, ObjectType> properties
          = new HashMap<String, ObjectType>();
      HashMap<String, ObjectType> currentProperties
          = new HashMap<String, ObjectType>();
      for (ObjectType interfaceType : functionType.getExtendedInterfaces()) {
        currentProperties.clear();
        if (com.google.javascript.jscomp.TypeCheck.this.unknownCount < com.google.javascript.jscomp.TypeCheck.this.typedCount) {
          checkInterfaceConflictProperties(t, n, functionPrivateName,
          properties, currentProperties, interfaceType);
        }
        properties.putAll(currentProperties);
      }
    }
  }
}
 
Example 18
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits a {@link Token#FUNCTION} node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitFunction(NodeTraversal t, Node n) {
  FunctionType functionType = JSType.toMaybeFunctionType(n.getJSType());
  String functionPrivateName = n.getFirstChild().getString();
  if (functionType.isConstructor()) {
    FunctionType baseConstructor = functionType.getSuperClassConstructor();
    if (baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE) &&
        baseConstructor != null &&
        baseConstructor.isInterface()) {
      compiler.report(
          t.makeError(n, CONFLICTING_EXTENDED_TYPE,
                      "constructor", functionPrivateName));
    } else {
      if (baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE)) {
        ObjectType proto = functionType.getPrototype();
        if (functionType.makesStructs() && !proto.isStruct()) {
          compiler.report(t.makeError(n, CONFLICTING_SHAPE_TYPE,
                                      "struct", functionPrivateName));
        } else if (functionType.makesDicts() && !proto.isDict()) {
          compiler.report(t.makeError(n, CONFLICTING_SHAPE_TYPE,
                                      "dict", functionPrivateName));
        }
      }
      // All interfaces are properly implemented by a class
      for (JSType baseInterface : functionType.getImplementedInterfaces()) {
        boolean badImplementedType = false;
        ObjectType baseInterfaceObj = ObjectType.cast(baseInterface);
        if (baseInterfaceObj != null) {
          FunctionType interfaceConstructor =
            baseInterfaceObj.getConstructor();
          if (interfaceConstructor != null &&
              !interfaceConstructor.isInterface()) {
            badImplementedType = true;
          }
        } else {
          badImplementedType = true;
        }
        if (badImplementedType) {
          report(t, n, BAD_IMPLEMENTED_TYPE, functionPrivateName);
        }
      }
      // check properties
      validator.expectAllInterfaceProperties(t, n, functionType);
    }
  } else if (functionType.isInterface()) {
    // Interface must extend only interfaces
    for (ObjectType extInterface : functionType.getExtendedInterfaces()) {
      if (extInterface.getConstructor() != null
          && !extInterface.getConstructor().isInterface()) {
        compiler.report(
            t.makeError(n, CONFLICTING_EXTENDED_TYPE,
                        "interface", functionPrivateName));
      }
    }

    // Check whether the extended interfaces have any conflicts
    if (functionType.getExtendedInterfacesCount() > 1) {
      // Only check when extending more than one interfaces
      HashMap<String, ObjectType> properties
          = new HashMap<String, ObjectType>();
      HashMap<String, ObjectType> currentProperties
          = new HashMap<String, ObjectType>();
      for (ObjectType interfaceType : functionType.getExtendedInterfaces()) {
        currentProperties.clear();
        checkInterfaceConflictProperties(t, n, functionPrivateName,
            properties, currentProperties, interfaceType);
        properties.putAll(currentProperties);
      }
    }
  }
}
 
Example 19
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits a {@link Token#FUNCTION} node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitFunction(NodeTraversal t, Node n) {
  FunctionType functionType = (FunctionType) n.getJSType();
  String functionPrivateName = n.getFirstChild().getString();
  if (functionType.isConstructor()) {
    FunctionType baseConstructor = functionType.
        getPrototype().getImplicitPrototype().getConstructor();
    if (baseConstructor != null &&
        baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE) &&
        (baseConstructor.isInterface() && functionType.isConstructor())) {
      compiler.report(
          t.makeError(n, CONFLICTING_EXTENDED_TYPE, functionPrivateName));
    } else {
      // All interfaces are properly implemented by a class
      for (JSType baseInterface : functionType.getImplementedInterfaces()) {
        boolean badImplementedType = false;
        ObjectType baseInterfaceObj = ObjectType.cast(baseInterface);
        if (baseInterfaceObj != null) {
          FunctionType interfaceConstructor =
            baseInterfaceObj.getConstructor();
          if (interfaceConstructor != null &&
              !interfaceConstructor.isInterface()) {
            badImplementedType = true;
          }
        } else {
          badImplementedType = true;
        }
        if (badImplementedType) {
          report(t, n, BAD_IMPLEMENTED_TYPE, functionPrivateName);
        }
      }
      // check properties
      validator.expectAllInterfaceProperties(t, n, functionType);
    }
  } else if (functionType.isInterface()) {
    // Interface must extend only interfaces
    for (ObjectType extInterface : functionType.getExtendedInterfaces()) {
      if (extInterface.getConstructor() != null
          && !extInterface.getConstructor().isInterface()) {
        compiler.report(
            t.makeError(n, CONFLICTING_EXTENDED_TYPE, functionPrivateName));
      }
    }
    // Interface cannot implement any interfaces
    if (functionType.hasImplementedInterfaces()) {
      compiler.report(t.makeError(n,
          CONFLICTING_IMPLEMENTED_TYPE, functionPrivateName));
    }
    // Check whether the extended interfaces have any conflicts
    if (functionType.getExtendedInterfacesCount() > 1) {
      // Only check when extending more than one interfaces
      HashMap<String, ObjectType> properties
          = new HashMap<String, ObjectType>();
      HashMap<String, ObjectType> currentProperties
          = new HashMap<String, ObjectType>();
      for (ObjectType interfaceType : functionType.getExtendedInterfaces()) {
        currentProperties.clear();
        checkInterfaceConflictProperties(t, n, functionPrivateName,
            properties, currentProperties, interfaceType);
        properties.putAll(currentProperties);
      }
    }
  }
}
 
Example 20
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits a {@link Token#FUNCTION} node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitFunction(NodeTraversal t, Node n) {
  FunctionType functionType = JSType.toMaybeFunctionType(n.getJSType());
  String functionPrivateName = n.getFirstChild().getString();
  if (functionType.isConstructor()) {
    FunctionType baseConstructor = functionType.getSuperClassConstructor();
    if (baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE) &&
        baseConstructor != null &&
        baseConstructor.isInterface()) {
      compiler.report(
          t.makeError(n, CONFLICTING_EXTENDED_TYPE,
                      "constructor", functionPrivateName));
    } else {
      if (baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE)) {
        ObjectType proto = functionType.getPrototype();
        if (functionType.makesStructs() && !proto.isStruct()) {
          compiler.report(t.makeError(n, CONFLICTING_EXTENDED_TYPE,
                                      "struct", functionPrivateName));
        } else if (functionType.makesDicts() && !proto.isDict()) {
          compiler.report(t.makeError(n, CONFLICTING_EXTENDED_TYPE,
                                      "dict", functionPrivateName));
        }
      }
      // All interfaces are properly implemented by a class
      for (JSType baseInterface : functionType.getImplementedInterfaces()) {
        boolean badImplementedType = false;
        ObjectType baseInterfaceObj = ObjectType.cast(baseInterface);
        if (baseInterfaceObj != null) {
          FunctionType interfaceConstructor =
            baseInterfaceObj.getConstructor();
          if (interfaceConstructor != null &&
              !interfaceConstructor.isInterface()) {
            badImplementedType = true;
          }
        } else {
          badImplementedType = true;
        }
        if (badImplementedType) {
          report(t, n, BAD_IMPLEMENTED_TYPE, functionPrivateName);
        }
      }
      // check properties
      validator.expectAllInterfaceProperties(t, n, functionType);
    }
  } else if (functionType.isInterface()) {
    // Interface must extend only interfaces
    for (ObjectType extInterface : functionType.getExtendedInterfaces()) {
      if (extInterface.getConstructor() != null
          && !extInterface.getConstructor().isInterface()) {
        compiler.report(
            t.makeError(n, CONFLICTING_EXTENDED_TYPE,
                        "interface", functionPrivateName));
      }
    }

    // Check whether the extended interfaces have any conflicts
    if (functionType.getExtendedInterfacesCount() > 1) {
      // Only check when extending more than one interfaces
      HashMap<String, ObjectType> properties
          = new HashMap<String, ObjectType>();
      HashMap<String, ObjectType> currentProperties
          = new HashMap<String, ObjectType>();
      for (ObjectType interfaceType : functionType.getExtendedInterfaces()) {
        currentProperties.clear();
        checkInterfaceConflictProperties(t, n, functionPrivateName,
            properties, currentProperties, interfaceType);
        properties.putAll(currentProperties);
      }
    }
  }
}