Skip to content

Commit

Permalink
Remove usage of Objects class in @CachedValue generation
Browse files Browse the repository at this point in the history
Summary: Objects is API 19+

Reviewed By: muraziz

Differential Revision: D15335806

fbshipit-source-id: 912828889ac4a3b9f0d33880f30b3000c2d440fd
  • Loading branch information
astreet authored and facebook-github-bot committed May 14, 2019
1 parent 253ac70 commit aabb24a
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 12 deletions.
6 changes: 6 additions & 0 deletions litho-core/src/main/java/com/facebook/litho/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.facebook.litho;

import androidx.annotation.Nullable;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

Expand Down Expand Up @@ -51,4 +52,9 @@ public static <A> List<A> addOrCreateList(@Nullable List<A> list, A item) {

return list;
}

/** Polyfill of Objects.hash that can be used on API<19. */
public static int hash(Object... values) {
return Arrays.hashCode(values);
}
}
4 changes: 2 additions & 2 deletions litho-it/src/test/resources/processor/TestHotswapLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import androidx.annotation.StringRes;
import androidx.annotation.VisibleForTesting;
import com.facebook.litho.ClickEvent;
import com.facebook.litho.CommonUtils;
import com.facebook.litho.Component;
import com.facebook.litho.ComponentContext;
import com.facebook.litho.ComponentLifecycle;
Expand All @@ -48,7 +49,6 @@
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.Objects;

/**
* @prop-required aspectRatio float
Expand Down Expand Up @@ -945,7 +945,7 @@ private static class CachedInputs {

@Override
public int hashCode() {
return Objects.hash(prop3, prop5, state1);
return CommonUtils.hash(prop3, prop5, state1);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions litho-it/src/test/resources/processor/TestLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import androidx.annotation.StringRes;
import androidx.annotation.VisibleForTesting;
import com.facebook.litho.ClickEvent;
import com.facebook.litho.CommonUtils;
import com.facebook.litho.Component;
import com.facebook.litho.ComponentContext;
import com.facebook.litho.ComponentLifecycle;
Expand All @@ -46,7 +47,6 @@
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.Objects;

/**
* @prop-required aspectRatio float
Expand Down Expand Up @@ -804,7 +804,7 @@ private static class CachedInputs {

@Override
public int hashCode() {
return Objects.hash(prop3, prop5, state1);
return CommonUtils.hash(prop3, prop5, state1);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions litho-it/src/test/resources/processor/TestMount.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import androidx.annotation.VisibleForTesting;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import com.facebook.litho.ClickEvent;
import com.facebook.litho.CommonUtils;
import com.facebook.litho.Component;
import com.facebook.litho.ComponentContext;
import com.facebook.litho.ComponentLayout;
Expand All @@ -48,7 +49,6 @@
import com.facebook.litho.annotations.TreeProp;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Objects;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -787,7 +787,7 @@ private static class CachedInputs {

@Override
public int hashCode() {
return Objects.hash(prop3, prop5, state1);
return CommonUtils.hash(prop3, prop5, state1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.view.View;
import androidx.annotation.VisibleForTesting;
import com.facebook.litho.ClickEvent;
import com.facebook.litho.CommonUtils;
import com.facebook.litho.Component;
import com.facebook.litho.Diff;
import com.facebook.litho.EventDispatcher;
Expand All @@ -37,7 +38,6 @@
import com.facebook.litho.sections.SectionLifecycle;
import java.util.BitSet;
import java.util.List;
import java.util.Objects;

/**
* @prop-required data java.util.List<T>
Expand Down Expand Up @@ -474,7 +474,7 @@ private static class CachedInputs {

@Override
public int hashCode() {
return Objects.hash(prop1);
return CommonUtils.hash(prop1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import androidx.annotation.StringRes;
import androidx.annotation.VisibleForTesting;
import com.facebook.litho.ClickEvent;
import com.facebook.litho.CommonUtils;
import com.facebook.litho.Component;
import com.facebook.litho.Diff;
import com.facebook.litho.EventDispatcher;
Expand All @@ -40,7 +41,6 @@
import com.facebook.litho.sections.SectionContext;
import com.facebook.litho.sections.SectionLifecycle;
import java.util.BitSet;
import java.util.Objects;

/**
* Comment to be copied in generated section
Expand Down Expand Up @@ -596,7 +596,7 @@ private static class CachedInputs {

@Override
public int hashCode() {
return Objects.hash(prop1);
return CommonUtils.hash(prop1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ private static TypeSpec createInputsClass(
.returns(TypeName.INT)
.addModifiers(Modifier.PUBLIC);

CodeBlock.Builder codeBlock = CodeBlock.builder().add("return $T.hash(", ClassNames.OBJECTS);
CodeBlock.Builder codeBlock =
CodeBlock.builder().add("return $T.hash(", ClassNames.COMMON_UTILS);
for (int i = 0, size = onCalculateCachedValueMethod.methodParams.size(); i < size; i++) {
if (i < size - 1) {
codeBlock.add("$L, ", onCalculateCachedValueMethod.methodParams.get(i).getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
public interface ClassNames {
ClassName CLASS = ClassName.bestGuess("java.lang.Class");
ClassName OBJECT = ClassName.bestGuess("java.lang.Object");
ClassName OBJECTS = ClassName.bestGuess("java.util.Objects");
ClassName STRING = ClassName.bestGuess("java.lang.String");
ClassName EXCEPTION = ClassName.bestGuess("java.lang.Exception");
ClassName COMMON_UTILS = ClassName.bestGuess("com.facebook.litho.CommonUtils");

String VIEW_NAME = "android.view.View";
ClassName VIEW = ClassName.bestGuess(VIEW_NAME);
Expand Down

0 comments on commit aabb24a

Please sign in to comment.