Skip to content

Commit 7c436c8

Browse files
mrjameshamiltonOberon Swings
authored andcommitted
Remove redundant casts
1 parent bfe4631 commit 7c436c8

File tree

1 file changed

+47
-52
lines changed

1 file changed

+47
-52
lines changed

base/src/main/java/proguard/analysis/cpa/jvm/cfa/visitors/JvmIntraproceduralCfaFillerAllInstructionVisitor.java

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818

1919
package proguard.analysis.cpa.jvm.cfa.visitors;
2020

21-
import java.util.Arrays;
22-
import java.util.LinkedHashSet;
23-
import java.util.Optional;
24-
import java.util.Set;
25-
import java.util.stream.Collectors;
2621
import org.apache.logging.log4j.LogManager;
2722
import org.apache.logging.log4j.Logger;
23+
import proguard.analysis.cpa.jvm.cfa.JvmCfa;
24+
import proguard.analysis.cpa.jvm.cfa.edges.JvmAssumeCaseCfaEdge;
25+
import proguard.analysis.cpa.jvm.cfa.edges.JvmAssumeCfaEdge;
26+
import proguard.analysis.cpa.jvm.cfa.edges.JvmAssumeDefaultCfaEdge;
27+
import proguard.analysis.cpa.jvm.cfa.edges.JvmAssumeExceptionCfaEdge;
28+
import proguard.analysis.cpa.jvm.cfa.edges.JvmCfaEdge;
29+
import proguard.analysis.cpa.jvm.cfa.edges.JvmInstructionCfaEdge;
30+
import proguard.analysis.cpa.jvm.cfa.nodes.JvmCatchCfaNode;
31+
import proguard.analysis.cpa.jvm.cfa.nodes.JvmCfaNode;
32+
import proguard.analysis.cpa.jvm.cfa.nodes.JvmUnknownCfaNode;
2833
import proguard.classfile.Clazz;
2934
import proguard.classfile.Method;
3035
import proguard.classfile.MethodSignature;
@@ -41,17 +46,12 @@
4146
import proguard.classfile.instruction.TableSwitchInstruction;
4247
import proguard.classfile.instruction.VariableInstruction;
4348
import proguard.classfile.instruction.visitor.InstructionVisitor;
44-
import proguard.analysis.cpa.defaults.Cfa;
45-
import proguard.analysis.cpa.jvm.cfa.JvmCfa;
46-
import proguard.analysis.cpa.jvm.cfa.edges.JvmAssumeCaseCfaEdge;
47-
import proguard.analysis.cpa.jvm.cfa.edges.JvmAssumeCfaEdge;
48-
import proguard.analysis.cpa.jvm.cfa.edges.JvmAssumeDefaultCfaEdge;
49-
import proguard.analysis.cpa.jvm.cfa.edges.JvmAssumeExceptionCfaEdge;
50-
import proguard.analysis.cpa.jvm.cfa.edges.JvmCfaEdge;
51-
import proguard.analysis.cpa.jvm.cfa.edges.JvmInstructionCfaEdge;
52-
import proguard.analysis.cpa.jvm.cfa.nodes.JvmCatchCfaNode;
53-
import proguard.analysis.cpa.jvm.cfa.nodes.JvmCfaNode;
54-
import proguard.analysis.cpa.jvm.cfa.nodes.JvmUnknownCfaNode;
49+
50+
import java.util.Arrays;
51+
import java.util.LinkedHashSet;
52+
import java.util.Optional;
53+
import java.util.Set;
54+
import java.util.stream.Collectors;
5555

