Skip to content

Commit 620303d

Browse files
committed
Add style guide for Scala code
1 parent 4154e03 commit 620303d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+5014
-3109
lines changed
Lines changed: 121 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,144 @@
1+
/**
2+
* *****************************************************************************
3+
* Copyright (c) 2025 Fraunhofer IEM, Paderborn, Germany. This program and the
4+
* accompanying materials are made available under the terms of the Eclipse
5+
* Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0.
6+
*
7+
* <p>SPDX-License-Identifier: EPL-2.0
8+
*
9+
* <p>Contributors: Johannes Spaeth - initial API and implementation
10+
* *****************************************************************************
11+
*/
112
package boomerang.scope.opal
213

3-
import boomerang.scope.{CallGraph, InvokeExpr}
14+
import boomerang.scope.CallGraph
415
import boomerang.scope.CallGraph.Edge
5-
import boomerang.scope.opal.tac.{OpalFunctionInvokeExpr, OpalMethod, OpalMethodInvokeExpr, OpalPhantomMethod, OpalStatement}
16+
import boomerang.scope.InvokeExpr
17+
import boomerang.scope.opal.tac.OpalFunctionInvokeExpr
18+
import boomerang.scope.opal.tac.OpalMethod
19+
import boomerang.scope.opal.tac.OpalMethodInvokeExpr
20+
import boomerang.scope.opal.tac.OpalPhantomMethod
21+
import boomerang.scope.opal.tac.OpalStatement
622
import boomerang.scope.opal.transformation.TacBodyBuilder
23+
import org.opalj.br.DefinedMethod
24+
import org.opalj.br.Method
25+
import org.opalj.br.MultipleDefinedMethods
26+
import org.opalj.br.VirtualDeclaredMethod
727
import org.opalj.br.analyses.Project
8-
import org.opalj.br.{DefinedMethod, Method, MultipleDefinedMethods, VirtualDeclaredMethod}
9-
import org.opalj.tac.{NonVirtualFunctionCall, StaticFunctionCall, VirtualFunctionCall}
28+
import org.opalj.tac.NonVirtualFunctionCall
29+
import org.opalj.tac.StaticFunctionCall
30+
import org.opalj.tac.VirtualFunctionCall
1031

