Java Code Examples for com.sun.org.apache.bcel.internal.generic.ConstantPoolGen#addMethodref()

The following examples show how to use com.sun.org.apache.bcel.internal.generic.ConstantPoolGen#addMethodref() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReferenceType.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Translates reference into object of internal type <code>type</code>.
 *
 * @see     com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
 */
public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
                        StringType type) {
    final int current = methodGen.getLocalIndex("current");
    ConstantPoolGen cpg = classGen.getConstantPool();
    InstructionList il = methodGen.getInstructionList();

    // If no current, conversion is a top-level
    if (current < 0) {
        il.append(new PUSH(cpg, DTM.ROOT_NODE));  // push root node
    }
    else {
        il.append(new ILOAD(current));
    }
    il.append(methodGen.loadDOM());
    final int stringF = cpg.addMethodref(BASIS_LIBRARY_CLASS,
                                         "stringF",
                                         "("
                                         + OBJECT_SIG
                                         + NODE_SIG
                                         + DOM_INTF_SIG
                                         + ")" + STRING_SIG);
    il.append(new INVOKESTATIC(stringF));
}
 
Example 2
Source File: Mode.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * Compiles the default handling for DOM elements: traverse all children
 */
private InstructionList compileDefaultRecursion(ClassGenerator classGen,
                                                MethodGenerator methodGen,
                                                InstructionHandle next) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = new InstructionList();
    final String applyTemplatesSig = classGen.getApplyTemplatesSig();
    final int git = cpg.addInterfaceMethodref(DOM_INTF,
                                              GET_CHILDREN,
                                              GET_CHILDREN_SIG);
    final int applyTemplates = cpg.addMethodref(getClassName(),
                                                functionName(),
                                                applyTemplatesSig);
    il.append(classGen.loadTranslet());
    il.append(methodGen.loadDOM());

    il.append(methodGen.loadDOM());
    il.append(new ILOAD(_currentIndex));
    il.append(new INVOKEINTERFACE(git, 2));
    il.append(methodGen.loadHandler());
    il.append(new INVOKEVIRTUAL(applyTemplates));
    il.append(new GOTO_W(next));
    return il;
}
 
Example 3
Source File: UnionPathExpr.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    final int init = cpg.addMethodref(UNION_ITERATOR_CLASS,
                                      "<init>",
                                      "("+DOM_INTF_SIG+")V");
    final int iter = cpg.addMethodref(UNION_ITERATOR_CLASS,
                                      ADD_ITERATOR,
                                      ADD_ITERATOR_SIG);

    // Create the UnionIterator and leave it on the stack
    il.append(new NEW(cpg.addClass(UNION_ITERATOR_CLASS)));
    il.append(DUP);
    il.append(methodGen.loadDOM());
    il.append(new INVOKESPECIAL(init));

    // Add the various iterators to the UnionIterator
    final int length = _components.length;
    for (int i = 0; i < length; i++) {
        _components[i].translate(classGen, methodGen);
        il.append(new INVOKEVIRTUAL(iter));
    }

    // Order the iterator only if strictly needed
    if (_reverse) {
        final int order = cpg.addInterfaceMethodref(DOM_INTF,
                                                    ORDER_ITERATOR,
                                                    ORDER_ITERATOR_SIG);
        il.append(methodGen.loadDOM());
        il.append(SWAP);
        il.append(methodGen.loadContextNode());
        il.append(new INVOKEINTERFACE(order, 3));

    }
}
 
Example 4
Source File: ReferenceType.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a reference to an object of internal type <code>type</code>.
 *
 * @see     com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
 */
public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
                        BooleanType type) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    int index = cpg.addMethodref(BASIS_LIBRARY_CLASS, "booleanF",
                                 "("
                                 + OBJECT_SIG
                                 + ")Z");
    il.append(new INVOKESTATIC(index));
}
 
