Class JUtil
java.lang.Object
com.pnfsoftware.jeb.core.units.code.java.JUtil
Collection of utility methods to manipulate
IJavaElement.-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanThrow(IJavaStatement stm) Recursively determine whether a statement may throw.static IJavaLabelCheck that the statement is a simple "if(...) { goto x; }" and if so, returns "x"static IJavaElementfindParent(IJavaElement root, IJavaElement elt) Retrieve the parent of an AST element (or first parent, if the element is reusable and has potentially multiple parents).static Stringgenerate(IJavaElement elt) Generate an AST element as raw Java text.static IJavaStatementgetFirstRealStatement(IJavaBlock b, int i) This convenience method attempts to determine the next "real" statement to be executed in the block, starting at the provided index 'i'.static IJavaLabelRetrieve the target label of a goto statement.static List<IJavaStatement> getGotos(IJavaLabel targetLabel, List<IJavaStatement> flatlist) Retrieve goto statements targeting a label.static IJavaIdentifierRetrieve the identifier represented by an expression.static List<IJavaIdentifier> static IJavaExpressionRetrieve the lock expression from a monitor-enter statement.static IJavaExpressionRetrieve the lock expression from a monitor-exit statement.static IJavaConstantgetNegatedConstant(IJavaConstant cst, IJavaConstantFactory factory) Get the negated value of a constant, if possible.static StringRetrieve the string value of a string constant element.static IJavaIdentifierDetect: x, (int)xstatic booleanDetermine whether an element is a class, method, or field element.static booleanDetermine whether the statement defines an identifier.static booleanDetermine whether a statement interrupts regular control flow.static booleanisGotoTo(IJavaStatement stm, IJavaLabel target) Check that the statement is a "goto target;"static booleanisIdentOrDefinition(IJavaElement e, IJavaIdentifier ident) Determine whether the elements is the provided identifier or a simple definition of the identifier.static booleanisIf(IJavaStatement stm) Determine whether a statement is a simple if: if(...){...}.static booleanDetermine whether a statement is an if statement whose only body statement is a simple continue.static booleanisIfElse(IJavaStatement stm) Determine whether a statement is like: if(...){...}else{...} which is the if-statement with 2 blocks: the main case block and the default block.static IJavaLabelisIfGoto(IJavaStatement stm) Determine if the provided statement is likeif(COND){goto LABEL;}and if so, return the label.static booleanisIfNoElse(IJavaStatement stm) Determine whether a statement is like: if(...){ }[else if(...){}]*static booleanDetect: 1, x, (int)xstatic booleanisIntegerConstant(IJavaElement e, int expected) Determine whether an element is an integer constant with an expected value.static booleanDetermine whether an element is a monitor-exit statement.static booleanisMonitorExit(IJavaElement stm, IJavaExpression expectedLock) static booleanDetermine whether the provided statement is a return, throw, goto, break, or continue.static booleanRecursively determine if the statement is or contains a Label.static booleanDetermine whether a statement is an unlabeled break.static booleanDetermine whether a statement is an unlabeled continue.static booleanDetermine whether an element is a string constant.static booleanisThrow(IJavaStatement stm, IJavaExpression expectedThrown) Determine whether a statement throws a specific expression.static IJavaExpressionisThrowLike(IJavaStatement stm, List<IJavaStatement> flatlist) static IJavaCatchBlockisTryCatchall(IJavaBlock b, int i) Determine whether the provided statement is a try-catch-all (single catch block, that catches all exceptions)static IJavaCatchBlockisTryCatchall(IJavaTry trystm) static voidmoveStatements(IJavaBlock src, int srcbegin, int srcend, IJavaBlock dst, int dstindex) static intremoveStatementsDeep(IJavaBlock b, Predicate<IJavaStatement> checker) Remove statements matching a predicate from a block and nested compound blocks.static IJavaExpressionresolveLogicalNot(IJavaExpression exp, IJavaGlobalContext jctx)
-
Method Details
-
generate
Generate an AST element as raw Java text.- Parameters:
elt- AST element to generate- Returns:
- generated text
-
findParent
Retrieve the parent of an AST element (or first parent, if the element is reusable and has potentially multiple parents).- Parameters:
root- a root element, typically, anIJavaClasselt- the element for which the first parent is to be found- Returns:
- the parent or null
-
isClassMethodField
Determine whether an element is a class, method, or field element.- Parameters:
e- element to test- Returns:
- true if the element is a class, method, or field
-
isIntegerConstant
Determine whether an element is an integer constant with an expected value.- Parameters:
e- element to testexpected- expected integer value- Returns:
- true if the element is an integer constant with the expected value
-
isStringConstant
Determine whether an element is a string constant.- Parameters:
e- element to test- Returns:
- true if the element is a string constant
-
getStringConstant
Retrieve the string value of a string constant element.- Parameters:
e- element to inspect- Returns:
- string value, or null
-
checkIfGoto
Check that the statement is a simple "if(...) { goto x; }" and if so, returns "x"- Parameters:
stm- statement to check- Returns:
- the label or null
-
isGotoTo
Check that the statement is a "goto target;"- Parameters:
stm- the statementtarget- the target- Returns:
-
isIf
Determine whether a statement is a simple if: if(...){...}.- Parameters:
stm-- Returns:
-
isIfElse
Determine whether a statement is like: if(...){...}else{...} which is the if-statement with 2 blocks: the main case block and the default block.- Parameters:
stm-- Returns:
-
isIfNoElse
Determine whether a statement is like: if(...){ }[else if(...){}]*ie, the if-block can have multiple case-blocks but NO default 'else' block
- Parameters:
stm-- Returns:
-
isIfGoto
Determine if the provided statement is likeif(COND){goto LABEL;}and if so, return the label.- Parameters:
stm- statement to inspect- Returns:
- target label, or null
-
getFirstRealStatement
This convenience method attempts to determine the next "real" statement to be executed in the block, starting at the provided index 'i'.This method can only go "deeper" (forward). It cannot go "up", which would require keeping track of parents. Use a
PStmManagerto do that.Important: Label statements are not skipped over. This method skips over:
- the "do {" part of a do-while block
- the "while(true) {" part of a while block
- the "if(true) {" part of a conditional statement
- the "try {" part of a try-catch block
- Parameters:
b- current blocki- current statement index in the block- Returns:
- the statement, or null if none were found
-
isTryCatchall
Determine whether the provided statement is a try-catch-all (single catch block, that catches all exceptions)- Parameters:
b- blocki- statement index in the block- Returns:
- the catch-all block or null
-
isTryCatchall
- Parameters:
trystm- aIJavaTrystatement- Returns:
- true if the try-statement is a try-catch-all (single catch block, that catches all exceptions)
-
getVarLike
Detect: x, (int)x- Parameters:
e-- Returns:
- x or null
-
isImmOrVarLike
Detect: 1, x, (int)x- Parameters:
e-- Returns:
- true or false
-
getIdentifiers
- Parameters:
e-- Returns:
-
isIdentOrDefinition
Determine whether the elements is the provided identifier or a simple definition of the identifier.- Parameters:
e-ident-- Returns:
-
isDeclarationOrDefinition
Determine whether the statement defines an identifier.Currently, definitions should only be found in four types of statements:
- pure decl: "int x;"
- decl + init: "int x = 0;"
- in top-level Method elements (NOT CHECKED HERE)
- in compound Try elements (NOT CHECKED HERE)- Parameters:
stm-- Returns:
-
isThrowLike
- Parameters:
stm- a statement, that we want to be a THROW or a GOTO-THROWflatlist-- Returns:
- on success, the THROWN expression; else, null
-
isThrow
Determine whether a statement throws a specific expression.- Parameters:
stm- statement to testexpectedThrown- expected thrown expression- Returns:
- true if the statement throws the expected expression
-
getMonitorEnter
Retrieve the lock expression from a monitor-enter statement.- Parameters:
stm- statement to inspect- Returns:
- lock expression, or null
-
getMonitorExit
Retrieve the lock expression from a monitor-exit statement.- Parameters:
stm- statement to inspect- Returns:
- lock expression, or null
-
isMonitorExit
Determine whether an element is a monitor-exit statement.- Parameters:
stm- element to test- Returns:
- true if the element is a monitor-exit statement
-
isMonitorExit
- Parameters:
stm-expectedLock- optional- Returns:
-
getGotos
Retrieve goto statements targeting a label.- Parameters:
targetLabel- target labelflatlist- statement list to inspect- Returns:
- matching goto statements
-
getGotoLabel
Retrieve the target label of a goto statement.- Parameters:
stm- statement to inspect- Returns:
- target label, or null
-
getIdentifier
Retrieve the identifier represented by an expression.- Parameters:
e- expression to inspect- Returns:
- identifier, or null
-
removeStatementsDeep
Remove statements matching a predicate from a block and nested compound blocks.- Parameters:
b- block to modifychecker- statement predicate- Returns:
- number of removed statements
-
isOrContainsLabel
Recursively determine if the statement is or contains a Label. Can be called for any statement, including compound statements.- Parameters:
stm-- Returns:
-
canThrow
Recursively determine whether a statement may throw.- Parameters:
stm- statement to inspect- Returns:
- true if the statement may throw
-
getNegatedConstant
Get the negated value of a constant, if possible.- Parameters:
cst- a constantfactory- constant factory- Returns:
- null if the negative value cannot be represented (e.g., constant type no fitting) or is the same as the provided constant
-
isNonCompoundFlowBreaker
Determine whether the provided statement is a return, throw, goto, break, or continue.- Parameters:
stm-- Returns:
-
moveStatements
public static void moveStatements(IJavaBlock src, int srcbegin, int srcend, IJavaBlock dst, int dstindex) - Parameters:
src-srcbegin-srcend-dst-dstindex-
-
isSimpleBreak
Determine whether a statement is an unlabeled break.- Parameters:
stm- statement to inspect- Returns:
- true if the statement is an unlabeled break
-
isSimpleContinue
Determine whether a statement is an unlabeled continue.- Parameters:
stm- statement to inspect- Returns:
- true if the statement is an unlabeled continue
-
isFlowBreaker
Determine whether a statement interrupts regular control flow.- Parameters:
stm- statement to inspect- Returns:
- true if the statement is a flow breaker
-
isIfContinue
Determine whether a statement is an if statement whose only body statement is a simple continue.- Parameters:
stm- statement to inspect- Returns:
- true if the statement is an if-continue pattern
-
resolveLogicalNot
- Parameters:
exp- a LOG_NOT operationjctx-- Returns:
- if the LOG_NOT was simplified, a non-null reference to the simplified IR (may be the same ref as the input expression, since modifications are also attempted in place)
-