Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / util / FieldSpec.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 import gov.nasa.jpf.vm.FieldInfo;
23
24 /**
25  * utility class that can match FieldInfos against specs.
26  * if the class or name part are omitted, "*" is assumed
27  * a preceeding '!' means the match is inverted
28  *
29  * spec examples
30  *   "x.y.Foo.bar  : field 'bar' in class 'x.y.Foo'
31  *   "x.y.Foo+.bar : all 'bar' fields in 'x.y.Foo' and all its supertypes
32  *   "x.y.Foo.*"   : all fields of x.y.Foo
33  *   "*.myData"    : all fields names 'myData'
34  *   "!x.y.*"      : all fields of types outside types in package x.y
35  */
36 public class FieldSpec extends FeatureSpec {
37
38   /**
39    * factory method that includes the parser
40    */
41   public static FieldSpec createFieldSpec (String s){
42     ParseData d = new ParseData();
43
44     s = s.trim();
45     String src = s;
46
47     s = parseInversion(s,d);
48     parseTypeAndName(s,d);
49
50     try {
51       return new FieldSpec(src, d.typeSpec, d.nameSpec, d.matchInverted);
52     } catch (IllegalArgumentException iax){
53       return null;
54     }
55   }
56
57
58   public FieldSpec (String rawSpec, String cls, String name, boolean inverted){
59     super(rawSpec,cls,name,inverted);
60   }
61
62   @Override
63   public boolean matches (Object feature){
64     if (feature instanceof FieldInfo){
65       return matches( (FieldInfo) feature);
66     } else {
67       return false;
68     }
69   }
70
71   public boolean matches (FieldInfo fi){
72
73     ClassInfo ci = fi.getClassInfo();
74     if (isMatchingType(ci)) {
75       if (nameSpec.matches(fi.getName()) != matchInverted) {
76         return true;
77       }
78     }
79
80     return matchInverted;
81   }
82
83   @Override
84   public String toString() {
85     StringBuilder sb = new StringBuilder();
86     sb.append("FieldSpec {");
87     if (clsSpec != null){
88       sb.append("clsSpec:\"");
89       sb.append(clsSpec);
90       sb.append('"');
91     }
92     if (nameSpec != null){
93       sb.append(",nameSpec:\"");
94       sb.append(nameSpec);
95       sb.append('"');
96     }
97     sb.append('}');
98     return sb.toString();
99   }
100 }