01: package org.gjt.sp.jedit.bsh;
02:
03: import java.util.Hashtable;
04:
05: /**
06:
07: @author Pat Niemeyer (pat@pat.net)
08: */
09: /*
10: Note: which of these things should be checked at parse time vs. run time?
11: */
12: public class Modifiers implements java.io.Serializable {
13: public static final int CLASS = 0, METHOD = 1, FIELD = 2;
14: Hashtable modifiers;
15:
16: /**
17: @param context is METHOD or FIELD
18: */
19: public void addModifier(int context, String name) {
20: if (modifiers == null)
21: modifiers = new Hashtable();
22:
23: Object existing = modifiers
24: .put(name, Void.TYPE/*arbitrary flag*/);
25: if (existing != null)
26: throw new IllegalStateException("Duplicate modifier: "
27: + name);
28:
29: int count = 0;
30: if (hasModifier("private"))
31: ++count;
32: if (hasModifier("protected"))
33: ++count;
34: if (hasModifier("public"))
35: ++count;
36: if (count > 1)
37: throw new IllegalStateException(
38: "public/private/protected cannot be used in combination.");
39:
40: switch (context) {
41: case CLASS:
42: validateForClass();
43: break;
44: case METHOD:
45: validateForMethod();
46: break;
47: case FIELD:
48: validateForField();
49: break;
50: }
51: }
52:
53: public boolean hasModifier(String name) {
54: if (modifiers == null)
55: modifiers = new Hashtable();
56: return modifiers.get(name) != null;
57: }
58:
59: // could refactor these a bit
60: private void validateForMethod() {
61: insureNo("volatile", "Method");
62: insureNo("transient", "Method");
63: }
64:
65: private void validateForField() {
66: insureNo("synchronized", "Variable");
67: insureNo("native", "Variable");
68: insureNo("abstract", "Variable");
69: }
70:
71: private void validateForClass() {
72: validateForMethod(); // volatile, transient
73: insureNo("native", "Class");
74: insureNo("synchronized", "Class");
75: }
76:
77: private void insureNo(String modifier, String context) {
78: if (hasModifier(modifier))
79: throw new IllegalStateException(context
80: + " cannot be declared '" + modifier + "'");
81: }
82:
83: public String toString() {
84: return "Modifiers: " + modifiers;
85: }
86:
87: }
|