Snippets

Michael Eichberg [OPAL - 0.8.10 - not 0.8.15] Demonstrates how to query call targets of methods (Basis for the CHA Call Graph Algorithm)

Updated by Michael Eichberg

File CHACallGraphInfrastructure.scala Modified

  • Ignore whitespace
  • Hide word diff
-// This code snippet demonstrates how to get resolve method call targets based on the available type information only.
-// This is the basis of the CHA call graph algorithms.
+// This code snippet demonstrates how to resolve method call targets based on the available type information only.
+// This is the basis/the first part of the CHA call graph algorithms.
 
 import org.opalj.collection.immutable._; import org.opalj.br._ ; import org.opalj.br.analyses._; import org.opalj.br.instructions._; import org.opalj.collection.immutable.ConstArray.find; import org.opalj.util.PerformanceEvaluation.time
 
Updated by Michael Eichberg

File CHACallGraphInfrastructure.scala Modified

  • Ignore whitespace
  • Hide word diff
+// This code snippet demonstrates how to get resolve method call targets based on the available type information only.
+// This is the basis of the CHA call graph algorithms.
+
 import org.opalj.collection.immutable._; import org.opalj.br._ ; import org.opalj.br.analyses._; import org.opalj.br.instructions._; import org.opalj.collection.immutable.ConstArray.find; import org.opalj.util.PerformanceEvaluation.time
-// create a project
-// implicit val p = Project(new java.io.File("OPAL/bi/target/scala-2.11/resource_managed/test/cg.jar"))
+
+// 1. Create a project which also includes the RT.jar as a library.
 implicit val p = Project(new java.io.File("OPAL/bi/target/scala-2.11/resource_managed/test/cg.jar"),org.opalj.bytecode.RTJar)
 
+// 1.1. Check that the project setup is as expected; depending on the configuration the number of call targets
+//		may change. 
 TypeExtensibilityKey.AllClosed = true // <= "HACK" only necessary as long as we don't have a decent "isExtensiblity" analysis
 val typeExtensibility = p.get(TypeExtensibilityKey)
 assert(typeExtensibility(ObjectType("cg/default_methods/ISuper")).isNo /* EXPECTED: org.opalj.Answer = No */ )
Updated by Michael Eichberg

File CHACallGraphInfrastructure.scala Modified

  • Ignore whitespace
  • Hide word diff
 import org.opalj.collection.immutable._; import org.opalj.br._ ; import org.opalj.br.analyses._; import org.opalj.br.instructions._; import org.opalj.collection.immutable.ConstArray.find; import org.opalj.util.PerformanceEvaluation.time
 // create a project
-implicit val p = Project(new java.io.File("OPAL/bi/target/scala-2.11/resource_managed/test/cg.jar"))
+// implicit val p = Project(new java.io.File("OPAL/bi/target/scala-2.11/resource_managed/test/cg.jar"))
+implicit val p = Project(new java.io.File("OPAL/bi/target/scala-2.11/resource_managed/test/cg.jar"),org.opalj.bytecode.RTJar)
 
 TypeExtensibilityKey.AllClosed = true // <= "HACK" only necessary as long as we don't have a decent "isExtensiblity" analysis
 val typeExtensibility = p.get(TypeExtensibilityKey)
 val typeBasedCallTargets = p.get(TypeBasedCallTargetsKey)
 typeBasedCallTargets.specialCall(ObjectType("cg/default_methods/Super"),"<init>",MethodDescriptor("()V")).map(m => m.toJava(p.classFile(m))) // EXPECTED: Set(java8methodresolution.Super{ public void <init>() })
 
+// ALWAYS OVERRIDDEN ABSTRACT METHOD
+val candidates = p.virtualCall("foo",ObjectType("cg/always_overridden/Super"),"doIt",MethodDescriptor("()V"))
+typeBasedCallTargets.filterAlwaysOverriddenMethods(candidates)
+
 // GET CALL TARGETS OF INTERFACE METHOD INVOKES (INVOKEINTERFACE)
 typeBasedCallTargets.interfaceCall(ObjectType("cg/default_methods/ISuperAlt"),"compute",MethodDescriptor("(II)I"))
 
 val pts = p.virtualCall("cg/visibility/a",invokeVirtuals(0)._2).map(m => m.toJava(p.classFile(m)))
 val tts = typeBasedCallTargets.virtualCall("cg/visibility/a",invokeVirtuals(0)._2)
 pts eq tts // => false
