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

The following examples show how to use com.google.javascript.rhino.jstype.ObjectType#getOwnPropertyJSDocInfo() . 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: DeclarationGenerator.java    From clutz with MIT License 6 votes vote down vote up
private void visitProperty(
    String propName,
    ObjectType objType,
    boolean isStatic,
    boolean forcePropDeclaration,
    boolean isNamespace,
    List<String> classTemplateTypeNames) {
  JSType propertyType = objType.getPropertyType(propName);
  // The static methods from the function prototype are provided by lib.d.ts.
  if (isStatic && isFunctionPrototypeProp(propName)) return;
  JSDocInfo jsdoc = objType.getOwnPropertyJSDocInfo(propName);
  maybeEmitJsDoc(jsdoc, /* ignoreParams */ false);
  boolean isProtected = isProtectedProperty(objType, propName);
  JSDocInfo jsDocInfo = objType.getOwnPropertyJSDocInfo(propName);
  boolean isAbstract = jsDocInfo != null && jsDocInfo.isAbstract();
  emitProperty(
      propName,
      propertyType,
      isStatic,
      isProtected,
      isAbstract,
      forcePropDeclaration,
      isNamespace,
      classTemplateTypeNames);
}
 
Example 2
Source File: Closure_71_CheckAccessControls_t.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 3
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 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 4
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns if a property is declared constant.
 */
private static boolean isPropertyDeclaredConstant(
    ObjectType objectType, String prop) {
  for (;
       // Only objects with reference names can have constant properties.
       objectType != null && objectType.hasReferenceName();

       objectType = objectType.getImplicitPrototype()) {
    JSDocInfo docInfo = objectType.getOwnPropertyJSDocInfo(prop);
    if (docInfo != null && docInfo.isConstant()) {
      return true;
    }
  }
  return false;
}
 
Example 5
Source File: Closure_96_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if any type in the chain has an implictCast 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 6
Source File: Closure_66_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if any type in the chain has an implictCast 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 7
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if any type in the chain has an implictCast 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 8
Source File: Closure_2_TypeCheck_s.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 9
Source File: Closure_125_TypeCheck_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 static 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 10
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if any type in the chain has an implictCast 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 11
Source File: Nopol2017_0029_s.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 12
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 13
Source File: Nopol2017_0051_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if any type in the chain has an implictCast 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 14
Source File: Nopol2017_0051_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if any type in the chain has an implictCast 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 15
Source File: Closure_2_TypeCheck_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 16
Source File: Closure_71_CheckAccessControls_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given property is visible in the current context.
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkPropertyVisibility(NodeTraversal t,
    Node getprop, Node parent) {
  ObjectType objectType =
      ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  if (objectType != null) {
    // Is this a normal property access, or are we trying to override
    // an existing property?
    boolean isOverride = t.inGlobalScope() &&
        parent.getType() == Token.ASSIGN &&
        parent.getFirstChild() == getprop;

    // Find the lowest property defined on a class with visibility
    // information.
    if (isOverride) {
      objectType = objectType.getImplicitPrototype();
    }
    JSDocInfo docInfo = null;
    for (; objectType != null;
         objectType = objectType.getImplicitPrototype()) {
      docInfo = objectType.getOwnPropertyJSDocInfo(propertyName);
      if (docInfo != null &&
          docInfo.getVisibility() != Visibility.INHERITED) {
        break;
      }
    }

    if (objectType == null) {
      // We couldn't find a visibility modifier; assume it's public.
      return;
    }

    boolean sameInput =
        t.getInput().getName().equals(docInfo.getSourceName());
    Visibility visibility = docInfo.getVisibility();
    JSType ownerType = normalizeClassType(objectType);
    if (isOverride) {
      // Check an ASSIGN statement that's trying to override a property
      // on a superclass.
      JSDocInfo overridingInfo = parent.getJSDocInfo();
      Visibility overridingVisibility = overridingInfo == null ?
          Visibility.INHERITED : overridingInfo.getVisibility();

      // Check that (a) the property *can* be overridden, and
      // (b) that the visibility of the override is the same as the
      // visibility of the original property.
      if (visibility == Visibility.PRIVATE && !sameInput) {
        compiler.report(
            t.makeError(getprop, PRIVATE_OVERRIDE,
                objectType.toString()));
      } else if (overridingVisibility != Visibility.INHERITED &&
          overridingVisibility != visibility) {
        compiler.report(
            t.makeError(getprop, VISIBILITY_MISMATCH,
                visibility.name(), objectType.toString(),
                overridingVisibility.name()));
      }
    } else {
      if (sameInput) {
        // private access is always allowed in the same file.
        return;
      } else if (visibility == Visibility.PRIVATE &&
          (currentClass == null || ownerType.differsFrom(currentClass))) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        // private access is not allowed outside the file from a different
        // enclosing class.
        compiler.report(
            t.makeError(getprop,
                BAD_PRIVATE_PROPERTY_ACCESS,
                propertyName,
                validator.getReadableJSTypeName(
                    getprop.getFirstChild(), true)));
      } else if (visibility == Visibility.PROTECTED) {
        // There are 3 types of legal accesses of a protected property:
        // 1) Accesses in the same file
        // 2) Overriding the property in a subclass
        // 3) Accessing the property from inside a subclass
        // The first two have already been checked for.
        if (currentClass == null || !currentClass.isSubtype(ownerType)) {
          compiler.report(
              t.makeError(getprop,  BAD_PROTECTED_PROPERTY_ACCESS,
                  propertyName,
                  validator.getReadableJSTypeName(
                      getprop.getFirstChild(), true)));
        }
      }
    }
  }
}
 
