Java Code Examples for com.google.javascript.rhino.JSDocInfo.Visibility#PRIVATE

The following examples show how to use com.google.javascript.rhino.JSDocInfo.Visibility#PRIVATE . 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_71_CheckAccessControls_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given name is visible in the current context.
 * @param t The current traversal.
 * @param name The name node.
 */
private void checkNameVisibility(NodeTraversal t, Node name, Node parent) {
  Var var = t.getScope().getVar(name.getString());
  if (var != null) {
    JSDocInfo docInfo = var.getJSDocInfo();
    if (docInfo != null) {
      // If a name is private, make sure that we're in the same file.
      Visibility visibility = docInfo.getVisibility();
      if (visibility == Visibility.PRIVATE &&
          !t.getInput().getName().equals(docInfo.getSourceName())) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        compiler.report(
            t.makeError(name, BAD_PRIVATE_GLOBAL_ACCESS,
                name.getString(), docInfo.getSourceName()));
      }
    }
  }
}
 
Example 2
Source File: Closure_71_CheckAccessControls_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given name is visible in the current context.
 * @param t The current traversal.
 * @param name The name node.
 */
private void checkNameVisibility(NodeTraversal t, Node name, Node parent) {
  Var var = t.getScope().getVar(name.getString());
  if (var != null) {
    JSDocInfo docInfo = var.getJSDocInfo();
    if (docInfo != null) {
      // If a name is private, make sure that we're in the same file.
      Visibility visibility = docInfo.getVisibility();
      if (visibility == Visibility.PRIVATE &&
          !t.getInput().getName().equals(docInfo.getSourceName())) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        compiler.report(
            t.makeError(name, BAD_PRIVATE_GLOBAL_ACCESS,
                name.getString(), docInfo.getSourceName()));
      }
    }
  }
}
 
Example 3
Source File: MemberCollector.java    From jsinterop-generator with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean visitConstructor(FunctionType constructor) {
  Method constructorMethod = Method.newConstructor();
  if (constructor.getJSDocInfo().getVisibility() == Visibility.PRIVATE) {
    constructorMethod.setAccessModifier(AccessModifier.PRIVATE);
  }

  getCurrentJavaType().addMethod(constructorMethod);

  currentJavaMethodDeque.push(constructorMethod);
  return true;
}
 
Example 4
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
private boolean isPrivate(@Nullable JSDocInfo docInfo) {
  if (docInfo == null) {
    return false;
  }
  // Closure Compiler has @package visibility, which makes symbols accessible to code in the same
  // package. ES6 modules do not have this concept at all, as there are no packages to begin with.
  // As TypeScript code can never be in the same package as JavaScript code, because TS code is in
  // no package at all, Clutz considers @package visible fields to be private.
  return docInfo.getVisibility() == Visibility.PRIVATE
      || docInfo.getVisibility() == Visibility.PACKAGE;
}
 
Example 5
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determines whether the given name is visible in the current context.
 * @param t The current traversal.
 * @param name The name node.
 */
private void checkNameVisibility(NodeTraversal t, Node name, Node parent) {
  Var var = t.getScope().getVar(name.getString());
  if (var != null) {
    JSDocInfo docInfo = var.getJSDocInfo();
    if (docInfo != null) {
      // If a name is private, make sure that we're in the same file.
      Visibility visibility = docInfo.getVisibility();
      if (visibility == Visibility.PRIVATE) {
        StaticSourceFile varSrc = var.getSourceFile();
        StaticSourceFile refSrc = name.getStaticSourceFile();
        if (varSrc != null &&
            refSrc != null &&
            !varSrc.getName().equals(refSrc.getName())) {
          if (docInfo.isConstructor() &&
              isValidPrivateConstructorAccess(parent)) {
            return;
          }

          compiler.report(
              t.makeError(name, BAD_PRIVATE_GLOBAL_ACCESS,
                  name.getString(), varSrc.getName()));
        }
      }
    }
  }
}
 
Example 6
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 7
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 8
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)));
        }
      }
    }
  }
}