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  import org.apache.bcel.verifier.VerifierFactory;
18  import org.apache.bcel.verifier.Verifier;
19  import org.apache.bcel.verifier.VerificationResult;
20  import org.apache.bcel.classfile.JavaClass;
21  import org.apache.bcel.classfile.ClassParser;
22  import org.apache.bcel.Repository;
23  
24  /**
25   * Reads a class file from stdin and runs BCEL Justice verifier.
26   *
27   * <p>
28   * This is supposed to help us by giving us where the generate byte code is wrong,
29   * but it turns out that this isn't as useful as I hoped for ...
30   *
31   * <h3>Usage</h3>
32   * <pre>
33   * $ cat Foo.class | java Justice
34   * </pre>
35   *
36   * @author Kohsuke Kawaguchi
37   */
38  public class Justice {
39      public static void main(String[] args) throws Exception {
40          ClassParser cp = new ClassParser(System.in,null);
41          JavaClass clazz = cp.parse();
42          Repository.addClass(clazz);
43          Verifier v = VerifierFactory.getVerifier(clazz.getClassName());
44  
45          System.out.println("Pass 1");
46          report(v.doPass1());
47          System.out.println("Pass 2");
48          report(v.doPass2());
49          for( int i=0; i<clazz.getMethods().length; i++ ) {
50              String name = clazz.getMethods()[i].getName();
51              System.out.println("Pass 3a on "+name);
52              report(v.doPass3a(i));
53              System.out.println("Pass 3b on "+name);
54              report(v.doPass3b(i));
55          }
56      }
57  
58      private static void report(VerificationResult r) {
59          System.out.println(r);
60      }
61  }