|
1 /* |
|
2 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package com.sun.codemodel.internal; |
|
27 |
|
28 import java.io.PrintWriter; |
|
29 import java.io.StringWriter; |
|
30 |
|
31 |
|
32 /** |
|
33 * Modifier groups. |
|
34 */ |
|
35 public class JMods implements JGenerable { |
|
36 |
|
37 // |
|
38 // mask |
|
39 // |
|
40 private static int VAR |
|
41 = JMod.FINAL; |
|
42 |
|
43 private static int FIELD |
|
44 = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED |
|
45 | JMod.STATIC | JMod.FINAL |
|
46 | JMod.TRANSIENT | JMod.VOLATILE); |
|
47 |
|
48 private static int METHOD |
|
49 = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED | JMod.FINAL |
|
50 | JMod.ABSTRACT | JMod.STATIC | JMod.NATIVE | JMod.SYNCHRONIZED); |
|
51 |
|
52 private static int CLASS |
|
53 = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED |
|
54 | JMod.STATIC | JMod.FINAL | JMod.ABSTRACT ); |
|
55 |
|
56 private static int INTERFACE = JMod.PUBLIC; |
|
57 |
|
58 /** bit-packed representation of modifiers. */ |
|
59 private int mods; |
|
60 |
|
61 private JMods(int mods) { |
|
62 this.mods = mods; |
|
63 } |
|
64 |
|
65 /** |
|
66 * Gets the bit-packed representaion of modifiers. |
|
67 */ |
|
68 public int getValue() { |
|
69 return mods; |
|
70 } |
|
71 |
|
72 private static void check(int mods, int legal, String what) { |
|
73 if ((mods & ~legal) != 0) |
|
74 throw new IllegalArgumentException("Illegal modifiers for " |
|
75 + what + ": " |
|
76 + new JMods(mods).toString()); |
|
77 /* ## check for illegal combinations too */ |
|
78 } |
|
79 |
|
80 static JMods forVar(int mods) { |
|
81 check(mods, VAR, "variable"); |
|
82 return new JMods(mods); |
|
83 } |
|
84 |
|
85 static JMods forField(int mods) { |
|
86 check(mods, FIELD, "field"); |
|
87 return new JMods(mods); |
|
88 } |
|
89 |
|
90 static JMods forMethod(int mods) { |
|
91 check(mods, METHOD, "method"); |
|
92 return new JMods(mods); |
|
93 } |
|
94 |
|
95 static JMods forClass(int mods) { |
|
96 check(mods, CLASS, "class"); |
|
97 return new JMods(mods); |
|
98 } |
|
99 |
|
100 static JMods forInterface(int mods) { |
|
101 check(mods, INTERFACE, "class"); |
|
102 return new JMods(mods); |
|
103 } |
|
104 |
|
105 public boolean isAbstract() { |
|
106 return (mods & JMod.ABSTRACT) != 0; |
|
107 } |
|
108 |
|
109 public boolean isNative() { |
|
110 return (mods & JMod.NATIVE) != 0; |
|
111 } |
|
112 |
|
113 public boolean isSynchronized() { |
|
114 return (mods & JMod.SYNCHRONIZED) != 0; |
|
115 } |
|
116 |
|
117 public void setSynchronized(boolean newValue) { |
|
118 setFlag( JMod.SYNCHRONIZED, newValue ); |
|
119 } |
|
120 |
|
121 // TODO: more |
|
122 |
|
123 private void setFlag( int bit, boolean newValue ) { |
|
124 mods = (mods & ~bit) | (newValue?bit:0); |
|
125 } |
|
126 |
|
127 public void generate(JFormatter f) { |
|
128 if ((mods & JMod.PUBLIC) != 0) f.p("public"); |
|
129 if ((mods & JMod.PROTECTED) != 0) f.p("protected"); |
|
130 if ((mods & JMod.PRIVATE) != 0) f.p("private"); |
|
131 if ((mods & JMod.FINAL) != 0) f.p("final"); |
|
132 if ((mods & JMod.STATIC) != 0) f.p("static"); |
|
133 if ((mods & JMod.ABSTRACT) != 0) f.p("abstract"); |
|
134 if ((mods & JMod.NATIVE) != 0) f.p("native"); |
|
135 if ((mods & JMod.SYNCHRONIZED) != 0) f.p("synchronized"); |
|
136 if ((mods & JMod.TRANSIENT) != 0) f.p("transient"); |
|
137 if ((mods & JMod.VOLATILE) != 0) f.p("volatile"); |
|
138 } |
|
139 |
|
140 public String toString() { |
|
141 StringWriter s = new StringWriter(); |
|
142 JFormatter f = new JFormatter(new PrintWriter(s)); |
|
143 this.generate(f); |
|
144 return s.toString(); |
|
145 } |
|
146 |
|
147 } |