11-
class OpalCallGraph(project: Project[_], callGraph: org.opalj.tac.cg.CallGraph, entryPoints: Set[Method]) extends CallGraph {
32+
class OpalCallGraph(
33+
project: Project[_],
34+
callGraph: org.opalj.tac.cg.CallGraph,
35+
entryPoints: Set[Method]
36+
) extends CallGraph {
1237

13-
// TODO Deal with <clinit>
14-
callGraph.reachableMethods().foreach(method => {
15-
method.method match {
16-
case definedMethod: DefinedMethod =>
17-
if (definedMethod.definedMethod.body.isDefined) {
18-
addEdgesFromMethod(definedMethod)
19-
}
20-
// TODO Should this case be considered?
21-
// case definedMethods: MultipleDefinedMethods =>
22-
// definedMethods.foreachDefinedMethod(m => addEdgesFromMethod(m))
23-
case _ =>
24-
}
25-
})
38+
// TODO Deal with <clinit>
39+
callGraph
40+
.reachableMethods()
41+
.foreach(method => {
42+
method.method match {
43+
case definedMethod: DefinedMethod =>
44+
if (definedMethod.definedMethod.body.isDefined) {
45+
addEdgesFromMethod(definedMethod)
46+
}
47+
// TODO Should this case be considered?
48+
// case definedMethods: MultipleDefinedMethods =>
49+
// definedMethods.foreachDefinedMethod(m => addEdgesFromMethod(m))
50+
case _ =>
51+
}
52+
})
2653

27-
private def addEdgesFromMethod(method: DefinedMethod): Unit = {
28-
val tacCode = TacBodyBuilder(project, method.definedMethod)
54+
private def addEdgesFromMethod(method: DefinedMethod): Unit = {
55+
val tacCode = TacBodyBuilder(project, method.definedMethod)
2956

30-
tacCode.statements.foreach(stmt => {
31-
val srcStatement = new OpalStatement(stmt, OpalMethod(method.definedMethod, tacCode))
57+
tacCode.statements.foreach(stmt => {
58+
val srcStatement =
59+
new OpalStatement(stmt, OpalMethod(method.definedMethod, tacCode))
3260

33-
if (srcStatement.containsInvokeExpr()) {
34-
// Due to inlining variables, the PC's of statements and invoke expressions may differ
35-
val invokeExprPc = getPcForInvokeExpr(srcStatement.getInvokeExpr)
36-
val callees = callGraph.directCalleesOf(method, invokeExprPc)
61+
if (srcStatement.containsInvokeExpr()) {
62+
// Due to inlining variables, the PC's of statements and invoke expressions may differ
63+
val invokeExprPc = getPcForInvokeExpr(srcStatement.getInvokeExpr)
64+
val callees = callGraph.directCalleesOf(method, invokeExprPc)
3765

38-
callees.foreach(callee => {
39-
callee.method match {
40-
case definedMethod: DefinedMethod =>
41-
val method = definedMethod.definedMethod
66+
callees.foreach(callee => {
67+
callee.method match {
68+
case definedMethod: DefinedMethod =>
69+
val method = definedMethod.definedMethod
4270

43-
if (method.body.isDefined) {
44-
val targetMethod = OpalMethod(method)
71+
if (method.body.isDefined) {
72+
val targetMethod = OpalMethod(method)
4573

46-
addEdge(new Edge(srcStatement, targetMethod))
47-
} else {
48-
val targetMethod = OpalPhantomMethod(definedMethod.declaringClassType, definedMethod.name, definedMethod.descriptor, method.isStatic)
74+
addEdge(new Edge(srcStatement, targetMethod))
75+
} else {
76+
val targetMethod = OpalPhantomMethod(
77+
definedMethod.declaringClassType,
78+
definedMethod.name,
79+
definedMethod.descriptor,
80+
method.isStatic
81+
)
4982

50-
addEdge(new Edge(srcStatement, targetMethod))
51-
}
52-
case virtualMethod: VirtualDeclaredMethod =>
53-
val targetMethod = OpalPhantomMethod(virtualMethod.declaringClassType, virtualMethod.name, virtualMethod.descriptor, srcStatement.getInvokeExpr.isStaticInvokeExpr)
83+
addEdge(new Edge(srcStatement, targetMethod))
84+
}
85+
case virtualMethod: VirtualDeclaredMethod =>
86+
val targetMethod = OpalPhantomMethod(
87+
virtualMethod.declaringClassType,
88+
virtualMethod.name,
89+
virtualMethod.descriptor,
90+
srcStatement.getInvokeExpr.isStaticInvokeExpr
91+
)
5492

55-
addEdge(new Edge(srcStatement, targetMethod))
56-
case definedMethods: MultipleDefinedMethods =>
57-
definedMethods.foreachDefinedMethod(method => {
58-
val targetMethod = OpalMethod(method)
93+
addEdge(new Edge(srcStatement, targetMethod))
94+
case definedMethods: MultipleDefinedMethods =>
95+
definedMethods.foreachDefinedMethod(method => {
96+
val targetMethod = OpalMethod(method)
5997

60-
addEdge(new Edge(srcStatement, targetMethod))
61-
})
62-
}
98+
addEdge(new Edge(srcStatement, targetMethod))
99+
})
100+
}
101+
})
102+
}
63103
})
64-
}
65-
})
66-
}
67-
68-
private def getPcForInvokeExpr(invokeExpr: InvokeExpr): Int = {
69-
invokeExpr match {
70-
case methodInvokeExpr: OpalMethodInvokeExpr => methodInvokeExpr.delegate.pc
71-
case functionInvokeExpr: OpalFunctionInvokeExpr =>
72-
functionInvokeExpr.delegate match {
73-
case call: NonVirtualFunctionCall[_] => call.pc
74-
case call: VirtualFunctionCall[_] => call.pc
75-
case call: StaticFunctionCall[_] => call.pc
76-
case _ => throw new RuntimeException("Unknown function call: " + functionInvokeExpr)
77-
}
78-
case _ => throw new RuntimeException("Unknown invoke expression: " + invokeExpr)
79104
}
80-
}
81105

82-
// Explicitly add static initializers (<clinit>) as they are called only implicitly
83-
callGraph.reachableMethods().foreach(method => {
84-
method.method match {
85-
case definedMethod: DefinedMethod if definedMethod.definedMethod.isStaticInitializer =>
86-
if (definedMethod.definedMethod.body.isDefined) {
87-
addEntryPoint(OpalMethod(definedMethod.definedMethod))
106+
private def getPcForInvokeExpr(invokeExpr: InvokeExpr): Int = {
107+
invokeExpr match {
108+
case methodInvokeExpr: OpalMethodInvokeExpr =>
109+
methodInvokeExpr.delegate.pc
110+
case functionInvokeExpr: OpalFunctionInvokeExpr =>
111+
functionInvokeExpr.delegate match {
112+
case call: NonVirtualFunctionCall[_] => call.pc
113+
case call: VirtualFunctionCall[_] => call.pc
114+
case call: StaticFunctionCall[_] => call.pc
115+
case _ =>
116+
throw new RuntimeException(
117+
"Unknown function call: " + functionInvokeExpr
118+
)
119+
}
120+
case _ =>
121+
throw new RuntimeException("Unknown invoke expression: " + invokeExpr)
88122
}
89-
case _ =>
90123
}
91-
})
92124

93-
entryPoints.foreach(entryPoint => {
94-
if (entryPoint.body.isDefined) {
95-
addEntryPoint(OpalMethod(entryPoint))
96-
}
97-
})
125+
// Explicitly add static initializers (<clinit>) as they are called only implicitly
126+
callGraph
127+
.reachableMethods()
128+
.foreach(method => {
129+
method.method match {
130+
case definedMethod: DefinedMethod
131+
if definedMethod.definedMethod.isStaticInitializer =>
132+
if (definedMethod.definedMethod.body.isDefined) {
133+
addEntryPoint(OpalMethod(definedMethod.definedMethod))
134+
}
135+
case _ =>
136+
}
137+
})
138+
139+
entryPoints.foreach(entryPoint => {
140+
if (entryPoint.body.isDefined) {
141+
addEntryPoint(OpalMethod(entryPoint))
142+
}
143+
})
98144
}
Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
1+
/**
2+
* *****************************************************************************
3+
* Copyright (c) 2025 Fraunhofer IEM, Paderborn, Germany. This program and the
4+
* accompanying materials are made available under the terms of the Eclipse
5+
* Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0.
6+
*
7+
* <p>SPDX-License-Identifier: EPL-2.0
8+
*
9+
* <p>Contributors: Johannes Spaeth - initial API and implementation
10+
* *****************************************************************************
11+
*/
112
package boomerang.scope.opal
213

3-
import org.opalj.br.{ClassFile, ClassHierarchy, DefinedMethod, Field, Method, MethodDescriptor, ObjectType, ReferenceType}
4-
import org.opalj.br.analyses.{DeclaredMethods, DeclaredMethodsKey, Project}
14+
import org.opalj.br.ClassFile
15+
import org.opalj.br.ClassHierarchy
16+
import org.opalj.br.DefinedMethod
17+
import org.opalj.br.Field
18+
import org.opalj.br.Method
19+
import org.opalj.br.MethodDescriptor
20+
import org.opalj.br.ObjectType
21+
import org.opalj.br.ReferenceType
22+
import org.opalj.br.analyses.DeclaredMethods
23+
import org.opalj.br.analyses.DeclaredMethodsKey
24+
import org.opalj.br.analyses.Project
525

626
object OpalClient {
727

8-
var project: Option[Project[_]] = None
28+
var project: Option[Project[_]] = None
929

10-
def init(p: Project[_]): Unit = {
11-
project = Some(p)
12-
}
30+
def init(p: Project[_]): Unit = {
31+
project = Some(p)
32+
}
1333

14-
def getClassHierarchy: ClassHierarchy = project.get.classHierarchy
34+
def getClassHierarchy: ClassHierarchy = project.get.classHierarchy
1535

16-
def getClassFileForType(objectType: ObjectType): Option[ClassFile] = project.get.classFile(objectType)
36+
def getClassFileForType(objectType: ObjectType): Option[ClassFile] =
37+
project.get.classFile(objectType)
1738

18-
def isApplicationClass(classFile: ClassFile): Boolean = project.get.allProjectClassFiles.toSet.contains(classFile)
39+
def isApplicationClass(classFile: ClassFile): Boolean =
40+
project.get.allProjectClassFiles.toSet.contains(classFile)
1941

2042
}
Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
1+
/**
2+
* *****************************************************************************
3+
* Copyright (c) 2025 Fraunhofer IEM, Paderborn, Germany. This program and the
4+
* accompanying materials are made available under the terms of the Eclipse
5+
* Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0.
6+
*
7+
* <p>SPDX-License-Identifier: EPL-2.0
8+
*
9+
* <p>Contributors: Johannes Spaeth - initial API and implementation
10+
* *****************************************************************************
11+
*/
112
package boomerang.scope.opal
213

314
import boomerang.scope._
4-
import org.opalj.br.analyses.Project
5-
615
import java.util.stream
16+
import org.opalj.br.analyses.Project
717

8-
class OpalFrameworkScope(project: Project[_], callGraph: org.opalj.tac.cg.CallGraph, entryPoints: Set[org.opalj.br.Method], dataFlowScope: DataFlowScope) extends FrameworkScope {
18+
class OpalFrameworkScope(
19+
project: Project[_],
20+
callGraph: org.opalj.tac.cg.CallGraph,
21+
entryPoints: Set[org.opalj.br.Method],
22+
dataFlowScope: DataFlowScope
23+
) extends FrameworkScope {
924

10-
OpalClient.init(project)
11-
private val opalCallGraph = new OpalCallGraph(project, callGraph, entryPoints)
25+
OpalClient.init(project)
26+
private val opalCallGraph = new OpalCallGraph(project, callGraph, entryPoints)
1227

13-
override def getCallGraph: CallGraph = opalCallGraph
28+
override def getCallGraph: CallGraph = opalCallGraph
1429

15-
override def getDataFlowScope: DataFlowScope = dataFlowScope
30+
override def getDataFlowScope: DataFlowScope = dataFlowScope
1631

17-
override def getTrueValue(m: Method): Val = ???
32+
override def getTrueValue(m: Method): Val = ???
1833

19-
override def getFalseValue(m: Method): Val = ???
34+
override def getFalseValue(m: Method): Val = ???
2035

21-
override def handleStaticFieldInitializers(fact: Val): stream.Stream[Method] = ???
36+
override def handleStaticFieldInitializers(fact: Val): stream.Stream[Method] =
37+
???
2238

23-
override def newStaticFieldVal(field: Field, m: Method): StaticFieldVal = ???
39+
override def newStaticFieldVal(field: Field, m: Method): StaticFieldVal = ???
2440
}
2541

2642
object OpalFrameworkScope {
27-
final val STATIC_INITIALIZER: String = "<clinit>"
28-
final val CONSTRUCTOR: String = "<init>"
43+
final val STATIC_INITIALIZER: String = "<clinit>"
44+
final val CONSTRUCTOR: String = "<init>"
2945
}

0 commit comments

Comments
 (0)