Example 5
Source File: RelationalExpr.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    if (hasNodeSetArgs() || hasReferenceArgs()) {
        final ConstantPoolGen cpg = classGen.getConstantPool();
        final InstructionList il = methodGen.getInstructionList();

        // Call compare() from the BasisLibrary
        _left.translate(classGen, methodGen);
        _left.startIterator(classGen, methodGen);
        _right.translate(classGen, methodGen);
        _right.startIterator(classGen, methodGen);

        il.append(new PUSH(cpg, _op));
        il.append(methodGen.loadDOM());

        int index = cpg.addMethodref(BASIS_LIBRARY_CLASS, "compare",
                                     "("
                                     + _left.getType().toSignature()
                                     + _right.getType().toSignature()
                                     + "I"
                                     + DOM_INTF_SIG
                                     + ")Z");
        il.append(new INVOKESTATIC(index));
    }
    else {
        translateDesynthesized(classGen, methodGen);
        synthesize(classGen, methodGen);
    }
}
 
Example 6
Source File: RelationalExpr.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    if (hasNodeSetArgs() || hasReferenceArgs()) {
        final ConstantPoolGen cpg = classGen.getConstantPool();
        final InstructionList il = methodGen.getInstructionList();

        // Call compare() from the BasisLibrary
        _left.translate(classGen, methodGen);
        _left.startIterator(classGen, methodGen);
        _right.translate(classGen, methodGen);
        _right.startIterator(classGen, methodGen);

        il.append(new PUSH(cpg, _op));
        il.append(methodGen.loadDOM());

        int index = cpg.addMethodref(BASIS_LIBRARY_CLASS, "compare",
                                     "("
                                     + _left.getType().toSignature()
                                     + _right.getType().toSignature()
                                     + "I"
                                     + DOM_INTF_SIG
                                     + ")Z");
        il.append(new INVOKESTATIC(index));
    }
    else {
        translateDesynthesized(classGen, methodGen);
        synthesize(classGen, methodGen);
    }
}
 
Example 7
Source File: UseAttributeSets.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate a call to the method compiled for this attribute set
 */
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {

    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    final SymbolTable symbolTable = getParser().getSymbolTable();

    // Go through each attribute set and generate a method call
    for (int i=0; i<_sets.size(); i++) {
        // Get the attribute set name
        final QName name = (QName)_sets.elementAt(i);
        // Get the AttributeSet reference from the symbol table
        final AttributeSet attrs = symbolTable.lookupAttributeSet(name);
        // Compile the call to the set's method if the set exists
        if (attrs != null) {
            final String methodName = attrs.getMethodName();
            il.append(classGen.loadTranslet());
            il.append(methodGen.loadDOM());
            il.append(methodGen.loadIterator());
            il.append(methodGen.loadHandler());
            il.append(methodGen.loadCurrentNode());
            final int method = cpg.addMethodref(classGen.getClassName(),
                                                methodName, ATTR_SET_SIG);
            il.append(new INVOKESPECIAL(method));
        }
        // Generate an error if the attribute set does not exist
        else {
            final Parser parser = getParser();
            final String atrs = name.toString();
            reportError(this, parser, ErrorMsg.ATTRIBSET_UNDEF_ERR, atrs);
        }
    }
}
 
Example 8
Source File: FunctionCall.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translate code to call the BasisLibrary.unallowed_extensionF(String)
 * method.
 */
private void translateUnallowedExtension(ConstantPoolGen cpg,
                                         InstructionList il) {
    int index = cpg.addMethodref(BASIS_LIBRARY_CLASS,
                                 "unallowed_extension_functionF",
                                 "(Ljava/lang/String;)V");
    il.append(new PUSH(cpg, _fname.toString()));
    il.append(new INVOKESTATIC(index));
}
 
Example 9
Source File: RelationalExpr.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    if (hasNodeSetArgs() || hasReferenceArgs()) {
        final ConstantPoolGen cpg = classGen.getConstantPool();
        final InstructionList il = methodGen.getInstructionList();

        // Call compare() from the BasisLibrary
        _left.translate(classGen, methodGen);
        _left.startIterator(classGen, methodGen);
        _right.translate(classGen, methodGen);
        _right.startIterator(classGen, methodGen);

        il.append(new PUSH(cpg, _op));
        il.append(methodGen.loadDOM());

        int index = cpg.addMethodref(BASIS_LIBRARY_CLASS, "compare",
                                     "("
                                     + _left.getType().toSignature()
                                     + _right.getType().toSignature()
                                     + "I"
                                     + DOM_INTF_SIG
                                     + ")Z");
        il.append(new INVOKESTATIC(index));
    }
    else {
        translateDesynthesized(classGen, methodGen);
        synthesize(classGen, methodGen);
    }
}
 
