com.google.auto.value.extension.memoized.Memoized Java Examples

The following examples show how to use com.google.auto.value.extension.memoized.Memoized. 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: TypeDeclaration.java    From j2cl with Apache License 2.0 6 votes vote down vote up
/**
 * Builds and caches a mapping from method override signature to matching method descriptors from
 * the entire super-type hierarchy. This map can *greatly* speed up method override checks.
 */
@Memoized
Multimap<String, MethodDescriptor> getMethodDescriptorsByOverrideSignature() {
  Multimap<String, MethodDescriptor> methodDescriptorsByOverrideSignature =
      LinkedHashMultimap.create();

  for (MethodDescriptor declaredMethodDescriptor : getDeclaredMethodDescriptors()) {
    if (declaredMethodDescriptor.isPolymorphic()) {
      methodDescriptorsByOverrideSignature.put(
          declaredMethodDescriptor.getOverrideSignature(), declaredMethodDescriptor);
    }
  }

  // Recurse into immediate super class and interfaces for overridden method.
  if (getSuperTypeDeclaration() != null) {
    methodDescriptorsByOverrideSignature.putAll(
        getSuperTypeDeclaration().getMethodDescriptorsByOverrideSignature());
  }

  for (DeclaredTypeDescriptor interfaceTypeDescriptor : getInterfaceTypeDescriptors()) {
    methodDescriptorsByOverrideSignature.putAll(
        interfaceTypeDescriptor.getTypeDeclaration().getMethodDescriptorsByOverrideSignature());
  }

  return methodDescriptorsByOverrideSignature;
}
 
Example #2
Source File: GoogleAdsHeaderProvider.java    From google-ads-java with Apache License 2.0 6 votes vote down vote up
@Override
@Memoized
public ImmutableMap<String, String> getHeaders() {
  ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
  builder.put("developer-token", getDeveloperToken());
  if (getLoginCustomerId() != null) {
    builder.put("login-customer-id", String.valueOf(getLoginCustomerId()));
  }
  // Add the x-goog-api-client header which is usually added by the stub settings. However,
  // this doesn't happen. Once we add headers, needsHeaders() is false, so GAX's
  // ClientContext.java doesn't add the additional headers.
  ApiClientHeaderProvider apiClient = ApiClientHeaderProvider.newBuilder()
      .setGeneratedLibToken(
          "gapic",
          GaxProperties.getLibraryVersion(GoogleAdsServiceStubSettings.class))
      .setTransportToken(
          GaxGrpcProperties.getGrpcTokenName(),
          GaxGrpcProperties.getGrpcVersion())
      .build();
  builder.putAll(apiClient.getHeaders());
  return builder.build();
}
 
Example #3
Source File: TypeDeclaration.java    From j2cl with Apache License 2.0 6 votes vote down vote up
/** Returns the type descriptor for the module that needs to be required for this type */
@Memoized
public TypeDeclaration getEnclosingModule() {
  String moduleRelativeJsName = getModuleRelativeJsName();
  if (!isNative() || !moduleRelativeJsName.contains(".")) {
    return this;
  }

  if (getEnclosingTypeDeclaration() != null && !hasCustomizedJsNamespace()) {
    // Make sure that if the enclosing module is a non native type, getEnclosing module returns
    // the normal Java TypeDeclaration instead of synthesizing a native one. This is important
    // because it guarantees that the type will be goog.required using the "$impl" module not the
    // header module which might cause dependency cycles.
    return getEnclosingTypeDeclaration().getEnclosingModule();
  }

  // Synthesize a module root.
  String enclosingJsName = Iterables.get(Splitter.on('.').split(moduleRelativeJsName), 0);
  String enclosingJsNamespace = getJsNamespace();
  return TypeDescriptors.createNativeTypeDescriptor(enclosingJsNamespace, enclosingJsName)
      .getTypeDeclaration();
}
 
Example #4
Source File: DeclaredTypeDescriptor.java    From j2cl with Apache License 2.0 6 votes vote down vote up
/**
 * The list of methods in the type from the JDT. Note: this does not include methods we synthesize
 * and add to the type like bridge methods.
 */
