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