Example 10
Source File: UseAttributeSets.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a call to the method compiled for this attribute set
 */
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {

    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    final SymbolTable symbolTable = getParser().getSymbolTable();

    // Go through each attribute set and generate a method call
    for (int i=0; i<_sets.size(); i++) {
        // Get the attribute set name
        final QName name = (QName)_sets.elementAt(i);
        // Get the AttributeSet reference from the symbol table
        final AttributeSet attrs = symbolTable.lookupAttributeSet(name);
        // Compile the call to the set's method if the set exists
        if (attrs != null) {
            final String methodName = attrs.getMethodName();
            il.append(classGen.loadTranslet());
            il.append(methodGen.loadDOM());
            il.append(methodGen.loadIterator());
            il.append(methodGen.loadHandler());
            il.append(methodGen.loadCurrentNode());
            final int method = cpg.addMethodref(classGen.getClassName(),
                                                methodName, ATTR_SET_SIG);
            il.append(new INVOKESPECIAL(method));
        }
        // Generate an error if the attribute set does not exist
        else {
            final Parser parser = getParser();
            final String atrs = name.toString();
            reportError(this, parser, ErrorMsg.ATTRIBSET_UNDEF_ERR, atrs);
        }
    }
}
 
Example 11
Source File: TransletOutput.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compile code that opens the give file for output, dumps the contents of
 * the element to the file, then closes the file.
 */
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    final boolean isSecureProcessing = classGen.getParser().getXSLTC()
                                       .isSecureProcessing();

    if (isSecureProcessing) {
        int index = cpg.addMethodref(BASIS_LIBRARY_CLASS,
                                     "unallowed_extension_elementF",
                                     "(Ljava/lang/String;)V");
        il.append(new PUSH(cpg, "redirect"));
        il.append(new INVOKESTATIC(index));
        return;
    }

    // Save the current output handler on the stack
    il.append(methodGen.loadHandler());

    final int open =  cpg.addMethodref(TRANSLET_CLASS,
                                       "openOutputHandler",
                                       "(" + STRING_SIG + "Z)" +
                                       TRANSLET_OUTPUT_SIG);

    final int close =  cpg.addMethodref(TRANSLET_CLASS,
                                        "closeOutputHandler",
                                        "("+TRANSLET_OUTPUT_SIG+")V");

    // Create the new output handler (leave it on stack)
    il.append(classGen.loadTranslet());
    _filename.translate(classGen, methodGen);
    il.append(new PUSH(cpg, _append));
    il.append(new INVOKEVIRTUAL(open));

    // Overwrite current handler
    il.append(methodGen.storeHandler());

    // Translate contents with substituted handler
    translateContents(classGen, methodGen);

    // Close the output handler (close file)
    il.append(classGen.loadTranslet());
    il.append(methodGen.loadHandler());
    il.append(new INVOKEVIRTUAL(close));

    // Restore old output handler from stack
    il.append(methodGen.storeHandler());
}
 
Example 12
Source File: Sort.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compiles code that instantiates a SortingIterator object.
 * This object's constructor needs referencdes to the current iterator
 * and a node sort record producing objects as its parameters.
 */
