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

The following examples show how to use com.google.javascript.rhino.jstype.FunctionBuilder. 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_90_FunctionTypeBuilder_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Builds the function type, and puts it in the registry.
 */
FunctionType buildAndRegister() {
  if (returnType == null) {
    returnType = typeRegistry.getNativeType(UNKNOWN_TYPE);
  }

  if (parametersNode == null) {
    throw new IllegalStateException(
        "All Function types must have params and a return type");
  }

  FunctionType fnType;
  if (isConstructor) {
    fnType = getOrCreateConstructor();
  } else if (isInterface) {
    fnType = typeRegistry.createInterfaceType(fnName, sourceNode);
    if (scope.isGlobal() && !fnName.isEmpty()) {
      typeRegistry.declareType(fnName, fnType.getInstanceType());
    }
    maybeSetBaseType(fnType);
  } else {
    fnType = new FunctionBuilder(typeRegistry)
        .withName(fnName)
        .withSourceNode(sourceNode)
        .withParamsNode(parametersNode)
        .withReturnType(returnType, returnTypeInferred)
        .withTypeOfThis(thisType)
        .withTemplateName(templateTypeName)
        .build();
    maybeSetBaseType(fnType);
  }

  if (implementedInterfaces != null) {
    fnType.setImplementedInterfaces(implementedInterfaces);
  }

  typeRegistry.clearTemplateTypeName();

  return fnType;
}
 
Example #2
Source File: Closure_90_FunctionTypeBuilder_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Builds the function type, and puts it in the registry.
 */
FunctionType buildAndRegister() {
  if (returnType == null) {
    returnType = typeRegistry.getNativeType(UNKNOWN_TYPE);
  }

  if (parametersNode == null) {
    throw new IllegalStateException(
        "All Function types must have params and a return type");
  }

  FunctionType fnType;
  if (isConstructor) {
    fnType = getOrCreateConstructor();
  } else if (isInterface) {
    fnType = typeRegistry.createInterfaceType(fnName, sourceNode);
    if (scope.isGlobal() && !fnName.isEmpty()) {
      typeRegistry.declareType(fnName, fnType.getInstanceType());
    }
    maybeSetBaseType(fnType);
  } else {
    fnType = new FunctionBuilder(typeRegistry)
        .withName(fnName)
        .withSourceNode(sourceNode)
        .withParamsNode(parametersNode)
        .withReturnType(returnType, returnTypeInferred)
        .withTypeOfThis(thisType)
        .withTemplateName(templateTypeName)
        .build();
    maybeSetBaseType(fnType);
  }

  if (implementedInterfaces != null) {
    fnType.setImplementedInterfaces(implementedInterfaces);
  }

  typeRegistry.clearTemplateTypeName();

  return fnType;
}
 
Example #3
Source File: BaseJSTypeTestCase.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static void addMethod(
    JSTypeRegistry registry, ObjectType receivingType, String methodName,
    JSType returnType) {
  receivingType.defineDeclaredProperty(methodName,
      new FunctionBuilder(registry).withReturnType(returnType).build(),
      null);
}
 
Example #4
Source File: BaseJSTypeTestCase.java    From ng-closure-runner with MIT License 5 votes vote down vote up
private static void addMethod(
    JSTypeRegistry registry, ObjectType receivingType, String methodName,
    JSType returnType) {
  receivingType.defineDeclaredProperty(methodName,
      new FunctionBuilder(registry).withReturnType(returnType).build(),
      null);
}
 
Example #5
Source File: Closure_41_FunctionTypeBuilder_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Builds the function type, and puts it in the registry.
 */
FunctionType buildAndRegister() {
  if (returnType == null &&
      !contents.mayHaveNonEmptyReturns() && !contents.mayBeFromExterns()) {
    returnType = typeRegistry.getNativeType(VOID_TYPE);
    returnTypeInferred = true;
  }

  if (returnType == null) {
    returnType = typeRegistry.getNativeType(UNKNOWN_TYPE);
  }

  if (parametersNode == null) {
    throw new IllegalStateException(
        "All Function types must have params and a return type");
  }

  FunctionType fnType;
  if (isConstructor) {
    fnType = getOrCreateConstructor();
  } else if (isInterface) {
    fnType = typeRegistry.createInterfaceType(
        fnName, contents.getSourceNode());
    if (getScopeDeclaredIn().isGlobal() && !fnName.isEmpty()) {
      typeRegistry.declareType(fnName, fnType.getInstanceType());
    }
    maybeSetBaseType(fnType);
  } else {
    fnType = new FunctionBuilder(typeRegistry)
        .withName(fnName)
        .withSourceNode(contents.getSourceNode())
        .withParamsNode(parametersNode)
        .withReturnType(returnType, returnTypeInferred)
        .withTypeOfThis(thisType)
        .withTemplateName(templateTypeName)
        .build();
    maybeSetBaseType(fnType);
  }

  if (implementedInterfaces != null) {
    fnType.setImplementedInterfaces(implementedInterfaces);
  }

  if (extendedInterfaces != null) {
    fnType.setExtendedInterfaces(extendedInterfaces);
  }

  typeRegistry.clearTemplateTypeName();

  return fnType;
}
 
