Example usage for org.aspectj.apache.bcel.classfile LineNumberTable getSourceLine

List of usage examples for org.aspectj.apache.bcel.classfile LineNumberTable getSourceLine

Introduction

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

Prototype

public int getSourceLine(int pos) 

Source Link

Document

Map byte code positions to source code lines.

Usage

From source file:br.jabuti.graph.view.gvf.GVFDrawPanel.java

License:Open Source License

public void mouseMoved(MouseEvent evt) {
    tip = null;// w w  w .  j a v a 2s. com
    boolean found = false;

    for (Enumeration e = vNodes.elements(); e.hasMoreElements();) {
        GVFNode newNode = (GVFNode) e.nextElement();

        if (newNode.itsMe(evt.getX(), evt.getY())) {
            tip = newNode.getGraphNode().toString();

            // Adding the corresponding source line numbers to
            // the GraphNode information...

            LineNumberTable lnTable = newNode.getMethodGen().getLineNumberTable(newNode.getConstantPoolGen());

            tip += "Corresponding Source Lines:\n";

            InstructionHandle ih = br.jabuti.util.InstructCtrl.findInstruction(newNode.getMethodGen(),
                    ((CFGNode) newNode.getGraphNode()).getStart());
            int srcLine = lnTable.getSourceLine(ih.getPosition());
            HashSet srcLines = new HashSet();

            srcLines.add(new Integer(srcLine));
            while (ih.getPosition() != ((CFGNode) newNode.getGraphNode()).getEnd()) {
                ih = ih.getNext();
                srcLine = lnTable.getSourceLine(ih.getPosition());
                srcLines.add(new Integer(srcLine));
            }
            Object[] lines = srcLines.toArray();

            Arrays.sort(lines);

            tip += "\t" + lines[0];
            for (int i = 1; i < lines.length; i++) {
                tip += ", " + lines[i];
            }
            tip += "\n";

            tipX = newNode.getX() + 30;
            tipY = newNode.getY();
            reDraw();
            found = true;
        }
    }
    if (!found) {
        reDraw();
    }
}

From source file:br.jabuti.project.ClassMethod.java

License:Open Source License

public int getBeginSourceLine() {
    if (beginSrcLine == -1) {
        beginSrcLine = 0;/*  w w w.  ja v  a 2  s .  c om*/

        try {
            CFG cfg = getCFG();
            GraphNode[] fdt = cfg.findDFTNodes(true);

            if (fdt.length > 0) {
                GraphNode gn = fdt[0];

                int begin = ((CFGNode) gn).getStart();
                LineNumberTable lnTable = mg.getLineNumberTable(cp);

                beginSrcLine = lnTable.getSourceLine(begin);
                // Decrementing two lines trying to
                // get the method header
                if (beginSrcLine > 1) {
                    beginSrcLine -= 2;
                }
            }
        } catch (Exception e) {
            beginSrcLine = 0;
            ToolConstants.reportException(e, ToolConstants.STDERR);
        }
    }
    return beginSrcLine;
}

From source file:br.jabuti.project.ClassMethod.java

License:Open Source License

public int getEndSourceLine() {
    if (endSrcLine == -1) {
        endSrcLine = 0;/*from www.  j av a  2  s. c om*/

        try {
            CFG cfg = getCFG();
            GraphNode[] fdt = cfg.findDFTNodes(true);

            LineNumberTable lnTable = mg.getLineNumberTable(cp);

            for (int i = 0; i < fdt.length; i++) {
                GraphNode gn = fdt[i];

                int end = ((CFGNode) gn).getEnd();
                int line = lnTable.getSourceLine(end);

                if (endSrcLine < line) {
                    endSrcLine = line;
                }
            }
        } catch (Exception e) {
            endSrcLine = 0;
            ToolConstants.reportException(e, ToolConstants.STDERR);
        }
    }
    return endSrcLine;
}

From source file:br.jabuti.project.ClassMethod.java

License:Open Source License

public int bytecodeOffset2SourceLine(int byteOffset) {
    int srcLine = -1;

    /*/*ww  w  .ja v a2  s .  c o m*/
     InstructionList instList = mg.getInstructionList();
     InstructionHandle handle = instList.findHandle( byteOffset );
     */
    LineNumberTable lnTable = mg.getLineNumberTable(cp);

    srcLine = lnTable.getSourceLine(byteOffset);

    return srcLine;
}

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

License:Open Source License

