Class AbstractJOptimizer

java.lang.Object
com.pnfsoftware.jeb.core.AbstractPlugin
com.pnfsoftware.jeb.core.units.code.java.AbstractJOptimizer
All Implemented Interfaces:
IPlugin, IJOptimizer
Direct Known Subclasses:
AbstractJBlockOptimizer, AbstractJElementOptimizer, AbstractJStatementOptimizer

public abstract class AbstractJOptimizer extends AbstractPlugin implements IJOptimizer
Base class for dexdec (DEX decompiler) AST optimizer plugins. Those plugins can access and modify the AST of a class or method being decompiled. They can be used to implement simple code clean-up and beautification/styling.

Optimizers are usually managed by master optimizers (also called orchestrators). Third-party optimizer plugins can be automatically registered when creating an orchestrator.

Life-cycle information:
- plugins are reused (i.e. the perform() method is called multiple times)
- thread-safety:
-- Python script plugins must be thread-safe: a plugin instance may be executed by multiple threads concurrently
-- Java plugins (including Java script plugins) are not required to be thread-safe: a plugin instance will not be executed by multiple threads concurrently

  • Field Details

    • logger

      public static final ILogger logger
      Public logger accessible by the implementing optimizer. Writing to the logger should be favored over writing directly to stdout.
    • c

      public IJavaClass c
      Target AST class to be optimized. May be null, in which case m is not.
    • m

      public IJavaMethod m
      Target AST method to be optimized. May be null, in which case c is not.
    • jctx

      public IJavaGlobalContext jctx
      Java AST global context. This context holds AST element factory methods as well as references to important factories, such as the constant, type, and operator factories.
    • tf

      public IJavaTypeFactory tf
      AST type factory (for convenience - referenced in jctx).
    • of

      AST operation factory (for convenience - referenced in jctx).
    • cf

      AST constant factory (for convenience - referenced in jctx).
    • dex

      public IDexUnit dex
      Underlying dex code. May be null.
    • decomp

      public IDexDecompilerUnit decomp
      Managing dex decompiler. May be null.
    • drcollector

      public DeferredRequestsCollector drcollector
      Collector for deferred requests. May be null. When non-null, optimizers may file requests to decompile other classes, methods, or fields. It is useful for optimizers needing to examine AST code located in other classes or methods to operate.

      How to use in your optimizer:

       
       // do some work
       // ...
       
       // need access to the AST of some other class (CX) to continue the work
       IJavaClass ast_CX = jctx.getClassFactory().get(csig_CX);
       if(ast_CX == null || !ast_CX.isBuilt()) {
           // the AST of CX is not available at this time
           // if we can, request a decompilation 
           if(drcollector != null) {
               drcollector.request(csig_MX);
           }
           return 0;  // done for now
       }
       
       // in a future call to this optimizer, the above check may pass, allowing further processing
       // ... 
       // ...
       
       
  • Constructor Details

    • AbstractJOptimizer

      public AbstractJOptimizer()
      Create a standard optimizer.
    • AbstractJOptimizer

      public AbstractJOptimizer(JOptimizerType type)
      Create an optimizer.
      Parameters:
      type -
    • AbstractJOptimizer

      public AbstractJOptimizer(JOptimizerType type, String name)
      Create an optimizer.
      Parameters:
      type -
      name -
  • Method Details

    • getPluginInformation

      public EditablePluginInformation getPluginInformation()
      Description copied from interface: IPlugin
      Retrieve basic information about the plugin, such as name, version, author, and organization.
      Specified by:
      getPluginInformation in interface IPlugin
      Returns:
      the plugin information
    • setName

      protected void setName(String name)
      Set the optimizer name. To be used by the constructor.
      Parameters:
      name - if null, a name will be auto-generated
    • getName

      public String getName()
      Description copied from interface: IJOptimizer
      Retrieve the plugin name. Should be consistent with the value returned by getPluginInformation().getName().
      Specified by:
      getName in interface IJOptimizer
      Returns:
    • setType

      protected void setType(JOptimizerType type)
      Set the optimizer type. To be used by the constructor.
      Parameters:
      type - if null, a standard optimizer is assumed
    • getType

      public JOptimizerType getType()
      Description copied from interface: IJOptimizer
      Get the optimizer type. Types are used by optimizer orchestrators to determine whether an optimizer should run.
      Specified by:
      getType in interface IJOptimizer
      Returns:
    • setPriority

      protected void setPriority(double priority)
      Set the optimizer priority. To be used by the constructor.
      Parameters:
      priority - the new priority (high means higher priority). When optimizers are managed and run by an orchestrator, the optimizers with a higher priority are run before those having a lower priority. The default priority is 0.
    • getPriority

      public double getPriority()
      Description copied from interface: IJOptimizer
      Get the optimizer priority. A higher value means a higher priority. Priorities are used by optimizer orchestrators to determine in which order optimizers should be executed.
      Specified by:
      getPriority in interface IJOptimizer
      Returns:
      the default priority
    • setEnabled

      public void setEnabled(boolean enabled)
      Enable or disable this optimizer. This flag is checked by a master optimizer.
      Parameters:
      enabled -
    • isEnabled

      public boolean isEnabled()
      Description copied from interface: IJOptimizer
      Determine whether the optimizer is enabled or not. This method is used by optimizer orchestrators to determine whether an optimizer can be scheduled for execution.
      Specified by:
      isEnabled in interface IJOptimizer
      Returns:
      true if this optimizer is enabled
    • perform

      public final int perform(IJavaDecompilableElement elt)
      Description copied from interface: IJOptimizer
      Run the optimizer on the provided target element (method or class).
      Specified by:
      perform in interface IJOptimizer
      Parameters:
      elt - an AST element, such as an IJavaMethod or IJavaClass
      Returns:
      number of optimizations performed
    • perform

      public abstract int perform()
      An optimizer must implement this method. This method is called by a master optimizer to perform the optimization on the selected target.

      Note that the optimizer is responsible for returning a legal method context, e.g.: the method IR instructions must be consistent with the CFG; the CFG must adhere to certain rules (see cleanGraph); if the data flow analysis is no longer valid, it should be invalidated (see resetDFA); etc.

      Returns:
      the number of optimizations performed, 0 if none