5656
/**
5757
* This {@link AttributeVisitor} visits the {@link CodeAttribute} of a {@link Method} and performs two different tasks:
@@ -72,30 +72,28 @@ public class JvmIntraproceduralCfaFillerAllInstructionVisitor
7272
implements AttributeVisitor
7373
{
7474

75-
private final Cfa<JvmCfaNode, JvmCfaEdge, MethodSignature> cfa;
76-
private static final Logger log = LogManager.getLogger(JvmIntraproceduralCfaFillerAllInstructionVisitor.class);
75+
private final JvmCfa cfa;
76+
private static final Logger log = LogManager.getLogger(JvmIntraproceduralCfaFillerAllInstructionVisitor.class);
7777

7878
public JvmIntraproceduralCfaFillerAllInstructionVisitor(JvmCfa cfa)
7979
{
8080
this.cfa = cfa;
8181
}
8282

83+
@Override
8384
public void visitAnyAttribute(Clazz clazz, Attribute attribute)
8485
{
8586
}
8687

88+
@Override
8789
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
8890
{
8991
MethodSignature signature = MethodSignature.computeIfAbsent(clazz, method);
9092

9193
generateCatchNodes(signature, codeAttribute, clazz);
9294

93-
JvmIntraproceduralCfaFillerVisitor instructionVisitor = new JvmIntraproceduralCfaFillerVisitor((JvmCfa) cfa);
94-
95-
instructionVisitor.setSignature(signature);
96-
9795
// visit all the instructions of the current method
98-
codeAttribute.instructionsAccept(clazz, method, instructionVisitor);
96+
codeAttribute.instructionsAccept(clazz, method, new JvmIntraproceduralCfaFillerVisitor(signature, cfa));
9997
}
10098

10199
private void generateCatchNodes(MethodSignature signature, CodeAttribute codeAttribute, Clazz clazz)
@@ -105,9 +103,10 @@ private void generateCatchNodes(MethodSignature signature, CodeAttribute codeAtt
105103
if (codeAttribute.exceptionTable != null)
106104
{
107105
// get the position of all handlers, use a LinkedHashSet to remove duplicates and keep order
108-
Set<Integer> handlersOffset = new LinkedHashSet<>(Arrays.stream(codeAttribute.exceptionTable)
109-
.map(x -> x.u2handlerPC)
110-
.collect(Collectors.toList()));
106+
Set<Integer> handlersOffset =
107+
Arrays.stream(codeAttribute.exceptionTable)
108+
.map(x -> x.u2handlerPC)
109+
.collect(Collectors.toCollection(LinkedHashSet::new));
111110

112111
// create the nodes for code locations at beginning of catch/finally statement
113112
for (int handlerOffset : handlersOffset)
@@ -119,11 +118,11 @@ private void generateCatchNodes(MethodSignature signature, CodeAttribute codeAtt
119118
.findFirst()
120119
.get().u2catchType,
121120
clazz);
122-
((JvmCfa) cfa).addFunctionCatchNode(signature, catchNode, handlerOffset);
121+
cfa.addFunctionCatchNode(signature, catchNode, handlerOffset);
123122

124123
// link the catch node to the starting node of the handler block with a true exception assumption
125124
new JvmAssumeExceptionCfaEdge(catchNode,
126-
((JvmCfa) cfa).addNodeIfAbsent(signature, handlerOffset, clazz),
125+
cfa.addNodeIfAbsent(signature, handlerOffset, clazz),
127126
true,
128127
catchNode.getCatchType());
129128
}
@@ -150,8 +149,8 @@ private void generateCatchNodes(MethodSignature signature, CodeAttribute codeAtt
150149
{
151150
// if there is a following catch block we link it with a false assume exception edge with the previous catch node type
152151
// i.e. the following catch block is considered only if the exception is not caught by the block before
153-
JvmCatchCfaNode currNode = ((JvmCfa) cfa).getFunctionCatchNode(signature, currHandlerOffset);
154-
JvmCatchCfaNode nextNode = ((JvmCfa) cfa).getFunctionCatchNode(signature, followingCatch.get().u2handlerPC);
152+
JvmCatchCfaNode currNode = cfa.getFunctionCatchNode(signature, currHandlerOffset);
153+
JvmCatchCfaNode nextNode = cfa.getFunctionCatchNode(signature, followingCatch.get().u2handlerPC);
155154

156155
new JvmAssumeExceptionCfaEdge(currNode,
157156
nextNode,
@@ -165,11 +164,11 @@ private void generateCatchNodes(MethodSignature signature, CodeAttribute codeAtt
165164

166165
// if the last node of a catch chain (i.e. linked only to the beginning of the corresponding catch block and not to other catch nodes)is not a `finally` (catch type == 0) which always throws
167166
// an exception in the end, add an edge to the exception exit block, since this is an intra-procedural implementation
168-
for (JvmCatchCfaNode node : ((JvmCfa) cfa).getFunctionCatchNodes(signature))
167+
for (JvmCatchCfaNode node : cfa.getFunctionCatchNodes(signature))
169168
{
170169
if (node.getLeavingEdges().size() == 1 && node.getCatchType() != 0)
171170
{
172-
JvmCfaNode exitNode = ((JvmCfa) cfa).getFunctionExceptionExitNode(signature, clazz);
171+
JvmCfaNode exitNode = cfa.getFunctionExceptionExitNode(signature, clazz);
173172

174173
// add edge for exception not caught
175174
new JvmAssumeExceptionCfaEdge(node, exitNode, false, node.getCatchType());
@@ -182,21 +181,22 @@ private void generateCatchNodes(MethodSignature signature, CodeAttribute codeAtt
182181
* plus eventual additional nodes (i.e. in case of a branch instruction a node for the code location of the jump is added).
183182
*
184183
* <p>The edge from the previously visited instruction is linked to the current instruction if the control flows from one to the other (e.g. the previous edge is linked if it represents an iadd
185-
* instruction but it's not if it represents a goto instruction).
184+
* instruction but not if it represents a goto instruction).
186185
*
187186
* @author Carlo Alberto Pozzoli
188187
*/
189-
private class JvmIntraproceduralCfaFillerVisitor
188+
private static class JvmIntraproceduralCfaFillerVisitor
190189
implements InstructionVisitor
191190
{
192191

193-
private final Cfa<JvmCfaNode, JvmCfaEdge, MethodSignature> cfa;
194-
private MethodSignature signature;
195-
private JvmCfaEdge previousEdge;
192+
private final JvmCfa cfa;
193+
private final MethodSignature signature;
194+
private JvmCfaEdge previousEdge;
196195

197-
public JvmIntraproceduralCfaFillerVisitor(JvmCfa cfa)
196+
public JvmIntraproceduralCfaFillerVisitor(MethodSignature methodSignature, JvmCfa cfa)
198197
{
199-
this.cfa = cfa;
198+
this.signature = methodSignature;
199+
this.cfa = cfa;
200200
}
201201

202202
@Override
@@ -275,11 +275,6 @@ public void visitAnySwitchInstruction(Clazz clazz, Method method, CodeAttribute
275275
}
276276
}
277277

