Class Javac


  • public class Javac
    extends java.lang.Object
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Javac.CtFieldWithInit  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String param0Name  
      static java.lang.String proceedName  
      static java.lang.String resultVarName  
    • Constructor Summary

      Constructors 
      Constructor Description
      Javac​(Bytecode b, CtClass thisClass)
      Constructs a compiler.
      Javac​(CtClass thisClass)
      Constructs a compiler.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      CtMember compile​(java.lang.String src)
      Compiles a method, constructor, or field declaration to a class.
      Bytecode compileBody​(CtBehavior method, java.lang.String src)
      Compiles a method (or constructor) body.
      void compileExpr​(java.lang.String src)
      Compiles an exression.
      void compileExpr​(ASTree e)
      Compiles an exression.
      void compileStmnt​(java.lang.String src)
      Compiles a statement (or a block).
      Bytecode getBytecode()
      Returns the produced bytecode.
      static ASTree parseExpr​(java.lang.String src, SymbolTable st)
      Parsers an expression.
      boolean recordLocalVariables​(CodeAttribute ca, int pc)
      Records local variables available at the specified program counter.
      boolean recordParamNames​(CodeAttribute ca, int numOfLocalVars)
      Records parameter names if the LocalVariableAttribute is available.
      int recordParams​(java.lang.String target, CtClass[] params, boolean use0, int varNo, boolean isStatic)
      Makes variables $0, $1, $2, ..., and $args represent method parameters.
      int recordParams​(CtClass[] params, boolean isStatic)
      Makes variables $0 (this), $1, $2, ..., and $args represent method parameters.
      void recordProceed​(java.lang.String target, java.lang.String method)
      Prepares to use $proceed().
      void recordProceed​(ProceedHandler h)
      Prepares to use $proceed().
      int recordReturnType​(CtClass type, boolean useResultVar)
      Prepares to use cast $r, $w, $_, and $type.
      void recordSpecialProceed​(java.lang.String target, java.lang.String classname, java.lang.String methodname, java.lang.String descriptor, int methodIndex)
      Prepares to use $proceed() representing a private/super's method.
      void recordStaticProceed​(java.lang.String targetClass, java.lang.String method)
      Prepares to use $proceed() representing a static method.
      void recordType​(CtClass t)
      Prepares to use $type.
      int recordVariable​(CtClass type, java.lang.String name)
      Makes the given variable available.
      void setMaxLocals​(int max)
      Sets maxLocals to max.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Javac

        public Javac​(CtClass thisClass)
        Constructs a compiler.
        Parameters:
        thisClass - the class that a compiled method/field belongs to.
      • Javac

        public Javac​(Bytecode b,
                     CtClass thisClass)
        Constructs a compiler. The produced bytecode is stored in the Bytecode object specified by b.
        Parameters:
        thisClass - the class that a compiled method/field belongs to.
    • Method Detail

      • getBytecode

        public Bytecode getBytecode()
        Returns the produced bytecode.
      • compile

        public CtMember compile​(java.lang.String src)
                         throws CompileError
        Compiles a method, constructor, or field declaration to a class. A field declaration can declare only one field.

        In a method or constructor body, $0, $1, ... and $_ are not available.

        Returns:
        a CtMethod, CtConstructor, or CtField object.
        Throws:
        CompileError
        See Also:
        recordProceed(String,String)
      • compileBody

        public Bytecode compileBody​(CtBehavior method,
                                    java.lang.String src)
                             throws CompileError
        Compiles a method (or constructor) body.
        Parameters:
        src - a single statement or a block. If null, this method produces a body returning zero or null.
        Throws:
        CompileError
      • recordLocalVariables

        public boolean recordLocalVariables​(CodeAttribute ca,
                                            int pc)
                                     throws CompileError
        Records local variables available at the specified program counter. If the LocalVariableAttribute is not available, this method does not record any local variable. It only returns false.
        Parameters:
        pc - program counter (>= 0)
        Returns:
        false if the CodeAttribute does not include a LocalVariableAttribute.
        Throws:
        CompileError
      • recordParamNames

        public boolean recordParamNames​(CodeAttribute ca,
                                        int numOfLocalVars)
                                 throws CompileError
        Records parameter names if the LocalVariableAttribute is available. It returns false unless the LocalVariableAttribute is available.
        Parameters:
        numOfLocalVars - the number of local variables used for storing the parameters.
        Returns:
        false if the CodeAttribute does not include a LocalVariableAttribute.
        Throws:
        CompileError
      • recordParams

        public int recordParams​(CtClass[] params,
                                boolean isStatic)
                         throws CompileError
        Makes variables $0 (this), $1, $2, ..., and $args represent method parameters. $args represents an array of all the parameters. It also makes $$ available as a parameter list of method call.

        This must be called before calling compileStmnt() and compileExpr(). The correct value of isStatic must be recorded before compilation. maxLocals is updated to include $0,...

        Throws:
        CompileError
      • recordParams

        public int recordParams​(java.lang.String target,
                                CtClass[] params,
                                boolean use0,
                                int varNo,
                                boolean isStatic)
                         throws CompileError
        Makes variables $0, $1, $2, ..., and $args represent method parameters. $args represents an array of all the parameters. It also makes $$ available as a parameter list of method call. $0 can represent a local variable other than THIS (variable 0). $class is also made available.

        This must be called before calling compileStmnt() and compileExpr(). The correct value of isStatic must be recorded before compilation. maxLocals is updated to include $0,...

        Parameters:
        use0 - true if $0 is used.
        varNo - the register number of $0 (use0 is true) or $1 (otherwise).
        target - the type of $0 (it can be null if use0 is false). It is used as the name of the type represented by $class.
        isStatic - true if the method in which the compiled bytecode is embedded is static.
        Throws:
        CompileError
      • setMaxLocals

        public void setMaxLocals​(int max)
        Sets maxLocals to max. This method tells the compiler the local variables that have been allocated for the rest of the code. When the compiler needs new local variables, the local variables at the index max, max + 1, ... are assigned.

        This method is indirectly called by recordParams.

      • recordReturnType

        public int recordReturnType​(CtClass type,
                                    boolean useResultVar)
                             throws CompileError
        Prepares to use cast $r, $w, $_, and $type. $type is made to represent the specified return type. It also enables to write a return statement with a return value for void method.

        If the return type is void, ($r) does nothing. The type of $_ is java.lang.Object.

        Parameters:
        type - the return type.
        useResultVar - true if $_ is used.
        Returns:
        -1 or the variable index assigned to $_.
        Throws:
        CompileError
        See Also:
        recordType(CtClass)
      • recordType

        public void recordType​(CtClass t)
        Prepares to use $type. Note that recordReturnType() overwrites the value of $type.
        Parameters:
        t - the type represented by $type.
      • recordVariable

        public int recordVariable​(CtClass type,
                                  java.lang.String name)
                           throws CompileError
        Makes the given variable available.
        Parameters:
        type - variable type
        name - variable name
        Throws:
        CompileError
      • recordProceed

        public void recordProceed​(java.lang.String target,
                                  java.lang.String method)
                           throws CompileError
        Prepares to use $proceed(). If the return type of $proceed() is void, null is pushed on the stack.
        Parameters:
        target - an expression specifying the target object. if null, "this" is the target.
        method - the method name.
        Throws:
        CompileError
      • recordStaticProceed

        public void recordStaticProceed​(java.lang.String targetClass,
                                        java.lang.String method)
                                 throws CompileError
        Prepares to use $proceed() representing a static method. If the return type of $proceed() is void, null is pushed on the stack.
        Parameters:
        targetClass - the fully-qualified dot-separated name of the class declaring the method.
        method - the method name.
        Throws:
        CompileError
      • recordSpecialProceed

        public void recordSpecialProceed​(java.lang.String target,
                                         java.lang.String classname,
                                         java.lang.String methodname,
                                         java.lang.String descriptor,
                                         int methodIndex)
                                  throws CompileError
        Prepares to use $proceed() representing a private/super's method. If the return type of $proceed() is void, null is pushed on the stack. This method is for methods invoked by INVOKESPECIAL.
        Parameters:
        target - an expression specifying the target object. if null, "this" is the target.
        classname - the class name declaring the method.
        methodname - the method name.
        descriptor - the method descriptor.
        Throws:
        CompileError
      • recordProceed

        public void recordProceed​(ProceedHandler h)
        Prepares to use $proceed().
      • compileStmnt

        public void compileStmnt​(java.lang.String src)
                          throws CompileError
        Compiles a statement (or a block). recordParams() must be called before invoking this method.

        Local variables that are not declared in the compiled source text might not be accessible within that source text. Fields and method parameters ($0, $1, ..) are available.

        Throws:
        CompileError
      • compileExpr

        public void compileExpr​(java.lang.String src)
                         throws CompileError
        Compiles an exression. recordParams() must be called before invoking this method.

        Local variables are not accessible within the compiled source text. Fields and method parameters ($0, $1, ..) are available if recordParams() have been invoked.

        Throws:
        CompileError
      • compileExpr

        public void compileExpr​(ASTree e)
                         throws CompileError
        Compiles an exression. recordParams() must be called before invoking this method.

        Local variables are not accessible within the compiled source text. Fields and method parameters ($0, $1, ..) are available if recordParams() have been invoked.

        Throws:
        CompileError