@Memoized
Map<String, MethodDescriptor> getMethodDescriptorsBySignature() {
  // TODO(rluble): update this code to handle package private methods, bridges and verify that it
  // correctly handles default methods.
  Map<String, MethodDescriptor> methodDescriptorsBySignature = new LinkedHashMap<>();

  // Add all methods declared in the current type itself
  methodDescriptorsBySignature.putAll(getDeclaredMethodDescriptorsBySignature());

  // Add all the methods from the super class.
  if (getSuperTypeDescriptor() != null) {
    AstUtils.addInheritedMethodsBySignature(
        methodDescriptorsBySignature, getSuperTypeDescriptor().getMethodDescriptors());
  }

  // Finally add the methods that appear in super interfaces.
  for (DeclaredTypeDescriptor implementedInterface : getInterfaceTypeDescriptors()) {
    AstUtils.addInheritedMethodsBySignature(
        methodDescriptorsBySignature, implementedInterface.getMethodDescriptors());
  }
  return methodDescriptorsBySignature;
}
 
Example #5
Source File: TypeDeclaration.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Override
@Memoized
public String getQualifiedJsName() {
  if (JsUtils.isGlobal(getJsNamespace())) {
    return getModuleRelativeJsName();
  }
  return AstUtils.buildQualifiedName(getJsNamespace(), getModuleRelativeJsName());
}
 
Example #6
Source File: DeclaredTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/** Returns the class initializer method descriptor for a particular type */
@Memoized
public MethodDescriptor getClinitMethodDescriptor() {
  return MethodDescriptor.newBuilder()
      .setStatic(true)
      .setEnclosingTypeDescriptor(this)
      .setName(MethodDescriptor.CLINIT_METHOD_NAME)
      .setOrigin(MethodOrigin.SYNTHETIC_CLASS_INITIALIZER)
      .setJsInfo(isNative() ? JsInfo.RAW_OVERLAY : JsInfo.RAW)
      .build();
}
 
Example #7
Source File: IntersectionTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Memoized
@Override
public Map<TypeVariable, TypeDescriptor> getSpecializedTypeArgumentByTypeParameters() {
  ImmutableMap.Builder<TypeVariable, TypeDescriptor> mapBuilder = ImmutableMap.builder();
  for (TypeDescriptor typeDescriptor : getIntersectionTypeDescriptors()) {
    mapBuilder.putAll(typeDescriptor.getSpecializedTypeArgumentByTypeParameters());
  }
  return mapBuilder.build();
}
 
Example #8
Source File: DiffResult.java    From curiostack with MIT License 5 votes vote down vote up
@Memoized
@Override
Iterable<? extends RecursableDiffEntity> childEntities() {
  // Assemble the diffs in field number order so it most closely matches the schema.
  ImmutableList.Builder<RecursableDiffEntity> builder =
      ImmutableList.builderWithExpectedSize(
          singularFields().size() + repeatedFields().size() + unknownFields().asSet().size());
  Set<Integer> fieldNumbers = Sets.union(singularFields().keySet(), repeatedFields().keySet());
  for (int fieldNumber : Ordering.natural().sortedCopy(fieldNumbers)) {
    builder.addAll(singularFields().get(fieldNumber));
    builder.addAll(repeatedFields().get(fieldNumber));
  }
  builder.addAll(unknownFields().asSet());
  return builder.build();
}
 
Example #9
Source File: DeclaredTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/** Returns the single declared constructor fo this class. */
@Memoized
public MethodDescriptor getSingleConstructor() {
  return getDeclaredMethodDescriptors()
      .stream()
      .filter(MethodDescriptor::isConstructor)
      .collect(MoreCollectors.onlyElement());
}
 
Example #10
Source File: MethodDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Override
@Memoized
public boolean isOrOverridesJavaLangObjectMethod() {
  if (!isPolymorphic()) {
    return false;
  }

  if (TypeDescriptors.isJavaLangObject(getEnclosingTypeDescriptor())) {
    return true;
  }

  return getOverriddenMethodDescriptors().stream()
      .map(MethodDescriptor::getEnclosingTypeDescriptor)
      .anyMatch(TypeDescriptors::isJavaLangObject);
}
 