278-
public void setSignature(MethodSignature signature)
279-
{
280-
this.signature = signature;
281-
}
282-
283278
/**
284279
* Performs the default operations needed for each instruction, must be called by all the other specific addNode methods:
285280
*
@@ -289,7 +284,7 @@ public void setSignature(MethodSignature signature)
289284
*/
290285
private JvmCfaNode connect(int offset, Clazz clazz)
291286
{
292-
JvmCfaNode currentNode = ((JvmCfa) cfa).addNodeIfAbsent(signature, offset, clazz);
287+
JvmCfaNode currentNode = cfa.addNodeIfAbsent(signature, offset, clazz);
293288

294289
if (previousEdge != null)
295290
{
@@ -322,7 +317,7 @@ private void connectReturn(int offset, Clazz clazz, CodeAttribute methodCode)
322317
// next instruction will not continue the control flow
323318
previousEdge = null;
324319

325-
JvmCfaNode exitNode = ((JvmCfa) cfa).getFunctionReturnExitNode(signature, clazz);
320+
JvmCfaNode exitNode = cfa.getFunctionReturnExitNode(signature, clazz);
326321

327322
// create and add edge to nodes
328323
new JvmInstructionCfaEdge(currentNode, exitNode, methodCode, offset);
@@ -356,7 +351,7 @@ private void connectGoto(int offset, Clazz clazz, CodeAttribute methodCode, Bran
356351
int branchTarget = offset + instruction.branchOffset;
357352

358353
// get the goto location node
359-
JvmCfaNode jumpNode = ((JvmCfa) cfa).addNodeIfAbsent(signature, branchTarget, clazz);
354+
JvmCfaNode jumpNode = cfa.addNodeIfAbsent(signature, branchTarget, clazz);
360355

361356
// create and add edge to nodes
362357
new JvmInstructionCfaEdge(currentNode, jumpNode, methodCode, offset);
@@ -376,7 +371,7 @@ private void connectBranch(int offset, Clazz clazz, CodeAttribute methodCode, Br
376371
// create the branch taken edge with the corresponding node
377372
int branchTarget = offset + instruction.branchOffset;
378373

379-
JvmCfaNode jumpNode = ((JvmCfa) cfa).addNodeIfAbsent(signature, branchTarget, clazz);
374+
JvmCfaNode jumpNode = cfa.addNodeIfAbsent(signature, branchTarget, clazz);
380375

381376
new JvmAssumeCfaEdge(currentNode, jumpNode, methodCode, offset, true);
382377
}
@@ -400,7 +395,7 @@ private void connectSwitch(int offset, Clazz clazz, CodeAttribute methodCode, Sw
400395
// add the default node/edge pair
401396
int branchTarget = offset + instruction.defaultOffset;
402397

403-
JvmCfaNode jumpNode = ((JvmCfa) cfa).addNodeIfAbsent(signature, branchTarget, clazz);
398+
JvmCfaNode jumpNode = cfa.addNodeIfAbsent(signature, branchTarget, clazz);
404399

405400
new JvmAssumeDefaultCfaEdge(currentNode, jumpNode, methodCode, offset);
406401

@@ -410,7 +405,7 @@ private void connectSwitch(int offset, Clazz clazz, CodeAttribute methodCode, Sw
410405
branchTarget = offset + instruction.jumpOffsets[index];
411406
int caseAssumption = instruction instanceof TableSwitchInstruction ? ((TableSwitchInstruction) instruction).lowCase + index : ((LookUpSwitchInstruction) instruction).cases[index];
412407

413-
jumpNode = ((JvmCfa) cfa).addNodeIfAbsent(signature, branchTarget, clazz);
408+
jumpNode = cfa.addNodeIfAbsent(signature, branchTarget, clazz);
414409

415410
new JvmAssumeCaseCfaEdge(currentNode, jumpNode, methodCode, offset, caseAssumption);
416411
}
@@ -443,12 +438,12 @@ private void connectThrow(int offset, Clazz clazz, CodeAttribute methodCode)
443438
// link edge to the first catch block
444439
if (firstCatch.isPresent())
445440
{
446-
new JvmInstructionCfaEdge(currentNode, ((JvmCfa) cfa).getFunctionCatchNode(signature, firstCatch.get().u2handlerPC), methodCode, offset);
441+
new JvmInstructionCfaEdge(currentNode, cfa.getFunctionCatchNode(signature, firstCatch.get().u2handlerPC), methodCode, offset);
447442
}
448443
// since this is an intra-procedural implementation if the thrown exception is not caught just add an edge to the exception exit node
449444
else
450445
{
451-
JvmCfaNode exitNode = ((JvmCfa) cfa).getFunctionExceptionExitNode(signature, clazz);
446+
JvmCfaNode exitNode = cfa.getFunctionExceptionExitNode(signature, clazz);
452447

453448
// add edge for the throw instruction
454449
new JvmInstructionCfaEdge(currentNode, exitNode, methodCode, offset);

0 commit comments

Comments
 (0)