public SourcePanel showSourcePanel(int toOffset) {

    parent = JabutiGUI.mainWindow();//w  ww .  j a  v a  2 s .  c o  m

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

    ClassFile cl = prj.getClassFile(className);
    ClassSourceFile src = cl.getSourceFile();

    tp.setText("");

    BufferedReader buffer = null;

    if (src.exists()) {
        buffer = src.getSourceCode();

        lineNodeTable = new Hashtable();

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

        Document doc = tp.getStyledDocument();
        String nl = System.getProperty("line.separator");

        SimpleAttributeSet attr = new SimpleAttributeSet();

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

        Method[] methods = cl.getMethods();

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

        Hashtable colorButtonTable = WeightColor.getColorButtonTable();

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

        Object[] keySet = colorButtonTable.keySet().toArray();

        Arrays.sort(keySet);

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

            int color = ((Integer) colorButtonTable.get((Integer) keySet[i])).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);

        if (buffer != null) {
            try {
                Vector sourceColor = new Vector();

                // Inserting something at position 0
                sourceColor.add(new Integer(ToolConstants.COLOR_0));

                // The code starts at position 1
                String line = buffer.readLine();

                while (line != null) {
                    sourceColor.add(new Integer(ToolConstants.COLOR_0));
                    line = buffer.readLine();
                }

                int[] labels = null;

                if (JabutiGUI.isAllPrimaryUses() || JabutiGUI.isAllSecondaryUses()
                        || JabutiGUI.isAllPrimaryPotUses() || JabutiGUI.isAllSecondaryPotUses()) {
                    labels = WeightColor.getColorBarLabels();
                }

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

                    ClassMethod method = cl.getMethod(methodName);

                    // Line number table to map bytecode offset into
                    // source code line...
                    LineNumberTable lnTable = method.getMethodGen()
                            .getLineNumberTable(method.getConstantPoolGen());

                    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) WeightColor.getClassVariableTable().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 varDefWgt = WeightColor
                                                .getVariableDefinitionWeight(method.getMethodId(), gn, varDef);
                                        Integer varDefColor = new Integer(
                                                WeightColor.getColorByWeight(labels, varDefWgt));

                                        System.out.println("MID " + mId + " name : " + method.getMethodName());

                                        System.out.println("Var Weight " + varDefWgt);
                                        System.out.println("Var Color " + varDefColor);
                                        System.out.println("Var Por " + pos);

                                        // TO HANDLE ASPECT ORIENTED PROGRAMS
                                        if (pos != null && pos.intValue() >= 0)
                                            sourceColor.setElementAt(varDefColor, pos.intValue());
                                    }
                                }
                            } else if (SelectedPoint.isSelected()
                                    && SelectedPoint.getMethod() == method.getMethodId()) {

                                Integer defOffset = ((CFGNode) SelectedPoint.getNode())
                                        .getDefinitionOffset(SelectedPoint.getVariable());
                                int defSrcLine = method.bytecodeOffset2SourceLine(defOffset.intValue());

                                Integer varDefColor = (Integer) SelectedPoint
                                        .recoverFromNode(ToolConstants.LABEL_COLOR);

                                // TO HANDLE ASPECT ORIENTED PROGRAMS
                                if (defSrcLine >= 0)
                                    // Painting the definition node
                                    sourceColor.setElementAt(varDefColor, defSrcLine);

                                // 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();
                                    Integer useWgt = (Integer) useTable.get(du);

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

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

                                    // TO HANDLE ASPECT ORIENTED PROGRAMS
                                    if (useLine >= 0) {
                                        Integer cor = (Integer) sourceColor.elementAt(useLine);

                                        if (cor.intValue() < useColor) {
                                            sourceColor.setElementAt(new Integer(useColor), useLine);
                                        }

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

                                        if (useLabel != null) {
                                            GraphNode gn = method.getGraphNodeByLabel(useLabel);
                                            int c = ((Integer) gn.getUserData(ToolConstants.LABEL_COLOR))
                                                    .intValue();

                                            InstructionHandle ih = br.jabuti.util.InstructCtrl.findInstruction(
                                                    method.getMethodGen(), ((CFGNode) gn).getStart());
                                            int srcLine = lnTable.getSourceLine(ih.getPosition());

                                            // TO HANDLE ASPECT ORIENTED PROGRAMS
                                            if (srcLine >= 0) {
                                                cor = (Integer) sourceColor.elementAt(srcLine);

                                                if (cor.intValue() < c) {
                                                    sourceColor.setElementAt(new Integer(c), srcLine);
                                                }

                                                while (ih.getPosition() != ((CFGNode) gn).getEnd()) {
                                                    ih = ih.getNext();
                                                    srcLine = lnTable.getSourceLine(ih.getPosition());
                                                    cor = (Integer) sourceColor.elementAt(srcLine);
                                                    if (cor.intValue() < c) {
                                                        sourceColor.setElementAt(new Integer(c), srcLine);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        // Traversing the CFG looking for the color of each node...
                        CFG cfg = method.getCFG();
                        GraphNode[] fdt = cfg.findDFTNodes(true);

                        int c = 0;

                        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 ((!SelectedPoint.isSelected()
                                    && (JabutiGUI.isAllPrimaryEdges() || JabutiGUI.isAllSecondaryEdges()))
                                    || (SelectedPoint.isSelected()
                                            && gn.getLabel().equals(SelectedPoint.getNodeLabel()))) {
                                InstructionHandle ih = br.jabuti.util.InstructCtrl
                                        .findInstruction(method.getMethodGen(), ((CFGNode) gn).getEnd());
                                int srcLine = lnTable.getSourceLine(ih.getPosition());

                                // TO HANDLE ASPECT ORIENTED PROGRAMS
                                if (srcLine >= 0) {
                                    int cor = ((Integer) sourceColor.elementAt(srcLine)).intValue();

                                    if (cor < c) {
                                        Integer nc = new Integer(c);
                                        Integer ln = new Integer(srcLine);

                                        sourceColor.setElementAt(nc, srcLine);
                                        lineNodeTable.put(ln, gn.getLabel());
                                        System.out.println("LINE " + ln + " NODE: " + gn.getLabel());
                                    }
                                }
                                // ALL-NODES: Painting all offset of a given CFG Node
                            } else {
                                InstructionHandle ih = br.jabuti.util.InstructCtrl
                                        .findInstruction(method.getMethodGen(), ((CFGNode) gn).getStart());
                                //System.out.println( "METHOD ATUAL: " + method.getMethodName() );

                                int srcLine = -1;
                                try {
                                    srcLine = lnTable.getSourceLine(ih.getPosition());
                                    int cor = ((Integer) sourceColor.elementAt(srcLine)).intValue();

                                    if (cor < c) {
                                        sourceColor.setElementAt(new Integer(c), srcLine);
                                    }
                                } catch (ArrayIndexOutOfBoundsException aobe) {
                                    //System.out.println("Exceo gerada");                           
                                }
                                //System.out.println( "\tBytecode position: " + ih.getPosition() );
                                //System.out.println( "\tSource line position: " + srcLine );
                                // TO HANDLE ASPECT ORIENTED PROGRAMS
                                while (ih.getPosition() != ((CFGNode) gn).getEnd()) {
                                    ih = ih.getNext();
                                    try {
                                        srcLine = lnTable.getSourceLine(ih.getPosition());
                                        int cor = ((Integer) sourceColor.elementAt(srcLine)).intValue();
                                        if (cor < c) {
                                            sourceColor.setElementAt(new Integer(c), srcLine);
                                        }
                                    } catch (ArrayIndexOutOfBoundsException aobe) {
                                        //System.out.println("Exceo gerada");                           
                                    }
                                    //System.out.println( "\tBytecode position: " + ih.getPosition() );
                                    //System.out.println( "\tSource line position: " + srcLine );
                                }
                            }
                        }
                    }
                }

                // Printing and painting the source code...
                buffer = src.getSourceCode();

                line = buffer.readLine();
                // The code starts at position 1
                int curLine = 1;

                while (line != null) {
                    int cor = ((Integer) sourceColor.elementAt(curLine)).intValue();

                    StyleConstants.setBackground(attr, ToolConstants.getColor(cor));

                    String lineNumber = new String("/* " + ToolConstants.getFourDigitNumber(curLine) + " */ ");

                    doc.insertString(doc.getLength(), lineNumber + line + nl, attr);

                    // To print the code in the standard output...
                    // System.out.println( lineNumber + line );

                    line = buffer.readLine();
                    curLine++;
                }
            } catch (Exception e) {
                ToolConstants.reportException(e, ToolConstants.STDERR);
            }
        }

        scrollPane.setViewportView(tp);

        setCaretByLine(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);
    } else {
        // buffer = cl.getBytecode();
        JOptionPane.showMessageDialog(null,
                "No source file available for the selected class file: " + className, "Warning...",
                JOptionPane.WARNING_MESSAGE);
    }
    return this;
}