public static void translateSortIterator(ClassGenerator classGen,
                                  MethodGenerator methodGen,
                                  Expression nodeSet,
                                  Vector<Sort> sortObjects)
{
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // SortingIterator.SortingIterator(NodeIterator,NodeSortRecordFactory);
    final int init = cpg.addMethodref(SORT_ITERATOR, "<init>",
                                      "("
                                      + NODE_ITERATOR_SIG
                                      + NODE_SORT_FACTORY_SIG
                                      + ")V");

    // Backwards branches are prohibited if an uninitialized object is
    // on the stack by section 4.9.4 of the JVM Specification, 2nd Ed.
    // We don't know whether this code might contain backwards branches
    // so we mustn't create the new object until after we've created
    // the suspect arguments to its constructor.  Instead we calculate
    // the values of the arguments to the constructor first, store them
    // in temporary variables, create the object and reload the
    // arguments from the temporaries to avoid the problem.

    LocalVariableGen nodesTemp =
        methodGen.addLocalVariable("sort_tmp1",
                                   Util.getJCRefType(NODE_ITERATOR_SIG),
                                   null, null);

    LocalVariableGen sortRecordFactoryTemp =
        methodGen.addLocalVariable("sort_tmp2",
                                  Util.getJCRefType(NODE_SORT_FACTORY_SIG),
                                  null, null);

    // Get the current node iterator
    if (nodeSet == null) {  // apply-templates default
        final int children = cpg.addInterfaceMethodref(DOM_INTF,
                                                       "getAxisIterator",
                                                       "(I)"+
                                                       NODE_ITERATOR_SIG);
        il.append(methodGen.loadDOM());
        il.append(new PUSH(cpg, Axis.CHILD));
        il.append(new INVOKEINTERFACE(children, 2));
    }
    else {
        nodeSet.translate(classGen, methodGen);
    }

    nodesTemp.setStart(il.append(new ASTORE(nodesTemp.getIndex())));

    // Compile the code for the NodeSortRecord producing class and pass
    // that as the last argument to the SortingIterator constructor.
    compileSortRecordFactory(sortObjects, classGen, methodGen);
    sortRecordFactoryTemp.setStart(
            il.append(new ASTORE(sortRecordFactoryTemp.getIndex())));

    il.append(new NEW(cpg.addClass(SORT_ITERATOR)));
    il.append(DUP);
    nodesTemp.setEnd(il.append(new ALOAD(nodesTemp.getIndex())));
    sortRecordFactoryTemp.setEnd(
            il.append(new ALOAD(sortRecordFactoryTemp.getIndex())));
    il.append(new INVOKESPECIAL(init));
}
 
Example 13
Source File: KeyCall.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * This method is called when the constructor is compiled in
 * Stylesheet.compileConstructor() and not as the syntax tree is traversed.
 * <p>This method will generate byte code that produces an iterator
 * for the nodes in the node set for the key or id function call.
 * @param classGen The Java class generator
 * @param methodGen The method generator
 */
public void translate(ClassGenerator classGen,
                      MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // Returns the KeyIndex object of a given name
    final int getKeyIndex = cpg.addMethodref(TRANSLET_CLASS,
                                             "getKeyIndex",
                                             "(Ljava/lang/String;)"+
                                             KEY_INDEX_SIG);

    // KeyIndex.setDom(Dom, node) => void
    final int keyDom = cpg.addMethodref(KEY_INDEX_CLASS,
                                        "setDom",
                                        "(" + DOM_INTF_SIG + "I)V");

    // Initialises a KeyIndex to return nodes with specific values
    final int getKeyIterator =
                    cpg.addMethodref(KEY_INDEX_CLASS,
                                     "getKeyIndexIterator",
                                     "(" + _valueType.toSignature() + "Z)"
                                         + KEY_INDEX_ITERATOR_SIG);

    // Initialise the index specified in the first parameter of key()
    il.append(classGen.loadTranslet());
    if (_name == null) {
        il.append(new PUSH(cpg,"##id"));
    } else if (_resolvedQName != null) {
        il.append(new PUSH(cpg, _resolvedQName.toString()));
    } else {
        _name.translate(classGen, methodGen);
    }

    // Generate following byte code:
    //
    //   KeyIndex ki = translet.getKeyIndex(_name)
    //   ki.setDom(translet.dom);
    //   ki.getKeyIndexIterator(_value, true)  - for key()
    //        OR
    //   ki.getKeyIndexIterator(_value, false)  - for id()
    il.append(new INVOKEVIRTUAL(getKeyIndex));
    il.append(DUP);
    il.append(methodGen.loadDOM());
    il.append(methodGen.loadCurrentNode());
    il.append(new INVOKEVIRTUAL(keyDom));

    _value.translate(classGen, methodGen);
    il.append((_name != null) ? ICONST_1: ICONST_0);
    il.append(new INVOKEVIRTUAL(getKeyIterator));
}
 
