⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asmcontenthandler.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/***
 * ASM XML Adapter
 * Copyright (c) 2004, Eugene Kuleshov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.drools.asm.xml;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.drools.asm.AnnotationVisitor;
import org.drools.asm.ClassVisitor;
import org.drools.asm.ClassWriter;
import org.drools.asm.FieldVisitor;
import org.drools.asm.Label;
import org.drools.asm.MethodVisitor;
import org.drools.asm.Opcodes;
import org.drools.asm.Type;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * A {@link org.xml.sax.ContentHandler ContentHandler} that transforms XML
 * document into Java class file. This class can be feeded by any kind of SAX
 * 2.0 event producers, e.g. XML parser, XSLT or XPath engines, or custom code.
 * 
 * @see org.drools.asm.xml.SAXClassAdapter
 * @see org.drools.asm.xml.Processor
 * 
 * @author Eugene Kuleshov
 */
public class ASMContentHandler extends DefaultHandler
    implements
    Opcodes {
    /**
     * Stack of the intermediate processing contexts.
     */
    private final List          stack = new ArrayList();

    /**
     * Complete name of the current element.
     */
    private String              match = "";

    /**
     * <tt>true</tt> if the maximum stack size and number of local variables
     * must be automatically computed.
     */
    protected boolean           computeMax;

    /**
     * Output stream to write result bytecode.
     */
    protected OutputStream      os;

    /**
     * Current instance of the {@link ClassWriter ClassWriter} used to write
     * class bytecode.
     */
    protected ClassWriter       cw;

    /**
     * Map of the active {@link Label Label} instances for current method.
     */
    protected Map               labels;

    private static final String BASE  = "class";

    private final RuleSet       RULES = new RuleSet();
    {
        this.RULES.add( ASMContentHandler.BASE,
                        new ClassRule() );
        this.RULES.add( ASMContentHandler.BASE + "/interfaces/interface",
                        new InterfaceRule() );
        this.RULES.add( ASMContentHandler.BASE + "/interfaces",
                        new InterfacesRule() );
        this.RULES.add( ASMContentHandler.BASE + "/outerclass",
                        new OuterClassRule() );
        this.RULES.add( ASMContentHandler.BASE + "/innerclass",
                        new InnerClassRule() );
        this.RULES.add( ASMContentHandler.BASE + "/source",
                        new SourceRule() );
        this.RULES.add( ASMContentHandler.BASE + "/field",
                        new FieldRule() );

        this.RULES.add( ASMContentHandler.BASE + "/method",
                        new MethodRule() );
        this.RULES.add( ASMContentHandler.BASE + "/method/exceptions/exception",
                        new ExceptionRule() );
        this.RULES.add( ASMContentHandler.BASE + "/method/exceptions",
                        new ExceptionsRule() );

        this.RULES.add( ASMContentHandler.BASE + "/method/annotationDefault",
                        new AnnotationDefaultRule() );

        this.RULES.add( ASMContentHandler.BASE + "/method/code/*",
                        new OpcodesRule() ); // opcodes

        this.RULES.add( ASMContentHandler.BASE + "/method/code/TABLESWITCH",
                        new TableSwitchRule() );
        this.RULES.add( ASMContentHandler.BASE + "/method/code/TABLESWITCH/label",
                        new TableSwitchLabelRule() );
        this.RULES.add( ASMContentHandler.BASE + "/method/code/LOOKUPSWITCH",
                        new LookupSwitchRule() );
        this.RULES.add( ASMContentHandler.BASE + "/method/code/LOOKUPSWITCH/label",
                        new LookupSwitchLabelRule() );

        this.RULES.add( ASMContentHandler.BASE + "/method/code/Label",
                        new LabelRule() );
        this.RULES.add( ASMContentHandler.BASE + "/method/code/TryCatch",
                        new TryCatchRule() );
        this.RULES.add( ASMContentHandler.BASE + "/method/code/LineNumber",
                        new LineNumberRule() );
        this.RULES.add( ASMContentHandler.BASE + "/method/code/LocalVar",
                        new LocalVarRule() );
        this.RULES.add( ASMContentHandler.BASE + "/method/code/Max",
                        new MaxRule() );

        this.RULES.add( "*/annotation",
                        new AnnotationRule() );
        this.RULES.add( "*/parameterAnnotation",
                        new AnnotationParameterRule() );
        this.RULES.add( "*/annotationValue",
                        new AnnotationValueRule() );
        this.RULES.add( "*/annotationValueAnnotation",
                        new AnnotationValueAnnotationRule() );
        this.RULES.add( "*/annotationValueEnum",
                        new AnnotationValueEnumRule() );
        this.RULES.add( "*/annotationValueArray",
                        new AnnotationValueArrayRule() );
    };

    private static interface OpcodeGroup {
        public static final int INSN                = 0;
        public static final int INSN_INT            = 1;
        public static final int INSN_VAR            = 2;
        public static final int INSN_TYPE           = 3;
        public static final int INSN_FIELD          = 4;
        public static final int INSN_METHOD         = 5;
        public static final int INSN_JUMP           = 6;
        public static final int INSN_LDC            = 7;
        public static final int INSN_IINC           = 8;
        public static final int INSN_MULTIANEWARRAY = 9;
    }

    /**
     * Map of the opcode names to opcode and opcode group
     */
    static final Map OPCODES = new HashMap();
    static {
        ASMContentHandler.OPCODES.put( "NOP",
                                       new Opcode( Opcodes.NOP,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "ACONST_NULL",
                                       new Opcode( Opcodes.ACONST_NULL,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "ICONST_M1",
                                       new Opcode( Opcodes.ICONST_M1,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "ICONST_0",
                                       new Opcode( Opcodes.ICONST_0,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "ICONST_1",
                                       new Opcode( Opcodes.ICONST_1,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "ICONST_2",
                                       new Opcode( Opcodes.ICONST_2,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "ICONST_3",
                                       new Opcode( Opcodes.ICONST_3,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "ICONST_4",
                                       new Opcode( Opcodes.ICONST_4,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "ICONST_5",
                                       new Opcode( Opcodes.ICONST_5,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "LCONST_0",
                                       new Opcode( Opcodes.LCONST_0,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "LCONST_1",
                                       new Opcode( Opcodes.LCONST_1,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "FCONST_0",
                                       new Opcode( Opcodes.FCONST_0,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "FCONST_1",
                                       new Opcode( Opcodes.FCONST_1,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "FCONST_2",
                                       new Opcode( Opcodes.FCONST_2,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "DCONST_0",
                                       new Opcode( Opcodes.DCONST_0,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "DCONST_1",
                                       new Opcode( Opcodes.DCONST_1,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "BIPUSH",
                                       new Opcode( Opcodes.BIPUSH,
                                                   OpcodeGroup.INSN_INT ) );
        ASMContentHandler.OPCODES.put( "SIPUSH",
                                       new Opcode( Opcodes.SIPUSH,
                                                   OpcodeGroup.INSN_INT ) );
        ASMContentHandler.OPCODES.put( "LDC",
                                       new Opcode( Opcodes.LDC,
                                                   OpcodeGroup.INSN_LDC ) );
        ASMContentHandler.OPCODES.put( "ILOAD",
                                       new Opcode( Opcodes.ILOAD,
                                                   OpcodeGroup.INSN_VAR ) );
        ASMContentHandler.OPCODES.put( "LLOAD",
                                       new Opcode( Opcodes.LLOAD,
                                                   OpcodeGroup.INSN_VAR ) );
        ASMContentHandler.OPCODES.put( "FLOAD",
                                       new Opcode( Opcodes.FLOAD,
                                                   OpcodeGroup.INSN_VAR ) );
        ASMContentHandler.OPCODES.put( "DLOAD",
                                       new Opcode( Opcodes.DLOAD,
                                                   OpcodeGroup.INSN_VAR ) );
        ASMContentHandler.OPCODES.put( "ALOAD",
                                       new Opcode( Opcodes.ALOAD,
                                                   OpcodeGroup.INSN_VAR ) );
        ASMContentHandler.OPCODES.put( "IALOAD",
                                       new Opcode( Opcodes.IALOAD,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "LALOAD",
                                       new Opcode( Opcodes.LALOAD,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "FALOAD",
                                       new Opcode( Opcodes.FALOAD,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "DALOAD",
                                       new Opcode( Opcodes.DALOAD,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "AALOAD",
                                       new Opcode( Opcodes.AALOAD,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "BALOAD",
                                       new Opcode( Opcodes.BALOAD,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "CALOAD",
                                       new Opcode( Opcodes.CALOAD,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "SALOAD",
                                       new Opcode( Opcodes.SALOAD,
                                                   OpcodeGroup.INSN ) );
        ASMContentHandler.OPCODES.put( "ISTORE",

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -