Skip to content

Commit

Permalink
fbandroid/libraries/components/litho-intellij-plugin/src/main/java/co…
Browse files Browse the repository at this point in the history
…m/facebook/litho/specmodels/processor/PsiAnnotationProxyUtils.java

Reviewed By: pentiumao

Differential Revision: D65128486

fbshipit-source-id: 60028fd052a2401220b6dcbcbb82716979b795de
  • Loading branch information
generatedunixname89002005287564 authored and facebook-github-bot committed Oct 29, 2024
1 parent 6d261b7 commit 2c369fa
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.facebook.litho.specmodels.processor;

import com.facebook.infer.annotation.Nullsafe;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiExpression;
Expand All @@ -36,6 +37,7 @@
* usable and implements methods like {@code hashCode}, which go unimplemented by Psi's default
* Annotation implementation.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
public class PsiAnnotationProxyUtils {

@Nullable
Expand Down Expand Up @@ -71,46 +73,56 @@ private static class AnnotationProxyInvocationHandler<T extends Annotation>

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws IllegalAccessException {
// NULLSAFE_FIXME[Not Vetted Third-Party]
switch (method.getName()) {
case "hashCode":
return proxyHashCode();
case "equals":
return proxyEquals(args[0]);
case "toString":
// NULLSAFE_FIXME[Not Vetted Third-Party]
return mStubbed.toString();
default:
return invoke(method, args);
}
}

private Object invoke(Method method) throws IllegalAccessException {
// NULLSAFE_FIXME[Parameter Not Nullable]
return invoke(method, null);
}

private Object invoke(Method method, Object[] args) throws IllegalAccessException {
// NULLSAFE_FIXME[Not Vetted Third-Party]
Class<?> returnType = method.getReturnType();
if (returnType.isEnum()) {
PsiAnnotation currentAnnotation =
AnnotationUtil.findAnnotationInHierarchy(
mListOwner, Collections.singleton(mAnnotationClass.getCanonicalName()));
PsiExpression declaredValue =
// NULLSAFE_FIXME[Nullable Dereference]
(PsiExpression) currentAnnotation.findAttributeValue(method.getName());
if (declaredValue == null) {
// NULLSAFE_FIXME[Not Vetted Third-Party]
return method.getDefaultValue();
}
PsiIdentifier identifier = PsiTreeUtil.getChildOfType(declaredValue, PsiIdentifier.class);
if (identifier == null) {
// NULLSAFE_FIXME[Not Vetted Third-Party]
return method.getDefaultValue();
}
return Enum.valueOf((Class<Enum>) returnType, identifier.getText());
}

try {
if (args == null) {
// NULLSAFE_FIXME[Return Not Nullable]
return method.invoke(mStubbed);
}
// NULLSAFE_FIXME[Return Not Nullable]
return method.invoke(mStubbed, args);
} catch (InvocationTargetException e) {
// NULLSAFE_FIXME[Not Vetted Third-Party]
return method.getDefaultValue();
}
}
Expand Down Expand Up @@ -147,6 +159,7 @@ private Object invoke(Method method, Object[] args) throws IllegalAccessExceptio
*/
private <A> int proxyHashCode() {
int hashCode = 0;
// NULLSAFE_FIXME[Not Vetted Third-Party]
for (Method method : mAnnotationClass.getDeclaredMethods()) {
Object returnedObject;
try {
Expand All @@ -157,7 +170,9 @@ private <A> int proxyHashCode() {
e);
}

// NULLSAFE_FIXME[Not Vetted Third-Party]
int memberHash = 127 * method.getName().hashCode();
// NULLSAFE_FIXME[Not Vetted Third-Party]
Class<?> returnType = method.getReturnType();
if (returnType.isArray()) {
A[] array = (A[]) returnedObject;
Expand All @@ -174,11 +189,14 @@ private <A> int proxyHashCode() {

// Approximates the Annotation.equals() javadoc's implementation
private <A> boolean proxyEquals(@Nullable Object other) {
// NULLSAFE_FIXME[Parameter Not Nullable]
if (!mAnnotationClass.isInstance(other)) {
return false;
}
boolean equals = true;
// NULLSAFE_FIXME[Not Vetted Third-Party]
for (Method method : mAnnotationClass.getDeclaredMethods()) {
// NULLSAFE_FIXME[Not Vetted Third-Party]
Class<?> returnType = method.getReturnType();

Object thisReturnedObject;
Expand Down

0 comments on commit 2c369fa

Please sign in to comment.