Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / tool / PrintEvents.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.tool;
20
21 import gov.nasa.jpf.Config;
22 import gov.nasa.jpf.JPFClassLoader;
23 import gov.nasa.jpf.util.FileUtils;
24 import gov.nasa.jpf.util.JPFSiteUtils;
25 import gov.nasa.jpf.util.event.EventTree;
26
27
28 /**
29  * very simple tool to print .util.script.EventTrees
30  * 
31  * <2do> this should use native_classpath / JPFClassLoader to load the EventTree
32  */
33 public class PrintEvents {
34
35   static boolean printTree;
36   static boolean printPaths;
37   static String clsName;
38   
39   static void showUsage () {
40     System.out.println("usage:   'PrintEvents [<option>..] <className>'");
41     System.out.println("options:  -t  : print tree");
42     System.out.println("          -p  : print paths");
43   }
44
45   static boolean readOptions (String[] args) {
46     for (int i = 0; i < args.length; i++) {
47       String arg = args[i];
48
49       if ("-t".equals(arg)) {
50         printTree = true;
51       } else if ("-p".equals(arg)) {
52         printPaths = true;
53       } else if (arg.charAt(0) != '-') {
54           clsName = arg;
55           if (clsName.charAt(0) == '.'){
56             clsName = "gov.nasa.jpf" + clsName;
57           }
58       } else {
59         System.err.println("unknown option: " + arg);
60         showUsage();
61
62         return false;
63       }
64     }
65
66     return (clsName != null);
67   }
68
69   static String[] getTestPathElements (Config config){
70     String projectId = JPFSiteUtils.getCurrentProjectId();
71     
72     if (projectId != null) {
73       String testCpKey = projectId + ".test_classpath";
74       return  config.getCompactTrimmedStringArray(testCpKey);
75       
76     } else {
77       return new String[0];
78     }    
79   }
80   
81   static void addTestClassPath (JPFClassLoader cl, String[] testPathElements){
82     if (testPathElements != null) {
83       for (String pe : testPathElements) {
84         try {
85           cl.addURL(FileUtils.getURL(pe));
86         } catch (Throwable x) {
87           System.err.println("malformed test_classpath URL: " + pe);
88         }
89       }
90     }
91   }
92   
93   public static void main (String[] args){
94     if ((args.length == 0) || !readOptions(args)) {
95       showUsage();
96     }
97      
98     Config config = new Config(args);
99     String[] testPathElements = getTestPathElements(config);
100     JPFClassLoader cl = config.initClassLoader(RunTest.class.getClassLoader());
101     addTestClassPath( cl, testPathElements);
102
103     try {     
104       Class<EventTree> cls = (Class<EventTree>)cl.loadClass(clsName);
105       EventTree et = cls.newInstance();
106       
107       if (printTree){
108         System.out.println("---------------- event tree of " + clsName);
109         et.printTree();
110       }
111       
112       if (printPaths){
113         System.out.println("---------------- event paths of " + clsName);
114         et.printPaths();
115       }
116     } catch (ClassNotFoundException cnfx){
117       System.err.println("class not found: " + clsName);
118     } catch (NoClassDefFoundError ncdf){
119       System.err.println("class does not load: " + ncdf.getMessage());      
120     } catch (InstantiationException ex) {
121       System.err.println("cannot instantiate: " + ex.getMessage());      
122     } catch (IllegalAccessException ex) {
123       System.err.println("cannot instantiate: " + ex.getMessage());      
124     }
125   }
126 }