Example 17
Source File: Closure_71_CheckAccessControls_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given property with @const tag got reassigned
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkConstantProperty(NodeTraversal t,
    Node getprop) {
  // Check whether the property is modified
  Node parent = getprop.getParent();
  if (!(NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == getprop)
      && (parent.getType() != Token.INC) && (parent.getType() != Token.DEC)) {
    return;
  }

  ObjectType objectType =
    ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  // Check whether constant properties are reassigned
  if (objectType != null) {
    ObjectType oType = objectType;
    while (oType != null) {
      if (oType.hasReferenceName()) {
        if (initializedConstantProperties.containsEntry(
                oType.getReferenceName(), propertyName)) {
          compiler.report(
              t.makeError(getprop, CONST_PROPERTY_REASSIGNED_VALUE,
                  propertyName));
          break;
        }
      }
      oType = oType.getImplicitPrototype();
    }

    JSDocInfo info = objectType.getOwnPropertyJSDocInfo(propertyName);
    if (info != null && info.isConstant()
        && objectType.hasReferenceName()) {
      initializedConstantProperties.put(objectType.getReferenceName(),
          propertyName);
    }

    // Add the prototype when we're looking at an instance object
    if (objectType.isInstanceType()) {
      ObjectType prototype = objectType.getImplicitPrototype();
      if (prototype != null) {
        JSDocInfo prototypeInfo
          = prototype.getOwnPropertyJSDocInfo(propertyName);
        if (prototypeInfo != null && prototypeInfo.isConstant()
            && prototype.hasReferenceName()) {
          initializedConstantProperties.put(prototype.getReferenceName(),
              propertyName);
        }
      }
    }
  }
}
 
Example 18
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Determines whether the given property is visible in the current context.
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkPropertyVisibility(NodeTraversal t,
    Node getprop, Node parent) {
  ObjectType objectType =
      ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  if (objectType != null) {
    // Is this a normal property access, or are we trying to override
    // an existing property?
    boolean isOverride = parent.getJSDocInfo() != null &&
        parent.isAssign() &&
        parent.getFirstChild() == getprop;

    // Find the lowest property defined on a class with visibility
    // information.
    if (isOverride) {
      objectType = objectType.getImplicitPrototype();
    }
    JSDocInfo docInfo = null;
    for (; objectType != null;
         objectType = objectType.getImplicitPrototype()) {
      docInfo = objectType.getOwnPropertyJSDocInfo(propertyName);
      if (docInfo != null &&
          docInfo.getVisibility() != Visibility.INHERITED) {
        break;
      }
    }

    if (objectType == null) {
      // We couldn't find a visibility modifier; assume it's public.
      return;
    }

    String referenceSource = getprop.getSourceFileName();
    String definingSource = docInfo.getSourceName();
    boolean sameInput = referenceSource != null
        && referenceSource.equals(definingSource);
    Visibility visibility = docInfo.getVisibility();
    JSType ownerType = normalizeClassType(objectType);
    if (isOverride) {
      // Check an ASSIGN statement that's trying to override a property
      // on a superclass.
      JSDocInfo overridingInfo = parent.getJSDocInfo();
      Visibility overridingVisibility = overridingInfo == null ?
          Visibility.INHERITED : overridingInfo.getVisibility();

      // Check that (a) the property *can* be overridden, and
      // (b) that the visibility of the override is the same as the
      // visibility of the original property.
      if (visibility == Visibility.PRIVATE && !sameInput) {
        compiler.report(
            t.makeError(getprop, PRIVATE_OVERRIDE,
                objectType.toString()));
      } else if (overridingVisibility != Visibility.INHERITED &&
          overridingVisibility != visibility) {
        compiler.report(
            t.makeError(getprop, VISIBILITY_MISMATCH,
                visibility.name(), objectType.toString(),
                overridingVisibility.name()));
      }
    } else {
      if (sameInput) {
        // private access is always allowed in the same file.
        return;
      } else if (visibility == Visibility.PRIVATE &&
          (currentClass == null || !ownerType.isEquivalentTo(currentClass))) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        // private access is not allowed outside the file from a different
        // enclosing class.
        compiler.report(
            t.makeError(getprop,
                BAD_PRIVATE_PROPERTY_ACCESS,
                propertyName,
                validator.getReadableJSTypeName(
                    getprop.getFirstChild(), true)));
      } else if (visibility == Visibility.PROTECTED) {
        // There are 3 types of legal accesses of a protected property:
        // 1) Accesses in the same file
        // 2) Overriding the property in a subclass
        // 3) Accessing the property from inside a subclass
        // The first two have already been checked for.
        if (currentClass == null || !currentClass.isSubtype(ownerType)) {
          compiler.report(
              t.makeError(getprop,  BAD_PROTECTED_PROPERTY_ACCESS,
                  propertyName,
                  validator.getReadableJSTypeName(
                      getprop.getFirstChild(), true)));
        }
      }
    }
  }
}
 