Example 14
Source File: FilteredAbsoluteLocationPath.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    if (_path != null) {
        final int initDFI = cpg.addMethodref(DUP_FILTERED_ITERATOR,
                                            "<init>",
                                            "("
                                            + NODE_ITERATOR_SIG
                                            + ")V");

        // Backwards branches are prohibited if an uninitialized object is
        // on the stack by section 4.9.4 of the JVM Specification, 2nd Ed.
        // We don't know whether this code might contain backwards branches,
        // so we mustn't create the new object until after we've created
        // the suspect arguments to its constructor.  Instead we calculate
        // the values of the arguments to the constructor first, store them
        // in temporary variables, create the object and reload the
        // arguments from the temporaries to avoid the problem.

        // Compile relative path iterator(s)
        LocalVariableGen pathTemp =
           methodGen.addLocalVariable("filtered_absolute_location_path_tmp",
                                      Util.getJCRefType(NODE_ITERATOR_SIG),
                                      null, null);
        _path.translate(classGen, methodGen);
        pathTemp.setStart(il.append(new ASTORE(pathTemp.getIndex())));

        // Create new Dup Filter Iterator
        il.append(new NEW(cpg.addClass(DUP_FILTERED_ITERATOR)));
        il.append(DUP);
        pathTemp.setEnd(il.append(new ALOAD(pathTemp.getIndex())));

        // Initialize Dup Filter Iterator with iterator from the stack
        il.append(new INVOKESPECIAL(initDFI));
    }
    else {
        final int git = cpg.addInterfaceMethodref(DOM_INTF,
                                                  "getIterator",
                                                  "()"+NODE_ITERATOR_SIG);
        il.append(methodGen.loadDOM());
        il.append(new INVOKEINTERFACE(git, 1));
    }
}
 
Example 15
Source File: CallTemplate.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final Stylesheet stylesheet = classGen.getStylesheet();
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // If there are Params in the stylesheet or WithParams in this call?
    if (stylesheet.hasLocalParams() || hasContents()) {
        _calleeTemplate = getCalleeTemplate();

        // Build the parameter list if the called template is simple named
        if (_calleeTemplate != null) {
            buildParameterList();
        }
        // This is only needed when the called template is not
        // a simple named template.
        else {
            // Push parameter frame
            final int push = cpg.addMethodref(TRANSLET_CLASS,
                                              PUSH_PARAM_FRAME,
                                              PUSH_PARAM_FRAME_SIG);
            il.append(classGen.loadTranslet());
            il.append(new INVOKEVIRTUAL(push));
            translateContents(classGen, methodGen);
        }
    }

    // Generate a valid Java method name
    final String className = stylesheet.getClassName();
    String methodName = Util.escape(_name.toString());

    // Load standard arguments
    il.append(classGen.loadTranslet());
    il.append(methodGen.loadDOM());
    il.append(methodGen.loadIterator());
    il.append(methodGen.loadHandler());
    il.append(methodGen.loadCurrentNode());

    // Initialize prefix of method signature
    StringBuffer methodSig = new StringBuffer("(" + DOM_INTF_SIG
        + NODE_ITERATOR_SIG + TRANSLET_OUTPUT_SIG + NODE_SIG);

    // If calling a simply named template, push actual arguments
    if (_calleeTemplate != null) {
        int numParams = _parameters.length;

        for (int i = 0; i < numParams; i++) {
            SyntaxTreeNode node = _parameters[i];
            methodSig.append(OBJECT_SIG);   // append Object to signature

            // Push 'null' if Param to indicate no actual parameter specified
            if (node instanceof Param) {
                il.append(ACONST_NULL);
            }
            else {  // translate WithParam
                node.translate(classGen, methodGen);
            }
        }
    }

    // Complete signature and generate invokevirtual call
    methodSig.append(")V");
    il.append(new INVOKEVIRTUAL(cpg.addMethodref(className,
                                                 methodName,
                                                 methodSig.toString())));

    // release temporary result trees
    if (_parameters != null) {
        for (int i = 0; i < _parameters.length; i++) {
            if (_parameters[i] instanceof WithParam) {
                ((WithParam)_parameters[i]).releaseResultTree(classGen, methodGen);
            }
        }
    }

    // Do not need to call Translet.popParamFrame() if we are
    // calling a simple named template.
    if (_calleeTemplate == null && (stylesheet.hasLocalParams() || hasContents())) {
        // Pop parameter frame
        final int pop = cpg.addMethodref(TRANSLET_CLASS,
                                         POP_PARAM_FRAME,
                                         POP_PARAM_FRAME_SIG);
        il.append(classGen.loadTranslet());
        il.append(new INVOKEVIRTUAL(pop));
    }
}
 
