Add FunctionMetadata for each method to allow internal visibility check.

This will allow us to check if a method is internal visibility, which is only possible with the metadata.

RELNOTES=N/A
PiperOrigin-RevId: 349454122
diff --git a/java/dagger/internal/codegen/kotlin/KotlinMetadata.java b/java/dagger/internal/codegen/kotlin/KotlinMetadata.java
index 4d8d906..c9bb6c6 100644
--- a/java/dagger/internal/codegen/kotlin/KotlinMetadata.java
+++ b/java/dagger/internal/codegen/kotlin/KotlinMetadata.java
@@ -25,6 +25,7 @@
 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableMap;
 import static dagger.internal.codegen.langmodel.DaggerElements.getAnnotationMirror;
 import static dagger.internal.codegen.langmodel.DaggerElements.getFieldDescriptor;
+import static dagger.internal.codegen.langmodel.DaggerElements.getMethodDescriptor;
 import static kotlinx.metadata.Flag.ValueParameter.DECLARES_DEFAULT_VALUE;
 
 import com.google.auto.value.AutoValue;
@@ -48,12 +49,17 @@
 import kotlin.Metadata;
 import kotlinx.metadata.Flag;
 import kotlinx.metadata.KmClassVisitor;
+import kotlinx.metadata.KmConstructorExtensionVisitor;
 import kotlinx.metadata.KmConstructorVisitor;
 import kotlinx.metadata.KmExtensionType;
+import kotlinx.metadata.KmFunctionExtensionVisitor;
+import kotlinx.metadata.KmFunctionVisitor;
 import kotlinx.metadata.KmPropertyExtensionVisitor;
 import kotlinx.metadata.KmPropertyVisitor;
 import kotlinx.metadata.KmValueParameterVisitor;
+import kotlinx.metadata.jvm.JvmConstructorExtensionVisitor;
 import kotlinx.metadata.jvm.JvmFieldSignature;
+import kotlinx.metadata.jvm.JvmFunctionExtensionVisitor;
 import kotlinx.metadata.jvm.JvmMethodSignature;
 import kotlinx.metadata.jvm.JvmPropertyExtensionVisitor;
 import kotlinx.metadata.jvm.KotlinClassHeader;
@@ -170,6 +176,10 @@
     }
   }
 
+  FunctionMetadata getFunctionMetadata(ExecutableElement method) {
+    return classMetadata().functionsBySignature().get(getMethodDescriptor(method));
+  }
+
   /** Parse Kotlin class metadata from a given type element * */
   static KotlinMetadata from(TypeElement typeElement) {
     return new AutoValue_KotlinMetadata(
@@ -231,6 +241,18 @@
         }
 
         @Override
+        public KmConstructorExtensionVisitor visitExtensions(KmExtensionType kmExtensionType) {
+          return kmExtensionType.equals(JvmConstructorExtensionVisitor.TYPE)
+              ? new JvmConstructorExtensionVisitor() {
+                @Override
+                public void visit(JvmMethodSignature jvmMethodSignature) {
+                  constructor.signature(jvmMethodSignature.asString());
+                }
+              }
+              : null;
+        }
+
+        @Override
         public void visitEnd() {
           classMetadata.addConstructor(constructor.build());
         }
@@ -238,6 +260,36 @@
     }
 
     @Override
+    public KmFunctionVisitor visitFunction(int flags, String name) {
+      return new KmFunctionVisitor() {
+        private final FunctionMetadata.Builder function = FunctionMetadata.builder(flags, name);
+
+        @Override
+        public KmValueParameterVisitor visitValueParameter(int flags, String name) {
+          function.addParameter(ValueParameterMetadata.create(flags, name));
+          return super.visitValueParameter(flags, name);
+        }
+
+        @Override
+        public KmFunctionExtensionVisitor visitExtensions(KmExtensionType kmExtensionType) {
+          return kmExtensionType.equals(JvmFunctionExtensionVisitor.TYPE)
+              ? new JvmFunctionExtensionVisitor() {
+                @Override
+                public void visit(JvmMethodSignature jvmMethodSignature) {
+                  function.signature(jvmMethodSignature.asString());
+                }
+              }
+              : null;
+        }
+
+        @Override
+        public void visitEnd() {
+          classMetadata.addFunction(function.build());
+        }
+      };
+    }
+
+    @Override
     public void visitCompanionObject(String companionObjectName) {
       classMetadata.companionObjectName(companionObjectName);
     }
@@ -290,6 +342,8 @@
 
     abstract ImmutableSet<FunctionMetadata> constructors();
 
+    abstract ImmutableMap<String, FunctionMetadata> functionsBySignature();
+
     abstract ImmutableMap<String, PropertyMetadata> propertiesByFieldSignature();
 
     static Builder builder() {
@@ -302,6 +356,8 @@
 
       abstract ImmutableSet.Builder<FunctionMetadata> constructorsBuilder();
 
+      abstract ImmutableMap.Builder<String, FunctionMetadata> functionsBySignatureBuilder();
+
       abstract ImmutableMap.Builder<String, PropertyMetadata> propertiesByFieldSignatureBuilder();
 
       Builder addConstructor(FunctionMetadata constructor) {
@@ -309,6 +365,11 @@
         return this;
       }
 
+      Builder addFunction(FunctionMetadata function) {
+        functionsBySignatureBuilder().put(function.signature(), function);
+        return this;
+      }
+
       Builder addProperty(PropertyMetadata property) {
         if (property.fieldSignature().isPresent()) {
           propertiesByFieldSignatureBuilder().put(property.fieldSignature().get(), property);
@@ -322,6 +383,8 @@
 
   @AutoValue
   abstract static class FunctionMetadata extends BaseMetadata {
+    abstract String signature();
+
     abstract ImmutableList<ValueParameterMetadata> parameters();
 
     static Builder builder(int flags, String name) {
@@ -330,6 +393,8 @@
 
     @AutoValue.Builder
     abstract static class Builder implements BaseMetadata.Builder<Builder> {
+      abstract Builder signature(String signature);
+
       abstract ImmutableList.Builder<ValueParameterMetadata> parametersBuilder();
 
       Builder addParameter(ValueParameterMetadata parameter) {
diff --git a/java/dagger/internal/codegen/kotlin/KotlinMetadataUtil.java b/java/dagger/internal/codegen/kotlin/KotlinMetadataUtil.java
index 7a5a3f3..e82c9f9 100644
--- a/java/dagger/internal/codegen/kotlin/KotlinMetadataUtil.java
+++ b/java/dagger/internal/codegen/kotlin/KotlinMetadataUtil.java
@@ -37,6 +37,7 @@
 import javax.lang.model.util.ElementFilter;
 import kotlin.Metadata;
 import kotlin.jvm.JvmStatic;
+import kotlinx.metadata.Flag;
 
 /** Utility class for interacting with Kotlin Metadata. */
 public final class KotlinMetadataUtil {
@@ -122,6 +123,15 @@
         && metadataFactory.create(typeElement).classMetadata().flags(IS_PRIVATE);
   }
 
+  /**
+   * Returns {@code true} if the given executable element was declared {@code internal} in its
+   * Kotlin source.
+   */
+  public boolean isVisibilityInternal(ExecutableElement method) {
+    return hasMetadata(method)
+        && metadataFactory.create(method).getFunctionMetadata(method).flags(Flag.IS_INTERNAL);
+  }
+
   public Optional<ExecutableElement> getPropertyGetter(VariableElement fieldElement) {
     return metadataFactory.create(fieldElement).getPropertyGetter(fieldElement);
   }