Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / util / ClassInfoFilter.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.util;
20
21 import gov.nasa.jpf.vm.ClassInfo;
22
23 /**
24  * utility class that can be used by InstructionFactory implementations to
25  * selectively replace bytecodes for specified class sets.
26  *
27  * Filtering is based on include/exclude name patterns (e.g. for packages) and/or
28  * on inheritance (both down- and upwards)
29  */
30 public class ClassInfoFilter {
31
32     // filter using an explicit set of class names (can be used for one-pass load)
33   protected StringSetMatcher includes;  // included classes that should use them
34   protected StringSetMatcher excludes;  // excluded classes (that should NOT use them)
35
36   // filter using base/derived class sets (only useful in subsequent pass)
37   ClassInfo ciLeaf;
38   ClassInfo ciRoot;
39
40   public ClassInfoFilter (String[] includeCls, String[] excludeCls,
41                                    ClassInfo rootCls, ClassInfo leafCls) {
42     includes = StringSetMatcher.getNonEmpty(includeCls);
43     excludes = StringSetMatcher.getNonEmpty(excludeCls);
44
45     ciRoot = rootCls;
46     ciLeaf = leafCls;
47   }
48
49
50   public boolean isPassing (ClassInfo ci){
51     if (ci == null){
52
53       // <??> not clear what to do in this case, since we have nothing to
54       // filter on. Since all reflection calls come in here, it's probably
55       // better to instrument by default (until we have a better mechanism)
56       return true;
57
58     } else {
59       String clsName = ci.getName();
60
61       if (StringSetMatcher.isMatch(clsName, includes, excludes)){
62         if (ciLeaf == null || ciLeaf.isInstanceOf(ci)){
63           if (ciRoot == null || ci.isInstanceOf(ciRoot)){
64             return true;
65           }
66         }
67       }
68     }
69
70     return false;
71   }
72
73 }