Example 16
Source File: StepPattern.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private void translateSimpleContext(ClassGenerator classGen,
                                    MethodGenerator methodGen) {
    int index;
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // Store matching node into a local variable
    LocalVariableGen match;
    match = methodGen.addLocalVariable("step_pattern_tmp1",
                                       Util.getJCRefType(NODE_SIG),
                                       null, null);
    match.setStart(il.append(new ISTORE(match.getIndex())));

    // If pattern not reduced then check kernel
    if (!_isEpsilon) {
        il.append(new ILOAD(match.getIndex()));
        translateKernel(classGen, methodGen);
    }

    // Push current iterator and current node on the stack
    il.append(methodGen.loadCurrentNode());
    il.append(methodGen.loadIterator());

    // Create a new matching iterator using the matching node
    index = cpg.addMethodref(MATCHING_ITERATOR, "<init>",
                             "(I" + NODE_ITERATOR_SIG + ")V");

    // Backwards branches are prohibited if an uninitialized object is
    // on the stack by section 4.9.4 of the JVM Specification, 2nd Ed.
    // We don't know whether this code might contain backwards branches,
    // so we mustn't create the new object until after we've created
    // the suspect arguments to its constructor.  Instead we calculate
    // the values of the arguments to the constructor first, store them
    // in temporary variables, create the object and reload the
    // arguments from the temporaries to avoid the problem.

    _step.translate(classGen, methodGen);
    LocalVariableGen stepIteratorTemp =
            methodGen.addLocalVariable("step_pattern_tmp2",
                                       Util.getJCRefType(NODE_ITERATOR_SIG),
                                       null, null);
    stepIteratorTemp.setStart(
            il.append(new ASTORE(stepIteratorTemp.getIndex())));

    il.append(new NEW(cpg.addClass(MATCHING_ITERATOR)));
    il.append(DUP);
    il.append(new ILOAD(match.getIndex()));
    stepIteratorTemp.setEnd(
            il.append(new ALOAD(stepIteratorTemp.getIndex())));
    il.append(new INVOKESPECIAL(index));

    // Get the parent of the matching node
    il.append(methodGen.loadDOM());
    il.append(new ILOAD(match.getIndex()));
    index = cpg.addInterfaceMethodref(DOM_INTF, GET_PARENT, GET_PARENT_SIG);
    il.append(new INVOKEINTERFACE(index, 2));

    // Start the iterator with the parent
    il.append(methodGen.setStartNode());

    // Overwrite current iterator and current node
    il.append(methodGen.storeIterator());
    match.setEnd(il.append(new ILOAD(match.getIndex())));
    il.append(methodGen.storeCurrentNode());

    // Translate the expression of the predicate
    Predicate pred = (Predicate) _predicates.elementAt(0);
    Expression exp = pred.getExpr();
    exp.translateDesynthesized(classGen, methodGen);

    // Backpatch true list and restore current iterator/node
    InstructionHandle restore = il.append(methodGen.storeIterator());
    il.append(methodGen.storeCurrentNode());
    exp.backPatchTrueList(restore);
    BranchHandle skipFalse = il.append(new GOTO(null));

    // Backpatch false list and restore current iterator/node
    restore = il.append(methodGen.storeIterator());
    il.append(methodGen.storeCurrentNode());
    exp.backPatchFalseList(restore);
    _falseList.add(il.append(new GOTO(null)));

    // True list falls through
    skipFalse.setTarget(il.append(NOP));
}
 
Example 17
Source File: TransletOutput.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compile code that opens the give file for output, dumps the contents of
 * the element to the file, then closes the file.
 */
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    final boolean isSecureProcessing = classGen.getParser().getXSLTC()
                                       .isSecureProcessing();

    if (isSecureProcessing) {
        int index = cpg.addMethodref(BASIS_LIBRARY_CLASS,
                                     "unallowed_extension_elementF",
                                     "(Ljava/lang/String;)V");
        il.append(new PUSH(cpg, "redirect"));
        il.append(new INVOKESTATIC(index));
        return;
    }

    // Save the current output handler on the stack
    il.append(methodGen.loadHandler());

    final int open =  cpg.addMethodref(TRANSLET_CLASS,
                                       "openOutputHandler",
                                       "(" + STRING_SIG + "Z)" +
                                       TRANSLET_OUTPUT_SIG);

    final int close =  cpg.addMethodref(TRANSLET_CLASS,
                                        "closeOutputHandler",
                                        "("+TRANSLET_OUTPUT_SIG+")V");

    // Create the new output handler (leave it on stack)
    il.append(classGen.loadTranslet());
    _filename.translate(classGen, methodGen);
    il.append(new PUSH(cpg, _append));
    il.append(new INVOKEVIRTUAL(open));

    // Overwrite current handler
    il.append(methodGen.storeHandler());

    // Translate contents with substituted handler
    translateContents(classGen, methodGen);

    // Close the output handler (close file)
    il.append(classGen.loadTranslet());
    il.append(methodGen.loadHandler());
    il.append(new INVOKEVIRTUAL(close));

    // Restore old output handler from stack
    il.append(methodGen.storeHandler());
}
 
