Skip to content

Commit

Permalink
replace generics in JIdentityStmt, JAssignStmt with covariant return …
Browse files Browse the repository at this point in the history
…types and moving fields to subclasses to remove necessity of internal casts
  • Loading branch information
swissiety committed Sep 15, 2023
1 parent a280859 commit 38b9734
Show file tree
Hide file tree
Showing 26 changed files with 166 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ protected Stream<MethodSignature> resolveAllStaticInitializerCallsFromSourceMeth

// constructor calls
if (stmt instanceof JAssignStmt) {
Value rightOp = ((JAssignStmt<?, ?>) stmt).getRightOp();
Value rightOp = ((JAssignStmt) stmt).getRightOp();
instantiateVisitor.init();
rightOp.accept(instantiateVisitor);
ClassType classType = instantiateVisitor.getResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected void collectInstantiatedClassesInMethod(SootMethod method) {
Set<ClassType> instantiated =
method.getBody().getStmts().stream()
.filter(stmt -> stmt instanceof JAssignStmt)
.map(stmt -> ((JAssignStmt<?, ?>) stmt).getRightOp())
.map(stmt -> ((JAssignStmt) stmt).getRightOp())
.filter(value -> value instanceof JNewExpr)
.map(value -> ((JNewExpr) value).getType())
.collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ public void initializeWith(
final boolean isRemoved = currentTrapMap.remove(exceptionType, trap);
final PriorityQueue<Trap> overridenTrapHandlers = overlappingTraps.get(exceptionType);
if (overridenTrapHandlers != null) {
if (!isRemoved && overridenTrapHandlers.size() > 0) {
if (!isRemoved && !overridenTrapHandlers.isEmpty()) {
// check if theres an overlapping trap that has a less specific TrapRange which is
// ending before it gets the active exception information again
// not logical as a compiler output... but possible.
overridenTrapHandlers.remove(trap);
}

if (overridenTrapHandlers.size() > 0) {
if (!overridenTrapHandlers.isEmpty()) {
currentTrapMap.put(exceptionType, overridenTrapHandlers.poll());
}
}
Expand All @@ -178,7 +178,7 @@ public void initializeWith(
overlappingTraps.computeIfAbsent(
trap.getExceptionType(),
k ->
new PriorityQueue<Trap>(
new PriorityQueue<>(
(trapA, trapB) -> {
if (trapA.getEndStmt() == trapB.getEndStmt()) {
final Integer startIdxA = trapstmtToIdx.get(trapA.getBeginStmt());
Expand Down Expand Up @@ -422,7 +422,7 @@ private MutableBasicBlock addBlockInternal(
final Iterator<Stmt> iterator = stmts.iterator();
final Stmt node = iterator.next();
MutableBasicBlock block = getOrCreateBlock(node);
if (block.getHead() != node || block.getSuccessors().size() > 0) {
if (block.getHead() != node || !block.getSuccessors().isEmpty()) {
throw new IllegalArgumentException(
"The first Stmt in the List is already in the StmtGraph and and is not the head of a Block where currently no successor are set, yet.");
} else if (block.getStmtCount() > 1) {
Expand Down Expand Up @@ -662,10 +662,7 @@ protected boolean isMergeable(
return false;
}
// check if the same traps are applied to both blocks
if (!firstBlock.getExceptionalSuccessors().equals(followingBlock.getExceptionalSuccessors())) {
return false;
}
return true;
return firstBlock.getExceptionalSuccessors().equals(followingBlock.getExceptionalSuccessors());
}

/** trys to merge the second block into the first one if possible */
Expand Down Expand Up @@ -998,7 +995,7 @@ protected void removeBlockBorderEdgesInternal(
// TODO: reuse tryMerge*Block?

// add BlockB to BlockA if blockA has no branchingstmt as tail && same traps
if (blockOfFrom.getStmts().size() > 0 && from == blockOfFrom.getTail()) {
if (!blockOfFrom.getStmts().isEmpty() && from == blockOfFrom.getTail()) {
if (blockOfFrom.getPredecessors().size() == 1) {
MutableBasicBlock singlePreviousBlock = blockOfFrom.getPredecessors().get(0);
if (!singlePreviousBlock.getTail().branches() && singlePreviousBlock != blockOfFrom) {
Expand All @@ -1018,7 +1015,7 @@ protected void removeBlockBorderEdgesInternal(

// remove outgoing connections from blockA if from stmt is the tail
if (!from.branches()) {
if (blockOfFrom.getStmts().size() > 0 && blockOfFrom.getSuccessors().size() == 1) {
if (!blockOfFrom.getStmts().isEmpty() && blockOfFrom.getSuccessors().size() == 1) {
// merge previous block if possible i.e. no branchingstmt as tail && same traps && no
// other predesccorblocks
MutableBasicBlock singleSuccessorBlock = blockOfFrom.getSuccessors().get(0);
Expand Down Expand Up @@ -1156,7 +1153,7 @@ public List<Stmt> exceptionalPredecessors(@Nonnull MutableBasicBlock block) {

Stmt head = block.getHead();
if (!(head instanceof JIdentityStmt
&& ((JIdentityStmt<?>) head).getRightOp() instanceof JCaughtExceptionRef)) {
&& ((JIdentityStmt) head).getRightOp() instanceof JCaughtExceptionRef)) {
// only an exception handler stmt can have exceptional predecessors
return Collections.emptyList();
}
Expand All @@ -1174,7 +1171,7 @@ public List<MutableBasicBlock> exceptionalPredecessorBlocks(@Nonnull MutableBasi

Stmt head = block.getHead();
if (!(head instanceof JIdentityStmt
&& ((JIdentityStmt<?>) head).getRightOp() instanceof JCaughtExceptionRef)) {
&& ((JIdentityStmt) head).getRightOp() instanceof JCaughtExceptionRef)) {
// only an exception handler stmt can have exceptional predecessors
return Collections.emptyList();
}
Expand Down
9 changes: 4 additions & 5 deletions sootup.core/src/main/java/sootup/core/jimple/Jimple.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,15 +519,14 @@ public static JIfStmt newIfStmt(AbstractConditionExpr condition, StmtPositionInf
}

/** Constructs a IdentityStmt(Local, IdentityRef) grammar chunk. */
public static <L extends IdentityRef> JIdentityStmt<L> newIdentityStmt(
public static <L extends IdentityRef> JIdentityStmt newIdentityStmt(
Local local, L identityRef, StmtPositionInfo posInfo) {
return new JIdentityStmt<>(local, identityRef, posInfo);
return new JIdentityStmt(local, identityRef, posInfo);
}

/** Constructs a AssignStmt(Variable, RValue) grammar chunk. */
public static <L extends Value, R extends Value> JAssignStmt<L, R> newAssignStmt(
L variable, R rvalue, StmtPositionInfo posInfo) {
return new JAssignStmt<>(variable, rvalue, posInfo);
public static JAssignStmt newAssignStmt(Value variable, Value rvalue, StmtPositionInfo posInfo) {
return new JAssignStmt(variable, rvalue, posInfo);
}

/** Constructs a InvokeStmt(InvokeExpr) grammar chunk. */
Expand Down
8 changes: 4 additions & 4 deletions sootup.core/src/main/java/sootup/core/jimple/basic/Local.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ public Position getPosition() {
}

/** returns the returned List can contain: Locals, JFieldRefs, JArrayRefs */
public List<AbstractDefinitionStmt<?, Value>> getDefsOfLocal(List<Stmt> defs) {
List<AbstractDefinitionStmt<?, Value>> localDefs = new ArrayList<>();
public List<AbstractDefinitionStmt> getDefsOfLocal(List<Stmt> defs) {
List<AbstractDefinitionStmt> localDefs = new ArrayList<>();
for (Stmt stmt : defs) {
if (stmt instanceof AbstractDefinitionStmt
&& ((AbstractDefinitionStmt<?, Value>) stmt).getLeftOp().equals(this)) {
localDefs.add((AbstractDefinitionStmt<?, Value>) stmt);
&& ((AbstractDefinitionStmt) stmt).getLeftOp().equals(this)) {
localDefs.add((AbstractDefinitionStmt) stmt);
}
}
return localDefs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,17 @@
import sootup.core.jimple.basic.Value;
import sootup.core.types.Type;

public abstract class AbstractDefinitionStmt<L extends Value, R extends Value> extends Stmt {
public abstract class AbstractDefinitionStmt extends Stmt {

@Nonnull private final L leftOp;
@Nonnull private final R rightOp;

AbstractDefinitionStmt(
@Nonnull L leftOp, @Nonnull R rightOp, @Nonnull StmtPositionInfo positionInfo) {
AbstractDefinitionStmt(@Nonnull StmtPositionInfo positionInfo) {
super(positionInfo);
this.leftOp = leftOp;
this.rightOp = rightOp;
}

@Nonnull
public final L getLeftOp() {
return leftOp;
}
public abstract Value getLeftOp();

@Nonnull
public R getRightOp() {
return rightOp;
}
public abstract Value getRightOp();

@Nonnull
public Type getType() {
Expand All @@ -61,14 +51,15 @@ public Type getType() {
@Nonnull
public List<Value> getDefs() {
final List<Value> defs = new ArrayList<>();
defs.add(leftOp);
defs.add(getLeftOp());
return defs;
}

@Override
@Nonnull
public final List<Value> getUses() {
final List<Value> defsuses = leftOp.getUses();
final List<Value> defsuses = getLeftOp().getUses();
final Value rightOp = getRightOp();
final List<Value> uses = rightOp.getUses();
List<Value> list = new ArrayList<>(defsuses.size() + uses.size() + 1);
list.addAll(defsuses);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sootup.core.jimple.common.stmt;

/*-
* #%L
* #%Value
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997-2020 Etienne Gagnon, Linghui Luo, Markus Schmidt and others
Expand All @@ -19,7 +19,7 @@
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
* #Value%
*/

import javax.annotation.Nonnull;
Expand All @@ -35,8 +35,10 @@
import sootup.core.util.printer.StmtPrinter;

/** Represents the assignment of one value to another */
public final class JAssignStmt<L extends Value, R extends Value>
extends AbstractDefinitionStmt<L, R> implements Copyable {
public final class JAssignStmt extends AbstractDefinitionStmt implements Copyable {

@Nonnull final Value leftOp;
@Nonnull final Value rightOp;

/**
* Instantiates a new JAssignStmt.
Expand All @@ -45,8 +47,11 @@ public final class JAssignStmt<L extends Value, R extends Value>
* @param rValue the value on the right side of the assign statement.
*/
public JAssignStmt(
@Nonnull L variable, @Nonnull R rValue, @Nonnull StmtPositionInfo positionInfo) {
super(variable, rValue, positionInfo);
@Nonnull Value variable, @Nonnull Value rValue, @Nonnull StmtPositionInfo positionInfo) {
super(positionInfo);
leftOp = variable;
rightOp = rValue;

if (!validateVariable(variable)) {
throw new RuntimeException(
"Illegal Assignment statement. Make sure that left hand side has a valid operand.");
Expand Down Expand Up @@ -198,18 +203,30 @@ public int equivHashCode() {
}

@Nonnull
public <N extends Value> JAssignStmt<N, R> withVariable(@Nonnull N variable) {
return new JAssignStmt<>(variable, getRightOp(), getPositionInfo());
public JAssignStmt withVariable(@Nonnull Value variable) {
return new JAssignStmt(variable, getRightOp(), getPositionInfo());
}

@Nonnull
public JAssignStmt withRValue(@Nonnull Value rValue) {
return new JAssignStmt(getLeftOp(), rValue, getPositionInfo());
}

@Nonnull
public <N extends Value> JAssignStmt<L, N> withRValue(@Nonnull N rValue) {
return new JAssignStmt<>(getLeftOp(), rValue, getPositionInfo());
public JAssignStmt withPositionInfo(@Nonnull StmtPositionInfo positionInfo) {
return new JAssignStmt(getLeftOp(), getRightOp(), positionInfo);
}

@Nonnull
public JAssignStmt<L, R> withPositionInfo(@Nonnull StmtPositionInfo positionInfo) {
return new JAssignStmt<>(getLeftOp(), getRightOp(), positionInfo);
@Override
public Value getLeftOp() {
return leftOp;
}

@Nonnull
@Override
public Value getRightOp() {
return rightOp;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,36 @@
import sootup.core.util.Copyable;
import sootup.core.util.printer.StmtPrinter;

public final class JIdentityStmt<T extends IdentityRef> extends AbstractDefinitionStmt<Local, T>
implements Copyable {
public final class JIdentityStmt extends AbstractDefinitionStmt implements Copyable {

@Nonnull final Local leftOp;
@Nonnull final IdentityRef rightOp;

public JIdentityStmt(
@Nonnull Local local, @Nonnull T identityValue, @Nonnull StmtPositionInfo positionInfo) {
super(local, identityValue, positionInfo);
@Nonnull Local local,
@Nonnull IdentityRef identityValue,
@Nonnull StmtPositionInfo positionInfo) {
super(positionInfo);
leftOp = local;
rightOp = identityValue;
}

@Override
public String toString() {
return getLeftOp() + " := " + getRightOp();
}

@Nonnull
public Local getLeftOp() {
return leftOp;
}

@Nonnull
@Override
public IdentityRef getRightOp() {
return rightOp;
}

@Override
public void toString(@Nonnull StmtPrinter up) {
getLeftOp().toString(up);
Expand All @@ -67,18 +84,18 @@ public int equivHashCode() {
}

@Nonnull
public JIdentityStmt<T> withLocal(@Nonnull Local local) {
return new JIdentityStmt<>(local, getRightOp(), getPositionInfo());
public JIdentityStmt withLocal(@Nonnull Local local) {
return new JIdentityStmt(local, getRightOp(), getPositionInfo());
}

@Nonnull
public <N extends IdentityRef> JIdentityStmt<N> withIdentityValue(@Nonnull N identityValue) {
return new JIdentityStmt<>(getLeftOp(), identityValue, getPositionInfo());
public JIdentityStmt withIdentityValue(@Nonnull IdentityRef identityValue) {
return new JIdentityStmt(getLeftOp(), identityValue, getPositionInfo());
}

@Nonnull
public JIdentityStmt<T> withPositionInfo(@Nonnull StmtPositionInfo positionInfo) {
return new JIdentityStmt<>(getLeftOp(), getRightOp(), positionInfo);
public JIdentityStmt withPositionInfo(@Nonnull StmtPositionInfo positionInfo) {
return new JIdentityStmt(getLeftOp(), getRightOp(), positionInfo);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public void caseInvokeStmt(@Nonnull JInvokeStmt stmt) {
}

@Override
public void caseAssignStmt(@Nonnull JAssignStmt<?, ?> stmt) {
public void caseAssignStmt(@Nonnull JAssignStmt stmt) {
defaultCaseStmt(stmt);
}

@Override
public void caseIdentityStmt(@Nonnull JIdentityStmt<?> stmt) {
public void caseIdentityStmt(@Nonnull JIdentityStmt stmt) {
defaultCaseStmt(stmt);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void caseInvokeStmt(@Nonnull JInvokeStmt stmt) {
}

@Override
public void caseAssignStmt(@Nonnull JAssignStmt<?, ?> stmt) {
public void caseAssignStmt(@Nonnull JAssignStmt stmt) {

// uses on the def side..
final Value leftOp = stmt.getLeftOp();
Expand Down Expand Up @@ -109,7 +109,7 @@ public void caseAssignStmt(@Nonnull JAssignStmt<?, ?> stmt) {
}

@Override
public void caseIdentityStmt(@Nonnull JIdentityStmt<?> stmt) {
public void caseIdentityStmt(@Nonnull JIdentityStmt stmt) {
defaultCaseStmt(stmt);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public interface StmtVisitor extends Visitor {

void caseInvokeStmt(JInvokeStmt stmt);

void caseAssignStmt(JAssignStmt<?, ?> stmt);
void caseAssignStmt(JAssignStmt stmt);

void caseIdentityStmt(JIdentityStmt<?> stmt);
void caseIdentityStmt(JIdentityStmt stmt);

void caseEnterMonitorStmt(JEnterMonitorStmt stmt);

Expand Down
Loading

0 comments on commit 38b9734

Please sign in to comment.