java.lang.Object
com.pnfsoftware.jeb.core.units.code.java.JUtil

public class JUtil extends Object
Collection of utility methods to manipulate IJavaElement.
  • Constructor Details

    • JUtil

      public JUtil()
  • Method Details

    • generate

      public static String generate(IJavaElement elt)
    • findParent

      public static IJavaElement findParent(IJavaElement root, IJavaElement elt)
      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, an IJavaClass
      elt - the element for which the first parent is to be found
      Returns:
      the parent or null
    • isClassMethodField

      public static boolean isClassMethodField(IJavaElement e)
      Parameters:
      e -
      Returns:
    • isIntegerConstant

      public static boolean isIntegerConstant(IJavaElement e, int expected)
      Parameters:
      e -
      Returns:
    • isStringConstant

      public static boolean isStringConstant(IJavaElement e)
      Returns:
    • getStringConstant

      public static String getStringConstant(IJavaElement e)
      Parameters:
      e -
      Returns:
    • checkIfGoto

      public static IJavaLabel checkIfGoto(IJavaStatement stm)
      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

      public static boolean isGotoTo(IJavaStatement stm, IJavaLabel target)
      Check that the statement is a "goto target;"
      Parameters:
      stm - the statement
      target - the target
      Returns:
    • isIf

      public static boolean isIf(IJavaStatement stm)
      Determine whether a statement is a simple if: if(...){...}.
      Parameters:
      stm -
      Returns:
    • isIfElse

      public static boolean isIfElse(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.
      Parameters:
      stm -
      Returns:
    • isIfNoElse

      public static boolean isIfNoElse(IJavaStatement stm)
      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

      public static IJavaLabel isIfGoto(IJavaStatement stm)
      Determine if the provided statement is like if(COND){goto LABEL;} and if so, return the label.
    • getFirstRealStatement

      public static IJavaStatement getFirstRealStatement(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'.

      This method can only go "deeper" (forward). It cannot go "up", which would require keeping track of parents. Use a PStmManager to 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 block
      i - current statement index in the block
      Returns:
      the statement, or null if none were found
    • isTryCatchall

      public static IJavaCatchBlock isTryCatchall(IJavaBlock b, int i)
      Determine whether the provided statement is a try-catch-all (single catch block, that catches all exceptions)
      Parameters:
      b - block
      i - statement index in the block
      Returns:
      the catch-all block or null
    • isTryCatchall

      public static IJavaCatchBlock isTryCatchall(IJavaTry trystm)
      Parameters:
      trystm - a IJavaTry statement
      Returns:
      true if the try-statement is a try-catch-all (single catch block, that catches all exceptions)
    • getVarLike

      public static IJavaIdentifier getVarLike(IJavaExpression e)
      Detect: x, (int)x
      Parameters:
      e -
      Returns:
      x or null
    • isImmOrVarLike

      public static boolean isImmOrVarLike(IJavaExpression e)
      Detect: 1, x, (int)x
      Parameters:
      e -
      Returns:
      true or false
    • getIdentifiers

      public static List<IJavaIdentifier> getIdentifiers(IJavaElement e)
      Parameters:
      e -
      Returns:
    • isIdentOrDefinition

      public static boolean isIdentOrDefinition(IJavaElement e, IJavaIdentifier ident)
      Determine whether the elements is the provided identifier or a simple definition of the identifier.
      Parameters:
      e -
      ident -
      Returns:
    • isDeclarationOrDefinition

      public static boolean isDeclarationOrDefinition(IJavaStatement stm)
      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

      public static IJavaExpression isThrowLike(IJavaStatement stm, List<IJavaStatement> flatlist)
      Parameters:
      stm - a statement, that we want to be a THROW or a GOTO-THROW
      flatlist -
      Returns:
      on success, the THROWN expression; else, null
    • isThrow

      public static boolean isThrow(IJavaStatement stm, IJavaExpression expectedThrown)
    • getMonitorEnter

      public static IJavaExpression getMonitorEnter(IJavaStatement stm)
    • getMonitorExit

      public static IJavaExpression getMonitorExit(IJavaStatement stm)
    • isMonitorExit

      public static boolean isMonitorExit(IJavaElement stm)
    • isMonitorExit

      public static boolean isMonitorExit(IJavaElement stm, IJavaExpression expectedLock)
      Parameters:
      stm -
      expectedLock - optional
      Returns:
    • getGotos

      public static List<IJavaStatement> getGotos(IJavaLabel targetLabel, List<IJavaStatement> flatlist)
    • getGotoLabel

      public static IJavaLabel getGotoLabel(IJavaStatement stm)
    • getIdentifier

      public static IJavaIdentifier getIdentifier(IJavaExpression e)
    • removeStatementsDeep

      public static int removeStatementsDeep(IJavaBlock b, Predicate<IJavaStatement> checker)
    • isOrContainsLabel

      public static boolean isOrContainsLabel(IJavaStatement stm)
      Recursively determine if the statement is or contains a Label. Can be called for any statement, including compound statements.
      Parameters:
      stm -
      Returns:
    • canThrow

      public static boolean canThrow(IJavaStatement stm)
    • getNegatedConstant

      public static IJavaConstant getNegatedConstant(IJavaConstant cst, IJavaConstantFactory factory)
      Get the negated value of a constant, if possible.
      Parameters:
      cst - a constant
      factory - 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

      public static boolean isNonCompoundFlowBreaker(IJavaStatement stm)
      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

      public static boolean isSimpleBreak(IJavaStatement stm)
    • isSimpleContinue

      public static boolean isSimpleContinue(IJavaStatement stm)
    • isFlowBreaker

      public static boolean isFlowBreaker(IJavaStatement stm)
    • isIfContinue

      public static boolean isIfContinue(IJavaStatement stm)
    • resolveLogicalNot

      public static IJavaExpression resolveLogicalNot(IJavaExpression exp, IJavaGlobalContext jctx)
      Parameters:
      exp - a LOG_NOT operation
      jctx -
      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)