Example 19
Source File: Closure_71_CheckAccessControls_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given property is visible in the current context.
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkPropertyVisibility(NodeTraversal t,
    Node getprop, Node parent) {
  ObjectType objectType =
      ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  if (objectType != null) {
    // Is this a normal property access, or are we trying to override
    // an existing property?
    boolean isOverride = parent.getJSDocInfo() != null &&
        parent.getType() == Token.ASSIGN &&
        parent.getFirstChild() == getprop;

    // Find the lowest property defined on a class with visibility
    // information.
    if (isOverride) {
      objectType = objectType.getImplicitPrototype();
    }
    JSDocInfo docInfo = null;
    for (; objectType != null;
         objectType = objectType.getImplicitPrototype()) {
      docInfo = objectType.getOwnPropertyJSDocInfo(propertyName);
      if (docInfo != null &&
          docInfo.getVisibility() != Visibility.INHERITED) {
        break;
      }
    }

    if (objectType == null) {
      // We couldn't find a visibility modifier; assume it's public.
      return;
    }

    boolean sameInput =
        t.getInput().getName().equals(docInfo.getSourceName());
    Visibility visibility = docInfo.getVisibility();
    JSType ownerType = normalizeClassType(objectType);
    if (isOverride) {
      // Check an ASSIGN statement that's trying to override a property
      // on a superclass.
      JSDocInfo overridingInfo = parent.getJSDocInfo();
      Visibility overridingVisibility = overridingInfo == null ?
          Visibility.INHERITED : overridingInfo.getVisibility();

      // Check that (a) the property *can* be overridden, and
      // (b) that the visibility of the override is the same as the
      // visibility of the original property.
      if (visibility == Visibility.PRIVATE && !sameInput) {
        compiler.report(
            t.makeError(getprop, PRIVATE_OVERRIDE,
                objectType.toString()));
      } else if (overridingVisibility != Visibility.INHERITED &&
          overridingVisibility != visibility) {
        compiler.report(
            t.makeError(getprop, VISIBILITY_MISMATCH,
                visibility.name(), objectType.toString(),
                overridingVisibility.name()));
      }
    } else {
      if (sameInput) {
        // private access is always allowed in the same file.
        return;
      } else if (visibility == Visibility.PRIVATE &&
          (currentClass == null || ownerType.differsFrom(currentClass))) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        // private access is not allowed outside the file from a different
        // enclosing class.
        compiler.report(
            t.makeError(getprop,
                BAD_PRIVATE_PROPERTY_ACCESS,
                propertyName,
                validator.getReadableJSTypeName(
                    getprop.getFirstChild(), true)));
      } else if (visibility == Visibility.PROTECTED) {
        // There are 3 types of legal accesses of a protected property:
        // 1) Accesses in the same file
        // 2) Overriding the property in a subclass
        // 3) Accessing the property from inside a subclass
        // The first two have already been checked for.
        if (currentClass == null || !currentClass.isSubtype(ownerType)) {
          compiler.report(
              t.makeError(getprop,  BAD_PROTECTED_PROPERTY_ACCESS,
                  propertyName,
                  validator.getReadableJSTypeName(
                      getprop.getFirstChild(), true)));
        }
      }
    }
  }
}
 
Example 20
Source File: DeclarationGenerator.java    From clutz with MIT License 4 votes vote down vote up
private boolean isTypeCheckSuppressedProperty(ObjectType obj, String propName) {
  JSDocInfo info = obj.getOwnPropertyJSDocInfo(propName);
  return info != null && info.getSuppressions().contains("checkTypes");
}