Example 18
Source File: Sort.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compiles code that instantiates a SortingIterator object.
 * This object's constructor needs referencdes to the current iterator
 * and a node sort record producing objects as its parameters.
 */
public static void translateSortIterator(ClassGenerator classGen,
                                  MethodGenerator methodGen,
                                  Expression nodeSet,
                                  Vector<Sort> sortObjects)
{
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // SortingIterator.SortingIterator(NodeIterator,NodeSortRecordFactory);
    final int init = cpg.addMethodref(SORT_ITERATOR, "<init>",
                                      "("
                                      + NODE_ITERATOR_SIG
                                      + NODE_SORT_FACTORY_SIG
                                      + ")V");

    // Backwards branches are prohibited if an uninitialized object is
    // on the stack by section 4.9.4 of the JVM Specification, 2nd Ed.
    // We don't know whether this code might contain backwards branches
    // so we mustn't create the new object until after we've created
    // the suspect arguments to its constructor.  Instead we calculate
    // the values of the arguments to the constructor first, store them
    // in temporary variables, create the object and reload the
    // arguments from the temporaries to avoid the problem.

    LocalVariableGen nodesTemp =
        methodGen.addLocalVariable("sort_tmp1",
                                   Util.getJCRefType(NODE_ITERATOR_SIG),
                                   null, null);

    LocalVariableGen sortRecordFactoryTemp =
        methodGen.addLocalVariable("sort_tmp2",
                                  Util.getJCRefType(NODE_SORT_FACTORY_SIG),
                                  null, null);

    // Get the current node iterator
    if (nodeSet == null) {  // apply-templates default
        final int children = cpg.addInterfaceMethodref(DOM_INTF,
                                                       "getAxisIterator",
                                                       "(I)"+
                                                       NODE_ITERATOR_SIG);
        il.append(methodGen.loadDOM());
        il.append(new PUSH(cpg, Axis.CHILD));
        il.append(new INVOKEINTERFACE(children, 2));
    }
    else {
        nodeSet.translate(classGen, methodGen);
    }

    nodesTemp.setStart(il.append(new ASTORE(nodesTemp.getIndex())));

    // Compile the code for the NodeSortRecord producing class and pass
    // that as the last argument to the SortingIterator constructor.
    compileSortRecordFactory(sortObjects, classGen, methodGen);
    sortRecordFactoryTemp.setStart(
            il.append(new ASTORE(sortRecordFactoryTemp.getIndex())));

    il.append(new NEW(cpg.addClass(SORT_ITERATOR)));
    il.append(DUP);
    nodesTemp.setEnd(il.append(new ALOAD(nodesTemp.getIndex())));
    sortRecordFactoryTemp.setEnd(
            il.append(new ALOAD(sortRecordFactoryTemp.getIndex())));
    il.append(new INVOKESPECIAL(init));
}
 
Example 19
Source File: ResultTreeType.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Expects a result tree on the stack and pushes a boxed result tree.
 * Result trees are already boxed so the translation is just a NOP.
 *
 * @param classGen A BCEL class generator
 * @param methodGen A BCEL method generator
 * @param type An instance of ReferenceType (any)
 * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
 */
