Skip to content

Commit 12603e7

Browse files
committed
Simplified a lot of uneeded Optionals and extra arguments
1 parent 9f4bbdc commit 12603e7

File tree

4 files changed

+83
-108
lines changed

4 files changed

+83
-108
lines changed

src/main/java/gr/gousiosg/javacg/stat/JCallGraph.java

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ public static void main(String[] args) {
103103
String propertyName = s.first.substring(s.first.lastIndexOf("/") + 1, s.first.length() - 4);
104104
LOGGER.info(propertyName);
105105
rt.testProperty(propertyName);
106-
JacocoCoverage jacocoCoverage = new JacocoCoverage(Optional.of(s.first));
106+
JacocoCoverage jacocoCoverage = new JacocoCoverage(s.first);
107107
// 3. Prune the graph with coverage
108-
Pruning.pruneOriginalGraph(callgraph, jacocoCoverage, arguments);
108+
Pruning.pruneOriginalGraph(callgraph, jacocoCoverage);
109109
// 4. Operate on the graph and write it to output
110-
maybeWriteGraph(callgraph.graph, Optional.of(propertyName));
111-
maybeInspectReachability(callgraph, arguments, jacocoCoverage, Optional.of(s.second), Optional.of(propertyName));
110+
maybeWriteGraph(callgraph.graph, JCallGraph.OUTPUT_DIRECTORY + propertyName);
111+
maybeInspectReachability(callgraph, arguments.maybeDepth(), jacocoCoverage, s.second, JCallGraph.OUTPUT_DIRECTORY + propertyName);
112112
maybeInspectAncestry(callgraph, arguments, jacocoCoverage, Optional.of(s.second), Optional.of(propertyName));
113113
rt.cleanTarget();
114114
}
@@ -144,56 +144,38 @@ public static void main(String[] args) {
144144
LOGGER.info("java-cg is finished! Enjoy!");
145145
}
146146

147-
private static void maybeWriteGraph(Graph<String, DefaultEdge> graph, Optional<String> output) {
148-
if(output.isPresent()) {
149-
Utilities.writeGraph(
150-
graph, Utilities.defaultExporter(), output.map(JCallGraph::asDot));
151-
}
147+
private static void maybeWriteGraph(Graph<String, DefaultEdge> graph, String output) {
148+
Utilities.writeGraph(graph, Utilities.defaultExporter(), JCallGraph.asDot(output));
152149
}
153150

154151
private static void maybeInspectReachability(
155-
StaticCallgraph callgraph, TestArguments arguments, JacocoCoverage jacocoCoverage, Optional<String> entryPoint, Optional<String> outputFile) {
156-
if (entryPoint.isEmpty()) {
157-
return;
158-
}
152+
StaticCallgraph callgraph, Optional<Integer> depth, JacocoCoverage jacocoCoverage, String entryPoint, String outputFile) {
159153

160154
/* Fetch reachability */
161155
Graph<ColoredNode, DefaultEdge> reachability =
162156
Reachability.compute(
163-
callgraph.graph, entryPoint.get(), arguments.maybeDepth());
157+
callgraph.graph, entryPoint, depth);
164158

165159
/* Apply coverage */
166160
jacocoCoverage.applyCoverage(reachability, callgraph.metadata);
167161

168-
Pruning.pruneReachabilityGraph(reachability, callgraph.metadata, jacocoCoverage, arguments);
162+
Pruning.pruneReachabilityGraph(reachability, callgraph.metadata, jacocoCoverage);
169163

170164
/* Should we write the graph to a file? */
171-
Optional<String> outputName =
172-
outputFile.isPresent()
173-
? Optional.of(outputFile.get() + DELIMITER + REACHABILITY)
174-
: Optional.empty();
165+
String outputName = outputFile + DELIMITER + REACHABILITY;
175166

176167
/* Attach depth to name if present */
177-
outputName =
178-
outputName.map(
179-
name -> {
180-
if (arguments.maybeDepth().isPresent()) {
181-
return name + DELIMITER + arguments.maybeDepth().get();
182-
} else {
183-
return name;
184-
}
185-
});
168+
if (depth.isPresent()) {
169+
outputName = outputName + DELIMITER + depth.get();
170+
}
186171

187172
/* Store reachability in file? */
188-
if (outputName.isPresent()) {
189-
Utilities.writeGraph(
190-
reachability, Utilities.coloredExporter(), outputName.map(JCallGraph::asDot));
191-
}
173+
Utilities.writeGraph(
174+
reachability, Utilities.coloredExporter(), JCallGraph.asDot(outputName));
192175

193176
/* Analyze reachability coverage? */
194177
if (jacocoCoverage.hasCoverage()) {
195-
CoverageStatistics.analyze(
196-
reachability, outputName.map(name -> asCsv(name + DELIMITER + COVERAGE)));
178+
CoverageStatistics.analyze( reachability, Optional.of(asCsv(outputName + DELIMITER + COVERAGE)));
197179
}
198180
}
199181