-tts eq typeBasedCallTargets.virtualCall("cg/visibility/a",invokeVirtuals(0)._2) // => true (results are cached)
+tts eq typeBasedCallTargets.virtualCall("cg/visibility/a",invokeVirtuals(0)._2)// => true (results are cached)
 
 p.virtualCall("cg/visibility/a",invokeVirtuals(0)._2).map(m => m.toJava(p.classFile(m)))
 //res: scala.collection.immutable.Set[String] = Set(cg.visibility.a.C{ protected void printIt() }, cg.visibility.a.A{ void printIt() })
 
 p.virtualCall("cg/visibility/b",invokeVirtuals(0)._2).map(m => m.toJava(p.classFile(m)))
 //res: scala.collection.immutable.Set[String] = Set(cg.visibility.b.B{ protected void printIt() }, cg.visibility.a.C{ protected void printIt() })
+
+// LOOKUP OF ARRAY BASED METHOD CALLS
+typeBasedCallTargets.virtualCall("N/A",ArrayType(ObjectType("java/lang/Object")),"hashCode",MethodDescriptor("()I"))
+
+typeBasedCallTargets.virtualCall("foo",ObjectType("cg/always_overridden/Super"),"doIt",MethodDescriptor("()V"))
Updated by Michael Eichberg

File CHACallGraphInfrastructure.scala Modified

  • Ignore whitespace
  • Hide word diff
 (96,INVOKEVIRTUAL(cg.visibility.a.C{ void printIt() }))
 */
 
-p.virtualCall("cg/visibility/a",invokeVirtuals(0)._2).map(m => m.toJava(p.classFile(m)))
-//res: Set[org.opalj.br.Method] = Set(protected void printIt(), void printIt())
+val pts = p.virtualCall("cg/visibility/a",invokeVirtuals(0)._2).map(m => m.toJava(p.classFile(m)))
+val tts = typeBasedCallTargets.virtualCall("cg/visibility/a",invokeVirtuals(0)._2)
+pts eq tts // => false
+tts eq typeBasedCallTargets.virtualCall("cg/visibility/a",invokeVirtuals(0)._2) // => true (results are cached)
 
 p.virtualCall("cg/visibility/a",invokeVirtuals(0)._2).map(m => m.toJava(p.classFile(m)))
 //res: scala.collection.immutable.Set[String] = Set(cg.visibility.a.C{ protected void printIt() }, cg.visibility.a.A{ void printIt() })
Updated by Michael Eichberg

File CHACallGraphInfrastructure.scala Modified

  • Ignore whitespace
  • Hide word diff
 val typeBasedCallTargets = p.get(TypeBasedCallTargetsKey)
 typeBasedCallTargets.specialCall(ObjectType("cg/default_methods/Super"),"<init>",MethodDescriptor("()V")).map(m => m.toJava(p.classFile(m))) // EXPECTED: Set(java8methodresolution.Super{ public void <init>() })
 
-// GET CALL TARGETS OF INTERFACE METHOD INVOKES (INVOKEVIRTUAL)
+// GET CALL TARGETS OF INTERFACE METHOD INVOKES (INVOKEINTERFACE)
 typeBasedCallTargets.interfaceCall(ObjectType("cg/default_methods/ISuperAlt"),"compute",MethodDescriptor("(II)I"))
 
 p.interfaceCall(ObjectType("cg/default_methods/ISuper"),"compute",MethodDescriptor("(II)I")).map(m => m.toJava(p.classFile(m))).mkString("Targets:\n\t","\n\t","\n") 
 (96,INVOKEVIRTUAL(cg.visibility.a.C{ void printIt() }))
 */
 
-p.virtualCall("cg/visibility/a",invokeVirtuals(0)._2)
+p.virtualCall("cg/visibility/a",invokeVirtuals(0)._2).map(m => m.toJava(p.classFile(m)))
 //res: Set[org.opalj.br.Method] = Set(protected void printIt(), void printIt())
 
 p.virtualCall("cg/visibility/a",invokeVirtuals(0)._2).map(m => m.toJava(p.classFile(m)))
  1. 1
  2. 2
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.