3232import pascal .taie .ir .IR ;
3333import pascal .taie .ir .IRBuilder ;
3434import pascal .taie .ir .exp .Var ;
35+ import pascal .taie .ir .proginfo .FieldRef ;
36+ import pascal .taie .ir .proginfo .MemberRef ;
37+ import pascal .taie .ir .proginfo .MethodRef ;
38+ import pascal .taie .ir .stmt .FieldStmt ;
3539import pascal .taie .ir .stmt .Invoke ;
3640import pascal .taie .language .classes .ClassHierarchy ;
3741import pascal .taie .language .classes .ClassNames ;
@@ -163,7 +167,17 @@ void compareIR() {
163167 World .set (world1 ); // World.world should be set for building IRs
164168 World world2 = SerializationUtils .serializedCopy (world1 );
165169 World .set (world2 ); // World.world should be set for getting IRs
166- // test1: compare the size of IR, a simple and loose test
170+
171+ this .compareIrSize (world1 , world2 );
172+ this .compareVarRelevantStmtsEmpty (world1 , world2 );
173+ this .compareGSig (world1 , world2 );
174+ this .compareMemberRef (world1 , world2 );
175+ }
176+
177+ /**
178+ * Compare the size of IR, a simple and loose test
179+ */
180+ private void compareIrSize (World world1 , World world2 ) {
167181 JClass concurrentHashMapClz1 = world1 .getClassHierarchy ()
168182 .getClass ("java.util.concurrent.ConcurrentHashMap" );
169183 JClass concurrentHashMapClz2 = world2 .getClassHierarchy ()
@@ -179,7 +193,12 @@ void compareIR() {
179193 assertEquals (ir1 .getParams ().size (), ir2 .getParams ().size ());
180194 assertEquals (ir1 .getStmts ().size (), ir2 .getStmts ().size ());
181195 assertEquals (ir1 .getReturnVars ().size (), ir2 .getReturnVars ().size ());
182- // test2: compare the Var.RelevantStmts.Empty
196+ }
197+
198+ /**
199+ * Compare the Var.RelevantStmts.Empty
200+ */
201+ private void compareVarRelevantStmtsEmpty (World world1 , World world2 ) {
183202 Var v21 = world2 .getClassHierarchy ()
184203 .getClass ("java.util.HashMap" )
185204 .getDeclaredMethod ("hash" )
@@ -197,7 +216,12 @@ void compareIR() {
197216 v22 .addInvoke (new Invoke (null , null , null ));
198217 assertEquals (0 , v21 .getInvokes ().size ()); // v21 should be not changed
199218 assertEquals (1 , v22 .getInvokes ().size ());
200- // test3: compare generics signature
219+ }
220+
221+ /**
222+ * Compare generics signature
223+ */
224+ private void compareGSig (World world1 , World world2 ) {
201225 JClass enumClz1 = world1 .getClassHierarchy ().getClass ("java.lang.Enum" );
202226 JClass enumClz2 = world2 .getClassHierarchy ().getClass ("java.lang.Enum" );
203227 ClassGSignature cSig1 = enumClz1 .getGSignature ();
@@ -210,6 +234,10 @@ void compareIR() {
210234 if (mSig1 != null && mSig2 != null ) {
211235 assertEquals (mSig1 .toString (), mSig2 .toString ());
212236 }
237+ JClass concurrentHashMapClz1 = world1 .getClassHierarchy ()
238+ .getClass ("java.util.concurrent.ConcurrentHashMap" );
239+ JClass concurrentHashMapClz2 = world2 .getClassHierarchy ()
240+ .getClass ("java.util.concurrent.ConcurrentHashMap" );
213241 JField tableField1 = concurrentHashMapClz1 .getDeclaredField ("table" );
214242 JField tableField2 = concurrentHashMapClz2 .getDeclaredField ("table" );
215243 ReferenceTypeGSignature fSig1 = tableField1 .getGSignature ();
@@ -219,4 +247,47 @@ void compareIR() {
219247 }
220248 }
221249
250+ /**
251+ * Compare {@link MemberRef} after cleaning their caches
252+ */
253+ private void compareMemberRef (World world1 , World world2 ) {
254+ JClass hashMapClz = world2 .getClassHierarchy ().getClass ("java.util.HashMap" );
255+ IR ir = hashMapClz .getDeclaredMethod ("putVal" ).getIR ();
256+ // Compare MethodRef
257+ Invoke invoke = ir .getStmts ().stream ()
258+ .filter (stmt -> stmt instanceof Invoke )
259+ .map (stmt -> (Invoke ) stmt )
260+ .findFirst ()
261+ .get ();
262+ MethodRef methodRef = invoke .getMethodRef ();
263+ MethodRef methodRef2 = methodRef .resolve ().getRef ();
264+ // TODO: support methodRef.equals(methodRef2)
265+ compareMethodRef (methodRef , methodRef2 );
266+ // Compare FieldRef
267+ FieldStmt fieldStmt = ir .getStmts ().stream ()
268+ .filter (stmt -> stmt instanceof FieldStmt )
269+ .map (stmt -> (FieldStmt ) stmt )
270+ .findFirst ()
271+ .get ();
272+ FieldRef fieldRef = fieldStmt .getFieldRef ();
273+ FieldRef fieldRef2 = fieldRef .resolve ().getRef ();
274+ // TODO: support fieldRef.equals(fieldRef2)
275+ compareFieldRef (fieldRef , fieldRef2 );
276+ }
277+
278+ private void compareMethodRef (MethodRef methodRef1 , MethodRef methodRef2 ) {
279+ assertEquals (methodRef1 .getDeclaringClass ().getName (), methodRef2 .getDeclaringClass ().getName ());
280+ assertEquals (methodRef1 .getName (), methodRef2 .getName ());
281+ assertEquals (methodRef1 .isStatic (), methodRef2 .isStatic ());
282+ assertEquals (methodRef1 .getSubsignature (), methodRef2 .getSubsignature ());
283+ }
284+
285+ private void compareFieldRef (FieldRef fieldRef1 , FieldRef fieldRef2 ) {
286+ assertEquals (fieldRef1 .getDeclaringClass ().getName (), fieldRef2 .getDeclaringClass ().getName ());
287+ assertEquals (fieldRef1 .getName (), fieldRef2 .getName ());
288+ assertEquals (fieldRef1 .isStatic (), fieldRef2 .isStatic ());
289+ assertEquals (fieldRef1 .getType ().getName (), fieldRef2 .getType ().getName ());
290+ }
291+
292+
222293}
0 commit comments