Example usage for org.aspectj.apache.bcel.classfile Code toString

List of usage examples for org.aspectj.apache.bcel.classfile Code toString

Introduction

In this page you can find the example usage for org.aspectj.apache.bcel.classfile Code toString.

Prototype

@Override
public final String toString() 

Source Link

Usage

From source file:br.jabuti.ui.gui.BytecodePanel.java

License:Open Source License

public BytecodePanel showBytecodePanel(int toOffset) {

    parent = JabutiGUI.mainWindow();/*from  w  w  w . ja  v  a  2s .  c o m*/

    String className = JabutiGUI.getProject().getCurClassName();

    ClassFile cl = JabutiGUI.getProject().getClassFile(className);

    // Empting the content of the text panel
    tp.setText("");

    // UPDATING THE SOURCE COLOR...
    tp.setBackground(ToolConstants.getColor(ToolConstants.COLOR_0));

    Document doc = tp.getStyledDocument();

    SimpleAttributeSet attr = new SimpleAttributeSet();

    //StyleConstants.setFontFamily(attr, "Courier New");
    //StyleConstants.setFontSize(attr, ToolConstants.sourceFontSize);
    StyleConstants.setBackground(attr, ToolConstants.getColor(ToolConstants.COLOR_0));

    JavaClass javaClass = cl.getJavaClass();
    Method[] methods = cl.getMethods();

    // BUILDING THE COLOR PANEL...
    buttonPanel.setVisible(false);
    buttonPanel.removeAll();

    Hashtable colorButtonTable = WeightColor.getColorButtonTable();

    buttonPanel.setLayout(new GridLayout(1, colorButtonTable.size()));

    int[] labels = WeightColor.getColorBarLabels();

    for (int i = 0; i < labels.length; i++) {
        Integer label = new Integer(labels[i]);
        JButton button = new JButton(label.toString());

        int color = ((Integer) colorButtonTable.get(label)).intValue();

        button.setBackground(ToolConstants.getColor(color));

        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setEnabled(false);

        buttonPanel.add(button);
    }
    buttonPanel.setVisible(true);
    add(buttonPanel, BorderLayout.NORTH);

    try {
        // Printing the header of the class file
        BufferedReader strReader = new BufferedReader(new StringReader(javaClass.toString()));
        String nl = System.getProperty("line.separator");

        // Printing the class name
        doc.insertString(doc.getLength(), strReader.readLine() + nl, attr);
        // Printing the class file name
        doc.insertString(doc.getLength(), strReader.readLine() + nl, attr);
        // Printing the source file name
        doc.insertString(doc.getLength(), strReader.readLine() + nl, attr);

        for (int i = 0; i < methods.length; i++) {
            String methodName = methods[i].getName() + methods[i].getSignature();

            // Reseting the Background Color...
            StyleConstants.setBackground(attr, ToolConstants.getColor(ToolConstants.COLOR_0));

            ClassMethod method = cl.getMethod(methodName);

            method.setBeginBytecodeOffset(doc.getEndPosition().getOffset());

            // Printing the name of the method
            doc.insertString(doc.getLength(),
                    nl + nl + method.getMethodGen().getReturnType() + " " + methodName + "\n", attr);

            Code code = methods[i].getCode();

            Hashtable offsetLines = new Hashtable();
            Hashtable offsetColors = new Hashtable();

            if (code != null) {
                strReader = new BufferedReader(new StringReader(code.toString()));
                String line = strReader.readLine();

                while (line != null) {
                    try {
                        int index = line.indexOf(":");

                        if (index > 0) {
                            Integer offset = new Integer(line.substring(0, index));

                            offsetLines.put(offset, line);
                            offsetColors.put(offset, new Integer(0));
                        }
                    } catch (NumberFormatException nfe) {
                    }
                    line = strReader.readLine();
                }
            }

            Hashtable positionTable = null;
            Hashtable classVariables = WeightColor.getClassVariableTable();

            // ALL-USES: Painting only the offset where the
            // variable definition is located
            if (JabutiGUI.isAllPrimaryUses() || JabutiGUI.isAllSecondaryUses()
                    || JabutiGUI.isAllPrimaryPotUses() || JabutiGUI.isAllSecondaryPotUses()) {

                // Position table can be null if
                // the method has no def-use requirement
                Integer mId = new Integer(method.getMethodId());

                // ERROR: remove this above if statement
                // Consequences: if the method has no def-use association a
                // null pointer exception is thrown.
                if (classVariables != null && classVariables.containsKey(mId)) {
                    positionTable = (Hashtable) classVariables.get(mId);

                    if (!SelectedPoint.isSelected()) {
                        // For each defined variable
                        Iterator itPos = positionTable.keySet().iterator();

                        while (itPos.hasNext()) {
                            Integer pos = (Integer) itPos.next();

                            Vector nodeVar = WeightColor.getWeightestVariableFromPosition(method.getMethodId(),
                                    pos);

                            if (nodeVar != null) {
                                GraphNode gn = (GraphNode) nodeVar.elementAt(0);
                                String varDef = (String) nodeVar.elementAt(1);
                                Integer varDefOff = ((CFGNode) gn).getDefinitionOffset(varDef);

                                Integer varDefWgt = WeightColor
                                        .getVariableDefinitionWeight(method.getMethodId(), gn, varDef);
                                Integer varDefColor = new Integer(
                                        WeightColor.getColorByWeight(labels, varDefWgt));

                                // Setting the color of the definition offset
                                if (offsetColors.containsKey(varDefOff)) {
                                    Integer curColor = (Integer) offsetColors.get(varDefOff);

                                    if (curColor.intValue() < varDefColor.intValue()) {
                                        offsetColors.put(varDefOff, varDefColor);
                                    }
                                }
                            }
                        }
                    } else if (SelectedPoint.isSelected()
                            && SelectedPoint.getMethod() == method.getMethodId()) {

                        Integer defOffset = ((CFGNode) SelectedPoint.getNode())
                                .getDefinitionOffset(SelectedPoint.getVariable());
                        Integer varDefColor = (Integer) SelectedPoint
                                .recoverFromNode(ToolConstants.LABEL_COLOR);

                        // Setting the color of the definition offset
                        if (offsetColors.containsKey(defOffset)) {
                            offsetColors.put(defOffset, varDefColor);
                        }

                        System.out.println("Selected method: " + SelectedPoint.getMethod());
                        System.out.println("Selected node: " + SelectedPoint.getNode());
                        System.out.println("Selected variable: " + SelectedPoint.getVariable());

                        // Getting all uses of the selected definition
                        Hashtable defUseTable = (Hashtable) WeightColor.getClassWeights()
                                .get(new Integer(SelectedPoint.getMethod()));
                        Hashtable defTable = (Hashtable) defUseTable.get(SelectedPoint.getNode());
                        Hashtable useTable = (Hashtable) defTable.get(SelectedPoint.getVariable());

                        Iterator itUse = useTable.keySet().iterator();

                        while (itUse.hasNext()) {
                            DefUse du = (DefUse) itUse.next();

                            System.out.println("DEF-USE: " + du);

                            Integer useWgt = (Integer) useTable.get(du);

                            System.out.println("\tDU weight: " + useWgt);

                            int useColor = WeightColor.getColorByWeight(labels, useWgt);

                            // C-Use color....
                            GraphNode gnUse = method.getGraphNodeByLabel(du.getUseFrom());
                            Integer useOffset = ((CFGNode) gnUse).getUseOffset(du.getVar());

                            System.out.println("\tUse node: " + gnUse);
                            System.out.println("\tUse offset: " + useOffset);

                            if (offsetColors.containsKey(useOffset)) {
                                Integer curColor = (Integer) offsetColors.get(useOffset);

                                if (curColor.intValue() < useColor) {
                                    offsetColors.put(useOffset, new Integer(useColor));
                                }
                            }

                            // If p-use, change the color of the entire second node
                            String useLabel = du.getUseTo();

                            if (useLabel != null) {
                                System.out.println("CHANGING THE COLOR OF NODE: " + useLabel);
                                GraphNode gn = method.getGraphNodeByLabel(useLabel);
                                int c = ((Integer) gn.getUserData(ToolConstants.LABEL_COLOR)).intValue();

                                System.out.println("Current color: " + c);

                                InstructionHandle ih = br.jabuti.util.InstructCtrl
                                        .findInstruction(method.getMethodGen(), ((CFGNode) gn).getStart());
                                String inst = ih.toString();
                                int index = inst.indexOf(":");
                                Integer offset = new Integer(inst.substring(0, index).trim());

                                if (offsetColors.containsKey(offset)) {
                                    int cNumber = ((Integer) offsetColors.get(offset)).intValue();

                                    if (cNumber < c) {
                                        offsetColors.put(offset, new Integer(c));
                                    }
                                }

                                while (ih.getPosition() != ((CFGNode) gn).getEnd()) {
                                    ih = ih.getNext();
                                    inst = ih.toString();
                                    index = inst.indexOf(":");
                                    offset = new Integer(inst.substring(0, index).trim());
                                    if (offsetColors.containsKey(offset)) {
                                        int cNumber = ((Integer) offsetColors.get(offset)).intValue();

                                        if (cNumber < c) {
                                            offsetColors.put(offset, new Integer(c));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                CFG cfg = method.getCFG();
                GraphNode[] fdt = cfg.findDFTNodes(true);

                int c = 0;

                /*
                 Hashtable defUseTable = null;
                        
                 if (allUsesCriterion.isSelected()) {
                 defUseTable = (Hashtable) classWeights.get(new Integer(method.getMethodId()));
                 }
                 */

                for (int x = 0; x < fdt.length; x++) {

                    GraphNode gn = fdt[x];

                    // Getting the color associated with this node
                    Integer weightColor = (Integer) gn.getUserData(ToolConstants.LABEL_COLOR);

                    if (weightColor != null) {
                        c = weightColor.intValue();
                    } else {
                        c = 0;
                    }

                    // ALL-EDGES: Painting only the decision bytecode instruction 
                    // when a decision node is selected
                    if ((JabutiGUI.isAllPrimaryEdges() || JabutiGUI.isAllSecondaryEdges())
                            && ((!SelectedPoint.isSelected()) || (SelectedPoint.isSelected()
                                    && gn.getLabel().equals(SelectedPoint.getNodeLabel())))) {
                        InstructionHandle ih = br.jabuti.util.InstructCtrl
                                .findInstruction(method.getMethodGen(), ((CFGNode) gn).getEnd());
                        String inst = ih.toString();
                        int index = inst.indexOf(":");
                        Integer offset = new Integer(inst.substring(0, index).trim());

                        if (offsetColors.containsKey(offset)) {
                            int cNumber = ((Integer) offsetColors.get(offset)).intValue();

                            if (cNumber < c) {
                                offsetColors.put(offset, new Integer(c));
                            }
                        }
                    } else {
                        InstructionHandle ih = br.jabuti.util.InstructCtrl
                                .findInstruction(method.getMethodGen(), ((CFGNode) gn).getStart());
                        String inst = ih.toString();
                        int index = inst.indexOf(":");
                        Integer offset = new Integer(inst.substring(0, index).trim());

                        if (offsetColors.containsKey(offset)) {
                            int cNumber = ((Integer) offsetColors.get(offset)).intValue();

                            if (cNumber < c) {
                                offsetColors.put(offset, new Integer(c));
                            }
                        }

                        while (ih.getPosition() != ((CFGNode) gn).getEnd()) {
                            ih = ih.getNext();
                            inst = ih.toString();
                            index = inst.indexOf(":");
                            offset = new Integer(inst.substring(0, index).trim());
                            if (offsetColors.containsKey(offset)) {
                                int cNumber = ((Integer) offsetColors.get(offset)).intValue();

                                if (cNumber < c) {
                                    offsetColors.put(offset, new Integer(c));
                                }
                            }
                        }
                    }
                }
            }

            // Printing the code, independently of the tsting
            // criterion                    

            Object[] orderedInstr = offsetLines.keySet().toArray();

            Arrays.sort(orderedInstr);
            for (int x = 0; x < orderedInstr.length; x++) {
                Integer offset = (Integer) orderedInstr[x];

                String line = (String) offsetLines.get(offset);

                int c;

                if (offsetColors.containsKey(offset)) {
                    c = ((Integer) offsetColors.get(offset)).intValue();
                } else {
                    c = 0;
                }
                StyleConstants.setBackground(attr, ToolConstants.getColor(c));
                doc.insertString(doc.getLength(), line + nl, attr);

                // To print the code in the standard output...
                // System.out.println( line );
            }
            method.setEndBytecodeOffset(doc.getEndPosition().getOffset());
        }
    } catch (Exception e) {
        ToolConstants.reportException(e, ToolConstants.STDERR);
    }

    scrollPane.setViewportView(tp);

    setCaret(toOffset);

    int row = tp.getStyledDocument().getDefaultRootElement().getElementIndex(tp.getCaretPosition());
    int end = tp.getStyledDocument().getDefaultRootElement().getElementIndex(tp.getDocument().getLength());

    String criterion = parent.getActiveCriterionName();

    ssp.setContent("File: " + className, "Line: " + (row + 1) + " of " + (end + 1), "Coverage: " + criterion,
            "Highlighting: All Priorized");
    ssp.setVisible(true);

    return this;
}