View Javadoc

1   /*
2    * Copyright  2000-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License"); 
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License. 
15   *
16   */
17  package org.apache.bcel.verifier;
18  
19  import java.awt.Color;
20  import org.apache.bcel.Repository;
21  import org.apache.bcel.classfile.JavaClass;
22  
23  /***
24   * A class for simple graphical class file verification.
25   * Use the main(String []) method with fully qualified
26   * class names as arguments to use it as a stand-alone
27   * application.
28   * Use the VerifyDialog(String) constructor to use this
29   * class in your application.
30   * [This class was created using VisualAge for Java,
31   * but it does not work under VAJ itself (Version 3.02 JDK 1.2)]
32   * @version $Id: VerifyDialog.java 386056 2006-03-15 11:31:56Z tcurdt $
33   * @author Enver Haase
34   * @see #main(String[])
35   * @see #VerifyDialog(String)
36   */
37  public class VerifyDialog extends javax.swing.JDialog {
38  
39      /*** Machine-generated. */
40      private javax.swing.JPanel ivjJDialogContentPane = null;
41      /*** Machine-generated. */
42      private javax.swing.JPanel ivjPass1Panel = null;
43      /*** Machine-generated. */
44      private javax.swing.JPanel ivjPass2Panel = null;
45      /*** Machine-generated. */
46      private javax.swing.JPanel ivjPass3Panel = null;
47      /*** Machine-generated. */
48      private javax.swing.JButton ivjPass1Button = null;
49      /*** Machine-generated. */
50      private javax.swing.JButton ivjPass2Button = null;
51      /*** Machine-generated. */
52      private javax.swing.JButton ivjPass3Button = null;
53      /*** Machine-generated. */
54      IvjEventHandler ivjEventHandler = new IvjEventHandler();
55      /***
56       * The class to verify. Default set to 'java.lang.Object'
57       * in case this class is instantiated via one of the many
58       * machine-generated constructors.
59       */
60      private String class_name = "java.lang.Object";
61      /***
62       * This field is here to count the number of open VerifyDialog
63       * instances so the JVM can be exited afer every Dialog had been
64       * closed.
65       */
66      private static int classes_to_verify;
67  
68      /*** Machine-generated. */
69      class IvjEventHandler implements java.awt.event.ActionListener {
70  
71          public void actionPerformed( java.awt.event.ActionEvent e ) {
72              if (e.getSource() == VerifyDialog.this.getPass1Button()) {
73                  connEtoC1(e);
74              }
75              if (e.getSource() == VerifyDialog.this.getPass2Button()) {
76                  connEtoC2(e);
77              }
78              if (e.getSource() == VerifyDialog.this.getPass3Button()) {
79                  connEtoC3(e);
80              }
81              if (e.getSource() == VerifyDialog.this.getFlushButton()) {
82                  connEtoC4(e);
83              }
84          };
85      };
86  
87      /*** Machine-generated. */
88      private javax.swing.JButton ivjFlushButton = null;
89  
90  
91      /*** Machine-generated. */
92      public VerifyDialog() {
93          super();
94          initialize();
95      }
96  
97  
98      /*** Machine-generated. */
99      public VerifyDialog(java.awt.Dialog owner) {
100         super(owner);
101     }
102 
103 
104     /*** Machine-generated. */
105     public VerifyDialog(java.awt.Dialog owner, String title) {
106         super(owner, title);
107     }
108 
109 
110     /*** Machine-generated. */
111     public VerifyDialog(java.awt.Dialog owner, String title, boolean modal) {
112         super(owner, title, modal);
113     }
114 
115 
116     /*** Machine-generated. */
117     public VerifyDialog(java.awt.Dialog owner, boolean modal) {
118         super(owner, modal);
119     }
120 
121 
122     /*** Machine-generated. */
123     public VerifyDialog(java.awt.Frame owner) {
124         super(owner);
125     }
126 
127 
128     /*** Machine-generated. */
129     public VerifyDialog(java.awt.Frame owner, String title) {
130         super(owner, title);
131     }
132 
133 
134     /*** Machine-generated. */
135     public VerifyDialog(java.awt.Frame owner, String title, boolean modal) {
136         super(owner, title, modal);
137     }
138 
139 
140     /*** Machine-generated. */
141     public VerifyDialog(java.awt.Frame owner, boolean modal) {
142         super(owner, modal);
143     }
144 
145 
146     /***
147      * Use this constructor if you want a possibility to verify other
148      * class files than java.lang.Object.
149      * @param fully_qualified_class_name java.lang.String
150      */
151     public VerifyDialog(String fully_qualified_class_name) {
152         super();
153         int dotclasspos = fully_qualified_class_name.lastIndexOf(".class");
154         if (dotclasspos != -1) {
155             fully_qualified_class_name = fully_qualified_class_name.substring(0, dotclasspos);
156         }
157         fully_qualified_class_name = fully_qualified_class_name.replace('/', '.');
158         class_name = fully_qualified_class_name;
159         initialize();
160     }
161 
162 
163     /*** Machine-generated. */
164     private void connEtoC1( java.awt.event.ActionEvent arg1 ) {
165         try {
166             // user code begin {1}
167             // user code end
168             this.pass1Button_ActionPerformed(arg1);
169             // user code begin {2}
170             // user code end
171         } catch (java.lang.Throwable ivjExc) {
172             // user code begin {3}
173             // user code end
174             handleException(ivjExc);
175         }
176     }
177 
178 
179     /*** Machine-generated. */
180     private void connEtoC2( java.awt.event.ActionEvent arg1 ) {
181         try {
182             // user code begin {1}
183             // user code end
184             this.pass2Button_ActionPerformed(arg1);
185             // user code begin {2}
186             // user code end
187         } catch (java.lang.Throwable ivjExc) {
188             // user code begin {3}
189             // user code end
190             handleException(ivjExc);
191         }
192     }
193 
194 
195     /*** Machine-generated. */
196     private void connEtoC3( java.awt.event.ActionEvent arg1 ) {
197         try {
198             // user code begin {1}
199             // user code end
200             this.pass4Button_ActionPerformed(arg1);
201             // user code begin {2}
202             // user code end
203         } catch (java.lang.Throwable ivjExc) {
204             // user code begin {3}
205             // user code end
206             handleException(ivjExc);
207         }
208     }
209 
210 
211     /*** Machine-generated. */
212     private void connEtoC4( java.awt.event.ActionEvent arg1 ) {
213         try {
214             // user code begin {1}
215             // user code end
216             this.flushButton_ActionPerformed(arg1);
217             // user code begin {2}
218             // user code end
219         } catch (java.lang.Throwable ivjExc) {
220             // user code begin {3}
221             // user code end
222             handleException(ivjExc);
223         }
224     }
225 
226 
227     /*** Machine-generated. */
228     public void flushButton_ActionPerformed( java.awt.event.ActionEvent actionEvent ) {
229         VerifierFactory.getVerifier(class_name).flush();
230         Repository.removeClass(class_name); // Make sure it will be reloaded.
231         getPass1Panel().setBackground(Color.gray);
232         getPass1Panel().repaint();
233         getPass2Panel().setBackground(Color.gray);
234         getPass2Panel().repaint();
235         getPass3Panel().setBackground(Color.gray);
236         getPass3Panel().repaint();
237     }
238 
239 
240     /*** Machine-generated. */
241     private javax.swing.JButton getFlushButton() {
242         if (ivjFlushButton == null) {
243             try {
244                 ivjFlushButton = new javax.swing.JButton();
245                 ivjFlushButton.setName("FlushButton");
246                 ivjFlushButton.setText("Flush: Forget old verification results");
247                 ivjFlushButton.setBackground(java.awt.SystemColor.controlHighlight);
248                 ivjFlushButton.setBounds(60, 215, 300, 30);
249                 ivjFlushButton.setForeground(java.awt.Color.red);
250                 ivjFlushButton.setActionCommand("FlushButton");
251                 // user code begin {1}
252                 // user code end
253             } catch (java.lang.Throwable ivjExc) {
254                 // user code begin {2}
255                 // user code end
256                 handleException(ivjExc);
257             }
258         }
259         return ivjFlushButton;
260     }
261 
262 
263     /*** Machine-generated. */
264     private javax.swing.JPanel getJDialogContentPane() {
265         if (ivjJDialogContentPane == null) {
266             try {
267                 ivjJDialogContentPane = new javax.swing.JPanel();
268                 ivjJDialogContentPane.setName("JDialogContentPane");
269                 ivjJDialogContentPane.setLayout(null);
270                 getJDialogContentPane().add(getPass1Panel(), getPass1Panel().getName());
271                 getJDialogContentPane().add(getPass3Panel(), getPass3Panel().getName());
272                 getJDialogContentPane().add(getPass2Panel(), getPass2Panel().getName());
273                 getJDialogContentPane().add(getPass1Button(), getPass1Button().getName());
274                 getJDialogContentPane().add(getPass2Button(), getPass2Button().getName());
275                 getJDialogContentPane().add(getPass3Button(), getPass3Button().getName());
276                 getJDialogContentPane().add(getFlushButton(), getFlushButton().getName());
277                 // user code begin {1}
278                 // user code end
279             } catch (java.lang.Throwable ivjExc) {
280                 // user code begin {2}
281                 // user code end
282                 handleException(ivjExc);
283             }
284         }
285         return ivjJDialogContentPane;
286     }
287 
288 
289     /*** Machine-generated. */
290     private javax.swing.JButton getPass1Button() {
291         if (ivjPass1Button == null) {
292             try {
293                 ivjPass1Button = new javax.swing.JButton();
294                 ivjPass1Button.setName("Pass1Button");
295                 ivjPass1Button.setText("Pass1: Verify binary layout of .class file");
296                 ivjPass1Button.setBackground(java.awt.SystemColor.controlHighlight);
297                 ivjPass1Button.setBounds(100, 40, 300, 30);
298                 ivjPass1Button.setActionCommand("Button1");
299                 // user code begin {1}
300                 // user code end
301             } catch (java.lang.Throwable ivjExc) {
302                 // user code begin {2}
303                 // user code end
304                 handleException(ivjExc);
305             }
306         }
307         return ivjPass1Button;
308     }
309 
310 
311     /*** Machine-generated. */
312     private javax.swing.JPanel getPass1Panel() {
313         if (ivjPass1Panel == null) {
314             try {
315                 ivjPass1Panel = new javax.swing.JPanel();
316                 ivjPass1Panel.setName("Pass1Panel");
317                 ivjPass1Panel.setLayout(null);
318                 ivjPass1Panel.setBackground(java.awt.SystemColor.controlShadow);
319                 ivjPass1Panel.setBounds(30, 30, 50, 50);
320                 // user code begin {1}
321                 // user code end
322             } catch (java.lang.Throwable ivjExc) {
323                 // user code begin {2}
324                 // user code end
325                 handleException(ivjExc);
326             }
327         }
328         return ivjPass1Panel;
329     }
330 
331 
332     /*** Machine-generated. */
333     private javax.swing.JButton getPass2Button() {
334         if (ivjPass2Button == null) {
335             try {
336                 ivjPass2Button = new javax.swing.JButton();
337                 ivjPass2Button.setName("Pass2Button");
338                 ivjPass2Button.setText("Pass 2: Verify static .class file constraints");
339                 ivjPass2Button.setBackground(java.awt.SystemColor.controlHighlight);
340                 ivjPass2Button.setBounds(100, 100, 300, 30);
341                 ivjPass2Button.setActionCommand("Button2");
342                 // user code begin {1}
343                 // user code end
344             } catch (java.lang.Throwable ivjExc) {
345                 // user code begin {2}
346                 // user code end
347                 handleException(ivjExc);
348             }
349         }
350         return ivjPass2Button;
351     }
352 
353 
354     /*** Machine-generated. */
355     private javax.swing.JPanel getPass2Panel() {
356         if (ivjPass2Panel == null) {
357             try {
358                 ivjPass2Panel = new javax.swing.JPanel();
359                 ivjPass2Panel.setName("Pass2Panel");
360                 ivjPass2Panel.setLayout(null);
361                 ivjPass2Panel.setBackground(java.awt.SystemColor.controlShadow);
362                 ivjPass2Panel.setBounds(30, 90, 50, 50);
363                 // user code begin {1}
364                 // user code end
365             } catch (java.lang.Throwable ivjExc) {
366                 // user code begin {2}
367                 // user code end
368                 handleException(ivjExc);
369             }
370         }
371         return ivjPass2Panel;
372     }
373 
374 
375     /*** Machine-generated. */
376     private javax.swing.JButton getPass3Button() {
377         if (ivjPass3Button == null) {
378             try {
379                 ivjPass3Button = new javax.swing.JButton();
380                 ivjPass3Button.setName("Pass3Button");
381                 ivjPass3Button.setText("Passes 3a+3b: Verify code arrays");
382                 ivjPass3Button.setBackground(java.awt.SystemColor.controlHighlight);
383                 ivjPass3Button.setBounds(100, 160, 300, 30);
384                 ivjPass3Button.setActionCommand("Button2");
385                 // user code begin {1}
386                 // user code end
387             } catch (java.lang.Throwable ivjExc) {
388                 // user code begin {2}
389                 // user code end
390                 handleException(ivjExc);
391             }
392         }
393         return ivjPass3Button;
394     }
395 
396 
397     /*** Machine-generated. */
398     private javax.swing.JPanel getPass3Panel() {
399         if (ivjPass3Panel == null) {
400             try {
401                 ivjPass3Panel = new javax.swing.JPanel();
402                 ivjPass3Panel.setName("Pass3Panel");
403                 ivjPass3Panel.setLayout(null);
404                 ivjPass3Panel.setBackground(java.awt.SystemColor.controlShadow);
405                 ivjPass3Panel.setBounds(30, 150, 50, 50);
406                 // user code begin {1}
407                 // user code end
408             } catch (java.lang.Throwable ivjExc) {
409                 // user code begin {2}
410                 // user code end
411                 handleException(ivjExc);
412             }
413         }
414         return ivjPass3Panel;
415     }
416 
417 
418     /*** Machine-generated. */
419     private void handleException( java.lang.Throwable exception ) {
420         /* Uncomment the following lines to print uncaught exceptions to stdout */
421         System.out.println("--------- UNCAUGHT EXCEPTION ---------");
422         exception.printStackTrace(System.out);
423     }
424 
425 
426     /*** Machine-generated. */
427     private void initConnections() throws java.lang.Exception {
428         // user code begin {1}
429         // user code end
430         getPass1Button().addActionListener(ivjEventHandler);
431         getPass2Button().addActionListener(ivjEventHandler);
432         getPass3Button().addActionListener(ivjEventHandler);
433         getFlushButton().addActionListener(ivjEventHandler);
434     }
435 
436 
437     /*** Machine-generated. */
438     private void initialize() {
439         try {
440             // user code begin {1}
441             // user code end
442             setName("VerifyDialog");
443             setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
444             setSize(430, 280);
445             setVisible(true);
446             setModal(true);
447             setResizable(false);
448             setContentPane(getJDialogContentPane());
449             initConnections();
450         } catch (java.lang.Throwable ivjExc) {
451             handleException(ivjExc);
452         }
453         // user code begin {2}
454         setTitle("'" + class_name + "' verification - JustIce / BCEL");
455         // user code end
456     }
457 
458 
459     /***
460      * Verifies one or more class files.
461      * Verification results are presented graphically: Red means 'rejected',
462      * green means 'passed' while yellow means 'could not be verified yet'.
463      * @param args java.lang.String[] fully qualified names of classes to verify.
464      */
465     public static void main( java.lang.String[] args ) {
466         classes_to_verify = args.length;
467         for (int i = 0; i < args.length; i++) {
468             try {
469                 VerifyDialog aVerifyDialog;
470                 aVerifyDialog = new VerifyDialog(args[i]);
471                 aVerifyDialog.setModal(true);
472                 aVerifyDialog.addWindowListener(new java.awt.event.WindowAdapter() {
473 
474                     public void windowClosing( java.awt.event.WindowEvent e ) {
475                         classes_to_verify--;
476                         if (classes_to_verify == 0) {
477                             System.exit(0);
478                         }
479                     };
480                 });
481                 aVerifyDialog.setVisible(true);
482             } catch (Throwable exception) {
483                 System.err.println("Exception occurred in main() of javax.swing.JDialog");
484                 exception.printStackTrace(System.out);
485             }
486         }
487     }
488 
489 
490     /*** Machine-generated. */
491     public void pass1Button_ActionPerformed( java.awt.event.ActionEvent actionEvent ) {
492         Verifier v = VerifierFactory.getVerifier(class_name);
493         VerificationResult vr = v.doPass1();
494         if (vr.getStatus() == VerificationResult.VERIFIED_OK) {
495             getPass1Panel().setBackground(Color.green);
496             getPass1Panel().repaint();
497         }
498         if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) {
499             getPass1Panel().setBackground(Color.red);
500             getPass1Panel().repaint();
501         }
502     }
503 
504 
505     /*** Machine-generated. */
506     public void pass2Button_ActionPerformed( java.awt.event.ActionEvent actionEvent ) {
507         pass1Button_ActionPerformed(actionEvent);
508         Verifier v = VerifierFactory.getVerifier(class_name);
509         VerificationResult vr = v.doPass2();
510         if (vr.getStatus() == VerificationResult.VERIFIED_OK) {
511             getPass2Panel().setBackground(Color.green);
512             getPass2Panel().repaint();
513         }
514         if (vr.getStatus() == VerificationResult.VERIFIED_NOTYET) {
515             getPass2Panel().setBackground(Color.yellow);
516             getPass2Panel().repaint();
517         }
518         if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) {
519             getPass2Panel().setBackground(Color.red);
520             getPass2Panel().repaint();
521         }
522     }
523 
524 
525     /*** Machine-generated. */
526     public void pass4Button_ActionPerformed( java.awt.event.ActionEvent actionEvent ) {
527         pass2Button_ActionPerformed(actionEvent);
528         Color color = Color.green;
529         Verifier v = VerifierFactory.getVerifier(class_name);
530         VerificationResult vr = v.doPass2();
531         if (vr.getStatus() == VerificationResult.VERIFIED_OK) {
532             JavaClass jc = null;
533             try {
534                 jc = Repository.lookupClass(class_name);
535                 int nr = jc.getMethods().length;
536                 for (int i = 0; i < nr; i++) {
537                     vr = v.doPass3b(i);
538                     if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
539                         color = Color.red;
540                         break;
541                     }
542                 }
543             } catch (ClassNotFoundException ex) {
544                 // FIXME: report the error
545                 ex.printStackTrace();
546             }
547         } else {
548             color = Color.yellow;
549         }
550         getPass3Panel().setBackground(color);
551         getPass3Panel().repaint();
552     }
553 }