Example #6
Source File: Closure_41_FunctionTypeBuilder_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Builds the function type, and puts it in the registry.
 */
FunctionType buildAndRegister() {
  if (returnType == null &&
      !contents.mayHaveNonEmptyReturns() && !contents.mayBeFromExterns()) {
    returnType = typeRegistry.getNativeType(VOID_TYPE);
    returnTypeInferred = true;
  }

  if (returnType == null) {
    returnType = typeRegistry.getNativeType(UNKNOWN_TYPE);
  }

  if (parametersNode == null) {
    throw new IllegalStateException(
        "All Function types must have params and a return type");
  }

  FunctionType fnType;
  if (isConstructor) {
    fnType = getOrCreateConstructor();
  } else if (isInterface) {
    fnType = typeRegistry.createInterfaceType(
        fnName, contents.getSourceNode());
    if (getScopeDeclaredIn().isGlobal() && !fnName.isEmpty()) {
      typeRegistry.declareType(fnName, fnType.getInstanceType());
    }
    maybeSetBaseType(fnType);
  } else {
    fnType = new FunctionBuilder(typeRegistry)
        .withName(fnName)
        .withSourceNode(contents.getSourceNode())
        .withParamsNode(parametersNode)
        .withReturnType(returnType, returnTypeInferred)
        .withTypeOfThis(thisType)
        .withTemplateName(templateTypeName)
        .build();
    maybeSetBaseType(fnType);
  }

  if (implementedInterfaces != null) {
    fnType.setImplementedInterfaces(implementedInterfaces);
  }

  if (extendedInterfaces != null) {
    fnType.setExtendedInterfaces(extendedInterfaces);
  }

  typeRegistry.clearTemplateTypeName();

  return fnType;
}
 
Example #7
Source File: FunctionTypeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Builds the function type, and puts it in the registry.
 */
FunctionType buildAndRegister() {
  if (returnType == null) {
    // Infer return types.
    // We need to be extremely conservative about this, because of two
    // competing needs.
    // 1) If we infer the return type of f too widely, then we won't be able
    //    to assign f to other functions.
    // 2) If we infer the return type of f too narrowly, then we won't be
    //    able to override f in subclasses.
    // So we only infer in cases where the user doesn't expect to write
    // @return annotations--when it's very obvious that the function returns
    // nothing.
    if (!contents.mayHaveNonEmptyReturns() &&
        !contents.mayHaveSingleThrow() &&
        !contents.mayBeFromExterns()) {
      returnType = typeRegistry.getNativeType(VOID_TYPE);
      returnTypeInferred = true;
    }
  }

  if (returnType == null) {
    returnType = typeRegistry.getNativeType(UNKNOWN_TYPE);
  }

  if (parametersNode == null) {
    throw new IllegalStateException(
        "All Function types must have params and a return type");
  }

  FunctionType fnType;
  if (isConstructor) {
    fnType = getOrCreateConstructor();
  } else if (isInterface) {
    fnType = typeRegistry.createInterfaceType(
        fnName, contents.getSourceNode());
    if (getScopeDeclaredIn().isGlobal() && !fnName.isEmpty()) {
      typeRegistry.declareType(fnName, fnType.getInstanceType());
    }
    maybeSetBaseType(fnType);
  } else {
    fnType = new FunctionBuilder(typeRegistry)
        .withName(fnName)
        .withSourceNode(contents.getSourceNode())
        .withParamsNode(parametersNode)
        .withReturnType(returnType, returnTypeInferred)
        .withTypeOfThis(thisType)
        .withTemplateKeys(templateTypeNames)
        .build();
    maybeSetBaseType(fnType);
  }

  if (implementedInterfaces != null) {
    fnType.setImplementedInterfaces(implementedInterfaces);
  }

  if (extendedInterfaces != null) {
    fnType.setExtendedInterfaces(extendedInterfaces);
  }

  typeRegistry.clearTemplateTypeNames();

  return fnType;
}