KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > runtime > Visibility


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
15  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
16  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
17  *
18  * Alternatively, the contents of this file may be used under the terms of
19  * either of the GNU General Public License Version 2 or later (the "GPL"),
20  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
21  * in which case the provisions of the GPL or the LGPL are applicable instead
22  * of those above. If you wish to allow use of your version of this file only
23  * under the terms of either the GPL or the LGPL, and not to allow others to
24  * use your version of this file under the terms of the CPL, indicate your
25  * decision by deleting the provisions above and replace them with the notice
26  * and other provisions required by the GPL or the LGPL. If you do not delete
27  * the provisions above, a recipient may use your version of this file under
28  * the terms of any one of the CPL, the GPL or the LGPL.
29  ***** END LICENSE BLOCK *****/

30 package org.jruby.runtime;
31
32 import java.io.Serializable JavaDoc;
33
34 import org.jruby.util.IdUtil;
35
36 /**
37  * This class represents two concepts: method visibility and a mask for
38  * determining a set of valid method visibilities. The first concept can only
39  * be a single value: PUBLIC, PRIVATE, PROTECTED, and MODULE_FUNCTION (see
40  * RubyModule#module_function). It is used to adorn a method with a _SINGLE_
41  * visibility. Some functions (see RubyModule#instance_methods) want to be
42  * able to see methods of multiple visibilities. The second concept allows
43  * making a vibility which is basically a mask (see
44  * Visibility.PUBLIC_PROTECTED). The method 'is' can then be used to see if a
45  * method visibility is in the mask.
46  *
47  * @author jpetersen
48  */

49 public final class Visibility implements Serializable JavaDoc {
50     private static final short PUBLIC_VALUE = (short) 1;
51     private static final short PROTECTED_VALUE = (short) 2;
52     private static final short PRIVATE_VALUE = (short) 4;
53     private static final short MODULE_FUNCTION_VALUE = (short) 8;
54     public static final Visibility PUBLIC = new Visibility(PUBLIC_VALUE);
55     public static final Visibility PROTECTED = new Visibility(PROTECTED_VALUE);
56     public static final Visibility PRIVATE = new Visibility(PRIVATE_VALUE);
57     public static final Visibility MODULE_FUNCTION = new Visibility(MODULE_FUNCTION_VALUE);
58     public static final Visibility PUBLIC_PROTECTED =
59         new Visibility((short) (PUBLIC_VALUE | PROTECTED_VALUE));
60
61     private final short restore;
62
63     static final long serialVersionUID = 2002102900L;
64
65     /**
66      * Constructor for MethodScope.
67      */

68     private Visibility(short restore) {
69         this.restore = restore;
70     }
71
72     public boolean isPublic() {
73         return (restore & PUBLIC_VALUE) != 0;
74     }
75
76     public boolean isProtected() {
77         return (restore & PROTECTED_VALUE) != 0;
78     }
79
80     public boolean isPrivate() {
81         return (restore & PRIVATE_VALUE) != 0;
82     }
83     
84     public boolean isModuleFunction() {
85         return (restore & MODULE_FUNCTION_VALUE) != 0;
86     }
87     
88     public boolean is(Visibility other) {
89         return (restore & other.restore) != 0;
90     }
91
92     public String JavaDoc toString() {
93         // XXXEnebo: will only print out actual visibilities
94
// and not masks
95
switch (restore) {
96             case 1:
97                 return "public";
98             case 2:
99                 return "protected";
100             case 4:
101                 return "private";
102             case 8:
103                 return "module_function";
104             default:
105                 return "mixed mask: " + restore;
106         }
107     }
108     
109     public String JavaDoc errorMessageFormat(CallType callType, String JavaDoc name) {
110         String JavaDoc format = "undefined method `%s' for %s%s%s";
111         if (callType == CallType.VARIABLE) {
112             if (IdUtil.isLocal(name)) {
113                 format = "undefined local variable or method '%s' for %s%s%s";
114             }
115         } else if (this == Visibility.PRIVATE && callType == CallType.NORMAL) {
116             format = "private method '%s' called for %s%s%s";
117         } else if (this == Visibility.PROTECTED) {
118             format = "protected method '%s' called for %s%s%s";
119         } else {
120         }
121         
122         return format;
123     }
124 }
125
Popular Tags