public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
                        ReferenceType type) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    if (_methodName == null) {
        il.append(NOP);
    }
    else {
        LocalVariableGen domBuilder, newDom;
        final String className = classGen.getClassName();
        final int current = methodGen.getLocalIndex("current");

        // Push required parameters
        il.append(classGen.loadTranslet());
        if (classGen.isExternal()) {
            il.append(new CHECKCAST(cpg.addClass(className)));
        }
        il.append(methodGen.loadDOM());

        // Create new instance of DOM class (with RTF_INITIAL_SIZE nodes)
        il.append(methodGen.loadDOM());
        int index = cpg.addInterfaceMethodref(DOM_INTF,
                             "getResultTreeFrag",
                             "(IZ)" + DOM_INTF_SIG);
        il.append(new PUSH(cpg, RTF_INITIAL_SIZE));
        il.append(new PUSH(cpg, false));
        il.append(new INVOKEINTERFACE(index,3));
        il.append(DUP);

        // Store new DOM into a local variable
        newDom = methodGen.addLocalVariable("rt_to_reference_dom",
                                            Util.getJCRefType(DOM_INTF_SIG),
                                            null, null);
        il.append(new CHECKCAST(cpg.addClass(DOM_INTF_SIG)));
        newDom.setStart(il.append(new ASTORE(newDom.getIndex())));

        // Overwrite old handler with DOM handler
        index = cpg.addInterfaceMethodref(DOM_INTF,
                             "getOutputDomBuilder",
                             "()" + TRANSLET_OUTPUT_SIG);

        il.append(new INVOKEINTERFACE(index,1));
        //index = cpg.addMethodref(DOM_IMPL,
            //                   "getOutputDomBuilder",
            //                   "()" + TRANSLET_OUTPUT_SIG);
        //il.append(new INVOKEVIRTUAL(index));
        il.append(DUP);
        il.append(DUP);

        // Store DOM handler in a local in order to call endDocument()
        domBuilder =
            methodGen.addLocalVariable("rt_to_reference_handler",
                                       Util.getJCRefType(TRANSLET_OUTPUT_SIG),
                                       null, null);
        domBuilder.setStart(il.append(new ASTORE(domBuilder.getIndex())));

        // Call startDocument on the new handler
        index = cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
                                          "startDocument", "()V");
        il.append(new INVOKEINTERFACE(index, 1));

        // Call the method that implements this result tree
        index = cpg.addMethodref(className,
                                 _methodName,
                                 "("
                                 + DOM_INTF_SIG
                                 + TRANSLET_OUTPUT_SIG
                                 +")V");
        il.append(new INVOKEVIRTUAL(index));

        // Call endDocument on the DOM handler
        domBuilder.setEnd(il.append(new ALOAD(domBuilder.getIndex())));
        index = cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
                                          "endDocument", "()V");
        il.append(new INVOKEINTERFACE(index, 1));

        // Push the new DOM on the stack
        newDom.setEnd(il.append(new ALOAD(newDom.getIndex())));
    }
}
 
Example 20
Source File: IdKeyPattern.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * This method is called when the constructor is compiled in
 * Stylesheet.compileConstructor() and not as the syntax tree is traversed.
 */
public void translate(ClassGenerator classGen,
                      MethodGenerator methodGen) {

    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // Returns the KeyIndex object of a given name
    final int getKeyIndex = cpg.addMethodref(TRANSLET_CLASS,
                                             "getKeyIndex",
                                             "(Ljava/lang/String;)"+
                                             KEY_INDEX_SIG);

    // Initialises a KeyIndex to return nodes with specific values
    final int lookupId = cpg.addMethodref(KEY_INDEX_CLASS,
                                          "containsID",
                                          "(ILjava/lang/Object;)I");
    final int lookupKey = cpg.addMethodref(KEY_INDEX_CLASS,
                                           "containsKey",
                                           "(ILjava/lang/Object;)I");
    final int getNodeIdent = cpg.addInterfaceMethodref(DOM_INTF,
                                                       "getNodeIdent",
                                                       "(I)"+NODE_SIG);

    // Call getKeyIndex in AbstractTranslet with the name of the key
    // to get the index for this key (which is also a node iterator).
    il.append(classGen.loadTranslet());
    il.append(new PUSH(cpg,_index));
    il.append(new INVOKEVIRTUAL(getKeyIndex));

    // Now use the value in the second argument to determine what nodes
    // the iterator should return.
    il.append(SWAP);
    il.append(new PUSH(cpg,_value));
    if (this instanceof IdPattern)
    {
        il.append(new INVOKEVIRTUAL(lookupId));
    }
    else
    {
        il.append(new INVOKEVIRTUAL(lookupKey));
    }

    _trueList.add(il.append(new IFNE(null)));
    _falseList.add(il.append(new GOTO(null)));
}