VerifyDialog.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */

  17. package org.apache.bcel.verifier;

  18. import java.awt.Color;
  19. import java.awt.Dialog;
  20. import java.awt.Frame;
  21. import java.awt.SystemColor;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import java.awt.event.WindowAdapter;
  25. import java.awt.event.WindowEvent;

  26. import javax.swing.JButton;
  27. import javax.swing.JDialog;
  28. import javax.swing.JPanel;

  29. import org.apache.bcel.Repository;
  30. import org.apache.bcel.classfile.JavaClass;
  31. import org.apache.bcel.classfile.Utility;

  32. /**
  33.  * A class for simple graphical class file verification. Use the main(String []) method with fully qualified class names
  34.  * as arguments to use it as a stand-alone application. Use the VerifyDialog(String) constructor to use this class in
  35.  * your application. [This class was created using VisualAge for Java, but it does not work under VAJ itself (Version
  36.  * 3.02 JDK 1.2)]
  37.  *
  38.  * @see #main(String[])
  39.  * @see #VerifyDialog(String)
  40.  */
  41. public class VerifyDialog extends JDialog {

  42.     /** Machine-generated, made final. */
  43.     final class IvjEventHandler implements ActionListener {

  44.         @Override
  45.         public void actionPerformed(final ActionEvent e) {
  46.             if (e.getSource() == getPass1Button()) {
  47.                 connEtoC1(e);
  48.             }
  49.             if (e.getSource() == getPass2Button()) {
  50.                 connEtoC2(e);
  51.             }
  52.             if (e.getSource() == getPass3Button()) {
  53.                 connEtoC3(e);
  54.             }
  55.             if (e.getSource() == getFlushButton()) {
  56.                 connEtoC4(e);
  57.             }
  58.         }
  59.     }

  60.     private static final long serialVersionUID = -6374807677043142313L;

  61.     /**
  62.      * This field is here to count the number of open VerifyDialog instances so the JVM can be exited afer every Dialog had
  63.      * been closed.
  64.      */
  65.     private static int classesToVerify;

  66.     /**
  67.      * Verifies one or more class files. Verification results are presented graphically: Red means 'rejected', green means
  68.      * 'passed' while yellow means 'could not be verified yet'.
  69.      *
  70.      * @param args String[] fully qualified names of classes to verify.
  71.      */
  72.     public static void main(final String[] args) {
  73.         classesToVerify = args.length;
  74.         for (final String arg : args) {
  75.             try {
  76.                 final VerifyDialog aVerifyDialog;
  77.                 aVerifyDialog = new VerifyDialog(arg);
  78.                 aVerifyDialog.setModal(true);
  79.                 aVerifyDialog.addWindowListener(new WindowAdapter() {

  80.                     @Override
  81.                     public void windowClosing(final WindowEvent e) {
  82.                         classesToVerify--;
  83.                         if (classesToVerify == 0) {
  84.                             System.exit(0);
  85.                         }
  86.                     }
  87.                 });
  88.                 aVerifyDialog.setVisible(true);
  89.             } catch (final Throwable exception) {
  90.                 System.err.println("Exception occurred in main() of JDialog");
  91.                 exception.printStackTrace(System.out);
  92.             }
  93.         }
  94.     }

  95.     /** Machine-generated. */
  96.     private JPanel ivjJDialogContentPane;
  97.     /** Machine-generated. */
  98.     private JPanel ivjPass1Panel;
  99.     /** Machine-generated. */
  100.     private JPanel ivjPass2Panel;
  101.     /** Machine-generated. */
  102.     private JPanel ivjPass3Panel;
  103.     /** Machine-generated. */
  104.     private JButton ivjPass1Button;
  105.     /** Machine-generated. */
  106.     private JButton ivjPass2Button;
  107.     /** Machine-generated. */
  108.     private JButton ivjPass3Button;

  109.     /** Machine-generated. */
  110.     private final IvjEventHandler ivjEventHandler = new IvjEventHandler();

  111.     /**
  112.      * The class to verify. Default set to 'java.lang.Object' in case this class is instantiated via one of the many
  113.      * machine-generated constructors.
  114.      */
  115.     private String className = "java.lang.Object";

  116.     /** Machine-generated. */
  117.     private JButton ivjFlushButton;

  118.     /** Machine-generated. */
  119.     public VerifyDialog() {
  120.         initialize();
  121.     }

  122.     /** Machine-generated. */
  123.     public VerifyDialog(final Dialog owner) {
  124.         super(owner);
  125.     }

  126.     /** Machine-generated. */
  127.     public VerifyDialog(final Dialog owner, final boolean modal) {
  128.         super(owner, modal);
  129.     }

  130.     /** Machine-generated. */
  131.     public VerifyDialog(final Dialog owner, final String title) {
  132.         super(owner, title);
  133.     }

  134.     /** Machine-generated. */
  135.     public VerifyDialog(final Dialog owner, final String title, final boolean modal) {
  136.         super(owner, title, modal);
  137.     }

  138.     /** Machine-generated. */
  139.     public VerifyDialog(final Frame owner) {
  140.         super(owner);
  141.     }

  142.     /** Machine-generated. */
  143.     public VerifyDialog(final Frame owner, final boolean modal) {
  144.         super(owner, modal);
  145.     }

  146.     /** Machine-generated. */
  147.     public VerifyDialog(final Frame owner, final String title) {
  148.         super(owner, title);
  149.     }

  150.     /** Machine-generated. */
  151.     public VerifyDialog(final Frame owner, final String title, final boolean modal) {
  152.         super(owner, title, modal);
  153.     }

  154.     /**
  155.      * Use this constructor if you want a possibility to verify other class files than {@link Object}.
  156.      *
  157.      * @param fullyQualifiedClassName "java.lang.String"
  158.      */
  159.     public VerifyDialog(String fullyQualifiedClassName) {
  160.         final int dotclasspos = fullyQualifiedClassName.lastIndexOf(JavaClass.EXTENSION);
  161.         if (dotclasspos != -1) {
  162.             fullyQualifiedClassName = fullyQualifiedClassName.substring(0, dotclasspos);
  163.         }
  164.         fullyQualifiedClassName = Utility.pathToPackage(fullyQualifiedClassName);
  165.         this.className = fullyQualifiedClassName;
  166.         initialize();
  167.     }

  168.     /** Machine-generated. */
  169.     private void connEtoC1(final ActionEvent arg1) {
  170.         try {
  171.             // user code begin {1}
  172.             // user code end
  173.             pass1Button_ActionPerformed(arg1);
  174.             // user code begin {2}
  175.             // user code end
  176.         } catch (final Throwable ivjExc) {
  177.             // user code begin {3}
  178.             // user code end
  179.             handleException(ivjExc);
  180.         }
  181.     }

  182.     /** Machine-generated. */
  183.     private void connEtoC2(final ActionEvent arg1) {
  184.         try {
  185.             // user code begin {1}
  186.             // user code end
  187.             pass2Button_ActionPerformed(arg1);
  188.             // user code begin {2}
  189.             // user code end
  190.         } catch (final Throwable ivjExc) {
  191.             // user code begin {3}
  192.             // user code end
  193.             handleException(ivjExc);
  194.         }
  195.     }

  196.     /** Machine-generated. */
  197.     private void connEtoC3(final ActionEvent arg1) {
  198.         try {
  199.             // user code begin {1}
  200.             // user code end
  201.             pass4Button_ActionPerformed(arg1);
  202.             // user code begin {2}
  203.             // user code end
  204.         } catch (final Throwable ivjExc) {
  205.             // user code begin {3}
  206.             // user code end
  207.             handleException(ivjExc);
  208.         }
  209.     }

  210.     /** Machine-generated. */
  211.     private void connEtoC4(final ActionEvent arg1) {
  212.         try {
  213.             // user code begin {1}
  214.             // user code end
  215.             flushButton_ActionPerformed(arg1);
  216.             // user code begin {2}
  217.             // user code end
  218.         } catch (final Throwable ivjExc) {
  219.             // user code begin {3}
  220.             // user code end
  221.             handleException(ivjExc);
  222.         }
  223.     }

  224.     /** Machine-generated. */
  225.     public void flushButton_ActionPerformed(final ActionEvent actionEvent) {
  226.         VerifierFactory.getVerifier(className).flush();
  227.         Repository.removeClass(className); // Make sure it will be reloaded.
  228.         getPass1Panel().setBackground(Color.gray);
  229.         getPass1Panel().repaint();
  230.         getPass2Panel().setBackground(Color.gray);
  231.         getPass2Panel().repaint();
  232.         getPass3Panel().setBackground(Color.gray);
  233.         getPass3Panel().repaint();
  234.     }

  235.     /** Machine-generated. */
  236.     private JButton getFlushButton() {
  237.         if (ivjFlushButton == null) {
  238.             try {
  239.                 ivjFlushButton = new JButton();
  240.                 ivjFlushButton.setName("FlushButton");
  241.                 ivjFlushButton.setText("Flush: Forget old verification results");
  242.                 ivjFlushButton.setBackground(SystemColor.controlHighlight);
  243.                 ivjFlushButton.setBounds(60, 215, 300, 30);
  244.                 ivjFlushButton.setForeground(Color.red);
  245.                 ivjFlushButton.setActionCommand("FlushButton");
  246.                 // user code begin {1}
  247.                 // user code end
  248.             } catch (final Throwable ivjExc) {
  249.                 // user code begin {2}
  250.                 // user code end
  251.                 handleException(ivjExc);
  252.             }
  253.         }
  254.         return ivjFlushButton;
  255.     }

  256.     /** Machine-generated. */
  257.     private JPanel getJDialogContentPane() {
  258.         if (ivjJDialogContentPane == null) {
  259.             try {
  260.                 ivjJDialogContentPane = new JPanel();
  261.                 ivjJDialogContentPane.setName("JDialogContentPane");
  262.                 ivjJDialogContentPane.setLayout(null);
  263.                 getJDialogContentPane().add(getPass1Panel(), getPass1Panel().getName());
  264.                 getJDialogContentPane().add(getPass3Panel(), getPass3Panel().getName());
  265.                 getJDialogContentPane().add(getPass2Panel(), getPass2Panel().getName());
  266.                 getJDialogContentPane().add(getPass1Button(), getPass1Button().getName());
  267.                 getJDialogContentPane().add(getPass2Button(), getPass2Button().getName());
  268.                 getJDialogContentPane().add(getPass3Button(), getPass3Button().getName());
  269.                 getJDialogContentPane().add(getFlushButton(), getFlushButton().getName());
  270.                 // user code begin {1}
  271.                 // user code end
  272.             } catch (final Throwable ivjExc) {
  273.                 // user code begin {2}
  274.                 // user code end
  275.                 handleException(ivjExc);
  276.             }
  277.         }
  278.         return ivjJDialogContentPane;
  279.     }

  280.     /** Machine-generated. */
  281.     private JButton getPass1Button() {
  282.         if (ivjPass1Button == null) {
  283.             try {
  284.                 ivjPass1Button = new JButton();
  285.                 ivjPass1Button.setName("Pass1Button");
  286.                 ivjPass1Button.setText("Pass1: Verify binary layout of .class file");
  287.                 ivjPass1Button.setBackground(SystemColor.controlHighlight);
  288.                 ivjPass1Button.setBounds(100, 40, 300, 30);
  289.                 ivjPass1Button.setActionCommand("Button1");
  290.                 // user code begin {1}
  291.                 // user code end
  292.             } catch (final Throwable ivjExc) {
  293.                 // user code begin {2}
  294.                 // user code end
  295.                 handleException(ivjExc);
  296.             }
  297.         }
  298.         return ivjPass1Button;
  299.     }

  300.     /** Machine-generated. */
  301.     private JPanel getPass1Panel() {
  302.         if (ivjPass1Panel == null) {
  303.             try {
  304.                 ivjPass1Panel = new JPanel();
  305.                 ivjPass1Panel.setName("Pass1Panel");
  306.                 ivjPass1Panel.setLayout(null);
  307.                 ivjPass1Panel.setBackground(SystemColor.controlShadow);
  308.                 ivjPass1Panel.setBounds(30, 30, 50, 50);
  309.                 // user code begin {1}
  310.                 // user code end
  311.             } catch (final Throwable ivjExc) {
  312.                 // user code begin {2}
  313.                 // user code end
  314.                 handleException(ivjExc);
  315.             }
  316.         }
  317.         return ivjPass1Panel;
  318.     }

  319.     /** Machine-generated. */
  320.     private JButton getPass2Button() {
  321.         if (ivjPass2Button == null) {
  322.             try {
  323.                 ivjPass2Button = new JButton();
  324.                 ivjPass2Button.setName("Pass2Button");
  325.                 ivjPass2Button.setText("Pass 2: Verify static .class file constraints");
  326.                 ivjPass2Button.setBackground(SystemColor.controlHighlight);
  327.                 ivjPass2Button.setBounds(100, 100, 300, 30);
  328.                 ivjPass2Button.setActionCommand("Button2");
  329.                 // user code begin {1}
  330.                 // user code end
  331.             } catch (final Throwable ivjExc) {
  332.                 // user code begin {2}
  333.                 // user code end
  334.                 handleException(ivjExc);
  335.             }
  336.         }
  337.         return ivjPass2Button;
  338.     }

  339.     /** Machine-generated. */
  340.     private JPanel getPass2Panel() {
  341.         if (ivjPass2Panel == null) {
  342.             try {
  343.                 ivjPass2Panel = new JPanel();
  344.                 ivjPass2Panel.setName("Pass2Panel");
  345.                 ivjPass2Panel.setLayout(null);
  346.                 ivjPass2Panel.setBackground(SystemColor.controlShadow);
  347.                 ivjPass2Panel.setBounds(30, 90, 50, 50);
  348.                 // user code begin {1}
  349.                 // user code end
  350.             } catch (final Throwable ivjExc) {
  351.                 // user code begin {2}
  352.                 // user code end
  353.                 handleException(ivjExc);
  354.             }
  355.         }
  356.         return ivjPass2Panel;
  357.     }

  358.     /** Machine-generated. */
  359.     private JButton getPass3Button() {
  360.         if (ivjPass3Button == null) {
  361.             try {
  362.                 ivjPass3Button = new JButton();
  363.                 ivjPass3Button.setName("Pass3Button");
  364.                 ivjPass3Button.setText("Passes 3a+3b: Verify code arrays");
  365.                 ivjPass3Button.setBackground(SystemColor.controlHighlight);
  366.                 ivjPass3Button.setBounds(100, 160, 300, 30);
  367.                 ivjPass3Button.setActionCommand("Button2");
  368.                 // user code begin {1}
  369.                 // user code end
  370.             } catch (final Throwable ivjExc) {
  371.                 // user code begin {2}
  372.                 // user code end
  373.                 handleException(ivjExc);
  374.             }
  375.         }
  376.         return ivjPass3Button;
  377.     }

  378.     /** Machine-generated. */
  379.     private JPanel getPass3Panel() {
  380.         if (ivjPass3Panel == null) {
  381.             try {
  382.                 ivjPass3Panel = new JPanel();
  383.                 ivjPass3Panel.setName("Pass3Panel");
  384.                 ivjPass3Panel.setLayout(null);
  385.                 ivjPass3Panel.setBackground(SystemColor.controlShadow);
  386.                 ivjPass3Panel.setBounds(30, 150, 50, 50);
  387.                 // user code begin {1}
  388.                 // user code end
  389.             } catch (final Throwable ivjExc) {
  390.                 // user code begin {2}
  391.                 // user code end
  392.                 handleException(ivjExc);
  393.             }
  394.         }
  395.         return ivjPass3Panel;
  396.     }

  397.     /** Machine-generated. */
  398.     private void handleException(final Throwable exception) {
  399.         /* Uncomment the following lines to print uncaught exceptions to stdout */
  400.         System.out.println("--------- UNCAUGHT EXCEPTION ---------");
  401.         exception.printStackTrace(System.out);
  402.         // manually added code
  403.         if (exception instanceof ThreadDeath) {
  404.             throw (ThreadDeath) exception;
  405.         }
  406.         if (exception instanceof VirtualMachineError) {
  407.             throw (VirtualMachineError) exception;
  408.         }
  409.     }

  410.     /** Machine-generated. */
  411.     private void initConnections() {
  412.         // user code begin {1}
  413.         // user code end
  414.         getPass1Button().addActionListener(ivjEventHandler);
  415.         getPass2Button().addActionListener(ivjEventHandler);
  416.         getPass3Button().addActionListener(ivjEventHandler);
  417.         getFlushButton().addActionListener(ivjEventHandler);
  418.     }

  419.     /** Machine-generated. */
  420.     private void initialize() {
  421.         try {
  422.             // user code begin {1}
  423.             // user code end
  424.             setName("VerifyDialog");
  425.             setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  426.             setSize(430, 280);
  427.             setVisible(true);
  428.             setModal(true);
  429.             setResizable(false);
  430.             setContentPane(getJDialogContentPane());
  431.             initConnections();
  432.         } catch (final Throwable ivjExc) {
  433.             handleException(ivjExc);
  434.         }
  435.         // user code begin {2}
  436.         setTitle("'" + className + "' verification - JustIce / BCEL");
  437.         // user code end
  438.     }

  439.     /** Machine-generated. */
  440.     public void pass1Button_ActionPerformed(final ActionEvent actionEvent) {
  441.         final Verifier v = VerifierFactory.getVerifier(className);
  442.         final VerificationResult vr = v.doPass1();
  443.         if (vr.getStatus() == VerificationResult.VERIFIED_OK) {
  444.             getPass1Panel().setBackground(Color.green);
  445.             getPass1Panel().repaint();
  446.         }
  447.         if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) {
  448.             getPass1Panel().setBackground(Color.red);
  449.             getPass1Panel().repaint();
  450.         }
  451.     }

  452.     /** Machine-generated. */
  453.     public void pass2Button_ActionPerformed(final ActionEvent actionEvent) {
  454.         pass1Button_ActionPerformed(actionEvent);
  455.         final Verifier v = VerifierFactory.getVerifier(className);
  456.         final VerificationResult vr = v.doPass2();
  457.         if (vr.getStatus() == VerificationResult.VERIFIED_OK) {
  458.             getPass2Panel().setBackground(Color.green);
  459.             getPass2Panel().repaint();
  460.         }
  461.         if (vr.getStatus() == VerificationResult.VERIFIED_NOTYET) {
  462.             getPass2Panel().setBackground(Color.yellow);
  463.             getPass2Panel().repaint();
  464.         }
  465.         if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) {
  466.             getPass2Panel().setBackground(Color.red);
  467.             getPass2Panel().repaint();
  468.         }
  469.     }

  470.     /** Machine-generated. */
  471.     public void pass4Button_ActionPerformed(final ActionEvent actionEvent) {
  472.         pass2Button_ActionPerformed(actionEvent);
  473.         Color color = Color.green;
  474.         final Verifier v = VerifierFactory.getVerifier(className);
  475.         VerificationResult vr = v.doPass2();
  476.         if (vr.getStatus() == VerificationResult.VERIFIED_OK) {
  477.             JavaClass jc = null;
  478.             try {
  479.                 jc = Repository.lookupClass(className);
  480.                 final int nr = jc.getMethods().length;
  481.                 for (int i = 0; i < nr; i++) {
  482.                     vr = v.doPass3b(i);
  483.                     if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
  484.                         color = Color.red;
  485.                         break;
  486.                     }
  487.                 }
  488.             } catch (final ClassNotFoundException ex) {
  489.                 // FIXME: report the error
  490.                 ex.printStackTrace();
  491.             }
  492.         } else {
  493.             color = Color.yellow;
  494.         }
  495.         getPass3Panel().setBackground(color);
  496.         getPass3Panel().repaint();
  497.     }
  498. }