From 5dfe55539414ad1da511ff1b9c78b076fbb58a22 Mon Sep 17 00:00:00 2001
From: Junyu Lai <junyulai@google.com>
Date: Fri, 26 Jan 2024 17:20:59 +0800
Subject: [PATCH] Use NewOject to invoke constructor of NetworkStats.Entry

AllocObject doesn't invoke the constructor, and when working on
Java code we wouldn't expect that stuff initialized in the
constructor wouldn't actually be initialized on instances
created from JNI.

Test: atest NetworkStackIntegrationTests:android.net.NetworkStatsIntegrationTest
Fix: 322456911
Change-Id: Ibc87ab5ebaf02a7bcaa88307511bd3aaea8698ed
---
 .../jni/com_android_server_net_NetworkStatsService.cpp    | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/service-t/jni/com_android_server_net_NetworkStatsService.cpp b/service-t/jni/com_android_server_net_NetworkStatsService.cpp
index 81912ae2cd..c1201a70b8 100644
--- a/service-t/jni/com_android_server_net_NetworkStatsService.cpp
+++ b/service-t/jni/com_android_server_net_NetworkStatsService.cpp
@@ -45,8 +45,14 @@ static jobject statsValueToEntry(JNIEnv* env, StatsValue* stats) {
         return nullptr;
     }
 
+    // Find the constructor.
+    jmethodID constructorID = env->GetMethodID(gEntryClass, "<init>", "()V");
+    if (constructorID == nullptr) {
+        return nullptr;
+    }
+
     // Create a new instance of the Java class
-    jobject result = env->AllocObject(gEntryClass);
+    jobject result = env->NewObject(gEntryClass, constructorID);
     if (result == nullptr) {
         return nullptr;
     }
-- 
GitLab