Example #11
Source File: DeclaredTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Memoized
@Override
public String getUniqueId() {
  String uniqueKey = getQualifiedBinaryName();
  String prefix = isNullable() ? "?" : "!";
  return prefix + uniqueKey + createTypeArgumentsUniqueId(getTypeArgumentDescriptors());
}
 
Example #12
Source File: DeclaredTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Override
@Memoized
public Set<TypeVariable> getAllTypeVariables() {
  Set<TypeVariable> typeVariables = new LinkedHashSet<>();
  collectAllTypeVariables(this, typeVariables);
  return typeVariables;
}
 
Example #13
Source File: IntersectionTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Override
@Memoized
public Set<TypeVariable> getAllTypeVariables() {
  return getIntersectionTypeDescriptors()
      .stream()
      .map(TypeDescriptor::getAllTypeVariables)
      .flatMap(set -> set.stream())
      .collect(Collectors.toSet());
}
 
Example #14
Source File: TypeDeclaration.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the qualifier for the type from the root of the module, @{code ""} if the type is the
 * module root.
 */
@Memoized
public String getInnerTypeQualifier() {
  String moduleRelativeJsName = getModuleRelativeJsName();
  int dotIndex = moduleRelativeJsName.indexOf('.');
  if (dotIndex == -1) {
    return "";
  }

  return moduleRelativeJsName.substring(dotIndex + 1);
}
 
Example #15
Source File: IntersectionTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Override
@Memoized
public DeclaredTypeDescriptor getFunctionalInterface() {
  return getIntersectionTypeDescriptors()
      .stream()
      .filter(DeclaredTypeDescriptor::isFunctionalInterface)
      .findFirst()
      .orElse(null);
}
 
Example #16
Source File: ArrayTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Memoized
public int getDimensions() {
  TypeDescriptor componentTypeDescriptor = getComponentTypeDescriptor();

  if (componentTypeDescriptor.isArray()) {
    return ((ArrayTypeDescriptor) componentTypeDescriptor).getDimensions() + 1;
  }

  return 1;
}
 
Example #17
Source File: TypeDeclaration.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
@Memoized
public String getJsNamespace() {
  if (hasCustomizedJsNamespace()) {
    return getCustomizedJsNamespace();
  }

  if (getEnclosingTypeDeclaration() == null) {
    return getPackageName();
  }

  if (isNative()) {
    return getEnclosingTypeDeclaration().getJsNamespace();
  }

  if (getEnclosingTypeDeclaration().isNative()) {
    // When there is a type nested within a native type, it's important not to generate a name
    // like "Array.1" (like would happen if the outer native type was claiming to be native
    // Array and the nested type was anonymous) since this is almost guaranteed to collide
    // with other people also creating nested classes within a native type that claims to be
    // native Array.
    return getEnclosingTypeDeclaration().getQualifiedSourceName();
  }
  // Use the parent qualified name.
  return getEnclosingTypeDeclaration().getQualifiedJsName();
}
 
Example #18
Source File: DeclaredTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/** Returns the default (parameterless) constructor for the type.. */
@Memoized
public MethodDescriptor getDefaultConstructorMethodDescriptor() {
  return getDeclaredMethodDescriptors()
      .stream()
      .filter(MethodDescriptor::isConstructor)
      .filter(methodDescriptor -> methodDescriptor.getParameterTypeDescriptors().isEmpty())
      .findFirst()
      .orElse(null);
}
 
Example #19
Source File: TypeDeclaration.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Memoized
public TypeDeclaration getMetadataTypeDeclaration() {
  if (isNative() || isJsEnum()) {
    return getOverlayImplementationTypeDeclaration();
  }

  if (isJsFunctionInterface()) {
    return BootstrapType.JAVA_SCRIPT_FUNCTION.getDeclaration();
  }

  return this;
}
 
Example #20
Source File: TypeDeclaration.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Memoized
public TypeDeclaration getOverlayImplementationTypeDeclaration() {
  return newBuilder()
      .setEnclosingTypeDeclaration(this)
      .setOverlaidTypeDeclaration(this)
      .setClassComponents(synthesizeInnerClassComponents(OVERLAY_IMPLEMENTATION_CLASS_SUFFIX))
      .setVisibility(Visibility.PUBLIC)
      .setKind(getKind())
      .build();
}
 
