    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017
    018package org.apache.bcel.verifier;
    019
    020import java.awt.Color;
    021
    022import org.apache.bcel.Repository;
    023import org.apache.bcel.classfile.JavaClass;
    024
    025/**
    026 * A class for simple graphical class file verification.
    027 * Use the main(String []) method with fully qualified
    028 * class names as arguments to use it as a stand-alone
    029 * application.
    030 * Use the VerifyDialog(String) constructor to use this
    031 * class in your application.
    032 * [This class was created using VisualAge for Java,
    033 * but it does not work under VAJ itself (Version 3.02 JDK 1.2)]
    034 * @version $Id$
    035 * @see #main(String[])
    036 * @see #VerifyDialog(String)
    037 */
    038public class VerifyDialog extends javax.swing.JDialog {
    039
    040    private static final long serialVersionUID = -6374807677043142313L;
    041    /** Machine-generated. */
    042    private javax.swing.JPanel ivjJDialogContentPane = null;
    043    /** Machine-generated. */
    044    private javax.swing.JPanel ivjPass1Panel = null;
    045    /** Machine-generated. */
    046    private javax.swing.JPanel ivjPass2Panel = null;
    047    /** Machine-generated. */
    048    private javax.swing.JPanel ivjPass3Panel = null;
    049    /** Machine-generated. */
    050    private javax.swing.JButton ivjPass1Button = null;
    051    /** Machine-generated. */
    052    private javax.swing.JButton ivjPass2Button = null;
    053    /** Machine-generated. */
    054    private javax.swing.JButton ivjPass3Button = null;
    055    /** Machine-generated. */
    056    private final IvjEventHandler ivjEventHandler = new IvjEventHandler();
    057    /**
    058     * The class to verify. Default set to 'java.lang.Object'
    059     * in case this class is instantiated via one of the many
    060     * machine-generated constructors.
    061     */
    062    private String class_name = "java.lang.Object";
    063    /**
    064     * This field is here to count the number of open VerifyDialog
    065     * instances so the JVM can be exited afer every Dialog had been
    066     * closed.
    067     */
    068    private static int classes_to_verify;
    069
    070    /** Machine-generated. */
    071    class IvjEventHandler implements java.awt.event.ActionListener {
    072
    073        @Override
    074        public void actionPerformed( final java.awt.event.ActionEvent e ) {
    075            if (e.getSource() == VerifyDialog.this.getPass1Button()) {
    076                connEtoC1(e);
    077            }
    078            if (e.getSource() == VerifyDialog.this.getPass2Button()) {
    079                connEtoC2(e);
    080            }
    081            if (e.getSource() == VerifyDialog.this.getPass3Button()) {
    082                connEtoC3(e);
    083            }
    084            if (e.getSource() == VerifyDialog.this.getFlushButton()) {
    085                connEtoC4(e);
    086            }
    087        }
    088    }
    089
    090    /** Machine-generated. */
    091    private javax.swing.JButton ivjFlushButton = null;
    092
    093
    094    /** Machine-generated. */
    095    public VerifyDialog() {
    096        super();
    097        initialize();
    098    }
    099
    100
    101    /** Machine-generated. */
    102    public VerifyDialog(final java.awt.Dialog owner) {
    103        super(owner);
    104    }
    105
    106
    107    /** Machine-generated. */
    108    public VerifyDialog(final java.awt.Dialog owner, final String title) {
    109        super(owner, title);
    110    }
    111
    112
    113    /** Machine-generated. */
    114    public VerifyDialog(final java.awt.Dialog owner, final String title, final boolean modal) {
    115        super(owner, title, modal);
    116    }
    117
    118
    119    /** Machine-generated. */
    120    public VerifyDialog(final java.awt.Dialog owner, final boolean modal) {
    121        super(owner, modal);
    122    }
    123
    124
    125    /** Machine-generated. */
    126    public VerifyDialog(final java.awt.Frame owner) {
    127        super(owner);
    128    }
    129
    130
    131    /** Machine-generated. */
    132    public VerifyDialog(final java.awt.Frame owner, final String title) {
    133        super(owner, title);
    134    }
    135
    136
    137    /** Machine-generated. */
    138    public VerifyDialog(final java.awt.Frame owner, final String title, final boolean modal) {
    139        super(owner, title, modal);
    140    }
    141
    142
    143    /** Machine-generated. */
    144    public VerifyDialog(final java.awt.Frame owner, final boolean modal) {
    145        super(owner, modal);
    146    }
    147
    148
    149    /**
    150     * Use this constructor if you want a possibility to verify other
    151     * class files than java.lang.Object.
    152     * @param fully_qualified_class_name java.lang.String
    153     */
    154    public VerifyDialog(String fully_qualified_class_name) {
    155        super();
    156        final int dotclasspos = fully_qualified_class_name.lastIndexOf(".class");
    157        if (dotclasspos != -1) {
    158            fully_qualified_class_name = fully_qualified_class_name.substring(0, dotclasspos);
    159        }
    160        fully_qualified_class_name = fully_qualified_class_name.replace('/', '.');
    161        class_name = fully_qualified_class_name;
    162        initialize();
    163    }
    164
    165
    166    /** Machine-generated. */
    167    private void connEtoC1( final java.awt.event.ActionEvent arg1 ) {
    168        try {
    169            // user code begin {1}
    170            // user code end
    171            this.pass1Button_ActionPerformed(arg1);
    172            // user code begin {2}
    173            // user code end
    174        } catch (final java.lang.Throwable ivjExc) {
    175            // user code begin {3}
    176            // user code end
    177            handleException(ivjExc);
    178        }
    179    }
    180
    181
    182    /** Machine-generated. */
    183    private void connEtoC2( final java.awt.event.ActionEvent arg1 ) {
    184        try {
    185            // user code begin {1}
    186            // user code end
    187            this.pass2Button_ActionPerformed(arg1);
    188            // user code begin {2}
    189            // user code end
    190        } catch (final java.lang.Throwable ivjExc) {
    191            // user code begin {3}
    192            // user code end
    193            handleException(ivjExc);
    194        }
    195    }
    196
    197
    198    /** Machine-generated. */
    199    private void connEtoC3( final java.awt.event.ActionEvent arg1 ) {
    200        try {
    201            // user code begin {1}
    202            // user code end
    203            this.pass4Button_ActionPerformed(arg1);
    204            // user code begin {2}
    205            // user code end
    206        } catch (final java.lang.Throwable ivjExc) {
    207            // user code begin {3}
    208            // user code end
    209            handleException(ivjExc);
    210        }
    211    }
    212
    213
    214    /** Machine-generated. */
    215    private void connEtoC4( final java.awt.event.ActionEvent arg1 ) {
    216        try {
    217            // user code begin {1}
    218            // user code end
    219            this.flushButton_ActionPerformed(arg1);
    220            // user code begin {2}
    221            // user code end
    222        } catch (final java.lang.Throwable ivjExc) {
    223            // user code begin {3}
    224            // user code end
    225            handleException(ivjExc);
    226        }
    227    }
    228
    229
    230    /** Machine-generated. */
    231    public void flushButton_ActionPerformed( final java.awt.event.ActionEvent actionEvent ) {
    232        VerifierFactory.getVerifier(class_name).flush();
    233        Repository.removeClass(class_name); // Make sure it will be reloaded.
    234        getPass1Panel().setBackground(Color.gray);
    235        getPass1Panel().repaint();
    236        getPass2Panel().setBackground(Color.gray);
    237        getPass2Panel().repaint();
    238        getPass3Panel().setBackground(Color.gray);
    239        getPass3Panel().repaint();
    240    }
    241
    242
    243    /** Machine-generated. */
    244    private javax.swing.JButton getFlushButton() {
    245        if (ivjFlushButton == null) {
    246            try {
    247                ivjFlushButton = new javax.swing.JButton();
    248                ivjFlushButton.setName("FlushButton");
    249                ivjFlushButton.setText("Flush: Forget old verification results");
    250                ivjFlushButton.setBackground(java.awt.SystemColor.controlHighlight);
    251                ivjFlushButton.setBounds(60, 215, 300, 30);
    252                ivjFlushButton.setForeground(java.awt.Color.red);
    253                ivjFlushButton.setActionCommand("FlushButton");
    254                // user code begin {1}
    255                // user code end
    256            } catch (final java.lang.Throwable ivjExc) {
    257                // user code begin {2}
    258                // user code end
    259                handleException(ivjExc);
    260            }
    261        }
    262        return ivjFlushButton;
    263    }
    264
    265
    266    /** Machine-generated. */
    267    private javax.swing.JPanel getJDialogContentPane() {
    268        if (ivjJDialogContentPane == null) {
    269            try {
    270                ivjJDialogContentPane = new javax.swing.JPanel();
    271                ivjJDialogContentPane.setName("JDialogContentPane");
    272                ivjJDialogContentPane.setLayout(null);
    273                getJDialogContentPane().add(getPass1Panel(), getPass1Panel().getName());
    274                getJDialogContentPane().add(getPass3Panel(), getPass3Panel().getName());
    275                getJDialogContentPane().add(getPass2Panel(), getPass2Panel().getName());
    276                getJDialogContentPane().add(getPass1Button(), getPass1Button().getName());
    277                getJDialogContentPane().add(getPass2Button(), getPass2Button().getName());
    278                getJDialogContentPane().add(getPass3Button(), getPass3Button().getName());
    279                getJDialogContentPane().add(getFlushButton(), getFlushButton().getName());
    280                // user code begin {1}
    281                // user code end
    282            } catch (final java.lang.Throwable ivjExc) {
    283                // user code begin {2}
    284                // user code end
    285                handleException(ivjExc);
    286            }
    287        }
    288        return ivjJDialogContentPane;
    289    }
    290
    291
    292    /** Machine-generated. */
    293    private javax.swing.JButton getPass1Button() {
    294        if (ivjPass1Button == null) {
    295            try {
    296                ivjPass1Button = new javax.swing.JButton();
    297                ivjPass1Button.setName("Pass1Button");
    298                ivjPass1Button.setText("Pass1: Verify binary layout of .class file");
    299                ivjPass1Button.setBackground(java.awt.SystemColor.controlHighlight);
    300                ivjPass1Button.setBounds(100, 40, 300, 30);
    301                ivjPass1Button.setActionCommand("Button1");
    302                // user code begin {1}
    303                // user code end
    304            } catch (final java.lang.Throwable ivjExc) {
    305                // user code begin {2}
    306                // user code end
    307                handleException(ivjExc);
    308            }
    309        }
    310        return ivjPass1Button;
    311    }
    312
    313
    314    /** Machine-generated. */
    315    private javax.swing.JPanel getPass1Panel() {
    316        if (ivjPass1Panel == null) {
    317            try {
    318                ivjPass1Panel = new javax.swing.JPanel();
    319                ivjPass1Panel.setName("Pass1Panel");
    320                ivjPass1Panel.setLayout(null);
    321                ivjPass1Panel.setBackground(java.awt.SystemColor.controlShadow);
    322                ivjPass1Panel.setBounds(30, 30, 50, 50);
    323                // user code begin {1}
    324                // user code end
    325            } catch (final java.lang.Throwable ivjExc) {
    326                // user code begin {2}
    327                // user code end
    328                handleException(ivjExc);
    329            }
    330        }
    331        return ivjPass1Panel;
    332    }
    333
    334
    335    /** Machine-generated. */
    336    private javax.swing.JButton getPass2Button() {
    337        if (ivjPass2Button == null) {
    338            try {
    339                ivjPass2Button = new javax.swing.JButton();
    340                ivjPass2Button.setName("Pass2Button");
    341                ivjPass2Button.setText("Pass 2: Verify static .class file constraints");
    342                ivjPass2Button.setBackground(java.awt.SystemColor.controlHighlight);
    343                ivjPass2Button.setBounds(100, 100, 300, 30);
    344                ivjPass2Button.setActionCommand("Button2");
    345                // user code begin {1}
    346                // user code end
    347            } catch (final java.lang.Throwable ivjExc) {
    348                // user code begin {2}
    349                // user code end
    350                handleException(ivjExc);
    351            }
    352        }
    353        return ivjPass2Button;
    354    }
    355
    356
    357    /** Machine-generated. */
    358    private javax.swing.JPanel getPass2Panel() {
    359        if (ivjPass2Panel == null) {
    360            try {
    361                ivjPass2Panel = new javax.swing.JPanel();
    362                ivjPass2Panel.setName("Pass2Panel");
    363                ivjPass2Panel.setLayout(null);
    364                ivjPass2Panel.setBackground(java.awt.SystemColor.controlShadow);
    365                ivjPass2Panel.setBounds(30, 90, 50, 50);
    366                // user code begin {1}
    367                // user code end
    368            } catch (final java.lang.Throwable ivjExc) {
    369                // user code begin {2}
    370                // user code end
    371                handleException(ivjExc);
    372            }
    373        }
    374        return ivjPass2Panel;
    375    }
    376
    377
    378    /** Machine-generated. */
    379    private javax.swing.JButton getPass3Button() {
    380        if (ivjPass3Button == null) {
    381            try {
    382                ivjPass3Button = new javax.swing.JButton();
    383                ivjPass3Button.setName("Pass3Button");
    384                ivjPass3Button.setText("Passes 3a+3b: Verify code arrays");
    385                ivjPass3Button.setBackground(java.awt.SystemColor.controlHighlight);
    386                ivjPass3Button.setBounds(100, 160, 300, 30);
    387                ivjPass3Button.setActionCommand("Button2");
    388                // user code begin {1}
    389                // user code end
    390            } catch (final java.lang.Throwable ivjExc) {
    391                // user code begin {2}
    392                // user code end
    393                handleException(ivjExc);
    394            }
    395        }
    396        return ivjPass3Button;
    397    }
    398
    399
    400    /** Machine-generated. */
    401    private javax.swing.JPanel getPass3Panel() {
    402        if (ivjPass3Panel == null) {
    403            try {
    404                ivjPass3Panel = new javax.swing.JPanel();
    405                ivjPass3Panel.setName("Pass3Panel");
    406                ivjPass3Panel.setLayout(null);
    407                ivjPass3Panel.setBackground(java.awt.SystemColor.controlShadow);
    408                ivjPass3Panel.setBounds(30, 150, 50, 50);
    409                // user code begin {1}
    410                // user code end
    411            } catch (final java.lang.Throwable ivjExc) {
    412                // user code begin {2}
    413                // user code end
    414                handleException(ivjExc);
    415            }
    416        }
    417        return ivjPass3Panel;
    418    }
    419
    420
    421    /** Machine-generated. */
    422    private void handleException( final java.lang.Throwable exception ) {
    423        /* Uncomment the following lines to print uncaught exceptions to stdout */
    424        System.out.println("--------- UNCAUGHT EXCEPTION ---------");
    425        exception.printStackTrace(System.out);
    426        // manually added code
    427        if (exception instanceof ThreadDeath) {
    428            throw (ThreadDeath) exception;
    429        }
    430        if (exception instanceof VirtualMachineError) {
    431            throw (VirtualMachineError) exception;
    432        }
    433    }
    434
    435
    436    /** Machine-generated. */
    437    private void initConnections() throws java.lang.Exception {
    438        // user code begin {1}
    439        // user code end
    440        getPass1Button().addActionListener(ivjEventHandler);
    441        getPass2Button().addActionListener(ivjEventHandler);
    442        getPass3Button().addActionListener(ivjEventHandler);
    443        getFlushButton().addActionListener(ivjEventHandler);
    444    }
    445
    446
    447    /** Machine-generated. */
    448    private void initialize() {
    449        try {
    450            // user code begin {1}
    451            // user code end
    452            setName("VerifyDialog");
    453            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    454            setSize(430, 280);
    455            setVisible(true);
    456            setModal(true);
    457            setResizable(false);
    458            setContentPane(getJDialogContentPane());
    459            initConnections();
    460        } catch (final java.lang.Throwable ivjExc) {
    461            handleException(ivjExc);
    462        }
    463        // user code begin {2}
    464        setTitle("'" + class_name + "' verification - JustIce / BCEL");
    465        // user code end
    466    }
    467
    468
    469    /**
    470     * Verifies one or more class files.
    471     * Verification results are presented graphically: Red means 'rejected',
    472     * green means 'passed' while yellow means 'could not be verified yet'.
    473     * @param args java.lang.String[] fully qualified names of classes to verify.
    474     */
    475    public static void main( final java.lang.String[] args ) {
    476        classes_to_verify = args.length;
    477        for (final String arg : args) {
    478            try {
    479                VerifyDialog aVerifyDialog;
    480                aVerifyDialog = new VerifyDialog(arg);
    481                aVerifyDialog.setModal(true);
    482                aVerifyDialog.addWindowListener(new java.awt.event.WindowAdapter() {
    483
    484                    @Override
    485                    public void windowClosing( final java.awt.event.WindowEvent e ) {
    486                        classes_to_verify--;
    487                        if (classes_to_verify == 0) {
    488                            System.exit(0);
    489                        }
    490                    }
    491                });
    492                aVerifyDialog.setVisible(true);
    493            } catch (final Throwable exception) {
    494                System.err.println("Exception occurred in main() of javax.swing.JDialog");
    495                exception.printStackTrace(System.out);
    496            }
    497        }
    498    }
    499
    500
    501    /** Machine-generated. */
    502    public void pass1Button_ActionPerformed( final java.awt.event.ActionEvent actionEvent ) {
    503        final Verifier v = VerifierFactory.getVerifier(class_name);
    504        final VerificationResult vr = v.doPass1();
    505        if (vr.getStatus() == VerificationResult.VERIFIED_OK) {
    506            getPass1Panel().setBackground(Color.green);
    507            getPass1Panel().repaint();
    508        }
    509        if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) {
    510            getPass1Panel().setBackground(Color.red);
    511            getPass1Panel().repaint();
    512        }
    513    }
    514
    515
    516    /** Machine-generated. */
    517    public void pass2Button_ActionPerformed( final java.awt.event.ActionEvent actionEvent ) {
    518        pass1Button_ActionPerformed(actionEvent);
    519        final Verifier v = VerifierFactory.getVerifier(class_name);
    520        final VerificationResult vr = v.doPass2();
    521        if (vr.getStatus() == VerificationResult.VERIFIED_OK) {
    522            getPass2Panel().setBackground(Color.green);
    523            getPass2Panel().repaint();
    524        }
    525        if (vr.getStatus() == VerificationResult.VERIFIED_NOTYET) {
    526            getPass2Panel().setBackground(Color.yellow);
    527            getPass2Panel().repaint();
    528        }
    529        if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) {
    530            getPass2Panel().setBackground(Color.red);
    531            getPass2Panel().repaint();
    532        }
    533    }
    534
    535
    536    /** Machine-generated. */
    537    public void pass4Button_ActionPerformed( final java.awt.event.ActionEvent actionEvent ) {
    538        pass2Button_ActionPerformed(actionEvent);
    539        Color color = Color.green;
    540        final Verifier v = VerifierFactory.getVerifier(class_name);
    541        VerificationResult vr = v.doPass2();
    542        if (vr.getStatus() == VerificationResult.VERIFIED_OK) {
    543            JavaClass jc = null;
    544            try {
    545                jc = Repository.lookupClass(class_name);
    546                final int nr = jc.getMethods().length;
    547                for (int i = 0; i < nr; i++) {
    548                    vr = v.doPass3b(i);
    549                    if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
    550                        color = Color.red;
    551                        break;
    552                    }
    553                }
    554            } catch (final ClassNotFoundException ex) {
    555                // FIXME: report the error
    556                ex.printStackTrace();
    557            }
    558        } else {
    559            color = Color.yellow;
    560        }
    561        getPass3Panel().setBackground(color);
    562        getPass3Panel().repaint();
    563    }
    564}