< prev index next >

src/java.xml/share/classes/com/sun/org/apache/bcel/internal/generic/InstructionFactory.java

Print this page




  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 package com.sun.org.apache.bcel.internal.generic;
  21 
  22 import com.sun.org.apache.bcel.internal.Const;
  23 
  24 /**
  25  * Instances of this class may be used, e.g., to generate typed versions of
  26  * instructions. Its main purpose is to be used as the byte code generating
  27  * backend of a compiler. You can subclass it to add your own create methods.
  28  * <p>
  29  * Note: The static createXXX methods return singleton instances from the
  30  * {@link InstructionConst} class.
  31  *
  32  * @version $Id: InstructionFactory.java 1749603 2016-06-21 20:50:19Z ggregory $
  33  * @see Const
  34  * @see InstructionConst

  35  */
  36 public class InstructionFactory {
  37 
  38     // N.N. These must agree with the order of Constants.T_CHAR through T_LONG
  39     private static final String[] short_names = {
  40         "C", "F", "D", "B", "S", "I", "L"
  41     };
  42 
  43     private ClassGen cg;
  44     private ConstantPoolGen cp;
  45 
  46     public InstructionFactory(final ClassGen cg, final ConstantPoolGen cp) {
  47         this.cg = cg;
  48         this.cp = cp;
  49     }
  50 
  51     /**
  52      * Initialize with ClassGen object
  53      */
  54     public InstructionFactory(final ClassGen cg) {


 556         }
 557     }
 558 
 559     /**
 560      * Create conversion operation for two stack operands, this may be an I2C,
 561      * instruction, e.g., if the operands are basic types and CHECKCAST if they
 562      * are reference types.
 563      */
 564     public Instruction createCast(final Type src_type, final Type dest_type) {
 565         if ((src_type instanceof BasicType) && (dest_type instanceof BasicType)) {
 566             final byte dest = dest_type.getType();
 567             byte src = src_type.getType();
 568             if (dest == Const.T_LONG
 569                     && (src == Const.T_CHAR || src == Const.T_BYTE || src == Const.T_SHORT)) {
 570                 src = Const.T_INT;
 571             }
 572             final String name = "com.sun.org.apache.bcel.internal.generic." + short_names[src - Const.T_CHAR] + "2"
 573                     + short_names[dest - Const.T_CHAR];
 574             Instruction i = null;
 575             try {
 576                 i = (Instruction) java.lang.Class.forName(name).newInstance();
 577             } catch (final Exception e) {
 578                 throw new RuntimeException("Could not find instruction: " + name, e);
 579             }
 580             return i;
 581         } else if ((src_type instanceof ReferenceType) && (dest_type instanceof ReferenceType)) {
 582             if (dest_type instanceof ArrayType) {
 583                 return new CHECKCAST(cp.addArrayClass((ArrayType) dest_type));
 584             }
 585             return new CHECKCAST(cp.addClass(((ObjectType) dest_type).getClassName()));
 586         } else {
 587             throw new RuntimeException("Can not cast " + src_type + " to " + dest_type);
 588         }
 589     }
 590 
 591     public GETFIELD createGetField(final String class_name, final String name, final Type t) {
 592         return new GETFIELD(cp.addFieldref(class_name, name, t.getSignature()));
 593     }
 594 
 595     public GETSTATIC createGetStatic(final String class_name, final String name, final Type t) {
 596         return new GETSTATIC(cp.addFieldref(class_name, name, t.getSignature()));




  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 package com.sun.org.apache.bcel.internal.generic;
  21 
  22 import com.sun.org.apache.bcel.internal.Const;
  23 
  24 /**
  25  * Instances of this class may be used, e.g., to generate typed versions of
  26  * instructions. Its main purpose is to be used as the byte code generating
  27  * backend of a compiler. You can subclass it to add your own create methods.
  28  * <p>
  29  * Note: The static createXXX methods return singleton instances from the
  30  * {@link InstructionConst} class.
  31  *
  32  * @version $Id: InstructionFactory.java 1749603 2016-06-21 20:50:19Z ggregory $
  33  * @see Const
  34  * @see InstructionConst
  35  * @LastModified: Nov 2017
  36  */
  37 public class InstructionFactory {
  38 
  39     // N.N. These must agree with the order of Constants.T_CHAR through T_LONG
  40     private static final String[] short_names = {
  41         "C", "F", "D", "B", "S", "I", "L"
  42     };
  43 
  44     private ClassGen cg;
  45     private ConstantPoolGen cp;
  46 
  47     public InstructionFactory(final ClassGen cg, final ConstantPoolGen cp) {
  48         this.cg = cg;
  49         this.cp = cp;
  50     }
  51 
  52     /**
  53      * Initialize with ClassGen object
  54      */
  55     public InstructionFactory(final ClassGen cg) {


 557         }
 558     }
 559 
 560     /**
 561      * Create conversion operation for two stack operands, this may be an I2C,
 562      * instruction, e.g., if the operands are basic types and CHECKCAST if they
 563      * are reference types.
 564      */
 565     public Instruction createCast(final Type src_type, final Type dest_type) {
 566         if ((src_type instanceof BasicType) && (dest_type instanceof BasicType)) {
 567             final byte dest = dest_type.getType();
 568             byte src = src_type.getType();
 569             if (dest == Const.T_LONG
 570                     && (src == Const.T_CHAR || src == Const.T_BYTE || src == Const.T_SHORT)) {
 571                 src = Const.T_INT;
 572             }
 573             final String name = "com.sun.org.apache.bcel.internal.generic." + short_names[src - Const.T_CHAR] + "2"
 574                     + short_names[dest - Const.T_CHAR];
 575             Instruction i = null;
 576             try {
 577                 i = (Instruction) java.lang.Class.forName(name).getDeclaredConstructor().newInstance();
 578             } catch (final Exception e) {
 579                 throw new RuntimeException("Could not find instruction: " + name, e);
 580             }
 581             return i;
 582         } else if ((src_type instanceof ReferenceType) && (dest_type instanceof ReferenceType)) {
 583             if (dest_type instanceof ArrayType) {
 584                 return new CHECKCAST(cp.addArrayClass((ArrayType) dest_type));
 585             }
 586             return new CHECKCAST(cp.addClass(((ObjectType) dest_type).getClassName()));
 587         } else {
 588             throw new RuntimeException("Can not cast " + src_type + " to " + dest_type);
 589         }
 590     }
 591 
 592     public GETFIELD createGetField(final String class_name, final String name, final Type t) {
 593         return new GETFIELD(cp.addFieldref(class_name, name, t.getSignature()));
 594     }
 595 
 596     public GETSTATIC createGetStatic(final String class_name, final String name, final Type t) {
 597         return new GETSTATIC(cp.addFieldref(class_name, name, t.getSignature()));


< prev index next >