Fixing a few bugs in the statistics printout.
[jpf-core.git] / src / classes / java / security / AllPermission.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 package java.security;
19
20 import java.util.Enumeration;
21 import sun.security.util.SecurityConstants;
22
23 // TODO: Fix for Groovy's model-checking
24 // TODO: This model class is a placeholder for future implementation
25 /**
26  * MJI model class for java.security.AllPermission library abstraction
27  */
28 public final class AllPermission extends Permission {
29
30     private static final long serialVersionUID = -2916474571451318075L;
31
32     public AllPermission() {
33         super("<all permissions>");
34     }
35
36     public boolean implies(Permission p) {
37         return true;
38     }
39
40     public boolean equals(Object obj) {
41         return (obj instanceof AllPermission);
42     }
43
44     public int hashCode() {
45         return 1;
46     }
47
48     public String getActions() {
49         return "<all actions>";
50     }
51
52     public PermissionCollection newPermissionCollection() {
53         return new AllPermissionCollection();
54     }
55
56 }
57
58
59 final class AllPermissionCollection
60         extends PermissionCollection
61         implements java.io.Serializable
62 {
63
64     // use serialVersionUID from JDK 1.2.2 for interoperability
65     private static final long serialVersionUID = -4023755556366636806L;
66
67     private boolean all_allowed; // true if any all permissions have been added
68
69     public AllPermissionCollection() {
70         all_allowed = false;
71     }
72
73     public void add(Permission permission) {
74         if (! (permission instanceof AllPermission))
75             throw new IllegalArgumentException("invalid permission: "+
76                     permission);
77         if (isReadOnly())
78             throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
79
80         all_allowed = true; // No sync; staleness OK
81     }
82
83     public boolean implies(Permission permission) {
84         return all_allowed; // No sync; staleness OK
85     }
86
87     public Enumeration<Permission> elements() {
88         return new Enumeration<Permission>() {
89             private boolean hasMore = all_allowed;
90
91             public boolean hasMoreElements() {
92                 return hasMore;
93             }
94
95             public Permission nextElement() {
96                 hasMore = false;
97                 return SecurityConstants.ALL_PERMISSION;
98             }
99         };
100     }
101 }