Example #21
Source File: TypeDeclaration.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Memoized
public boolean hasOverlayImplementationType() {
  // TODO(b/116825224): this should just be
  //           isJsEnum() || isNative() || isJsFunctionInteface() && declaresJsOverlayMembers.
  // but there are some synthetic type descriptors created by
  // TypeDescriptors.createNativeGlobalTypeDescriptor that do are marked native and confuse the
  // rewriting of overlay references.
  return isJsEnum()
      || (isJsType() && isNative())
      || (isJsFunctionInterface() && declaresJsOverlayMembers());
}
 
Example #22
Source File: ArrayTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Override
@Memoized
public TypeDescriptor toNullable() {
  if (isNullable()) {
    return this;
  }
  return toBuilder().setNullable(true).build();
}
 
Example #23
Source File: ArrayTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Override
@Memoized
public ArrayTypeDescriptor toRawTypeDescriptor() {
  return toBuilder()
      .setComponentTypeDescriptor(getComponentTypeDescriptor().toRawTypeDescriptor())
      .setNullable(true)
      .build();
}
 
Example #24
Source File: DeclaredTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/** Returns the instance initializer method descriptor for a particular type */
@Memoized
public MethodDescriptor getInitMethodDescriptor() {
  return MethodDescriptor.newBuilder()
      .setEnclosingTypeDescriptor(this)
      .setName(MethodDescriptor.INIT_METHOD_NAME)
      .setVisibility(Visibility.PRIVATE)
      .setOrigin(MethodOrigin.SYNTHETIC_INSTANCE_INITIALIZER)
      .build();
}
 
Example #25
Source File: UnionTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Override
@Memoized
public Set<TypeVariable> getAllTypeVariables() {
  return getUnionTypeDescriptors()
      .stream()
      .map(TypeDescriptor::getAllTypeVariables)
      .flatMap(Set::stream)
      .collect(Collectors.toSet());
}
 
Example #26
Source File: UnionTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Override
@Memoized
public DeclaredTypeDescriptor toRawTypeDescriptor() {
  DeclaredTypeDescriptor typeDescriptor =
      (DeclaredTypeDescriptor) getUnionTypeDescriptors().get(0).toRawTypeDescriptor();
  // Find the closest common ancestor of all the types in the union.
  while (typeDescriptor != null && !isAssignableTo(typeDescriptor)) {
    typeDescriptor = typeDescriptor.getSuperTypeDescriptor();
  }
  return typeDescriptor == null ? TypeDescriptors.get().javaLangObject : typeDescriptor;
}
 
Example #27
Source File: DeclaredTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Override
@Memoized
public DeclaredTypeDescriptor getFunctionalInterface() {
  return isFunctionalInterface()
      ? this
      : getInterfaceTypeDescriptors()
          .stream()
          .filter(DeclaredTypeDescriptor::isFunctionalInterface)
          .findFirst()
          .orElse(null);
}
 
Example #28
Source File: ArrayTypeDescriptor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Override
@Memoized
public ArrayTypeDescriptor toUnparameterizedTypeDescriptor() {
  return toBuilder()
      .setComponentTypeDescriptor(getComponentTypeDescriptor().toUnparameterizedTypeDescriptor())
      .setNullable(true)
      .build();
}
 
Example #29
Source File: TypeDeclaration.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/** Returns the JsConstructor for this class if any. */
@Memoized
@Nullable
public List<MethodDescriptor> getJsConstructorMethodDescriptors() {
  return getDeclaredMethodDescriptors().stream()
      .filter(MethodDescriptor::isJsConstructor)
      .collect(ImmutableList.toImmutableList());
}
 
Example #30
Source File: DeclaredTypeDescriptor.java    From j2cl with Apache License 2.0 4 votes vote down vote up
@Memoized
@Nullable
public MethodDescriptor getJsFunctionMethodDescriptor() {
  return getJsFunctionMethodDescriptorFactory().get(this);
}