@@ -217,7 +199,7 @@ private static void maybeInspectAncestry(
217199
+ DELIMITER
218200
+ arguments.maybeAncestry().get();
219201
Utilities.writeGraph(
220-
ancestry, Utilities.coloredExporter(), Optional.of(asDot(subgraphOutputName)));
202+
ancestry, Utilities.coloredExporter(), JCallGraph.OUTPUT_DIRECTORY + asDot(subgraphOutputName));
221203
}
222204
}
223205

@@ -248,15 +230,16 @@ private static void maybeSerializeStaticCallGraph(StaticCallgraph callgraph, Bui
248230
// Throws: IOException when file cannot be read
249231
// Throws: ClassNotFoundException when object cannot be read properly
250232
private static StaticCallgraph deserializeStaticCallGraph(TestArguments arguments) throws IOException, ClassNotFoundException{
251-
File filename = new File(arguments.maybeBytecodeFile().get());
252-
FileInputStream file = new FileInputStream(filename);
253-
ObjectInputStream ois = new ObjectInputStream(file);
254-
StaticCallgraph scg = (StaticCallgraph) ois.readObject();
255-
ois.close();
256-
file.close();
257-
return scg;
233+
return deserializeStaticCallGraph(new File(arguments.maybeBytecodeFile().get()));
234+
}
235+
236+
private static StaticCallgraph deserializeStaticCallGraph(File f) throws IOException, ClassNotFoundException{
237+
try (ObjectInput ois = new ObjectInputStream(new FileInputStream(f))) {
238+
return (StaticCallgraph) ois.readObject();
239+
}
258240
}
259241

242+
260243
private static RepoTool maybeObtainTool(GitArguments arguments) throws FileNotFoundException{
261244
Optional<RepoTool> rt = RepoTool.obtainTool(arguments.maybeGetConfig().get());
262245
if(rt.isPresent())

src/main/java/gr/gousiosg/javacg/stat/coverage/JacocoCoverage.java

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,64 +31,62 @@ public class JacocoCoverage {
3131
/**
3232
* Create a {@link JacocoCoverage} object
3333
*
34-
* @param maybeFilepath the JaCoCo coverage XML file to parse
34+
* @param path the JaCoCo coverage XML file to parse
3535
*/
36-
public JacocoCoverage(Optional<String> maybeFilepath)
36+
public JacocoCoverage(String path)
3737
throws IOException, ParserConfigurationException, JAXBException, SAXException {
3838

39-
if (maybeFilepath.isPresent()) {
40-
/* Convert the jacoco.xml file into a Report object */
41-
Report report = JacocoCoverageParser.getReport(maybeFilepath.get());
42-
43-
/* Iterate over all packages in report */
44-
for (Report.Package pkg : report.getPackage()) {
45-
46-
/* Iterate over all classes in a package */
47-
pkg.getClazz()
48-
.forEach(
49-
clazz -> {
50-
/* Find all methods in a class */
51-
List<Report.Package.Class.Method> methods =
52-
clazz.getContent().stream()
53-
.filter(s -> s instanceof JAXBElement)
54-
.map(s -> (JAXBElement<?>) s)
55-
.filter(je -> je.getValue() instanceof Report.Package.Class.Method)
56-
.map(je -> (Report.Package.Class.Method) je.getValue())
57-
.collect(Collectors.toList());
58-
59-
/* Store "covered" methods in methodCoverage */
60-
methods.forEach(
61-
method -> {
62-
String qualifiedName =
63-
MethodSignatureUtil.fullyQualifiedMethodSignature(
64-
clazz.getName(), method.getName(), method.getDesc());
65-
methodCoverage.putIfAbsent(qualifiedName, method);
66-
});
67-
});
68-
69-
/* Iterate over all source files in a package */
70-
pkg.getSourcefile()
71-
.forEach(
72-
rawSrcFile ->
73-
rawSrcFile.getContent().stream()
39+
/* Convert the jacoco.xml file into a Report object */
40+
Report report = JacocoCoverageParser.getReport(path);
41+
42+
/* Iterate over all packages in report */
43+
for (Report.Package pkg : report.getPackage()) {
44+
45+
/* Iterate over all classes in a package */
46+
pkg.getClazz()
47+
.forEach(
48+
clazz -> {
49+
/* Find all methods in a class */
50+
List<Report.Package.Class.Method> methods =
51+
clazz.getContent().stream()
7452
.filter(s -> s instanceof JAXBElement)
7553
.map(s -> (JAXBElement<?>) s)
76-
.filter(je -> je.getValue() instanceof Report.Package.Sourcefile.Line)
77-
.map(je -> (Report.Package.Sourcefile.Line) je.getValue())
78-
.filter(
79-
line ->
80-
Byte.toUnsignedInt(line.cb) > 0 || Byte.toUnsignedInt(line.ci) > 0)
81-
.forEach(
82-
line ->
83-
coveredLines.add(
84-
String.format(
85-
"%s:%d",
86-
rawSrcFile.getName(), Byte.toUnsignedInt(line.nr)))));
87-
}
88-
89-
/* Indicate that coverage has been applied */
90-
hasCoverage = true;
54+
.filter(je -> je.getValue() instanceof Report.Package.Class.Method)
55+
.map(je -> (Report.Package.Class.Method) je.getValue())
56+
.collect(Collectors.toList());
57+
58+
/* Store "covered" methods in methodCoverage */
59+
methods.forEach(
60+
method -> {
61+
String qualifiedName =
62+
MethodSignatureUtil.fullyQualifiedMethodSignature(
63+
clazz.getName(), method.getName(), method.getDesc());
64+
methodCoverage.putIfAbsent(qualifiedName, method);
65+
});
66+
});
67+
68+
/* Iterate over all source files in a package */
69+
pkg.getSourcefile()
70+
.forEach(
71+
rawSrcFile ->
72+
rawSrcFile.getContent().stream()
73+
.filter(s -> s instanceof JAXBElement)
74+
.map(s -> (JAXBElement<?>) s)
75+
.filter(je -> je.getValue() instanceof Report.Package.Sourcefile.Line)
76+
.map(je -> (Report.Package.Sourcefile.Line) je.getValue())
77+
.filter(
78+
line ->
79+
Byte.toUnsignedInt(line.cb) > 0 || Byte.toUnsignedInt(line.ci) > 0)
80+
.forEach(
81+
line ->
82+
coveredLines.add(
83+
String.format(
84+
"%s:%d",
85+
rawSrcFile.getName(), Byte.toUnsignedInt(line.nr)))));
9186
}
87+
88+
/* Indicate that coverage has been applied */
89+
hasCoverage = true;
9290
}
9391

9492
public void applyCoverage(Graph<ColoredNode, DefaultEdge> graph, JarMetadata metadata) {

src/main/java/gr/gousiosg/javacg/stat/graph/Pruning.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ public class Pruning {
2626
* @param callgraph the graph
2727
* @param coverage the coverage
2828
*/
29-
public static void pruneOriginalGraph(StaticCallgraph callgraph, JacocoCoverage coverage, TestArguments testArguments) {
29+
public static void pruneOriginalGraph(StaticCallgraph callgraph, JacocoCoverage coverage) {
3030
markConcreteBridgeTargets(callgraph.graph, callgraph.metadata);
3131
pruneBridgeMethods(callgraph.graph, callgraph.metadata);
3232
pruneConcreteMethods(callgraph.graph, callgraph.metadata, coverage);
33-
pruneMethodsFromTests(callgraph.graph, callgraph.metadata, testArguments, coverage);
33+
pruneMethodsFromTests(callgraph.graph, callgraph.metadata, coverage);
3434
}
3535

36-
public static void pruneReachabilityGraph(Graph<ColoredNode, DefaultEdge> reachability, JarMetadata metadata, JacocoCoverage coverage, TestArguments testArguments) {
37-
pruneMethodsFromTestsThatAreReachable(reachability, metadata, testArguments, coverage);
36+
public static void pruneReachabilityGraph(Graph<ColoredNode, DefaultEdge> reachability, JarMetadata metadata, JacocoCoverage coverage) {
37+
pruneMethodsFromTestsThatAreReachable(reachability, metadata, coverage);
3838
}
3939

4040
/**
@@ -129,7 +129,7 @@ private static void markConcreteBridgeTargets(
129129
* @param graph the graph
130130
* @param metadata the metadata of the graph
131131
*/
132-
private static void pruneMethodsFromTests(Graph<String, DefaultEdge> graph, JarMetadata metadata, TestArguments testArguments, JacocoCoverage coverage) {
132+
private static void pruneMethodsFromTests(Graph<String, DefaultEdge> graph, JarMetadata metadata, JacocoCoverage coverage) {
133133
var testTargetNodes = metadata.testMethods.stream()
134134
.filter(graph::containsVertex)
135135
.map(graph::outgoingEdgesOf)
@@ -168,7 +168,7 @@ private static void pruneMethodsFromTests(Graph<String, DefaultEdge> graph, JarM
168168
*
169169
* @param metadata the metadata of the graph
170170
*/
171-
private static void pruneMethodsFromTestsThatAreReachable(Graph<ColoredNode, DefaultEdge> graph, JarMetadata metadata, TestArguments testArguments, JacocoCoverage coverage) {
171+
private static void pruneMethodsFromTestsThatAreReachable(Graph<ColoredNode, DefaultEdge> graph, JarMetadata metadata, JacocoCoverage coverage) {
172172
Map<String, ColoredNode> nodeMap = nodeMap(graph.vertexSet());
173173

174174
var testTargetNodes = metadata.testMethods.stream()

src/main/java/gr/gousiosg/javacg/stat/graph/Utilities.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,16 @@ public static Map<String, ColoredNode> nodeMap(Set<ColoredNode> nodes) {
3535
*
3636
* @param graph the graph
3737
* @param exporter the exporter that will write the graph to a file
38-
* @param maybeOutputName the name to use
38+
* @param path the file to use
3939
* @param <T> the type of the elements in the graph
4040
*/
4141
public static <T> void writeGraph(
4242
Graph<T, DefaultEdge> graph,
4343
DOTExporter<T, DefaultEdge> exporter,
44-
Optional<String> maybeOutputName) {
44+
String path) {
4545
LOGGER.info("Attempting to store callgraph...");
4646

47-
if (maybeOutputName.isEmpty()) {
48-
LOGGER.error("No output name specified!");
49-
return;
50-
}
51-
5247
/* Write to .dot file in output directory */
53-
String path = JCallGraph.OUTPUT_DIRECTORY + maybeOutputName.get();
5448
try {
5549
Writer writer = new FileWriter(path);
5650
exporter.exportGraph(graph, writer);

0 commit comments

Comments
 (0)